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