xref: /aosp_15_r20/external/icu/libicu/cts_headers/unicode/stsearch.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker **********************************************************************
5*0e209d39SAndroid Build Coastguard Worker *   Copyright (C) 2001-2014 IBM and others. All rights reserved.
6*0e209d39SAndroid Build Coastguard Worker **********************************************************************
7*0e209d39SAndroid Build Coastguard Worker *   Date        Name        Description
8*0e209d39SAndroid Build Coastguard Worker *  03/22/2000   helena      Creation.
9*0e209d39SAndroid Build Coastguard Worker **********************************************************************
10*0e209d39SAndroid Build Coastguard Worker */
11*0e209d39SAndroid Build Coastguard Worker 
12*0e209d39SAndroid Build Coastguard Worker #ifndef STSEARCH_H
13*0e209d39SAndroid Build Coastguard Worker #define STSEARCH_H
14*0e209d39SAndroid Build Coastguard Worker 
15*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
16*0e209d39SAndroid Build Coastguard Worker 
17*0e209d39SAndroid Build Coastguard Worker #if U_SHOW_CPLUSPLUS_API
18*0e209d39SAndroid Build Coastguard Worker 
19*0e209d39SAndroid Build Coastguard Worker /**
20*0e209d39SAndroid Build Coastguard Worker  * \file
21*0e209d39SAndroid Build Coastguard Worker  * \brief C++ API: Service for searching text based on RuleBasedCollator.
22*0e209d39SAndroid Build Coastguard Worker  */
23*0e209d39SAndroid Build Coastguard Worker 
24*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION
25*0e209d39SAndroid Build Coastguard Worker 
26*0e209d39SAndroid Build Coastguard Worker #include "unicode/tblcoll.h"
27*0e209d39SAndroid Build Coastguard Worker #include "unicode/coleitr.h"
28*0e209d39SAndroid Build Coastguard Worker #include "unicode/search.h"
29*0e209d39SAndroid Build Coastguard Worker 
30*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN
31*0e209d39SAndroid Build Coastguard Worker 
32*0e209d39SAndroid Build Coastguard Worker /**
33*0e209d39SAndroid Build Coastguard Worker  *
34*0e209d39SAndroid Build Coastguard Worker  * <tt>StringSearch</tt> is a <tt>SearchIterator</tt> that provides
35*0e209d39SAndroid Build Coastguard Worker  * language-sensitive text searching based on the comparison rules defined
36*0e209d39SAndroid Build Coastguard Worker  * in a {@link RuleBasedCollator} object.
37*0e209d39SAndroid Build Coastguard Worker  * StringSearch ensures that language eccentricity can be
38*0e209d39SAndroid Build Coastguard Worker  * handled, e.g. for the German collator, characters &szlig; and SS will be matched
39*0e209d39SAndroid Build Coastguard Worker  * if case is chosen to be ignored.
40*0e209d39SAndroid Build Coastguard Worker  * See the <a href="https://htmlpreview.github.io/?https://github.com/unicode-org/icu-docs/blob/main/design/collation/ICU_collation_design.htm">
41*0e209d39SAndroid Build Coastguard Worker  * "ICU Collation Design Document"</a> for more information.
42*0e209d39SAndroid Build Coastguard Worker  * <p>
43*0e209d39SAndroid Build Coastguard Worker  * There are 2 match options for selection:<br>
44*0e209d39SAndroid Build Coastguard Worker  * Let S' be the sub-string of a text string S between the offsets start and
45*0e209d39SAndroid Build Coastguard Worker  * end [start, end].
46*0e209d39SAndroid Build Coastguard Worker  * <br>
47*0e209d39SAndroid Build Coastguard Worker  * A pattern string P matches a text string S at the offsets [start, end]
48*0e209d39SAndroid Build Coastguard Worker  * if
49*0e209d39SAndroid Build Coastguard Worker  * <pre>
50*0e209d39SAndroid Build Coastguard Worker  * option 1. Some canonical equivalent of P matches some canonical equivalent
51*0e209d39SAndroid Build Coastguard Worker  *           of S'
52*0e209d39SAndroid Build Coastguard Worker  * option 2. P matches S' and if P starts or ends with a combining mark,
53*0e209d39SAndroid Build Coastguard Worker  *           there exists no non-ignorable combining mark before or after S?
54*0e209d39SAndroid Build Coastguard Worker  *           in S respectively.
55*0e209d39SAndroid Build Coastguard Worker  * </pre>
56*0e209d39SAndroid Build Coastguard Worker  * Option 2. will be the default.
57*0e209d39SAndroid Build Coastguard Worker  * <p>
58*0e209d39SAndroid Build Coastguard Worker  * This search has APIs similar to that of other text iteration mechanisms
59*0e209d39SAndroid Build Coastguard Worker  * such as the break iterators in <tt>BreakIterator</tt>. Using these
60*0e209d39SAndroid Build Coastguard Worker  * APIs, it is easy to scan through text looking for all occurrences of
61*0e209d39SAndroid Build Coastguard Worker  * a given pattern. This search iterator allows changing of direction by
62*0e209d39SAndroid Build Coastguard Worker  * calling a <tt>reset</tt> followed by a <tt>next</tt> or <tt>previous</tt>.
63*0e209d39SAndroid Build Coastguard Worker  * Though a direction change can occur without calling <tt>reset</tt> first,
64*0e209d39SAndroid Build Coastguard Worker  * this operation comes with some speed penalty.
65*0e209d39SAndroid Build Coastguard Worker  * Match results in the forward direction will match the result matches in
66*0e209d39SAndroid Build Coastguard Worker  * the backwards direction in the reverse order
67*0e209d39SAndroid Build Coastguard Worker  * <p>
68*0e209d39SAndroid Build Coastguard Worker  * <tt>SearchIterator</tt> provides APIs to specify the starting position
69*0e209d39SAndroid Build Coastguard Worker  * within the text string to be searched, e.g. <tt>setOffset</tt>,
70*0e209d39SAndroid Build Coastguard Worker  * <tt>preceding</tt> and <tt>following</tt>. Since the
71*0e209d39SAndroid Build Coastguard Worker  * starting position will be set as it is specified, please take note that
72*0e209d39SAndroid Build Coastguard Worker  * there are some danger points which the search may render incorrect
73*0e209d39SAndroid Build Coastguard Worker  * results:
74*0e209d39SAndroid Build Coastguard Worker  * <ul>
75*0e209d39SAndroid Build Coastguard Worker  * <li> The midst of a substring that requires normalization.
76*0e209d39SAndroid Build Coastguard Worker  * <li> If the following match is to be found, the position should not be the
77*0e209d39SAndroid Build Coastguard Worker  *      second character which requires to be swapped with the preceding
78*0e209d39SAndroid Build Coastguard Worker  *      character. Vice versa, if the preceding match is to be found,
79*0e209d39SAndroid Build Coastguard Worker  *      position to search from should not be the first character which
80*0e209d39SAndroid Build Coastguard Worker  *      requires to be swapped with the next character. E.g certain Thai and
81*0e209d39SAndroid Build Coastguard Worker  *      Lao characters require swapping.
82*0e209d39SAndroid Build Coastguard Worker  * <li> If a following pattern match is to be found, any position within a
83*0e209d39SAndroid Build Coastguard Worker  *      contracting sequence except the first will fail. Vice versa if a
84*0e209d39SAndroid Build Coastguard Worker  *      preceding pattern match is to be found, a invalid starting point
85*0e209d39SAndroid Build Coastguard Worker  *      would be any character within a contracting sequence except the last.
86*0e209d39SAndroid Build Coastguard Worker  * </ul>
87*0e209d39SAndroid Build Coastguard Worker  * <p>
88*0e209d39SAndroid Build Coastguard Worker  * A <tt>BreakIterator</tt> can be used if only matches at logical breaks are desired.
89*0e209d39SAndroid Build Coastguard Worker  * Using a <tt>BreakIterator</tt> will only give you results that exactly matches the
90*0e209d39SAndroid Build Coastguard Worker  * boundaries given by the breakiterator. For instance the pattern "e" will
91*0e209d39SAndroid Build Coastguard Worker  * not be found in the string "\u00e9" if a character break iterator is used.
92*0e209d39SAndroid Build Coastguard Worker  * <p>
93*0e209d39SAndroid Build Coastguard Worker  * Options are provided to handle overlapping matches.
94*0e209d39SAndroid Build Coastguard Worker  * E.g. In English, overlapping matches produces the result 0 and 2
95*0e209d39SAndroid Build Coastguard Worker  * for the pattern "abab" in the text "ababab", where else mutually
96*0e209d39SAndroid Build Coastguard Worker  * exclusive matches only produce the result of 0.
97*0e209d39SAndroid Build Coastguard Worker  * <p>
98*0e209d39SAndroid Build Coastguard Worker  * Though collator attributes will be taken into consideration while
99*0e209d39SAndroid Build Coastguard Worker  * performing matches, there are no APIs here for setting and getting the
100*0e209d39SAndroid Build Coastguard Worker  * attributes. These attributes can be set by getting the collator
101*0e209d39SAndroid Build Coastguard Worker  * from <tt>getCollator</tt> and using the APIs in <tt>coll.h</tt>.
102*0e209d39SAndroid Build Coastguard Worker  * Lastly to update <tt>StringSearch</tt> to the new collator attributes,
103*0e209d39SAndroid Build Coastguard Worker  * <tt>reset</tt> has to be called.
104*0e209d39SAndroid Build Coastguard Worker  * <p>
105*0e209d39SAndroid Build Coastguard Worker  * Restriction: <br>
106*0e209d39SAndroid Build Coastguard Worker  * Currently there are no composite characters that consists of a
107*0e209d39SAndroid Build Coastguard Worker  * character with combining class > 0 before a character with combining
108*0e209d39SAndroid Build Coastguard Worker  * class == 0. However, if such a character exists in the future,
109*0e209d39SAndroid Build Coastguard Worker  * <tt>StringSearch</tt> does not guarantee the results for option 1.
110*0e209d39SAndroid Build Coastguard Worker  * <p>
111*0e209d39SAndroid Build Coastguard Worker  * Consult the <tt>SearchIterator</tt> documentation for information on
112*0e209d39SAndroid Build Coastguard Worker  * and examples of how to use instances of this class to implement text
113*0e209d39SAndroid Build Coastguard Worker  * searching.
114*0e209d39SAndroid Build Coastguard Worker  * <pre><code>
115*0e209d39SAndroid Build Coastguard Worker  * UnicodeString target("The quick brown fox jumps over the lazy dog.");
116*0e209d39SAndroid Build Coastguard Worker  * UnicodeString pattern("fox");
117*0e209d39SAndroid Build Coastguard Worker  *
118*0e209d39SAndroid Build Coastguard Worker  * UErrorCode      error = U_ZERO_ERROR;
119*0e209d39SAndroid Build Coastguard Worker  * StringSearch iter(pattern, target, Locale::getUS(), nullptr, status);
120*0e209d39SAndroid Build Coastguard Worker  * for (int pos = iter.first(error);
121*0e209d39SAndroid Build Coastguard Worker  *      pos != USEARCH_DONE;
122*0e209d39SAndroid Build Coastguard Worker  *      pos = iter.next(error))
123*0e209d39SAndroid Build Coastguard Worker  * {
124*0e209d39SAndroid Build Coastguard Worker  *     printf("Found match at %d pos, length is %d\n", pos, iter.getMatchedLength());
125*0e209d39SAndroid Build Coastguard Worker  * }
126*0e209d39SAndroid Build Coastguard Worker  * </code></pre>
127*0e209d39SAndroid Build Coastguard Worker  * <p>
128*0e209d39SAndroid Build Coastguard Worker  * Note, <tt>StringSearch</tt> is not to be subclassed.
129*0e209d39SAndroid Build Coastguard Worker  * </p>
130*0e209d39SAndroid Build Coastguard Worker  * @see SearchIterator
131*0e209d39SAndroid Build Coastguard Worker  * @see RuleBasedCollator
132*0e209d39SAndroid Build Coastguard Worker  * @since ICU 2.0
133*0e209d39SAndroid Build Coastguard Worker  */
134*0e209d39SAndroid Build Coastguard Worker 
135*0e209d39SAndroid Build Coastguard Worker class U_I18N_API StringSearch final : public SearchIterator
136*0e209d39SAndroid Build Coastguard Worker {
137*0e209d39SAndroid Build Coastguard Worker public:
138*0e209d39SAndroid Build Coastguard Worker 
139*0e209d39SAndroid Build Coastguard Worker     // public constructors and destructors --------------------------------
140*0e209d39SAndroid Build Coastguard Worker 
141*0e209d39SAndroid Build Coastguard Worker     /**
142*0e209d39SAndroid Build Coastguard Worker      * Creating a <tt>StringSearch</tt> instance using the argument locale
143*0e209d39SAndroid Build Coastguard Worker      * language rule set. A collator will be created in the process, which
144*0e209d39SAndroid Build Coastguard Worker      * will be owned by this instance and will be deleted during
145*0e209d39SAndroid Build Coastguard Worker      * destruction
146*0e209d39SAndroid Build Coastguard Worker      * @param pattern The text for which this object will search.
147*0e209d39SAndroid Build Coastguard Worker      * @param text    The text in which to search for the pattern.
148*0e209d39SAndroid Build Coastguard Worker      * @param locale  A locale which defines the language-sensitive
149*0e209d39SAndroid Build Coastguard Worker      *                comparison rules used to determine whether text in the
150*0e209d39SAndroid Build Coastguard Worker      *                pattern and target matches.
151*0e209d39SAndroid Build Coastguard Worker      * @param breakiter A <tt>BreakIterator</tt> object used to constrain
152*0e209d39SAndroid Build Coastguard Worker      *                the matches that are found. Matches whose start and end
153*0e209d39SAndroid Build Coastguard Worker      *                indices in the target text are not boundaries as
154*0e209d39SAndroid Build Coastguard Worker      *                determined by the <tt>BreakIterator</tt> are
155*0e209d39SAndroid Build Coastguard Worker      *                ignored. If this behavior is not desired,
156*0e209d39SAndroid Build Coastguard Worker      *                <tt>nullptr</tt> can be passed in instead.
157*0e209d39SAndroid Build Coastguard Worker      * @param status  for errors if any. If pattern or text is nullptr, or if
158*0e209d39SAndroid Build Coastguard Worker      *               either the length of pattern or text is 0 then an
159*0e209d39SAndroid Build Coastguard Worker      *               U_ILLEGAL_ARGUMENT_ERROR is returned.
160*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
161*0e209d39SAndroid Build Coastguard Worker      */
162*0e209d39SAndroid Build Coastguard Worker     StringSearch(const UnicodeString &pattern, const UnicodeString &text,
163*0e209d39SAndroid Build Coastguard Worker                  const Locale        &locale,
164*0e209d39SAndroid Build Coastguard Worker                        BreakIterator *breakiter,
165*0e209d39SAndroid Build Coastguard Worker                        UErrorCode    &status);
166*0e209d39SAndroid Build Coastguard Worker 
167*0e209d39SAndroid Build Coastguard Worker     /**
168*0e209d39SAndroid Build Coastguard Worker      * Creating a <tt>StringSearch</tt> instance using the argument collator
169*0e209d39SAndroid Build Coastguard Worker      * language rule set. Note, user retains the ownership of this collator,
170*0e209d39SAndroid Build Coastguard Worker      * it does not get destroyed during this instance's destruction.
171*0e209d39SAndroid Build Coastguard Worker      * @param pattern The text for which this object will search.
172*0e209d39SAndroid Build Coastguard Worker      * @param text    The text in which to search for the pattern.
173*0e209d39SAndroid Build Coastguard Worker      * @param coll    A <tt>RuleBasedCollator</tt> object which defines
174*0e209d39SAndroid Build Coastguard Worker      *                the language-sensitive comparison rules used to
175*0e209d39SAndroid Build Coastguard Worker      *                determine whether text in the pattern and target
176*0e209d39SAndroid Build Coastguard Worker      *                matches. User is responsible for the clearing of this
177*0e209d39SAndroid Build Coastguard Worker      *                object.
178*0e209d39SAndroid Build Coastguard Worker      * @param breakiter A <tt>BreakIterator</tt> object used to constrain
179*0e209d39SAndroid Build Coastguard Worker      *                the matches that are found. Matches whose start and end
180*0e209d39SAndroid Build Coastguard Worker      *                indices in the target text are not boundaries as
181*0e209d39SAndroid Build Coastguard Worker      *                determined by the <tt>BreakIterator</tt> are
182*0e209d39SAndroid Build Coastguard Worker      *                ignored. If this behavior is not desired,
183*0e209d39SAndroid Build Coastguard Worker      *                <tt>nullptr</tt> can be passed in instead.
184*0e209d39SAndroid Build Coastguard Worker      * @param status for errors if any. If either the length of pattern or
185*0e209d39SAndroid Build Coastguard Worker      *               text is 0 then an U_ILLEGAL_ARGUMENT_ERROR is returned.
186*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
187*0e209d39SAndroid Build Coastguard Worker      */
188*0e209d39SAndroid Build Coastguard Worker     StringSearch(const UnicodeString     &pattern,
189*0e209d39SAndroid Build Coastguard Worker                  const UnicodeString     &text,
190*0e209d39SAndroid Build Coastguard Worker                        RuleBasedCollator *coll,
191*0e209d39SAndroid Build Coastguard Worker                        BreakIterator     *breakiter,
192*0e209d39SAndroid Build Coastguard Worker                        UErrorCode        &status);
193*0e209d39SAndroid Build Coastguard Worker 
194*0e209d39SAndroid Build Coastguard Worker     /**
195*0e209d39SAndroid Build Coastguard Worker      * Creating a <tt>StringSearch</tt> instance using the argument locale
196*0e209d39SAndroid Build Coastguard Worker      * language rule set. A collator will be created in the process, which
197*0e209d39SAndroid Build Coastguard Worker      * will be owned by this instance and will be deleted during
198*0e209d39SAndroid Build Coastguard Worker      * destruction
199*0e209d39SAndroid Build Coastguard Worker      * <p>
200*0e209d39SAndroid Build Coastguard Worker      * Note: No parsing of the text within the <tt>CharacterIterator</tt>
201*0e209d39SAndroid Build Coastguard Worker      * will be done during searching for this version. The block of text
202*0e209d39SAndroid Build Coastguard Worker      * in <tt>CharacterIterator</tt> will be used as it is.
203*0e209d39SAndroid Build Coastguard Worker      * @param pattern The text for which this object will search.
204*0e209d39SAndroid Build Coastguard Worker      * @param text    The text iterator in which to search for the pattern.
205*0e209d39SAndroid Build Coastguard Worker      * @param locale  A locale which defines the language-sensitive
206*0e209d39SAndroid Build Coastguard Worker      *                comparison rules used to determine whether text in the
207*0e209d39SAndroid Build Coastguard Worker      *                pattern and target matches. User is responsible for
208*0e209d39SAndroid Build Coastguard Worker      *                the clearing of this object.
209*0e209d39SAndroid Build Coastguard Worker      * @param breakiter A <tt>BreakIterator</tt> object used to constrain
210*0e209d39SAndroid Build Coastguard Worker      *                the matches that are found. Matches whose start and end
211*0e209d39SAndroid Build Coastguard Worker      *                indices in the target text are not boundaries as
212*0e209d39SAndroid Build Coastguard Worker      *                determined by the <tt>BreakIterator</tt> are
213*0e209d39SAndroid Build Coastguard Worker      *                ignored. If this behavior is not desired,
214*0e209d39SAndroid Build Coastguard Worker      *                <tt>nullptr</tt> can be passed in instead.
215*0e209d39SAndroid Build Coastguard Worker      * @param status for errors if any. If either the length of pattern or
216*0e209d39SAndroid Build Coastguard Worker      *               text is 0 then an U_ILLEGAL_ARGUMENT_ERROR is returned.
217*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
218*0e209d39SAndroid Build Coastguard Worker      */
219*0e209d39SAndroid Build Coastguard Worker     StringSearch(const UnicodeString &pattern, CharacterIterator &text,
220*0e209d39SAndroid Build Coastguard Worker                  const Locale        &locale,
221*0e209d39SAndroid Build Coastguard Worker                        BreakIterator *breakiter,
222*0e209d39SAndroid Build Coastguard Worker                        UErrorCode    &status);
223*0e209d39SAndroid Build Coastguard Worker 
224*0e209d39SAndroid Build Coastguard Worker     /**
225*0e209d39SAndroid Build Coastguard Worker      * Creating a <tt>StringSearch</tt> instance using the argument collator
226*0e209d39SAndroid Build Coastguard Worker      * language rule set. Note, user retains the ownership of this collator,
227*0e209d39SAndroid Build Coastguard Worker      * it does not get destroyed during this instance's destruction.
228*0e209d39SAndroid Build Coastguard Worker      * <p>
229*0e209d39SAndroid Build Coastguard Worker      * Note: No parsing of the text within the <tt>CharacterIterator</tt>
230*0e209d39SAndroid Build Coastguard Worker      * will be done during searching for this version. The block of text
231*0e209d39SAndroid Build Coastguard Worker      * in <tt>CharacterIterator</tt> will be used as it is.
232*0e209d39SAndroid Build Coastguard Worker      * @param pattern The text for which this object will search.
233*0e209d39SAndroid Build Coastguard Worker      * @param text    The text in which to search for the pattern.
234*0e209d39SAndroid Build Coastguard Worker      * @param coll    A <tt>RuleBasedCollator</tt> object which defines
235*0e209d39SAndroid Build Coastguard Worker      *                the language-sensitive comparison rules used to
236*0e209d39SAndroid Build Coastguard Worker      *                determine whether text in the pattern and target
237*0e209d39SAndroid Build Coastguard Worker      *                matches. User is responsible for the clearing of this
238*0e209d39SAndroid Build Coastguard Worker      *                object.
239*0e209d39SAndroid Build Coastguard Worker      * @param breakiter A <tt>BreakIterator</tt> object used to constrain
240*0e209d39SAndroid Build Coastguard Worker      *                the matches that are found. Matches whose start and end
241*0e209d39SAndroid Build Coastguard Worker      *                indices in the target text are not boundaries as
242*0e209d39SAndroid Build Coastguard Worker      *                determined by the <tt>BreakIterator</tt> are
243*0e209d39SAndroid Build Coastguard Worker      *                ignored. If this behavior is not desired,
244*0e209d39SAndroid Build Coastguard Worker      *                <tt>nullptr</tt> can be passed in instead.
245*0e209d39SAndroid Build Coastguard Worker      * @param status for errors if any. If either the length of pattern or
246*0e209d39SAndroid Build Coastguard Worker      *               text is 0 then an U_ILLEGAL_ARGUMENT_ERROR is returned.
247*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
248*0e209d39SAndroid Build Coastguard Worker      */
249*0e209d39SAndroid Build Coastguard Worker     StringSearch(const UnicodeString     &pattern, CharacterIterator &text,
250*0e209d39SAndroid Build Coastguard Worker                        RuleBasedCollator *coll,
251*0e209d39SAndroid Build Coastguard Worker                        BreakIterator     *breakiter,
252*0e209d39SAndroid Build Coastguard Worker                        UErrorCode        &status);
253*0e209d39SAndroid Build Coastguard Worker 
254*0e209d39SAndroid Build Coastguard Worker     /**
255*0e209d39SAndroid Build Coastguard Worker      * Copy constructor that creates a StringSearch instance with the same
256*0e209d39SAndroid Build Coastguard Worker      * behavior, and iterating over the same text.
257*0e209d39SAndroid Build Coastguard Worker      * @param that StringSearch instance to be copied.
258*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
259*0e209d39SAndroid Build Coastguard Worker      */
260*0e209d39SAndroid Build Coastguard Worker     StringSearch(const StringSearch &that);
261*0e209d39SAndroid Build Coastguard Worker 
262*0e209d39SAndroid Build Coastguard Worker     /**
263*0e209d39SAndroid Build Coastguard Worker     * Destructor. Cleans up the search iterator data struct.
264*0e209d39SAndroid Build Coastguard Worker     * If a collator is created in the constructor, it will be destroyed here.
265*0e209d39SAndroid Build Coastguard Worker     * @stable ICU 2.0
266*0e209d39SAndroid Build Coastguard Worker     */
267*0e209d39SAndroid Build Coastguard Worker     virtual ~StringSearch();
268*0e209d39SAndroid Build Coastguard Worker 
269*0e209d39SAndroid Build Coastguard Worker     /**
270*0e209d39SAndroid Build Coastguard Worker      * Clone this object.
271*0e209d39SAndroid Build Coastguard Worker      * Clones can be used concurrently in multiple threads.
272*0e209d39SAndroid Build Coastguard Worker      * If an error occurs, then nullptr is returned.
273*0e209d39SAndroid Build Coastguard Worker      * The caller must delete the clone.
274*0e209d39SAndroid Build Coastguard Worker      *
275*0e209d39SAndroid Build Coastguard Worker      * @return a clone of this object
276*0e209d39SAndroid Build Coastguard Worker      *
277*0e209d39SAndroid Build Coastguard Worker      * @see getDynamicClassID
278*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.8
279*0e209d39SAndroid Build Coastguard Worker      */
280*0e209d39SAndroid Build Coastguard Worker     StringSearch *clone() const;
281*0e209d39SAndroid Build Coastguard Worker 
282*0e209d39SAndroid Build Coastguard Worker     // operator overloading ---------------------------------------------
283*0e209d39SAndroid Build Coastguard Worker 
284*0e209d39SAndroid Build Coastguard Worker     /**
285*0e209d39SAndroid Build Coastguard Worker      * Assignment operator. Sets this iterator to have the same behavior,
286*0e209d39SAndroid Build Coastguard Worker      * and iterate over the same text, as the one passed in.
287*0e209d39SAndroid Build Coastguard Worker      * @param that instance to be copied.
288*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
289*0e209d39SAndroid Build Coastguard Worker      */
290*0e209d39SAndroid Build Coastguard Worker     StringSearch & operator=(const StringSearch &that);
291*0e209d39SAndroid Build Coastguard Worker 
292*0e209d39SAndroid Build Coastguard Worker     /**
293*0e209d39SAndroid Build Coastguard Worker      * Equality operator.
294*0e209d39SAndroid Build Coastguard Worker      * @param that instance to be compared.
295*0e209d39SAndroid Build Coastguard Worker      * @return true if both instances have the same attributes,
296*0e209d39SAndroid Build Coastguard Worker      *         breakiterators, collators and iterate over the same text
297*0e209d39SAndroid Build Coastguard Worker      *         while looking for the same pattern.
298*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
299*0e209d39SAndroid Build Coastguard Worker      */
300*0e209d39SAndroid Build Coastguard Worker     virtual bool operator==(const SearchIterator &that) const override;
301*0e209d39SAndroid Build Coastguard Worker 
302*0e209d39SAndroid Build Coastguard Worker     // public get and set methods ----------------------------------------
303*0e209d39SAndroid Build Coastguard Worker 
304*0e209d39SAndroid Build Coastguard Worker     /**
305*0e209d39SAndroid Build Coastguard Worker      * Sets the index to point to the given position, and clears any state
306*0e209d39SAndroid Build Coastguard Worker      * that's affected.
307*0e209d39SAndroid Build Coastguard Worker      * <p>
308*0e209d39SAndroid Build Coastguard Worker      * This method takes the argument index and sets the position in the text
309*0e209d39SAndroid Build Coastguard Worker      * string accordingly without checking if the index is pointing to a
310*0e209d39SAndroid Build Coastguard Worker      * valid starting point to begin searching.
311*0e209d39SAndroid Build Coastguard Worker      * @param position within the text to be set. If position is less
312*0e209d39SAndroid Build Coastguard Worker      *          than or greater than the text range for searching,
313*0e209d39SAndroid Build Coastguard Worker      *          an U_INDEX_OUTOFBOUNDS_ERROR will be returned
314*0e209d39SAndroid Build Coastguard Worker      * @param status for errors if it occurs
315*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
316*0e209d39SAndroid Build Coastguard Worker      */
317*0e209d39SAndroid Build Coastguard Worker     virtual void setOffset(int32_t position, UErrorCode &status) override;
318*0e209d39SAndroid Build Coastguard Worker 
319*0e209d39SAndroid Build Coastguard Worker     /**
320*0e209d39SAndroid Build Coastguard Worker      * Return the current index in the text being searched.
321*0e209d39SAndroid Build Coastguard Worker      * If the iteration has gone past the end of the text
322*0e209d39SAndroid Build Coastguard Worker      * (or past the beginning for a backwards search), USEARCH_DONE
323*0e209d39SAndroid Build Coastguard Worker      * is returned.
324*0e209d39SAndroid Build Coastguard Worker      * @return current index in the text being searched.
325*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
326*0e209d39SAndroid Build Coastguard Worker      */
327*0e209d39SAndroid Build Coastguard Worker     virtual int32_t getOffset() const override;
328*0e209d39SAndroid Build Coastguard Worker 
329*0e209d39SAndroid Build Coastguard Worker     /**
330*0e209d39SAndroid Build Coastguard Worker      * Set the target text to be searched.
331*0e209d39SAndroid Build Coastguard Worker      * Text iteration will hence begin at the start of the text string.
332*0e209d39SAndroid Build Coastguard Worker      * This method is
333*0e209d39SAndroid Build Coastguard Worker      * useful if you want to re-use an iterator to search for the same
334*0e209d39SAndroid Build Coastguard Worker      * pattern within a different body of text.
335*0e209d39SAndroid Build Coastguard Worker      * @param text text string to be searched
336*0e209d39SAndroid Build Coastguard Worker      * @param status for errors if any. If the text length is 0 then an
337*0e209d39SAndroid Build Coastguard Worker      *        U_ILLEGAL_ARGUMENT_ERROR is returned.
338*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
339*0e209d39SAndroid Build Coastguard Worker      */
340*0e209d39SAndroid Build Coastguard Worker     virtual void setText(const UnicodeString &text, UErrorCode &status) override;
341*0e209d39SAndroid Build Coastguard Worker 
342*0e209d39SAndroid Build Coastguard Worker     /**
343*0e209d39SAndroid Build Coastguard Worker      * Set the target text to be searched.
344*0e209d39SAndroid Build Coastguard Worker      * Text iteration will hence begin at the start of the text string.
345*0e209d39SAndroid Build Coastguard Worker      * This method is
346*0e209d39SAndroid Build Coastguard Worker      * useful if you want to re-use an iterator to search for the same
347*0e209d39SAndroid Build Coastguard Worker      * pattern within a different body of text.
348*0e209d39SAndroid Build Coastguard Worker      * Note: No parsing of the text within the <tt>CharacterIterator</tt>
349*0e209d39SAndroid Build Coastguard Worker      * will be done during searching for this version. The block of text
350*0e209d39SAndroid Build Coastguard Worker      * in <tt>CharacterIterator</tt> will be used as it is.
351*0e209d39SAndroid Build Coastguard Worker      * @param text text string to be searched
352*0e209d39SAndroid Build Coastguard Worker      * @param status for errors if any. If the text length is 0 then an
353*0e209d39SAndroid Build Coastguard Worker      *        U_ILLEGAL_ARGUMENT_ERROR is returned.
354*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
355*0e209d39SAndroid Build Coastguard Worker      */
356*0e209d39SAndroid Build Coastguard Worker     virtual void setText(CharacterIterator &text, UErrorCode &status) override;
357*0e209d39SAndroid Build Coastguard Worker 
358*0e209d39SAndroid Build Coastguard Worker     /**
359*0e209d39SAndroid Build Coastguard Worker      * Gets the collator used for the language rules.
360*0e209d39SAndroid Build Coastguard Worker      * <p>
361*0e209d39SAndroid Build Coastguard Worker      * Caller may modify but <b>must not</b> delete the <tt>RuleBasedCollator</tt>!
362*0e209d39SAndroid Build Coastguard Worker      * Modifications to this collator will affect the original collator passed in to
363*0e209d39SAndroid Build Coastguard Worker      * the <tt>StringSearch></tt> constructor or to setCollator, if any.
364*0e209d39SAndroid Build Coastguard Worker      * @return collator used for string search
365*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
366*0e209d39SAndroid Build Coastguard Worker      */
367*0e209d39SAndroid Build Coastguard Worker     RuleBasedCollator * getCollator() const;
368*0e209d39SAndroid Build Coastguard Worker 
369*0e209d39SAndroid Build Coastguard Worker     /**
370*0e209d39SAndroid Build Coastguard Worker      * Sets the collator used for the language rules. User retains the
371*0e209d39SAndroid Build Coastguard Worker      * ownership of this collator, thus the responsibility of deletion lies
372*0e209d39SAndroid Build Coastguard Worker      * with the user. The iterator's position will not be changed by this method.
373*0e209d39SAndroid Build Coastguard Worker      * @param coll    collator
374*0e209d39SAndroid Build Coastguard Worker      * @param status  for errors if any
375*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
376*0e209d39SAndroid Build Coastguard Worker      */
377*0e209d39SAndroid Build Coastguard Worker     void setCollator(RuleBasedCollator *coll, UErrorCode &status);
378*0e209d39SAndroid Build Coastguard Worker 
379*0e209d39SAndroid Build Coastguard Worker     /**
380*0e209d39SAndroid Build Coastguard Worker      * Sets the pattern used for matching.
381*0e209d39SAndroid Build Coastguard Worker      * The iterator's position will not be changed by this method.
382*0e209d39SAndroid Build Coastguard Worker      * @param pattern search pattern to be found
383*0e209d39SAndroid Build Coastguard Worker      * @param status for errors if any. If the pattern length is 0 then an
384*0e209d39SAndroid Build Coastguard Worker      *               U_ILLEGAL_ARGUMENT_ERROR is returned.
385*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
386*0e209d39SAndroid Build Coastguard Worker      */
387*0e209d39SAndroid Build Coastguard Worker     void setPattern(const UnicodeString &pattern, UErrorCode &status);
388*0e209d39SAndroid Build Coastguard Worker 
389*0e209d39SAndroid Build Coastguard Worker     /**
390*0e209d39SAndroid Build Coastguard Worker      * Gets the search pattern.
391*0e209d39SAndroid Build Coastguard Worker      * @return pattern used for matching
392*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
393*0e209d39SAndroid Build Coastguard Worker      */
394*0e209d39SAndroid Build Coastguard Worker     const UnicodeString & getPattern() const;
395*0e209d39SAndroid Build Coastguard Worker 
396*0e209d39SAndroid Build Coastguard Worker     // public methods ----------------------------------------------------
397*0e209d39SAndroid Build Coastguard Worker 
398*0e209d39SAndroid Build Coastguard Worker     /**
399*0e209d39SAndroid Build Coastguard Worker      * Reset the iteration.
400*0e209d39SAndroid Build Coastguard Worker      * Search will begin at the start of the text string if a forward
401*0e209d39SAndroid Build Coastguard Worker      * iteration is initiated before a backwards iteration. Otherwise if
402*0e209d39SAndroid Build Coastguard Worker      * a backwards iteration is initiated before a forwards iteration, the
403*0e209d39SAndroid Build Coastguard Worker      * search will begin at the end of the text string.
404*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
405*0e209d39SAndroid Build Coastguard Worker      */
406*0e209d39SAndroid Build Coastguard Worker     virtual void reset() override;
407*0e209d39SAndroid Build Coastguard Worker 
408*0e209d39SAndroid Build Coastguard Worker     /**
409*0e209d39SAndroid Build Coastguard Worker      * Returns a copy of StringSearch with the same behavior, and
410*0e209d39SAndroid Build Coastguard Worker      * iterating over the same text, as this one. Note that all data will be
411*0e209d39SAndroid Build Coastguard Worker      * replicated, except for the user-specified collator and the
412*0e209d39SAndroid Build Coastguard Worker      * breakiterator.
413*0e209d39SAndroid Build Coastguard Worker      * @return cloned object
414*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
415*0e209d39SAndroid Build Coastguard Worker      */
416*0e209d39SAndroid Build Coastguard Worker     virtual StringSearch * safeClone() const override;
417*0e209d39SAndroid Build Coastguard Worker 
418*0e209d39SAndroid Build Coastguard Worker     /**
419*0e209d39SAndroid Build Coastguard Worker      * ICU "poor man's RTTI", returns a UClassID for the actual class.
420*0e209d39SAndroid Build Coastguard Worker      *
421*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.2
422*0e209d39SAndroid Build Coastguard Worker      */
423*0e209d39SAndroid Build Coastguard Worker     virtual UClassID getDynamicClassID() const override;
424*0e209d39SAndroid Build Coastguard Worker 
425*0e209d39SAndroid Build Coastguard Worker     /**
426*0e209d39SAndroid Build Coastguard Worker      * ICU "poor man's RTTI", returns a UClassID for this class.
427*0e209d39SAndroid Build Coastguard Worker      *
428*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.2
429*0e209d39SAndroid Build Coastguard Worker      */
430*0e209d39SAndroid Build Coastguard Worker     static UClassID U_EXPORT2 getStaticClassID();
431*0e209d39SAndroid Build Coastguard Worker 
432*0e209d39SAndroid Build Coastguard Worker protected:
433*0e209d39SAndroid Build Coastguard Worker 
434*0e209d39SAndroid Build Coastguard Worker     // protected method -------------------------------------------------
435*0e209d39SAndroid Build Coastguard Worker 
436*0e209d39SAndroid Build Coastguard Worker     /**
437*0e209d39SAndroid Build Coastguard Worker      * Search forward for matching text, starting at a given location.
438*0e209d39SAndroid Build Coastguard Worker      * Clients should not call this method directly; instead they should
439*0e209d39SAndroid Build Coastguard Worker      * call {@link SearchIterator#next }.
440*0e209d39SAndroid Build Coastguard Worker      * <p>
441*0e209d39SAndroid Build Coastguard Worker      * If a match is found, this method returns the index at which the match
442*0e209d39SAndroid Build Coastguard Worker      * starts and calls {@link SearchIterator#setMatchLength } with the number
443*0e209d39SAndroid Build Coastguard Worker      * of characters in the target text that make up the match. If no match
444*0e209d39SAndroid Build Coastguard Worker      * is found, the method returns <tt>USEARCH_DONE</tt>.
445*0e209d39SAndroid Build Coastguard Worker      * <p>
446*0e209d39SAndroid Build Coastguard Worker      * The <tt>StringSearch</tt> is adjusted so that its current index
447*0e209d39SAndroid Build Coastguard Worker      * (as returned by {@link #getOffset }) is the match position if one was
448*0e209d39SAndroid Build Coastguard Worker      * found.
449*0e209d39SAndroid Build Coastguard Worker      * If a match is not found, <tt>USEARCH_DONE</tt> will be returned and
450*0e209d39SAndroid Build Coastguard Worker      * the <tt>StringSearch</tt> will be adjusted to the index USEARCH_DONE.
451*0e209d39SAndroid Build Coastguard Worker      * @param position The index in the target text at which the search
452*0e209d39SAndroid Build Coastguard Worker      *                 starts
453*0e209d39SAndroid Build Coastguard Worker      * @param status for errors if any occurs
454*0e209d39SAndroid Build Coastguard Worker      * @return The index at which the matched text in the target starts, or
455*0e209d39SAndroid Build Coastguard Worker      *         USEARCH_DONE if no match was found.
456*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
457*0e209d39SAndroid Build Coastguard Worker      */
458*0e209d39SAndroid Build Coastguard Worker     virtual int32_t handleNext(int32_t position, UErrorCode &status) override;
459*0e209d39SAndroid Build Coastguard Worker 
460*0e209d39SAndroid Build Coastguard Worker     /**
461*0e209d39SAndroid Build Coastguard Worker      * Search backward for matching text, starting at a given location.
462*0e209d39SAndroid Build Coastguard Worker      * Clients should not call this method directly; instead they should call
463*0e209d39SAndroid Build Coastguard Worker      * <tt>SearchIterator.previous()</tt>, which this method overrides.
464*0e209d39SAndroid Build Coastguard Worker      * <p>
465*0e209d39SAndroid Build Coastguard Worker      * If a match is found, this method returns the index at which the match
466*0e209d39SAndroid Build Coastguard Worker      * starts and calls {@link SearchIterator#setMatchLength } with the number
467*0e209d39SAndroid Build Coastguard Worker      * of characters in the target text that make up the match. If no match
468*0e209d39SAndroid Build Coastguard Worker      * is found, the method returns <tt>USEARCH_DONE</tt>.
469*0e209d39SAndroid Build Coastguard Worker      * <p>
470*0e209d39SAndroid Build Coastguard Worker      * The <tt>StringSearch</tt> is adjusted so that its current index
471*0e209d39SAndroid Build Coastguard Worker      * (as returned by {@link #getOffset }) is the match position if one was
472*0e209d39SAndroid Build Coastguard Worker      * found.
473*0e209d39SAndroid Build Coastguard Worker      * If a match is not found, <tt>USEARCH_DONE</tt> will be returned and
474*0e209d39SAndroid Build Coastguard Worker      * the <tt>StringSearch</tt> will be adjusted to the index USEARCH_DONE.
475*0e209d39SAndroid Build Coastguard Worker      * @param position The index in the target text at which the search
476*0e209d39SAndroid Build Coastguard Worker      *                 starts.
477*0e209d39SAndroid Build Coastguard Worker      * @param status for errors if any occurs
478*0e209d39SAndroid Build Coastguard Worker      * @return The index at which the matched text in the target starts, or
479*0e209d39SAndroid Build Coastguard Worker      *         USEARCH_DONE if no match was found.
480*0e209d39SAndroid Build Coastguard Worker      * @stable ICU 2.0
481*0e209d39SAndroid Build Coastguard Worker      */
482*0e209d39SAndroid Build Coastguard Worker     virtual int32_t handlePrev(int32_t position, UErrorCode &status) override;
483*0e209d39SAndroid Build Coastguard Worker 
484*0e209d39SAndroid Build Coastguard Worker private :
485*0e209d39SAndroid Build Coastguard Worker     StringSearch() = delete; // default constructor not implemented
486*0e209d39SAndroid Build Coastguard Worker 
487*0e209d39SAndroid Build Coastguard Worker     // private data members ----------------------------------------------
488*0e209d39SAndroid Build Coastguard Worker 
489*0e209d39SAndroid Build Coastguard Worker     /**
490*0e209d39SAndroid Build Coastguard Worker     * Pattern text
491*0e209d39SAndroid Build Coastguard Worker     * @stable ICU 2.0
492*0e209d39SAndroid Build Coastguard Worker     */
493*0e209d39SAndroid Build Coastguard Worker     UnicodeString      m_pattern_;
494*0e209d39SAndroid Build Coastguard Worker     /**
495*0e209d39SAndroid Build Coastguard Worker     * String search struct data
496*0e209d39SAndroid Build Coastguard Worker     * @stable ICU 2.0
497*0e209d39SAndroid Build Coastguard Worker     */
498*0e209d39SAndroid Build Coastguard Worker     UStringSearch     *m_strsrch_;
499*0e209d39SAndroid Build Coastguard Worker 
500*0e209d39SAndroid Build Coastguard Worker };
501*0e209d39SAndroid Build Coastguard Worker 
502*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END
503*0e209d39SAndroid Build Coastguard Worker 
504*0e209d39SAndroid Build Coastguard Worker #endif /* #if !UCONFIG_NO_COLLATION */
505*0e209d39SAndroid Build Coastguard Worker 
506*0e209d39SAndroid Build Coastguard Worker #endif /* U_SHOW_CPLUSPLUS_API */
507*0e209d39SAndroid Build Coastguard Worker 
508*0e209d39SAndroid Build Coastguard Worker #endif
509*0e209d39SAndroid Build Coastguard Worker 
510