2 **********************************************************************
3 * Copyright (C) 2001-2006 IBM and others. All rights reserved.
4 **********************************************************************
5 * Date Name Description
6 * 03/22/2000 helena Creation.
7 **********************************************************************
13 #include "unicode/utypes.h"
17 * \brief C++ API: Service for searching text based on RuleBasedCollator.
20 #if !UCONFIG_NO_COLLATION
22 #include "unicode/tblcoll.h"
23 #include "unicode/coleitr.h"
24 #include "unicode/search.h"
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 ß 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.
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.
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
49 * A pattern string P matches a text string S at the offsets <start, end>
52 * option 1. Some canonical equivalent of P matches some canonical equivalent
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?
58 * Option 2. will be the default.
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
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
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.
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.
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.
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.
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.
113 * Consult the <tt>SearchIterator</tt> documentation for information on
114 * and examples of how to use instances of this class to implement text
117 * UnicodeString target("The quick brown fox jumped over the lazy fox");
118 * UnicodeString pattern("fox");
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());
129 * Note, StringSearch is not to be subclassed.
131 * @see SearchIterator
132 * @see RuleBasedCollator
136 class U_I18N_API StringSearch
: public SearchIterator
140 // public constructors and destructors --------------------------------
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
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.
163 StringSearch(const UnicodeString
&pattern
, const UnicodeString
&text
,
164 const Locale
&locale
,
165 BreakIterator
*breakiter
,
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
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.
189 StringSearch(const UnicodeString
&pattern
,
190 const UnicodeString
&text
,
191 RuleBasedCollator
*coll
,
192 BreakIterator
*breakiter
,
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
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.
220 StringSearch(const UnicodeString
&pattern
, CharacterIterator
&text
,
221 const Locale
&locale
,
222 BreakIterator
*breakiter
,
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.
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
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.
250 StringSearch(const UnicodeString
&pattern
, CharacterIterator
&text
,
251 RuleBasedCollator
*coll
,
252 BreakIterator
*breakiter
,
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.
261 StringSearch(const StringSearch
&that
);
264 * Destructor. Cleans up the search iterator data struct.
265 * If a collator is created in the constructor, it will be destroyed here.
268 virtual ~StringSearch(void);
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.
276 * @return a clone of this object
278 * @see getDynamicClassID
281 StringSearch
*clone() const;
283 // operator overloading ---------------------------------------------
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.
291 StringSearch
& operator=(const StringSearch
&that
);
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.
301 virtual UBool
operator==(const SearchIterator
&that
) const;
303 // public get and set methods ----------------------------------------
306 * Sets the index to point to the given position, and clears any state
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
318 virtual void setOffset(int32_t position
, UErrorCode
&status
);
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
325 * @return current index in the text being searched.
328 virtual int32_t getOffset(void) const;
331 * Set the target text to be searched.
332 * Text iteration will hence begin at the start of the text string.
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.
341 virtual void setText(const UnicodeString
&text
, UErrorCode
&status
);
344 * Set the target text to be searched.
345 * Text iteration will hence begin at the start of the text string.
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.
357 virtual void setText(CharacterIterator
&text
, UErrorCode
&status
);
360 * Gets the collator used for the language rules.
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
368 RuleBasedCollator
* getCollator() const;
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
376 * @param coll collator
377 * @param status for errors if any
380 void setCollator(RuleBasedCollator
*coll
, UErrorCode
&status
);
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.
391 void setPattern(const UnicodeString
&pattern
, UErrorCode
&status
);
394 * Gets the search pattern.
395 * @return pattern used for matching
398 const UnicodeString
& getPattern() const;
400 // public methods ----------------------------------------------------
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.
410 virtual void reset();
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
417 * @return cloned object
420 virtual SearchIterator
* safeClone(void) const;
423 * ICU "poor man's RTTI", returns a UClassID for the actual class.
427 virtual UClassID
getDynamicClassID() const;
430 * ICU "poor man's RTTI", returns a UClassID for this class.
434 static UClassID U_EXPORT2
getStaticClassID();
438 // protected method -------------------------------------------------
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 }.
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>.
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
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
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.
462 virtual int32_t handleNext(int32_t position
, UErrorCode
&status
);
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.
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>.
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
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
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.
486 virtual int32_t handlePrev(int32_t position
, UErrorCode
&status
);
489 StringSearch(); // default constructor not implemented
491 // private data members ----------------------------------------------
494 * RuleBasedCollator, contains exactly the same UCollator * in m_strsrch_
497 RuleBasedCollator m_collator_
;
502 UnicodeString m_pattern_
;
504 * String search struct data
507 UStringSearch
*m_strsrch_
;
513 #endif /* #if !UCONFIG_NO_COLLATION */