Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / include / unotools / textsearch.hxx
blob14a9558f237e4ef8866da58ef8138439dc083521
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 { namespace sun { namespace star { namespace lang { struct Locale; } } } }
33 namespace com { namespace sun { namespace star { namespace util { class XTextSearch2; } } } }
35 namespace com {
36 namespace sun {
37 namespace star {
38 namespace util {
39 struct SearchResult;
44 namespace i18nutil {
45 struct SearchOptions;
46 struct SearchOptions2;
48 enum class TransliterationFlags;
50 namespace utl
53 // Utility class for searching
54 class UNOTOOLS_DLLPUBLIC SearchParam
56 public:
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.
61 @param 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 )
67 if (bWildcard)
69 if (rbRegExp)
70 rbRegExp = false;
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 )
80 switch (eSearchType)
82 case SearchType::Wildcard:
83 rbWildcard = true;
84 rbRegExp = false;
85 break;
86 case SearchType::Regexp:
87 rbWildcard = false;
88 rbRegExp = true;
89 break;
90 default:
91 rbWildcard = false;
92 rbRegExp = false;
93 break;
97 private:
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
107 public:
108 SearchParam( const OUString &rText,
109 SearchType eSrchType,
110 bool bCaseSensitive = true,
111 sal_uInt32 cWildEscChar = '\\',
112 bool bWildMatchSel = false );
114 SearchParam( const SearchParam& );
116 ~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)
132 switch (eType)
134 case SearchParam::SearchType::Normal:
135 stream << "N";
136 break;
137 case SearchParam::SearchType::Regexp:
138 stream << "RE";
139 break;
140 case SearchParam::SearchType::Wildcard:
141 stream << "WC";
142 break;
143 case SearchParam::SearchType::Unknown:
144 stream << "UNK";
145 break;
146 default:
147 stream << static_cast<int>(eType) << '?';
148 break;
151 return stream;
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 >
169 xTextSearch;
171 void Init( const SearchParam & rParam,
172 const css::lang::Locale& rLocale );
174 public:
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 );
181 ~TextSearch();
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
207 * of the given text
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
224 wildcard search.
226 static i18nutil::SearchOptions2 UpgradeToSearchOptions2( const i18nutil::SearchOptions& rOptions );
230 } // namespace utl
232 #endif
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */