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