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 .
20 #ifndef INCLUDED_UNOTOOLS_TEXTSEARCH_HXX
21 #define INCLUDED_UNOTOOLS_TEXTSEARCH_HXX
23 #include <unotools/unotoolsdllapi.h>
24 #include <i18nlangtag/lang.h>
25 #include <rtl/ustring.hxx>
26 #include <com/sun/star/uno/Reference.h>
32 namespace com
{ namespace sun
{ namespace star
{ namespace lang
{ struct Locale
; } } } }
33 namespace com
{ namespace sun
{ namespace star
{ namespace util
{ class XTextSearch2
; } } } }
46 struct SearchOptions2
;
48 enum class TransliterationFlags
;
53 // Utility class for searching
54 class UNOTOOLS_DLLPUBLIC SearchParam
57 enum class SearchType
{ Normal
, Regexp
, Wildcard
, Unknown
= -1 };
59 /** Convert configuration and document boolean settings to SearchType.
60 If bWildcard is true it takes precedence over rbRegExp.
62 If true and bWildcard is also true, rbRegExp is set to false to
63 adapt the caller's settings.
65 static SearchType
ConvertToSearchType( bool bWildcard
, bool & rbRegExp
)
71 return SearchType::Wildcard
;
73 return rbRegExp
? SearchType::Regexp
: SearchType::Normal
;
76 /** Convert SearchType to configuration and document boolean settings.
78 static void ConvertToBool( const SearchType eSearchType
, bool& rbWildcard
, bool& rbRegExp
)
82 case SearchType::Wildcard
:
86 case SearchType::Regexp
:
98 OUString sSrchStr
; // the search string
100 SearchType m_eSrchType
; // search normal/regular/LevDist
102 sal_uInt32 m_cWildEscChar
; // wildcard escape character
104 bool m_bCaseSense
: 1;
105 bool m_bWildMatchSel
: 1; // wildcard pattern must match entire selection
108 SearchParam( const OUString
&rText
,
109 SearchType eSrchType
,
110 bool bCaseSensitive
= true,
111 sal_uInt32 cWildEscChar
= '\\',
112 bool bWildMatchSel
= false );
114 SearchParam( const SearchParam
& );
118 const OUString
& GetSrchStr() const { return sSrchStr
; }
119 SearchType
GetSrchType() const { return m_eSrchType
; }
121 bool IsCaseSensitive() const { return m_bCaseSense
; }
122 bool IsWildMatchSel() const { return m_bWildMatchSel
; }
124 // signed return for API use
125 sal_Int32
GetWildEscChar() const { return static_cast<sal_Int32
>(m_cWildEscChar
); }
128 // For use in SAL_DEBUG etc. Output format not guaranteed to be stable.
129 template<typename charT
, typename traits
>
130 inline std::basic_ostream
<charT
, traits
> & operator <<(std::basic_ostream
<charT
, traits
> & stream
, const SearchParam::SearchType
& eType
)
134 case SearchParam::SearchType::Normal
:
137 case SearchParam::SearchType::Regexp
:
140 case SearchParam::SearchType::Wildcard
:
143 case SearchParam::SearchType::Unknown
:
147 stream
<< static_cast<int>(eType
) << '?';
154 // Utility class for searching a substring in a string.
155 // The following metrics are supported
156 // - ordinary text (Bayer/Moore)
157 // - regular expressions
158 // - weighted Levenshtein distance
159 // - wildcards '*' and '?'
161 // This class allows forward and backward searching!
163 class UNOTOOLS_DLLPUBLIC TextSearch
165 static css::uno::Reference
< css::util::XTextSearch2
>
166 getXTextSearch( const i18nutil::SearchOptions2
& rPara
);
168 css::uno::Reference
< css::util::XTextSearch2
>
171 void Init( const SearchParam
& rParam
,
172 const css::lang::Locale
& rLocale
);
175 // rText is the string being searched for
176 // this first two CTORs are deprecated!
177 TextSearch( const SearchParam
& rPara
, LanguageType nLanguage
);
178 TextSearch( const SearchParam
& rPara
, const CharClass
& rCClass
);
180 TextSearch( const i18nutil::SearchOptions2
& rPara
);
183 /* search in the (selected) text the search string:
184 rScrTxt - the text, in which we search
185 pStart - start position for the search
186 pEnde - end position for the search
188 RETURN values == true: something is found
189 - pStart start pos of the found text,
190 - pStart end pos of the found text,
191 - pSrchResult - the search result with all found
192 positions. Is only filled with more positions
193 if the regular expression handles groups.
195 == false: nothing found, pStart,pEnde unchanged.
197 Definitions: start pos always inclusive, end pos always exclusive!
198 The position must always in the right direction!
199 search forward: start <= end
200 search backward: end <= start
202 bool SearchForward( const OUString
&rStr
,
203 sal_Int32
* pStart
, sal_Int32
* pEnd
,
204 css::util::SearchResult
* pRes
= nullptr );
206 * @brief searchForward Search forward beginning from the start to the end
208 * @param rStr The text in which we search
209 * @return True if the search term is found in the text
211 bool searchForward( const OUString
&rStr
);
212 bool SearchBackward( const OUString
&rStr
,
213 sal_Int32
* pStart
, sal_Int32
* pEnd
,
214 css::util::SearchResult
* pRes
= nullptr );
216 void SetLocale( const i18nutil::SearchOptions2
& rOpt
,
217 const css::lang::Locale
& rLocale
);
219 /* replace back references in the replace string by the sub expressions from the search result */
220 void ReplaceBackReferences( OUString
& rReplaceStr
, const OUString
&rStr
, const css::util::SearchResult
& rResult
) const;
222 /** Upgrade SearchOptions to SearchOptions2 for places that don't handle
223 SearchOptions2 yet. Better fix your module if you want to support
226 static i18nutil::SearchOptions2
UpgradeToSearchOptions2( const i18nutil::SearchOptions
& rOptions
);
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */