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