usearch.h 38 KB

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