Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / unotools / textsearch.hxx
blob140200fec83f0b6bb2754bfa3ab6913ba2a5e7e1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
28 #include <ostream>
30 class CharClass;
32 namespace com::sun::star::lang { struct Locale; }
33 namespace com::sun::star::util { class XTextSearch2; }
34 namespace com::sun::star::util { struct SearchResult; }
35 namespace i18nutil {
36 struct SearchOptions;
37 struct SearchOptions2;
39 enum class TransliterationFlags;
41 namespace utl
44 // Utility class for searching
45 class UNOTOOLS_DLLPUBLIC SearchParam
47 public:
48 enum class SearchType { Normal, Regexp, Wildcard, Unknown = -1 };
50 /** Convert configuration and document boolean settings to SearchType.
51 If bWildcard is true it takes precedence over rbRegExp.
52 @param rbRegExp
53 If true and bWildcard is also true, rbRegExp is set to false to
54 adapt the caller's settings.
56 static SearchType ConvertToSearchType( bool bWildcard, bool & rbRegExp )
58 if (bWildcard)
60 if (rbRegExp)
61 rbRegExp = false;
62 return SearchType::Wildcard;
64 return rbRegExp ? SearchType::Regexp : SearchType::Normal;
67 /** Convert SearchType to configuration and document boolean settings.
69 static void ConvertToBool( const SearchType eSearchType, bool& rbWildcard, bool& rbRegExp )
71 switch (eSearchType)
73 case SearchType::Wildcard:
74 rbWildcard = true;
75 rbRegExp = false;
76 break;
77 case SearchType::Regexp:
78 rbWildcard = false;
79 rbRegExp = true;
80 break;
81 default:
82 rbWildcard = false;
83 rbRegExp = false;
84 break;
88 private:
89 OUString sSrchStr; // the search string
91 SearchType m_eSrchType; // search normal/regular/LevDist
93 sal_uInt32 m_cWildEscChar; // wildcard escape character
95 bool m_bCaseSense : 1;
96 bool m_bWildMatchSel : 1; // wildcard pattern must match entire selection
98 public:
99 SearchParam( const OUString &rText,
100 SearchType eSrchType,
101 bool bCaseSensitive = true,
102 sal_uInt32 cWildEscChar = '\\',
103 bool bWildMatchSel = false );
105 SearchParam( const SearchParam& );
107 ~SearchParam();
109 const OUString& GetSrchStr() const { return sSrchStr; }
110 SearchType GetSrchType() const { return m_eSrchType; }
112 bool IsCaseSensitive() const { return m_bCaseSense; }
113 bool IsWildMatchSel() const { return m_bWildMatchSel; }
115 // signed return for API use
116 sal_Int32 GetWildEscChar() const { return static_cast<sal_Int32>(m_cWildEscChar); }
119 // For use in SAL_DEBUG etc. Output format not guaranteed to be stable.
120 template<typename charT, typename traits>
121 inline std::basic_ostream<charT, traits> & operator <<(std::basic_ostream<charT, traits> & stream, const SearchParam::SearchType& eType)
123 switch (eType)
125 case SearchParam::SearchType::Normal:
126 stream << "N";
127 break;
128 case SearchParam::SearchType::Regexp:
129 stream << "RE";
130 break;
131 case SearchParam::SearchType::Wildcard:
132 stream << "WC";
133 break;
134 case SearchParam::SearchType::Unknown:
135 stream << "UNK";
136 break;
137 default:
138 stream << static_cast<int>(eType) << '?';
139 break;
142 return stream;
145 // Utility class for searching a substring in a string.
146 // The following metrics are supported
147 // - ordinary text (Bayer/Moore)
148 // - regular expressions
149 // - weighted Levenshtein distance
150 // - wildcards '*' and '?'
152 // This class allows forward and backward searching!
154 class UNOTOOLS_DLLPUBLIC TextSearch
156 static css::uno::Reference< css::util::XTextSearch2 >
157 getXTextSearch( const i18nutil::SearchOptions2& rPara );
159 css::uno::Reference < css::util::XTextSearch2 >
160 xTextSearch;
162 void Init( const SearchParam & rParam,
163 const css::lang::Locale& rLocale );
165 public:
166 // rText is the string being searched for
167 // this first two CTORs are deprecated!
168 TextSearch( const SearchParam & rPara, LanguageType nLanguage );
169 TextSearch( const SearchParam & rPara, const CharClass& rCClass );
171 TextSearch( const i18nutil::SearchOptions2& rPara );
172 ~TextSearch();
174 /* search in the (selected) text the search string:
175 rScrTxt - the text, in which we search
176 pStart - start position for the search
177 pEnd - end position for the search
179 RETURN values == true: something is found
180 - pStart start pos of the found text,
181 - pEnd end pos of the found text,
182 - pSrchResult - the search result with all found
183 positions. Is only filled with more positions
184 if the regular expression handles groups.
186 == false: nothing found, pStart, pEnd unchanged.
188 Definitions: start pos always inclusive, end pos always exclusive!
189 The position must always in the right direction!
190 search forward: start <= end
191 search backward: end <= start
193 bool SearchForward( const OUString &rStr,
194 sal_Int32* pStart, sal_Int32* pEnd,
195 css::util::SearchResult* pRes = nullptr );
197 * @brief searchForward Search forward beginning from the start to the end
198 * of the given text
199 * @param rStr The text in which we search
200 * @return True if the search term is found in the text
202 bool searchForward( const OUString &rStr );
203 bool SearchBackward( const OUString &rStr,
204 sal_Int32* pStart, sal_Int32* pEnd,
205 css::util::SearchResult* pRes = nullptr );
207 void SetLocale( const i18nutil::SearchOptions2& rOpt,
208 const css::lang::Locale& rLocale );
210 /* replace back references in the replace string by the sub expressions from the search result */
211 void ReplaceBackReferences( OUString& rReplaceStr, std::u16string_view rStr, const css::util::SearchResult& rResult ) const;
213 /** Upgrade SearchOptions to SearchOptions2 for places that don't handle
214 SearchOptions2 yet. Better fix your module if you want to support
215 wildcard search.
217 static i18nutil::SearchOptions2 UpgradeToSearchOptions2( const i18nutil::SearchOptions& rOptions );
221 } // namespace utl
223 #endif
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */