usearch.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*
  2. **********************************************************************
  3. * Copyright (C) 2001-2011,2014 IBM and others. All rights reserved.
  4. **********************************************************************
  5. * Date Name Description
  6. * 06/28/2001 synwee Creation.
  7. **********************************************************************
  8. */
  9. #ifndef USEARCH_H
  10. #define USEARCH_H
  11. #include "unicode/utypes.h"
  12. #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION
  13. #include "unicode/localpointer.h"
  14. #include "unicode/ucol.h"
  15. #include "unicode/ucoleitr.h"
  16. #include "unicode/ubrk.h"
  17. /**
  18. * \file
  19. * \brief C API: StringSearch
  20. *
  21. * C Apis for an engine that provides language-sensitive text searching based
  22. * on the comparison rules defined in a <tt>UCollator</tt> data struct,
  23. * see <tt>ucol.h</tt>. This ensures that language eccentricity can be
  24. * handled, e.g. for the German collator, characters &szlig; and SS will be matched
  25. * if case is chosen to be ignored.
  26. * See the <a href="http://source.icu-project.org/repos/icu/icuhtml/trunk/design/collation/ICU_collation_design.htm">
  27. * "ICU Collation Design Document"</a> for more information.
  28. * <p>
  29. * The implementation may use a linear search or a modified form of the Boyer-Moore
  30. * search; for more information on the latter see
  31. * <a href="http://icu-project.org/docs/papers/efficient_text_searching_in_java.html">
  32. * "Efficient Text Searching in Java"</a>, published in <i>Java Report</i>
  33. * in February, 1999.
  34. * <p>
  35. * There are 2 match options for selection:<br>
  36. * Let S' be the sub-string of a text string S between the offsets start and
  37. * end <start, end>.
  38. * <br>
  39. * A pattern string P matches a text string S at the offsets <start, end>
  40. * if
  41. * <pre>
  42. * option 1. Some canonical equivalent of P matches some canonical equivalent
  43. * of S'
  44. * option 2. P matches S' and if P starts or ends with a combining mark,
  45. * there exists no non-ignorable combining mark before or after S'
  46. * in S respectively.
  47. * </pre>
  48. * Option 2. will be the default.
  49. * <p>
  50. * This search has APIs similar to that of other text iteration mechanisms
  51. * such as the break iterators in <tt>ubrk.h</tt>. Using these
  52. * APIs, it is easy to scan through text looking for all occurances of
  53. * a given pattern. This search iterator allows changing of direction by
  54. * calling a <tt>reset</tt> followed by a <tt>next</tt> or <tt>previous</tt>.
  55. * Though a direction change can occur without calling <tt>reset</tt> first,
  56. * this operation comes with some speed penalty.
  57. * Generally, match results in the forward direction will match the result
  58. * matches in the backwards direction in the reverse order
  59. * <p>
  60. * <tt>usearch.h</tt> provides APIs to specify the starting position
  61. * within the text string to be searched, e.g. <tt>usearch_setOffset</tt>,
  62. * <tt>usearch_preceding</tt> and <tt>usearch_following</tt>. Since the
  63. * starting position will be set as it is specified, please take note that
  64. * there are some dangerous positions which the search may render incorrect
  65. * results:
  66. * <ul>
  67. * <li> The midst of a substring that requires normalization.
  68. * <li> If the following match is to be found, the position should not be the
  69. * second character which requires to be swapped with the preceding
  70. * character. Vice versa, if the preceding match is to be found,
  71. * position to search from should not be the first character which
  72. * requires to be swapped with the next character. E.g certain Thai and
  73. * Lao characters require swapping.
  74. * <li> If a following pattern match is to be found, any position within a
  75. * contracting sequence except the first will fail. Vice versa if a
  76. * preceding pattern match is to be found, a invalid starting point
  77. * would be any character within a contracting sequence except the last.
  78. * </ul>
  79. * <p>
  80. * A breakiterator can be used if only matches at logical breaks are desired.
  81. * Using a breakiterator will only give you results that exactly matches the
  82. * boundaries given by the breakiterator. For instance the pattern "e" will
  83. * not be found in the string "\u00e9" if a character break iterator is used.
  84. * <p>
  85. * Options are provided to handle overlapping matches.
  86. * E.g. In English, overlapping matches produces the result 0 and 2
  87. * for the pattern "abab" in the text "ababab", where else mutually
  88. * exclusive matches only produce the result of 0.
  89. * <p>
  90. * Options are also provided to implement "asymmetric search" as described in
  91. * <a href="http://www.unicode.org/reports/tr10/#Asymmetric_Search">
  92. * UTS #10 Unicode Collation Algorithm</a>, specifically the USearchAttribute
  93. * USEARCH_ELEMENT_COMPARISON and its values.
  94. * <p>
  95. * Though collator attributes will be taken into consideration while
  96. * performing matches, there are no APIs here for setting and getting the
  97. * attributes. These attributes can be set by getting the collator
  98. * from <tt>usearch_getCollator</tt> and using the APIs in <tt>ucol.h</tt>.
  99. * Lastly to update String Search to the new collator attributes,
  100. * usearch_reset() has to be called.
  101. * <p>
  102. * Restriction: <br>
  103. * Currently there are no composite characters that consists of a
  104. * character with combining class > 0 before a character with combining
  105. * class == 0. However, if such a character exists in the future, the
  106. * search mechanism does not guarantee the results for option 1.
  107. *
  108. * <p>
  109. * Example of use:<br>
  110. * <pre><code>
  111. * char *tgtstr = "The quick brown fox jumped over the lazy fox";
  112. * char *patstr = "fox";
  113. * UChar target[64];
  114. * UChar pattern[16];
  115. * UErrorCode status = U_ZERO_ERROR;
  116. * u_uastrcpy(target, tgtstr);
  117. * u_uastrcpy(pattern, patstr);
  118. *
  119. * UStringSearch *search = usearch_open(pattern, -1, target, -1, "en_US",
  120. * NULL, &status);
  121. * if (U_SUCCESS(status)) {
  122. * for (int pos = usearch_first(search, &status);
  123. * pos != USEARCH_DONE;
  124. * pos = usearch_next(search, &status))
  125. * {
  126. * printf("Found match at %d pos, length is %d\n", pos,
  127. * usearch_getMatchLength(search));
  128. * }
  129. * }
  130. *
  131. * usearch_close(search);
  132. * </code></pre>
  133. * @stable ICU 2.4
  134. */
  135. /**
  136. * DONE is returned by previous() and next() after all valid matches have
  137. * been returned, and by first() and last() if there are no matches at all.
  138. * @stable ICU 2.4
  139. */
  140. #define USEARCH_DONE -1
  141. /**
  142. * Data structure for searching
  143. * @stable ICU 2.4
  144. */
  145. struct UStringSearch;
  146. /**
  147. * Data structure for searching
  148. * @stable ICU 2.4
  149. */
  150. typedef struct UStringSearch UStringSearch;
  151. /**
  152. * @stable ICU 2.4
  153. */
  154. typedef enum {
  155. /**
  156. * Option for overlapping matches
  157. * @stable ICU 2.4
  158. */
  159. USEARCH_OVERLAP = 0,
  160. #ifndef U_HIDE_DEPRECATED_API
  161. /**
  162. * Option for canonical matches; option 1 in header documentation.
  163. * The default value will be USEARCH_OFF.
  164. * Note: Setting this option to USEARCH_ON currently has no effect on
  165. * search behavior, and this option is deprecated. Instead, to control
  166. * canonical match behavior, you must set UCOL_NORMALIZATION_MODE
  167. * appropriately (to UCOL_OFF or UCOL_ON) in the UCollator used by
  168. * the UStringSearch object.
  169. * @see usearch_openFromCollator
  170. * @see usearch_getCollator
  171. * @see usearch_setCollator
  172. * @see ucol_getAttribute
  173. * @deprecated ICU 53
  174. */
  175. USEARCH_CANONICAL_MATCH = 1,
  176. #endif /* U_HIDE_DEPRECATED_API */
  177. /**
  178. * Option to control how collation elements are compared.
  179. * The default value will be USEARCH_STANDARD_ELEMENT_COMPARISON.
  180. * @stable ICU 4.4
  181. */
  182. USEARCH_ELEMENT_COMPARISON = 2,
  183. /**
  184. * Count of attribute types
  185. * @stable ICU 2.4
  186. */
  187. USEARCH_ATTRIBUTE_COUNT = 3
  188. } USearchAttribute;
  189. /**
  190. * @stable ICU 2.4
  191. */
  192. typedef enum {
  193. /**
  194. * Default value for any USearchAttribute
  195. * @stable ICU 2.4
  196. */
  197. USEARCH_DEFAULT = -1,
  198. /**
  199. * Value for USEARCH_OVERLAP and USEARCH_CANONICAL_MATCH
  200. * @stable ICU 2.4
  201. */
  202. USEARCH_OFF,
  203. /**
  204. * Value for USEARCH_OVERLAP and USEARCH_CANONICAL_MATCH
  205. * @stable ICU 2.4
  206. */
  207. USEARCH_ON,
  208. /**
  209. * Value (default) for USEARCH_ELEMENT_COMPARISON;
  210. * standard collation element comparison at the specified collator
  211. * strength.
  212. * @stable ICU 4.4
  213. */
  214. USEARCH_STANDARD_ELEMENT_COMPARISON,
  215. /**
  216. * Value for USEARCH_ELEMENT_COMPARISON;
  217. * collation element comparison is modified to effectively provide
  218. * behavior between the specified strength and strength - 1. Collation
  219. * elements in the pattern that have the base weight for the specified
  220. * strength are treated as "wildcards" that match an element with any
  221. * other weight at that collation level in the searched text. For
  222. * example, with a secondary-strength English collator, a plain 'e' in
  223. * the pattern will match a plain e or an e with any diacritic in the
  224. * searched text, but an e with diacritic in the pattern will only
  225. * match an e with the same diacritic in the searched text.
  226. *
  227. * This supports "asymmetric search" as described in
  228. * <a href="http://www.unicode.org/reports/tr10/#Asymmetric_Search">
  229. * UTS #10 Unicode Collation Algorithm</a>.
  230. *
  231. * @stable ICU 4.4
  232. */
  233. USEARCH_PATTERN_BASE_WEIGHT_IS_WILDCARD,
  234. /**
  235. * Value for USEARCH_ELEMENT_COMPARISON.
  236. * collation element comparison is modified to effectively provide
  237. * behavior between the specified strength and strength - 1. Collation
  238. * elements in either the pattern or the searched text that have the
  239. * base weight for the specified strength are treated as "wildcards"
  240. * that match an element with any other weight at that collation level.
  241. * For example, with a secondary-strength English collator, a plain 'e'
  242. * in the pattern will match a plain e or an e with any diacritic in the
  243. * searched text, but an e with diacritic in the pattern will only
  244. * match an e with the same diacritic or a plain e in the searched text.
  245. *
  246. * This option is similar to "asymmetric search" as described in
  247. * <a href="http://www.unicode.org/reports/tr10/#Asymmetric_Search">
  248. * UTS #10 Unicode Collation Algorithm</a, but also allows unmarked
  249. * characters in the searched text to match marked or unmarked versions of
  250. * that character in the pattern.
  251. *
  252. * @stable ICU 4.4
  253. */
  254. USEARCH_ANY_BASE_WEIGHT_IS_WILDCARD,
  255. /**
  256. * Count of attribute values
  257. * @stable ICU 2.4
  258. */
  259. USEARCH_ATTRIBUTE_VALUE_COUNT
  260. } USearchAttributeValue;
  261. /* open and close ------------------------------------------------------ */
  262. /**
  263. * Creating a search iterator data struct using the argument locale language
  264. * rule set. A collator will be created in the process, which will be owned by
  265. * this search and will be deleted in <tt>usearch_close</tt>.
  266. * @param pattern for matching
  267. * @param patternlength length of the pattern, -1 for null-termination
  268. * @param text text string
  269. * @param textlength length of the text string, -1 for null-termination
  270. * @param locale name of locale for the rules to be used
  271. * @param breakiter A BreakIterator that will be used to restrict the points
  272. * at which matches are detected. If a match is found, but
  273. * the match's start or end index is not a boundary as
  274. * determined by the <tt>BreakIterator</tt>, the match will
  275. * be rejected and another will be searched for.
  276. * If this parameter is <tt>NULL</tt>, no break detection is
  277. * attempted.
  278. * @param status for errors if it occurs. If pattern or text is NULL, or if
  279. * patternlength or textlength is 0 then an
  280. * U_ILLEGAL_ARGUMENT_ERROR is returned.
  281. * @return search iterator data structure, or NULL if there is an error.
  282. * @stable ICU 2.4
  283. */
  284. U_STABLE UStringSearch * U_EXPORT2 usearch_open(const UChar *pattern,
  285. int32_t patternlength,
  286. const UChar *text,
  287. int32_t textlength,
  288. const char *locale,
  289. UBreakIterator *breakiter,
  290. UErrorCode *status);
  291. /**
  292. * Creating a search iterator data struct using the argument collator language
  293. * rule set. Note, user retains the ownership of this collator, thus the
  294. * responsibility of deletion lies with the user.
  295. * NOTE: string search cannot be instantiated from a collator that has
  296. * collate digits as numbers (CODAN) turned on.
  297. * @param pattern for matching
  298. * @param patternlength length of the pattern, -1 for null-termination
  299. * @param text text string
  300. * @param textlength length of the text string, -1 for null-termination
  301. * @param collator used for the language rules
  302. * @param breakiter A BreakIterator that will be used to restrict the points
  303. * at which matches are detected. If a match is found, but
  304. * the match's start or end index is not a boundary as
  305. * determined by the <tt>BreakIterator</tt>, the match will
  306. * be rejected and another will be searched for.
  307. * If this parameter is <tt>NULL</tt>, no break detection is
  308. * attempted.
  309. * @param status for errors if it occurs. If collator, pattern or text is NULL,
  310. * or if patternlength or textlength is 0 then an
  311. * U_ILLEGAL_ARGUMENT_ERROR is returned.
  312. * @return search iterator data structure, or NULL if there is an error.
  313. * @stable ICU 2.4
  314. */
  315. U_STABLE UStringSearch * U_EXPORT2 usearch_openFromCollator(
  316. const UChar *pattern,
  317. int32_t patternlength,
  318. const UChar *text,
  319. int32_t textlength,
  320. const UCollator *collator,
  321. UBreakIterator *breakiter,
  322. UErrorCode *status);
  323. /**
  324. * Destroying and cleaning up the search iterator data struct.
  325. * If a collator is created in <tt>usearch_open</tt>, it will be destroyed here.
  326. * @param searchiter data struct to clean up
  327. * @stable ICU 2.4
  328. */
  329. U_STABLE void U_EXPORT2 usearch_close(UStringSearch *searchiter);
  330. #if U_SHOW_CPLUSPLUS_API
  331. U_NAMESPACE_BEGIN
  332. /**
  333. * \class LocalUStringSearchPointer
  334. * "Smart pointer" class, closes a UStringSearch via usearch_close().
  335. * For most methods see the LocalPointerBase base class.
  336. *
  337. * @see LocalPointerBase
  338. * @see LocalPointer
  339. * @stable ICU 4.4
  340. */
  341. U_DEFINE_LOCAL_OPEN_POINTER(LocalUStringSearchPointer, UStringSearch, usearch_close);
  342. U_NAMESPACE_END
  343. #endif
  344. /* get and set methods -------------------------------------------------- */
  345. /**
  346. * Sets the current position in the text string which the next search will
  347. * start from. Clears previous states.
  348. * This method takes the argument index and sets the position in the text
  349. * string accordingly without checking if the index is pointing to a
  350. * valid starting point to begin searching.
  351. * Search positions that may render incorrect results are highlighted in the
  352. * header comments
  353. * @param strsrch search iterator data struct
  354. * @param position position to start next search from. If position is less
  355. * than or greater than the text range for searching,
  356. * an U_INDEX_OUTOFBOUNDS_ERROR will be returned
  357. * @param status error status if any.
  358. * @stable ICU 2.4
  359. */
  360. U_STABLE void U_EXPORT2 usearch_setOffset(UStringSearch *strsrch,
  361. int32_t position,
  362. UErrorCode *status);
  363. /**
  364. * Return the current index in the string text being searched.
  365. * If the iteration has gone past the end of the text (or past the beginning
  366. * for a backwards search), <tt>USEARCH_DONE</tt> is returned.
  367. * @param strsrch search iterator data struct
  368. * @see #USEARCH_DONE
  369. * @stable ICU 2.4
  370. */
  371. U_STABLE int32_t U_EXPORT2 usearch_getOffset(const UStringSearch *strsrch);
  372. /**
  373. * Sets the text searching attributes located in the enum USearchAttribute
  374. * with values from the enum USearchAttributeValue.
  375. * <tt>USEARCH_DEFAULT</tt> can be used for all attributes for resetting.
  376. * @param strsrch search iterator data struct
  377. * @param attribute text attribute to be set
  378. * @param value text attribute value
  379. * @param status for errors if it occurs
  380. * @see #usearch_getAttribute
  381. * @stable ICU 2.4
  382. */
  383. U_STABLE void U_EXPORT2 usearch_setAttribute(UStringSearch *strsrch,
  384. USearchAttribute attribute,
  385. USearchAttributeValue value,
  386. UErrorCode *status);
  387. /**
  388. * Gets the text searching attributes.
  389. * @param strsrch search iterator data struct
  390. * @param attribute text attribute to be retrieve
  391. * @return text attribute value
  392. * @see #usearch_setAttribute
  393. * @stable ICU 2.4
  394. */
  395. U_STABLE USearchAttributeValue U_EXPORT2 usearch_getAttribute(
  396. const UStringSearch *strsrch,
  397. USearchAttribute attribute);
  398. /**
  399. * Returns the index to the match in the text string that was searched.
  400. * This call returns a valid result only after a successful call to
  401. * <tt>usearch_first</tt>, <tt>usearch_next</tt>, <tt>usearch_previous</tt>,
  402. * or <tt>usearch_last</tt>.
  403. * Just after construction, or after a searching method returns
  404. * <tt>USEARCH_DONE</tt>, this method will return <tt>USEARCH_DONE</tt>.
  405. * <p>
  406. * Use <tt>usearch_getMatchedLength</tt> to get the matched string length.
  407. * @param strsrch search iterator data struct
  408. * @return index to a substring within the text string that is being
  409. * searched.
  410. * @see #usearch_first
  411. * @see #usearch_next
  412. * @see #usearch_previous
  413. * @see #usearch_last
  414. * @see #USEARCH_DONE
  415. * @stable ICU 2.4
  416. */
  417. U_STABLE int32_t U_EXPORT2 usearch_getMatchedStart(
  418. const UStringSearch *strsrch);
  419. /**
  420. * Returns the length of text in the string which matches the search pattern.
  421. * This call returns a valid result only after a successful call to
  422. * <tt>usearch_first</tt>, <tt>usearch_next</tt>, <tt>usearch_previous</tt>,
  423. * or <tt>usearch_last</tt>.
  424. * Just after construction, or after a searching method returns
  425. * <tt>USEARCH_DONE</tt>, this method will return 0.
  426. * @param strsrch search iterator data struct
  427. * @return The length of the match in the string text, or 0 if there is no
  428. * match currently.
  429. * @see #usearch_first
  430. * @see #usearch_next
  431. * @see #usearch_previous
  432. * @see #usearch_last
  433. * @see #USEARCH_DONE
  434. * @stable ICU 2.4
  435. */
  436. U_STABLE int32_t U_EXPORT2 usearch_getMatchedLength(
  437. const UStringSearch *strsrch);
  438. /**
  439. * Returns the text that was matched by the most recent call to
  440. * <tt>usearch_first</tt>, <tt>usearch_next</tt>, <tt>usearch_previous</tt>,
  441. * or <tt>usearch_last</tt>.
  442. * If the iterator is not pointing at a valid match (e.g. just after
  443. * construction or after <tt>USEARCH_DONE</tt> has been returned, returns
  444. * an empty string. If result is not large enough to store the matched text,
  445. * result will be filled with the partial text and an U_BUFFER_OVERFLOW_ERROR
  446. * will be returned in status. result will be null-terminated whenever
  447. * possible. If the buffer fits the matched text exactly, a null-termination
  448. * is not possible, then a U_STRING_NOT_TERMINATED_ERROR set in status.
  449. * Pre-flighting can be either done with length = 0 or the API
  450. * <tt>usearch_getMatchLength</tt>.
  451. * @param strsrch search iterator data struct
  452. * @param result UChar buffer to store the matched string
  453. * @param resultCapacity length of the result buffer
  454. * @param status error returned if result is not large enough
  455. * @return exact length of the matched text, not counting the null-termination
  456. * @see #usearch_first
  457. * @see #usearch_next
  458. * @see #usearch_previous
  459. * @see #usearch_last
  460. * @see #USEARCH_DONE
  461. * @stable ICU 2.4
  462. */
  463. U_STABLE int32_t U_EXPORT2 usearch_getMatchedText(const UStringSearch *strsrch,
  464. UChar *result,
  465. int32_t resultCapacity,
  466. UErrorCode *status);
  467. #if !UCONFIG_NO_BREAK_ITERATION
  468. /**
  469. * Set the BreakIterator that will be used to restrict the points at which
  470. * matches are detected.
  471. * @param strsrch search iterator data struct
  472. * @param breakiter A BreakIterator that will be used to restrict the points
  473. * at which matches are detected. If a match is found, but
  474. * the match's start or end index is not a boundary as
  475. * determined by the <tt>BreakIterator</tt>, the match will
  476. * be rejected and another will be searched for.
  477. * If this parameter is <tt>NULL</tt>, no break detection is
  478. * attempted.
  479. * @param status for errors if it occurs
  480. * @see #usearch_getBreakIterator
  481. * @stable ICU 2.4
  482. */
  483. U_STABLE void U_EXPORT2 usearch_setBreakIterator(UStringSearch *strsrch,
  484. UBreakIterator *breakiter,
  485. UErrorCode *status);
  486. /**
  487. * Returns the BreakIterator that is used to restrict the points at which
  488. * matches are detected. This will be the same object that was passed to the
  489. * constructor or to <tt>usearch_setBreakIterator</tt>. Note that
  490. * <tt>NULL</tt>
  491. * is a legal value; it means that break detection should not be attempted.
  492. * @param strsrch search iterator data struct
  493. * @return break iterator used
  494. * @see #usearch_setBreakIterator
  495. * @stable ICU 2.4
  496. */
  497. U_STABLE const UBreakIterator * U_EXPORT2 usearch_getBreakIterator(
  498. const UStringSearch *strsrch);
  499. #endif
  500. /**
  501. * Set the string text to be searched. Text iteration will hence begin at the
  502. * start of the text string. This method is useful if you want to re-use an
  503. * iterator to search for the same pattern within a different body of text.
  504. * @param strsrch search iterator data struct
  505. * @param text new string to look for match
  506. * @param textlength length of the new string, -1 for null-termination
  507. * @param status for errors if it occurs. If text is NULL, or textlength is 0
  508. * then an U_ILLEGAL_ARGUMENT_ERROR is returned with no change
  509. * done to strsrch.
  510. * @see #usearch_getText
  511. * @stable ICU 2.4
  512. */
  513. U_STABLE void U_EXPORT2 usearch_setText( UStringSearch *strsrch,
  514. const UChar *text,
  515. int32_t textlength,
  516. UErrorCode *status);
  517. /**
  518. * Return the string text to be searched.
  519. * @param strsrch search iterator data struct
  520. * @param length returned string text length
  521. * @return string text
  522. * @see #usearch_setText
  523. * @stable ICU 2.4
  524. */
  525. U_STABLE const UChar * U_EXPORT2 usearch_getText(const UStringSearch *strsrch,
  526. int32_t *length);
  527. /**
  528. * Gets the collator used for the language rules.
  529. * <p>
  530. * Deleting the returned <tt>UCollator</tt> before calling
  531. * <tt>usearch_close</tt> would cause the string search to fail.
  532. * <tt>usearch_close</tt> will delete the collator if this search owns it.
  533. * @param strsrch search iterator data struct
  534. * @return collator
  535. * @stable ICU 2.4
  536. */
  537. U_STABLE UCollator * U_EXPORT2 usearch_getCollator(
  538. const UStringSearch *strsrch);
  539. /**
  540. * Sets the collator used for the language rules. User retains the ownership
  541. * of this collator, thus the responsibility of deletion lies with the user.
  542. * This method causes internal data such as Boyer-Moore shift tables to
  543. * be recalculated, but the iterator's position is unchanged.
  544. * @param strsrch search iterator data struct
  545. * @param collator to be used
  546. * @param status for errors if it occurs
  547. * @stable ICU 2.4
  548. */
  549. U_STABLE void U_EXPORT2 usearch_setCollator( UStringSearch *strsrch,
  550. const UCollator *collator,
  551. UErrorCode *status);
  552. /**
  553. * Sets the pattern used for matching.
  554. * Internal data like the Boyer Moore table will be recalculated, but the
  555. * iterator's position is unchanged.
  556. * @param strsrch search iterator data struct
  557. * @param pattern string
  558. * @param patternlength pattern length, -1 for null-terminated string
  559. * @param status for errors if it occurs. If text is NULL, or textlength is 0
  560. * then an U_ILLEGAL_ARGUMENT_ERROR is returned with no change
  561. * done to strsrch.
  562. * @stable ICU 2.4
  563. */
  564. U_STABLE void U_EXPORT2 usearch_setPattern( UStringSearch *strsrch,
  565. const UChar *pattern,
  566. int32_t patternlength,
  567. UErrorCode *status);
  568. /**
  569. * Gets the search pattern
  570. * @param strsrch search iterator data struct
  571. * @param length return length of the pattern, -1 indicates that the pattern
  572. * is null-terminated
  573. * @return pattern string
  574. * @stable ICU 2.4
  575. */
  576. U_STABLE const UChar * U_EXPORT2 usearch_getPattern(
  577. const UStringSearch *strsrch,
  578. int32_t *length);
  579. /* methods ------------------------------------------------------------- */
  580. /**
  581. * Returns the first index at which the string text matches the search
  582. * pattern.
  583. * The iterator is adjusted so that its current index (as returned by
  584. * <tt>usearch_getOffset</tt>) is the match position if one was found.
  585. * If a match is not found, <tt>USEARCH_DONE</tt> will be returned and
  586. * the iterator will be adjusted to the index <tt>USEARCH_DONE</tt>.
  587. * @param strsrch search iterator data struct
  588. * @param status for errors if it occurs
  589. * @return The character index of the first match, or
  590. * <tt>USEARCH_DONE</tt> if there are no matches.
  591. * @see #usearch_getOffset
  592. * @see #USEARCH_DONE
  593. * @stable ICU 2.4
  594. */
  595. U_STABLE int32_t U_EXPORT2 usearch_first(UStringSearch *strsrch,
  596. UErrorCode *status);
  597. /**
  598. * Returns the first index equal or greater than <tt>position</tt> at which
  599. * the string text
  600. * matches the search pattern. The iterator is adjusted so that its current
  601. * index (as returned by <tt>usearch_getOffset</tt>) is the match position if
  602. * one was found.
  603. * If a match is not found, <tt>USEARCH_DONE</tt> will be returned and
  604. * the iterator will be adjusted to the index <tt>USEARCH_DONE</tt>
  605. * <p>
  606. * Search positions that may render incorrect results are highlighted in the
  607. * header comments. If position is less than or greater than the text range
  608. * for searching, an U_INDEX_OUTOFBOUNDS_ERROR will be returned
  609. * @param strsrch search iterator data struct
  610. * @param position to start the search at
  611. * @param status for errors if it occurs
  612. * @return The character index of the first match following <tt>pos</tt>,
  613. * or <tt>USEARCH_DONE</tt> if there are no matches.
  614. * @see #usearch_getOffset
  615. * @see #USEARCH_DONE
  616. * @stable ICU 2.4
  617. */
  618. U_STABLE int32_t U_EXPORT2 usearch_following(UStringSearch *strsrch,
  619. int32_t position,
  620. UErrorCode *status);
  621. /**
  622. * Returns the last index in the target text at which it matches the search
  623. * pattern. The iterator is adjusted so that its current
  624. * index (as returned by <tt>usearch_getOffset</tt>) is the match position if
  625. * one was found.
  626. * If a match is not found, <tt>USEARCH_DONE</tt> will be returned and
  627. * the iterator will be adjusted to the index <tt>USEARCH_DONE</tt>.
  628. * @param strsrch search iterator data struct
  629. * @param status for errors if it occurs
  630. * @return The index of the first match, or <tt>USEARCH_DONE</tt> if there
  631. * are no matches.
  632. * @see #usearch_getOffset
  633. * @see #USEARCH_DONE
  634. * @stable ICU 2.4
  635. */
  636. U_STABLE int32_t U_EXPORT2 usearch_last(UStringSearch *strsrch,
  637. UErrorCode *status);
  638. /**
  639. * Returns the first index less than <tt>position</tt> at which the string text
  640. * matches the search pattern. The iterator is adjusted so that its current
  641. * index (as returned by <tt>usearch_getOffset</tt>) is the match position if
  642. * one was found.
  643. * If a match is not found, <tt>USEARCH_DONE</tt> will be returned and
  644. * the iterator will be adjusted to the index <tt>USEARCH_DONE</tt>
  645. * <p>
  646. * Search positions that may render incorrect results are highlighted in the
  647. * header comments. If position is less than or greater than the text range
  648. * for searching, an U_INDEX_OUTOFBOUNDS_ERROR will be returned.
  649. * <p>
  650. * When <tt>USEARCH_OVERLAP</tt> option is off, the last index of the
  651. * result match is always less than <tt>position</tt>.
  652. * When <tt>USERARCH_OVERLAP</tt> is on, the result match may span across
  653. * <tt>position</tt>.
  654. * @param strsrch search iterator data struct
  655. * @param position index position the search is to begin at
  656. * @param status for errors if it occurs
  657. * @return The character index of the first match preceding <tt>pos</tt>,
  658. * or <tt>USEARCH_DONE</tt> if there are no matches.
  659. * @see #usearch_getOffset
  660. * @see #USEARCH_DONE
  661. * @stable ICU 2.4
  662. */
  663. U_STABLE int32_t U_EXPORT2 usearch_preceding(UStringSearch *strsrch,
  664. int32_t position,
  665. UErrorCode *status);
  666. /**
  667. * Returns the index of the next point at which the string text matches the
  668. * search pattern, starting from the current position.
  669. * The iterator is adjusted so that its current
  670. * index (as returned by <tt>usearch_getOffset</tt>) is the match position if
  671. * one was found.
  672. * If a match is not found, <tt>USEARCH_DONE</tt> will be returned and
  673. * the iterator will be adjusted to the index <tt>USEARCH_DONE</tt>
  674. * @param strsrch search iterator data struct
  675. * @param status for errors if it occurs
  676. * @return The index of the next match after the current position, or
  677. * <tt>USEARCH_DONE</tt> if there are no more matches.
  678. * @see #usearch_first
  679. * @see #usearch_getOffset
  680. * @see #USEARCH_DONE
  681. * @stable ICU 2.4
  682. */
  683. U_STABLE int32_t U_EXPORT2 usearch_next(UStringSearch *strsrch,
  684. UErrorCode *status);
  685. /**
  686. * Returns the index of the previous point at which the string text matches
  687. * the search pattern, starting at the current position.
  688. * The iterator is adjusted so that its current
  689. * index (as returned by <tt>usearch_getOffset</tt>) is the match position if
  690. * one was found.
  691. * If a match is not found, <tt>USEARCH_DONE</tt> will be returned and
  692. * the iterator will be adjusted to the index <tt>USEARCH_DONE</tt>
  693. * @param strsrch search iterator data struct
  694. * @param status for errors if it occurs
  695. * @return The index of the previous match before the current position,
  696. * or <tt>USEARCH_DONE</tt> if there are no more matches.
  697. * @see #usearch_last
  698. * @see #usearch_getOffset
  699. * @see #USEARCH_DONE
  700. * @stable ICU 2.4
  701. */
  702. U_STABLE int32_t U_EXPORT2 usearch_previous(UStringSearch *strsrch,
  703. UErrorCode *status);
  704. /**
  705. * Reset the iteration.
  706. * Search will begin at the start of the text string if a forward iteration
  707. * is initiated before a backwards iteration. Otherwise if a backwards
  708. * iteration is initiated before a forwards iteration, the search will begin
  709. * at the end of the text string.
  710. * @param strsrch search iterator data struct
  711. * @see #usearch_first
  712. * @stable ICU 2.4
  713. */
  714. U_STABLE void U_EXPORT2 usearch_reset(UStringSearch *strsrch);
  715. #ifndef U_HIDE_INTERNAL_API
  716. /**
  717. * Simple forward search for the pattern, starting at a specified index,
  718. * and using using a default set search options.
  719. *
  720. * This is an experimental function, and is not an official part of the
  721. * ICU API.
  722. *
  723. * The collator options, such as UCOL_STRENGTH and UCOL_NORMALIZTION, are honored.
  724. *
  725. * The UStringSearch options USEARCH_CANONICAL_MATCH, USEARCH_OVERLAP and
  726. * any Break Iterator are ignored.
  727. *
  728. * Matches obey the following constraints:
  729. *
  730. * Characters at the start or end positions of a match that are ignorable
  731. * for collation are not included as part of the match, unless they
  732. * are part of a combining sequence, as described below.
  733. *
  734. * A match will not include a partial combining sequence. Combining
  735. * character sequences are considered to be inseperable units,
  736. * and either match the pattern completely, or are considered to not match
  737. * at all. Thus, for example, an A followed a combining accent mark will
  738. * not be found when searching for a plain (unaccented) A. (unless
  739. * the collation strength has been set to ignore all accents).
  740. *
  741. * When beginning a search, the initial starting position, startIdx,
  742. * is assumed to be an acceptable match boundary with respect to
  743. * combining characters. A combining sequence that spans across the
  744. * starting point will not supress a match beginning at startIdx.
  745. *
  746. * Characters that expand to multiple collation elements
  747. * (German sharp-S becoming 'ss', or the composed forms of accented
  748. * characters, for example) also must match completely.
  749. * Searching for a single 's' in a string containing only a sharp-s will
  750. * find no match.
  751. *
  752. *
  753. * @param strsrch the UStringSearch struct, which references both
  754. * the text to be searched and the pattern being sought.
  755. * @param startIdx The index into the text to begin the search.
  756. * @param matchStart An out parameter, the starting index of the matched text.
  757. * This parameter may be NULL.
  758. * A value of -1 will be returned if no match was found.
  759. * @param matchLimit Out parameter, the index of the first position following the matched text.
  760. * The matchLimit will be at a suitable position for beginning a subsequent search
  761. * in the input text.
  762. * This parameter may be NULL.
  763. * A value of -1 will be returned if no match was found.
  764. *
  765. * @param status Report any errors. Note that no match found is not an error.
  766. * @return TRUE if a match was found, FALSE otherwise.
  767. *
  768. * @internal
  769. */
  770. U_INTERNAL UBool U_EXPORT2 usearch_search(UStringSearch *strsrch,
  771. int32_t startIdx,
  772. int32_t *matchStart,
  773. int32_t *matchLimit,
  774. UErrorCode *status);
  775. /**
  776. * Simple backwards search for the pattern, starting at a specified index,
  777. * and using using a default set search options.
  778. *
  779. * This is an experimental function, and is not an official part of the
  780. * ICU API.
  781. *
  782. * The collator options, such as UCOL_STRENGTH and UCOL_NORMALIZTION, are honored.
  783. *
  784. * The UStringSearch options USEARCH_CANONICAL_MATCH, USEARCH_OVERLAP and
  785. * any Break Iterator are ignored.
  786. *
  787. * Matches obey the following constraints:
  788. *
  789. * Characters at the start or end positions of a match that are ignorable
  790. * for collation are not included as part of the match, unless they
  791. * are part of a combining sequence, as described below.
  792. *
  793. * A match will not include a partial combining sequence. Combining
  794. * character sequences are considered to be inseperable units,
  795. * and either match the pattern completely, or are considered to not match
  796. * at all. Thus, for example, an A followed a combining accent mark will
  797. * not be found when searching for a plain (unaccented) A. (unless
  798. * the collation strength has been set to ignore all accents).
  799. *
  800. * When beginning a search, the initial starting position, startIdx,
  801. * is assumed to be an acceptable match boundary with respect to
  802. * combining characters. A combining sequence that spans across the
  803. * starting point will not supress a match beginning at startIdx.
  804. *
  805. * Characters that expand to multiple collation elements
  806. * (German sharp-S becoming 'ss', or the composed forms of accented
  807. * characters, for example) also must match completely.
  808. * Searching for a single 's' in a string containing only a sharp-s will
  809. * find no match.
  810. *
  811. *
  812. * @param strsrch the UStringSearch struct, which references both
  813. * the text to be searched and the pattern being sought.
  814. * @param startIdx The index into the text to begin the search.
  815. * @param matchStart An out parameter, the starting index of the matched text.
  816. * This parameter may be NULL.
  817. * A value of -1 will be returned if no match was found.
  818. * @param matchLimit Out parameter, the index of the first position following the matched text.
  819. * The matchLimit will be at a suitable position for beginning a subsequent search
  820. * in the input text.
  821. * This parameter may be NULL.
  822. * A value of -1 will be returned if no match was found.
  823. *
  824. * @param status Report any errors. Note that no match found is not an error.
  825. * @return TRUE if a match was found, FALSE otherwise.
  826. *
  827. * @internal
  828. */
  829. U_INTERNAL UBool U_EXPORT2 usearch_searchBackwards(UStringSearch *strsrch,
  830. int32_t startIdx,
  831. int32_t *matchStart,
  832. int32_t *matchLimit,
  833. UErrorCode *status);
  834. #endif /* U_HIDE_INTERNAL_API */
  835. #endif /* #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION */
  836. #endif