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_I18NPOOL_SOURCE_SEARCH_TEXTSEARCH_HXX
21 #define INCLUDED_I18NPOOL_SOURCE_SEARCH_TEXTSEARCH_HXX
22 
23 #include <cppuhelper/implbase.hxx>
24 #include <com/sun/star/util/XTextSearch2.hpp>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
26 
27 #include <map>
28 #include <memory>
29 
30 #include <unicode/regex.h>
31 #include <unicode/unistr.h>
32 #include <unicode/uversion.h>
33 
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; }
38 
39 
40 typedef U_ICU_NAMESPACE::UnicodeString IcuUniString;
41 
42 class WLevDistance;
43 typedef ::std::map< sal_Unicode, sal_Int32 > TextSearchJumpTable;
44 
45 class TextSearch: public cppu::WeakImplHelper
46 <
47     css::util::XTextSearch2,
48     css::lang::XServiceInfo
49 >
50 {
51     osl::Mutex m_aMutex;
52     css::uno::Reference < css::uno::XComponentContext > m_xContext;
53 
54     css::util::SearchOptions2 aSrchPara;
55     OUString sSrchStr;
56     OUString sSrchStr2;
57 
58     mutable css::uno::Reference< css::i18n::XCharacterClassification > xCharClass;
59 
60     css::uno::Reference< css::i18n::XExtendedTransliteration > xTranslit;
61     css::uno::Reference< css::i18n::XExtendedTransliteration > xTranslit2;
62 
63     // define a function pointer for the different search methods
64     typedef css::util::SearchResult
65         (SAL_CALL TextSearch::*FnSrch)( const OUString& searchStr,
66                                 sal_Int32 startPos, sal_Int32 endPos );
67 
68     FnSrch fnForward;
69     FnSrch fnBackward;
70 
71     // to fix UX regression, U+0027 matches also U+2019 in non-regex search
72     bool bSearchApostrophe;
73 
74     // Members and methods for the normal (Boyer-Moore) search
75     std::unique_ptr<TextSearchJumpTable> pJumpTable;
76     std::unique_ptr<TextSearchJumpTable> pJumpTable2;
77     bool bIsForwardTab;
78     bool bUsePrimarySrchStr;
79     void MakeForwardTab();
80     void MakeForwardTab2();
81     void MakeBackwardTab();
82     void MakeBackwardTab2();
83     sal_Int32 GetDiff( const sal_Unicode ) const;
84     /// @throws css::uno::RuntimeException
85     css::util::SearchResult SAL_CALL
86         NSrchFrwrd( const OUString& searchStr,
87                                 sal_Int32 startPos, sal_Int32 endPos );
88     /// @throws css::uno::RuntimeException
89     css::util::SearchResult SAL_CALL
90         NSrchBkwrd( const OUString& searchStr,
91                                 sal_Int32 startPos, sal_Int32 endPos );
92 
93     // Members and methods for the regular expression search
94     std::unique_ptr<icu::RegexMatcher> pRegexMatcher;
95     /// @throws css::uno::RuntimeException
96     css::util::SearchResult SAL_CALL
97         RESrchFrwrd( const OUString& searchStr,
98                                 sal_Int32 startPos, sal_Int32 endPos );
99     /// @throws css::uno::RuntimeException
100     css::util::SearchResult SAL_CALL
101         RESrchBkwrd( const OUString& searchStr,
102                                 sal_Int32 startPos, sal_Int32 endPos );
103     void RESrchPrepare( const css::util::SearchOptions2&);
104 
105     // Members and methods for the "Weight Levenshtein-Distance" search
106     int nLimit;
107     std::unique_ptr<WLevDistance> pWLD;
108     css::uno::Reference < css::i18n::XBreakIterator > xBreak;
109     /// @throws css::uno::RuntimeException
110     css::util::SearchResult SAL_CALL
111         ApproxSrchFrwrd( const OUString& searchStr,
112                                 sal_Int32 startPos, sal_Int32 endPos );
113     /// @throws css::uno::RuntimeException
114     css::util::SearchResult SAL_CALL
115         ApproxSrchBkwrd( const OUString& searchStr,
116                                 sal_Int32 startPos, sal_Int32 endPos );
117 
118     // Members and methods for the wildcard search
119     OUString    maWildcardReversePattern;
120     OUString    maWildcardReversePattern2;
121     sal_uInt32  mcWildcardEscapeChar;
122     bool        mbWildcardAllowSubstring;
123     /// @throws css::uno::RuntimeException
124     css::util::SearchResult SAL_CALL
125         WildcardSrchFrwrd( const OUString& searchStr,
126                                 sal_Int32 startPos, sal_Int32 endPos );
127     /// @throws css::uno::RuntimeException
128     css::util::SearchResult SAL_CALL
129         WildcardSrchBkwrd( const OUString& searchStr,
130                                 sal_Int32 startPos, sal_Int32 endPos );
131 
132     bool IsDelimiter( const OUString& rStr, sal_Int32 nPos ) const;
133 
134     bool checkCTLStart, checkCTLEnd;
135     /// @throws css::uno::RuntimeException
136     bool isCellStart(const OUString& searchStr, sal_Int32 nPos);
137 
138 public:
139     explicit TextSearch(
140         const css::uno::Reference < css::uno::XComponentContext >& rxContext );
141 
142     virtual ~TextSearch() override;
143 
144     // XTextSearch
145     virtual void SAL_CALL
146         setOptions( const css::util::SearchOptions& options ) override;
147     virtual css::util::SearchResult SAL_CALL
148         searchForward( const OUString& searchStr,
149                         sal_Int32 startPos, sal_Int32 endPos ) override;
150     virtual css::util::SearchResult SAL_CALL
151         searchBackward( const OUString& searchStr,
152                         sal_Int32 startPos, sal_Int32 endPos ) override;
153 
154     // XTextSearch2
155     virtual void SAL_CALL
156         setOptions2( const css::util::SearchOptions2& options ) override;
157 
158     //XServiceInfo
159     virtual OUString SAL_CALL getImplementationName() override;
160     virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
161     virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
162 };
163 
164 #endif
165 
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
167