stsearch.h 21 KB

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