1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include "unotools/unotoolsdllapi.h"
21 #ifndef _UNOTOOLS_TEXTSEARCH_HXX
22 #define _UNOTOOLS_TEXTSEARCH_HXX
23 #include <i18nlangtag/lang.h>
24 #include <rtl/ustring.hxx>
25 #include <tools/string.hxx>
26 #include <com/sun/star/uno/Reference.h>
27 #include <com/sun/star/lang/Locale.hpp>
28 #include <com/sun/star/util/XTextSearch.hpp>
29 #include <com/sun/star/util/SearchOptions.hpp>
43 // ............................................................................
46 // ............................................................................
48 // Utility class for searching
49 class UNOTOOLS_DLLPUBLIC SearchParam
52 enum SearchType
{ SRCH_NORMAL
, SRCH_REGEXP
, SRCH_LEVDIST
};
55 String sSrchStr
; // the search string
56 String sReplaceStr
; // the replace string
58 SearchType m_eSrchType
; // search normal/regular/LevDist
60 int m_bWordOnly
: 1; // used by normal search
61 int m_bSrchInSel
: 1; // search only in the selection
62 int m_bCaseSense
: 1; //
64 // values for the "weight Levenshtein-Distance"
70 // asian flags - used for the transliteration
71 long nTransliterationFlags
;
74 SearchParam( const OUString
&rText
,
75 SearchType eSrchType
= SearchParam::SRCH_NORMAL
,
76 sal_Bool bCaseSensitive
= sal_True
,
77 sal_Bool bWordOnly
= sal_False
,
78 sal_Bool bSearchInSelection
= sal_False
);
80 SearchParam( const SearchParam
& );
84 const String
& GetSrchStr() const { return sSrchStr
; }
85 const String
& GetReplaceStr() const { return sReplaceStr
; }
86 SearchType
GetSrchType() const { return m_eSrchType
; }
88 int IsCaseSensitive() const { return m_bCaseSense
; }
89 int IsSrchInSelection() const { return m_bSrchInSel
; }
90 int IsSrchWordOnly() const { return m_bWordOnly
; }
93 void SetSrchStr( const String
& rStr
) { sSrchStr
= rStr
; }
94 void SetReplaceStr( const String
& rStr
) { sReplaceStr
= rStr
; }
95 void SetSrchType( SearchType eType
) { m_eSrchType
= eType
; }
97 void SetCaseSensitive( int bFlag
) { m_bCaseSense
= bFlag
; }
98 void SetSrchInSelection( int bFlag
) { m_bSrchInSel
= bFlag
; }
99 void SetSrchWordOnly( int bFlag
) { m_bWordOnly
= bFlag
; }
101 int IsSrchRelaxed() const { return bLEV_Relaxed
; }
102 int GetLEVOther() const { return nLEV_OtherX
; }
103 int GetLEVShorter() const { return nLEV_ShorterY
; }
104 int GetLEVLonger() const { return nLEV_LongerZ
; }
106 void SetSrchRelaxed( int bFlag
) { bLEV_Relaxed
= bFlag
; }
107 void SetLEVOther( int nValue
) { nLEV_OtherX
= nValue
; }
108 void SetLEVShorter( int nValue
) { nLEV_ShorterY
= nValue
; }
109 void SetLEVLonger( int nValue
) { nLEV_LongerZ
= nValue
; }
111 long GetTransliterationFlags() const { return nTransliterationFlags
; }
112 void SetTransliterationFlags( long nValue
) { nTransliterationFlags
= nValue
; }
115 // Utility class for searching a substring in a string.
116 // The following metrics are supported
117 // - ordinary text (Bayer/Moore)
118 // - regular expressions
119 // - weighted Levenshtein distance
121 // This class allows forward and backward searching!
123 class UNOTOOLS_DLLPUBLIC TextSearch
125 static ::com::sun::star::uno::Reference
< ::com::sun::star::util::XTextSearch
>
126 getXTextSearch( const ::com::sun::star::util::SearchOptions
& rPara
);
128 com::sun::star::uno::Reference
< com::sun::star::util::XTextSearch
>
131 void Init( const SearchParam
& rParam
,
132 const ::com::sun::star::lang::Locale
& rLocale
);
135 // rText is the string being searched for
136 // this first two CTORs are deprecated!
137 TextSearch( const SearchParam
& rPara
, LanguageType nLanguage
);
138 TextSearch( const SearchParam
& rPara
, const CharClass
& rCClass
);
140 TextSearch( const ::com::sun::star::util::SearchOptions
& rPara
);
143 /* search in the (selected) text the search string:
144 rScrTxt - the text, in in which we search
145 pStart - start position for the search
146 pEnde - end position for the search
148 RETURN values == sal_True: something is found
149 - pStart start pos of the found text,
150 - pStart end pos of the found text,
151 - pSrchResult - the search result with all found
152 positions. Is only filled with more positions
153 if the regular expression handles groups.
155 == sal_False: nothing found, pStart,pEnde unchanged.
157 Definitions: start pos always inclusive, end pos always exclusive!
158 The position must always in the right direction!
159 search forward: start <= end
160 search backward: end <= start
162 int SearchFrwrd( const String
&rStr
,
163 xub_StrLen
* pStart
, xub_StrLen
* pEnde
,
164 ::com::sun::star::util::SearchResult
* pSrchResult
= 0 );
165 sal_Bool
SearchForward( const OUString
&rStr
,
166 sal_Int32
* pStart
, sal_Int32
* pEnd
,
167 ::com::sun::star::util::SearchResult
* pRes
= 0 );
168 int SearchBkwrd( const String
&rStr
,
169 xub_StrLen
* pStart
, xub_StrLen
* pEnde
,
170 ::com::sun::star::util::SearchResult
* pSrchResult
= 0 );
172 void SetLocale( const ::com::sun::star::util::SearchOptions
& rOpt
,
173 const ::com::sun::star::lang::Locale
& rLocale
);
175 /* replace back references in the replace string by the sub expressions from the search result */
176 void ReplaceBackReferences( String
& rReplaceStr
, const String
&rStr
, const ::com::sun::star::util::SearchResult
& rResult
);
180 // ............................................................................
182 // ............................................................................
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */