LibreOffice Module unotools (master) 1
textsearch.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_UNOTOOLS_TEXTSEARCH_HXX
21#define INCLUDED_UNOTOOLS_TEXTSEARCH_HXX
22
24#include <i18nlangtag/lang.h>
25#include <rtl/ustring.hxx>
26#include <com/sun/star/uno/Reference.h>
27
28#include <ostream>
29
30class CharClass;
31
32namespace com::sun::star::lang { struct Locale; }
33namespace com::sun::star::util { class XTextSearch2; }
34namespace com::sun::star::util { struct SearchResult; }
35namespace i18nutil {
36 struct SearchOptions;
37 struct SearchOptions2;
38}
39enum class TransliterationFlags;
40
41namespace utl
42{
43
44// Utility class for searching
46{
47public:
48 enum class SearchType { Normal, Regexp, Wildcard, Unknown = -1 };
49
56 static SearchType ConvertToSearchType( bool bWildcard, bool & rbRegExp )
57 {
58 if (bWildcard)
59 {
60 if (rbRegExp)
61 rbRegExp = false;
62 return SearchType::Wildcard;
63 }
64 return rbRegExp ? SearchType::Regexp : SearchType::Normal;
65 }
66
69 static void ConvertToBool( const SearchType eSearchType, bool& rbWildcard, bool& rbRegExp )
70 {
71 switch (eSearchType)
72 {
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;
85 }
86 }
87
88private:
89 OUString sSrchStr; // the search string
90
91 SearchType m_eSrchType; // search normal/regular/LevDist
92
93 sal_uInt32 m_cWildEscChar; // wildcard escape character
94
95 bool m_bCaseSense : 1;
96 bool m_bWildMatchSel : 1; // wildcard pattern must match entire selection
97
98public:
99 SearchParam( const OUString &rText,
100 SearchType eSrchType,
101 bool bCaseSensitive = true,
102 sal_uInt32 cWildEscChar = '\\',
103 bool bWildMatchSel = false );
104
105 SearchParam( const SearchParam& );
106
107 ~SearchParam();
108
109 const OUString& GetSrchStr() const { return sSrchStr; }
110 SearchType GetSrchType() const { return m_eSrchType; }
111
112 bool IsCaseSensitive() const { return m_bCaseSense; }
113 bool IsWildMatchSel() const { return m_bWildMatchSel; }
114
115 // signed return for API use
116 sal_Int32 GetWildEscChar() const { return static_cast<sal_Int32>(m_cWildEscChar); }
117};
118
119// For use in SAL_DEBUG etc. Output format not guaranteed to be stable.
120template<typename charT, typename traits>
121inline std::basic_ostream<charT, traits> & operator <<(std::basic_ostream<charT, traits> & stream, const SearchParam::SearchType& eType)
122{
123 switch (eType)
124 {
126 stream << "N";
127 break;
129 stream << "RE";
130 break;
132 stream << "WC";
133 break;
135 stream << "UNK";
136 break;
137 default:
138 stream << static_cast<int>(eType) << '?';
139 break;
140 }
141
142 return stream;
143}
144
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 '?'
151
152// This class allows forward and backward searching!
153
155{
156 static css::uno::Reference< css::util::XTextSearch2 >
157 getXTextSearch( const i18nutil::SearchOptions2& rPara );
158
159 css::uno::Reference < css::util::XTextSearch2 >
161
162 void Init( const SearchParam & rParam,
163 const css::lang::Locale& rLocale );
164
165public:
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 );
170
171 TextSearch( const i18nutil::SearchOptions2& rPara );
172 ~TextSearch();
173
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
178
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.
185
186 == false: nothing found, pStart, pEnd unchanged.
187
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
192 */
193 bool SearchForward( const OUString &rStr,
194 sal_Int32* pStart, sal_Int32* pEnd,
195 css::util::SearchResult* pRes = nullptr );
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 );
206
207 void SetLocale( const i18nutil::SearchOptions2& rOpt,
208 const css::lang::Locale& rLocale );
209
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;
212};
213
214} // namespace utl
215
216#endif
217
218/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 GetWildEscChar() const
Definition: textsearch.hxx:116
sal_uInt32 m_cWildEscChar
Definition: textsearch.hxx:93
static void ConvertToBool(const SearchType eSearchType, bool &rbWildcard, bool &rbRegExp)
Convert SearchType to configuration and document boolean settings.
Definition: textsearch.hxx:69
OUString sSrchStr
Definition: textsearch.hxx:89
SearchType m_eSrchType
Definition: textsearch.hxx:91
bool IsCaseSensitive() const
Definition: textsearch.hxx:112
SearchType GetSrchType() const
Definition: textsearch.hxx:110
const OUString & GetSrchStr() const
Definition: textsearch.hxx:109
static SearchType ConvertToSearchType(bool bWildcard, bool &rbRegExp)
Convert configuration and document boolean settings to SearchType.
Definition: textsearch.hxx:56
bool IsWildMatchSel() const
Definition: textsearch.hxx:113
css::uno::Reference< css::util::XTextSearch2 > xTextSearch
Definition: textsearch.hxx:160
void Init()
Reference< XOutputStream > stream
Unknown
std::optional< OUString > ReplaceBackReferences(const i18nutil::SearchOptions2 &rSearchOpt, SwPaM *pPam, SwRootFrame const *pLayout)
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &stream, const SearchParam::SearchType &eType)
Definition: textsearch.hxx:121
TransliterationFlags
#define UNOTOOLS_DLLPUBLIC