cid#1607171 Data race condition
[LibreOffice.git] / i18npool / source / search / textsearch.hxx
blob67ba1f50a3201600e6c9f6728b2812ebbb94bf35
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 #pragma once
22 #include <cppuhelper/implbase.hxx>
23 #include <com/sun/star/util/XTextSearch2.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <map>
27 #include <memory>
28 #include <mutex>
30 #include <unicode/regex.h>
31 #include <unicode/unistr.h>
32 #include <unicode/uversion.h>
34 namespace com::sun::star::i18n { class XBreakIterator; }
35 namespace com::sun::star::i18n { class XCharacterClassification; }
36 namespace com::sun::star::i18n { class XExtendedTransliteration; }
37 namespace com::sun::star::uno { class XComponentContext; }
40 class WLevDistance;
41 typedef ::std::map< sal_Unicode, sal_Int32 > TextSearchJumpTable;
43 class TextSearch: public cppu::WeakImplHelper
45 css::util::XTextSearch2,
46 css::lang::XServiceInfo
49 std::mutex m_aMutex;
50 css::uno::Reference < css::uno::XComponentContext > m_xContext;
52 css::util::SearchOptions2 aSrchPara;
53 OUString sSrchStr;
54 OUString sSrchStr2;
56 mutable css::uno::Reference< css::i18n::XCharacterClassification > xCharClass;
58 css::uno::Reference< css::i18n::XExtendedTransliteration > xTranslit;
59 css::uno::Reference< css::i18n::XExtendedTransliteration > xTranslit2;
61 // define a function pointer for the different search methods
62 typedef css::util::SearchResult
63 (TextSearch::*FnSrch)( std::unique_lock<std::mutex>& rGuard, const OUString& searchStr,
64 sal_Int32 startPos, sal_Int32 endPos );
66 FnSrch fnForward;
67 FnSrch fnBackward;
69 // to fix UX regression, U+0027 matches also U+2019 in non-regex search
70 bool bSearchApostrophe;
72 // Members and methods for the normal (Boyer-Moore) search
73 std::unique_ptr<TextSearchJumpTable> pJumpTable;
74 std::unique_ptr<TextSearchJumpTable> pJumpTable2;
75 bool bIsForwardTab;
76 bool bUsePrimarySrchStr;
77 void MakeForwardTab();
78 void MakeForwardTab2();
79 void MakeBackwardTab();
80 void MakeBackwardTab2();
81 sal_Int32 GetDiff( const sal_Unicode ) const;
82 /// @throws css::uno::RuntimeException
83 css::util::SearchResult
84 NSrchFrwrd( std::unique_lock<std::mutex>& rGuard, const OUString& searchStr,
85 sal_Int32 startPos, sal_Int32 endPos );
86 /// @throws css::uno::RuntimeException
87 css::util::SearchResult
88 NSrchBkwrd( std::unique_lock<std::mutex>& rGuard, const OUString& searchStr,
89 sal_Int32 startPos, sal_Int32 endPos );
91 // Members and methods for the regular expression search
92 std::unique_ptr<icu::RegexMatcher> pRegexMatcher;
93 /// @throws css::uno::RuntimeException
94 css::util::SearchResult
95 RESrchFrwrd( std::unique_lock<std::mutex>& rGuard, const OUString& searchStr,
96 sal_Int32 startPos, sal_Int32 endPos );
97 /// @throws css::uno::RuntimeException
98 css::util::SearchResult
99 RESrchBkwrd( std::unique_lock<std::mutex>& rGuard, const OUString& searchStr,
100 sal_Int32 startPos, sal_Int32 endPos );
101 void RESrchPrepare( const css::util::SearchOptions2&);
103 // Members and methods for the "Weight Levenshtein-Distance" search
104 int nLimit;
105 std::unique_ptr<WLevDistance> pWLD;
106 css::uno::Reference < css::i18n::XBreakIterator > xBreak;
107 /// @throws css::uno::RuntimeException
108 css::util::SearchResult
109 ApproxSrchFrwrd( std::unique_lock<std::mutex>& rGuard, const OUString& searchStr,
110 sal_Int32 startPos, sal_Int32 endPos );
111 /// @throws css::uno::RuntimeException
112 css::util::SearchResult
113 ApproxSrchBkwrd( std::unique_lock<std::mutex>& rGuard, const OUString& searchStr,
114 sal_Int32 startPos, sal_Int32 endPos );
116 // Members and methods for the wildcard search
117 OUString maWildcardReversePattern;
118 OUString maWildcardReversePattern2;
119 sal_uInt32 mcWildcardEscapeChar;
120 bool mbWildcardAllowSubstring;
121 /// @throws css::uno::RuntimeException
122 css::util::SearchResult
123 WildcardSrchFrwrd( std::unique_lock<std::mutex>& rGuard, const OUString& searchStr,
124 sal_Int32 startPos, sal_Int32 endPos );
125 /// @throws css::uno::RuntimeException
126 css::util::SearchResult
127 WildcardSrchBkwrd( std::unique_lock<std::mutex>& rGuard, const OUString& searchStr,
128 sal_Int32 startPos, sal_Int32 endPos );
130 bool IsDelimiter( const OUString& rStr, sal_Int32 nPos ) const;
132 public:
133 explicit TextSearch(
134 const css::uno::Reference < css::uno::XComponentContext >& rxContext );
136 virtual ~TextSearch() override;
138 // XTextSearch
139 virtual void SAL_CALL
140 setOptions( const css::util::SearchOptions& options ) override;
141 virtual css::util::SearchResult SAL_CALL
142 searchForward( const OUString& searchStr,
143 sal_Int32 startPos, sal_Int32 endPos ) override;
144 virtual css::util::SearchResult SAL_CALL
145 searchBackward( const OUString& searchStr,
146 sal_Int32 startPos, sal_Int32 endPos ) override;
148 // XTextSearch2
149 virtual void SAL_CALL
150 setOptions2( const css::util::SearchOptions2& options ) override;
152 //XServiceInfo
153 virtual OUString SAL_CALL getImplementationName() override;
154 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
155 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */