intl/icu/source/i18n/unicode/usearch.h

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial