OLD | NEW |
1 /* | 1 /* |
2 ********************************************************************** | 2 ********************************************************************** |
3 * Copyright (C) 2001-2008 IBM and others. All rights reserved. | 3 * Copyright (C) 2001-2014 IBM and others. All rights reserved. |
4 ********************************************************************** | 4 ********************************************************************** |
5 * Date Name Description | 5 * Date Name Description |
6 * 03/22/2000 helena Creation. | 6 * 03/22/2000 helena Creation. |
7 ********************************************************************** | 7 ********************************************************************** |
8 */ | 8 */ |
9 | 9 |
10 #ifndef STSEARCH_H | 10 #ifndef STSEARCH_H |
11 #define STSEARCH_H | 11 #define STSEARCH_H |
12 | 12 |
13 #include "unicode/utypes.h" | 13 #include "unicode/utypes.h" |
14 | 14 |
15 /** | 15 /** |
16 * \file | 16 * \file |
17 * \brief C++ API: Service for searching text based on RuleBasedCollator. | 17 * \brief C++ API: Service for searching text based on RuleBasedCollator. |
18 */ | 18 */ |
19 | 19 |
20 #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION | 20 #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION |
21 | 21 |
22 #include "unicode/tblcoll.h" | 22 #include "unicode/tblcoll.h" |
23 #include "unicode/coleitr.h" | 23 #include "unicode/coleitr.h" |
24 #include "unicode/search.h" | 24 #include "unicode/search.h" |
25 | 25 |
26 U_NAMESPACE_BEGIN | 26 U_NAMESPACE_BEGIN |
27 | 27 |
28 /** | 28 /** |
29 * | 29 * |
30 * <tt>StringSearch</tt> is a <tt>SearchIterator</tt> that provides | 30 * <tt>StringSearch</tt> is a <tt>SearchIterator</tt> that provides |
31 * language-sensitive text searching based on the comparison rules defined | 31 * language-sensitive text searching based on the comparison rules defined |
32 * in a {@link RuleBasedCollator} object. | 32 * in a {@link RuleBasedCollator} object. |
33 * StringSearch ensures that language eccentricity can be | 33 * StringSearch ensures that language eccentricity can be |
34 * handled, e.g. for the German collator, characters ß and SS will be matc
hed | 34 * handled, e.g. for the German collator, characters ß and SS will be matc
hed |
35 * if case is chosen to be ignored. | 35 * if case is chosen to be ignored. |
36 * See the <a href="http://source.icu-project.org/repos/icu/icuhtml/trunk/design
/collation/ICU_collation_design.htm"> | 36 * See the <a href="http://source.icu-project.org/repos/icu/icuhtml/trunk/design
/collation/ICU_collation_design.htm"> |
37 * "ICU Collation Design Document"</a> for more information. | 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-project.org/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> | 38 * <p> |
45 * There are 2 match options for selection:<br> | 39 * 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 | 40 * Let S' be the sub-string of a text string S between the offsets start and |
47 * end <start, end>. | 41 * end [start, end]. |
48 * <br> | 42 * <br> |
49 * A pattern string P matches a text string S at the offsets <start, end> | 43 * A pattern string P matches a text string S at the offsets [start, end] |
50 * if | 44 * if |
51 * <pre> | 45 * <pre> |
52 * option 1. Some canonical equivalent of P matches some canonical equivalent | 46 * option 1. Some canonical equivalent of P matches some canonical equivalent |
53 * of S' | 47 * of S' |
54 * option 2. P matches S' and if P starts or ends with a combining mark, | 48 * 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? | 49 * there exists no non-ignorable combining mark before or after S? |
56 * in S respectively. | 50 * in S respectively. |
57 * </pre> | 51 * </pre> |
58 * Option 2. will be the default. | 52 * Option 2. will be the default. |
59 * <p> | 53 * <p> |
60 * This search has APIs similar to that of other text iteration mechanisms | 54 * This search has APIs similar to that of other text iteration mechanisms |
61 * such as the break iterators in <tt>BreakIterator</tt>. Using these | 55 * 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 | 56 * APIs, it is easy to scan through text looking for all occurrences of |
63 * a given pattern. This search iterator allows changing of direction by | 57 * 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>. | 58 * 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, | 59 * Though a direction change can occur without calling <tt>reset</tt> first, |
66 * this operation comes with some speed penalty. | 60 * this operation comes with some speed penalty. |
67 * Match results in the forward direction will match the result matches in | 61 * Match results in the forward direction will match the result matches in |
68 * the backwards direction in the reverse order | 62 * the backwards direction in the reverse order |
69 * <p> | 63 * <p> |
70 * <tt>SearchIterator</tt> provides APIs to specify the starting position | 64 * <tt>SearchIterator</tt> provides APIs to specify the starting position |
71 * within the text string to be searched, e.g. <tt>setOffset</tt>, | 65 * within the text string to be searched, e.g. <tt>setOffset</tt>, |
72 * <tt>preceding</tt> and <tt>following</tt>. Since the | 66 * <tt>preceding</tt> and <tt>following</tt>. Since the |
73 * starting position will be set as it is specified, please take note that | 67 * 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 | 68 * there are some danger points which the search may render incorrect |
75 * results: | 69 * results: |
76 * <ul> | 70 * <ul> |
77 * <li> The midst of a substring that requires normalization. | 71 * <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 | 72 * <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 | 73 * second character which requires to be swapped with the preceding |
80 * character. Vice versa, if the preceding match is to be found, | 74 * character. Vice versa, if the preceding match is to be found, |
81 * position to search from should not be the first character which | 75 * 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 | 76 * requires to be swapped with the next character. E.g certain Thai and |
83 * Lao characters require swapping. | 77 * Lao characters require swapping. |
84 * <li> If a following pattern match is to be found, any position within a | 78 * <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 | 79 * contracting sequence except the first will fail. Vice versa if a |
86 * preceding pattern match is to be found, a invalid starting point | 80 * preceding pattern match is to be found, a invalid starting point |
87 * would be any character within a contracting sequence except the last. | 81 * would be any character within a contracting sequence except the last. |
88 * </ul> | 82 * </ul> |
89 * <p> | 83 * <p> |
90 * A breakiterator can be used if only matches at logical breaks are desired. | 84 * A <tt>BreakIterator</tt> can be used if only matches at logical breaks are de
sired. |
91 * Using a breakiterator will only give you results that exactly matches the | 85 * Using a <tt>BreakIterator</tt> will only give you results that exactly matche
s the |
92 * boundaries given by the breakiterator. For instance the pattern "e" will | 86 * 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. | 87 * not be found in the string "\u00e9" if a character break iterator is used. |
94 * <p> | 88 * <p> |
95 * Options are provided to handle overlapping matches. | 89 * Options are provided to handle overlapping matches. |
96 * E.g. In English, overlapping matches produces the result 0 and 2 | 90 * E.g. In English, overlapping matches produces the result 0 and 2 |
97 * for the pattern "abab" in the text "ababab", where else mutually | 91 * for the pattern "abab" in the text "ababab", where else mutually |
98 * exclusive matches only produce the result of 0. | 92 * exclusive matches only produce the result of 0. |
99 * <p> | 93 * <p> |
100 * Though collator attributes will be taken into consideration while | 94 * Though collator attributes will be taken into consideration while |
101 * performing matches, there are no APIs here for setting and getting the | 95 * performing matches, there are no APIs here for setting and getting the |
102 * attributes. These attributes can be set by getting the collator | 96 * attributes. These attributes can be set by getting the collator |
103 * from <tt>getCollator</tt> and using the APIs in <tt>coll.h</tt>. | 97 * from <tt>getCollator</tt> and using the APIs in <tt>coll.h</tt>. |
104 * Lastly to update StringSearch to the new collator attributes, | 98 * Lastly to update <tt>StringSearch</tt> to the new collator attributes, |
105 * reset() has to be called. | 99 * <tt>reset</tt> has to be called. |
106 * <p> | 100 * <p> |
107 * Restriction: <br> | 101 * Restriction: <br> |
108 * Currently there are no composite characters that consists of a | 102 * Currently there are no composite characters that consists of a |
109 * character with combining class > 0 before a character with combining | 103 * character with combining class > 0 before a character with combining |
110 * class == 0. However, if such a character exists in the future, | 104 * class == 0. However, if such a character exists in the future, |
111 * StringSearch does not guarantee the results for option 1. | 105 * <tt>StringSearch</tt> does not guarantee the results for option 1. |
112 * <p> | 106 * <p> |
113 * Consult the <tt>SearchIterator</tt> documentation for information on | 107 * Consult the <tt>SearchIterator</tt> documentation for information on |
114 * and examples of how to use instances of this class to implement text | 108 * and examples of how to use instances of this class to implement text |
115 * searching. | 109 * searching. |
116 * <pre><code> | 110 * <pre><code> |
117 * UnicodeString target("The quick brown fox jumps over the lazy dog."); | 111 * UnicodeString target("The quick brown fox jumps over the lazy dog."); |
118 * UnicodeString pattern("fox"); | 112 * UnicodeString pattern("fox"); |
119 * | 113 * |
120 * UErrorCode error = U_ZERO_ERROR; | 114 * UErrorCode error = U_ZERO_ERROR; |
121 * StringSearch iter(pattern, target, Locale::getUS(), NULL, status); | 115 * StringSearch iter(pattern, target, Locale::getUS(), NULL, status); |
122 * for (int pos = iter.first(error); | 116 * for (int pos = iter.first(error); |
123 * pos != USEARCH_DONE; | 117 * pos != USEARCH_DONE; |
124 * pos = iter.next(error)) | 118 * pos = iter.next(error)) |
125 * { | 119 * { |
126 * printf("Found match at %d pos, length is %d\n", pos, | 120 * printf("Found match at %d pos, length is %d\n", pos, |
127 * iter.getMatchLength()); | 121 * iter.getMatchLength()); |
128 * } | 122 * } |
129 * </code></pre> | 123 * </code></pre> |
130 * <p> | 124 * <p> |
131 * Note, StringSearch is not to be subclassed. | 125 * Note, <tt>StringSearch</tt> is not to be subclassed. |
132 * </p> | 126 * </p> |
133 * @see SearchIterator | 127 * @see SearchIterator |
134 * @see RuleBasedCollator | 128 * @see RuleBasedCollator |
135 * @since ICU 2.0 | 129 * @since ICU 2.0 |
136 */ | 130 */ |
137 | 131 |
138 class U_I18N_API StringSearch : public SearchIterator | 132 class U_I18N_API StringSearch U_FINAL : public SearchIterator |
139 { | 133 { |
140 public: | 134 public: |
141 | 135 |
142 // public constructors and destructors -------------------------------- | 136 // public constructors and destructors -------------------------------- |
143 | 137 |
144 /** | 138 /** |
145 * Creating a <tt>StringSearch</tt> instance using the argument locale | 139 * Creating a <tt>StringSearch</tt> instance using the argument locale |
146 * language rule set. A collator will be created in the process, which | 140 * language rule set. A collator will be created in the process, which |
147 * will be owned by this instance and will be deleted during | 141 * will be owned by this instance and will be deleted during |
148 * destruction | 142 * destruction |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 * Modifications to this collator will affect the original collator passed i
n to | 359 * Modifications to this collator will affect the original collator passed i
n to |
366 * the <tt>StringSearch></tt> constructor or to setCollator, if any. | 360 * the <tt>StringSearch></tt> constructor or to setCollator, if any. |
367 * @return collator used for string search | 361 * @return collator used for string search |
368 * @stable ICU 2.0 | 362 * @stable ICU 2.0 |
369 */ | 363 */ |
370 RuleBasedCollator * getCollator() const; | 364 RuleBasedCollator * getCollator() const; |
371 | 365 |
372 /** | 366 /** |
373 * Sets the collator used for the language rules. User retains the | 367 * Sets the collator used for the language rules. User retains the |
374 * ownership of this collator, thus the responsibility of deletion lies | 368 * ownership of this collator, thus the responsibility of deletion lies |
375 * with the user. This method causes internal data such as Boyer-Moore | 369 * with the user. The iterator's position will not be changed by this method
. |
376 * shift tables to be recalculated, but the iterator's position is | |
377 * unchanged. | |
378 * @param coll collator | 370 * @param coll collator |
379 * @param status for errors if any | 371 * @param status for errors if any |
380 * @stable ICU 2.0 | 372 * @stable ICU 2.0 |
381 */ | 373 */ |
382 void setCollator(RuleBasedCollator *coll, UErrorCode &status); | 374 void setCollator(RuleBasedCollator *coll, UErrorCode &status); |
383 | 375 |
384 /** | 376 /** |
385 * Sets the pattern used for matching. | 377 * Sets the pattern used for matching. |
386 * Internal data like the Boyer Moore table will be recalculated, but | 378 * The iterator's position will not be changed by this method. |
387 * the iterator's position is unchanged. | |
388 * @param pattern search pattern to be found | 379 * @param pattern search pattern to be found |
389 * @param status for errors if any. If the pattern length is 0 then an | 380 * @param status for errors if any. If the pattern length is 0 then an |
390 * U_ILLEGAL_ARGUMENT_ERROR is returned. | 381 * U_ILLEGAL_ARGUMENT_ERROR is returned. |
391 * @stable ICU 2.0 | 382 * @stable ICU 2.0 |
392 */ | 383 */ |
393 void setPattern(const UnicodeString &pattern, UErrorCode &status); | 384 void setPattern(const UnicodeString &pattern, UErrorCode &status); |
394 | 385 |
395 /** | 386 /** |
396 * Gets the search pattern. | 387 * Gets the search pattern. |
397 * @return pattern used for matching | 388 * @return pattern used for matching |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 * @stable ICU 2.0 | 477 * @stable ICU 2.0 |
487 */ | 478 */ |
488 virtual int32_t handlePrev(int32_t position, UErrorCode &status); | 479 virtual int32_t handlePrev(int32_t position, UErrorCode &status); |
489 | 480 |
490 private : | 481 private : |
491 StringSearch(); // default constructor not implemented | 482 StringSearch(); // default constructor not implemented |
492 | 483 |
493 // private data members ---------------------------------------------- | 484 // private data members ---------------------------------------------- |
494 | 485 |
495 /** | 486 /** |
496 * RuleBasedCollator, contains exactly the same UCollator * in m_strsrch_ | |
497 * @stable ICU 2.0 | |
498 */ | |
499 RuleBasedCollator m_collator_; | |
500 /** | |
501 * Pattern text | 487 * Pattern text |
502 * @stable ICU 2.0 | 488 * @stable ICU 2.0 |
503 */ | 489 */ |
504 UnicodeString m_pattern_; | 490 UnicodeString m_pattern_; |
505 /** | 491 /** |
506 * String search struct data | 492 * String search struct data |
507 * @stable ICU 2.0 | 493 * @stable ICU 2.0 |
508 */ | 494 */ |
509 UStringSearch *m_strsrch_; | 495 UStringSearch *m_strsrch_; |
510 | 496 |
511 }; | 497 }; |
512 | 498 |
513 U_NAMESPACE_END | 499 U_NAMESPACE_END |
514 | 500 |
515 #endif /* #if !UCONFIG_NO_COLLATION */ | 501 #endif /* #if !UCONFIG_NO_COLLATION */ |
516 | 502 |
517 #endif | 503 #endif |
518 | 504 |
OLD | NEW |