+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/*
**********************************************************************
-* Copyright (C) 2001-2003 IBM and others. All rights reserved.
+* Copyright (C) 2001-2011,2014 IBM and others. All rights reserved.
**********************************************************************
* Date Name Description
* 06/28/2001 synwee Creation.
#include "unicode/utypes.h"
-#if !UCONFIG_NO_COLLATION
+#if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION
+#include "unicode/localpointer.h"
#include "unicode/ucol.h"
#include "unicode/ucoleitr.h"
#include "unicode/ubrk.h"
* C Apis for an engine that provides language-sensitive text searching based
* on the comparison rules defined in a <tt>UCollator</tt> data struct,
* see <tt>ucol.h</tt>. This ensures that language eccentricity can be
- * handled, e.g. for the German collator, characters ß and SS will be matched
+ * handled, e.g. for the German collator, characters ß and SS will be matched
* if case is chosen to be ignored.
- * See the <a href="http://oss.software.ibm.com/cvs/icu/~checkout~/icuhtml/design/collation/ICU_collation_design.htm">
+ * See the <a href="http://source.icu-project.org/repos/icu/icuhtml/trunk/design/collation/ICU_collation_design.htm">
* "ICU Collation Design Document"</a> for more information.
* <p>
- * The algorithm implemented is a modified form of the Boyer Moore's search.
- * For more information see
- * <a href="http://oss.software.ibm.com/icu/docs/papers/efficient_text_searching_in_java.html">
+ * The implementation may use a linear search or a modified form of the Boyer-Moore
+ * search; for more information on the latter see
+ * <a href="http://icu-project.org/docs/papers/efficient_text_searching_in_java.html">
* "Efficient Text Searching in Java"</a>, published in <i>Java Report</i>
- * in February, 1999, for further information on the algorithm.
+ * in February, 1999.
* <p>
* There are 2 match options for selection:<br>
* Let S' be the sub-string of a text string S between the offsets start and
* option 1. Some canonical equivalent of P matches some canonical equivalent
* of S'
* option 2. P matches S' and if P starts or ends with a combining mark,
- * there exists no non-ignorable combining mark before or after S\92
+ * there exists no non-ignorable combining mark before or after S'
* in S respectively.
* </pre>
- * Option 2. will be the default·
+ * Option 2. will be the default.
* <p>
* This search has APIs similar to that of other text iteration mechanisms
* such as the break iterators in <tt>ubrk.h</tt>. Using these
* for the pattern "abab" in the text "ababab", where else mutually
* exclusive matches only produce the result of 0.
* <p>
+ * Options are also provided to implement "asymmetric search" as described in
+ * <a href="http://www.unicode.org/reports/tr10/#Asymmetric_Search">
+ * UTS #10 Unicode Collation Algorithm</a>, specifically the USearchAttribute
+ * USEARCH_ELEMENT_COMPARISON and its values.
+ * <p>
* Though collator attributes will be taken into consideration while
* performing matches, there are no APIs here for setting and getting the
* attributes. These attributes can be set by getting the collator
* u_uastrcpy(pattern, patstr);
*
* UStringSearch *search = usearch_open(pattern, -1, target, -1, "en_US",
- * &status);
+ * NULL, &status);
* if (U_SUCCESS(status)) {
* for (int pos = usearch_first(search, &status);
- * pos != USEARCH_DONE;
- * pos = usearch_next(search, &status)) {
+ * pos != USEARCH_DONE;
+ * pos = usearch_next(search, &status))
+ * {
* printf("Found match at %d pos, length is %d\n", pos,
* usearch_getMatchLength(search));
* }
* }
+ *
+ * usearch_close(search);
* </code></pre>
* @stable ICU 2.4
*/
* @stable ICU 2.4
*/
typedef enum {
- /** Option for overlapping matches */
- USEARCH_OVERLAP,
+ /**
+ * Option for overlapping matches
+ * @stable ICU 2.4
+ */
+ USEARCH_OVERLAP = 0,
+#ifndef U_HIDE_DEPRECATED_API
+ /**
+ * Option for canonical matches; option 1 in header documentation.
+ * The default value will be USEARCH_OFF.
+ * Note: Setting this option to USEARCH_ON currently has no effect on
+ * search behavior, and this option is deprecated. Instead, to control
+ * canonical match behavior, you must set UCOL_NORMALIZATION_MODE
+ * appropriately (to UCOL_OFF or UCOL_ON) in the UCollator used by
+ * the UStringSearch object.
+ * @see usearch_openFromCollator
+ * @see usearch_getCollator
+ * @see usearch_setCollator
+ * @see ucol_getAttribute
+ * @deprecated ICU 53
+ */
+ USEARCH_CANONICAL_MATCH = 1,
+#endif /* U_HIDE_DEPRECATED_API */
/**
- Option for canonical matches. option 1 in header documentation.
- The default value will be USEARCH_OFF
- */
- USEARCH_CANONICAL_MATCH,
- USEARCH_ATTRIBUTE_COUNT
+ * Option to control how collation elements are compared.
+ * The default value will be USEARCH_STANDARD_ELEMENT_COMPARISON.
+ * @stable ICU 4.4
+ */
+ USEARCH_ELEMENT_COMPARISON = 2,
+
+#ifndef U_HIDE_DEPRECATED_API
+ /**
+ * One more than the highest normal USearchAttribute value.
+ * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
+ */
+ USEARCH_ATTRIBUTE_COUNT = 3
+#endif /* U_HIDE_DEPRECATED_API */
} USearchAttribute;
/**
* @stable ICU 2.4
*/
typedef enum {
- /** default value for any USearchAttribute */
+ /**
+ * Default value for any USearchAttribute
+ * @stable ICU 2.4
+ */
USEARCH_DEFAULT = -1,
- /** value for USEARCH_OVERLAP and USEARCH_CANONICAL_MATCH */
+ /**
+ * Value for USEARCH_OVERLAP and USEARCH_CANONICAL_MATCH
+ * @stable ICU 2.4
+ */
USEARCH_OFF,
- /** value for USEARCH_OVERLAP and USEARCH_CANONICAL_MATCH */
+ /**
+ * Value for USEARCH_OVERLAP and USEARCH_CANONICAL_MATCH
+ * @stable ICU 2.4
+ */
USEARCH_ON,
+ /**
+ * Value (default) for USEARCH_ELEMENT_COMPARISON;
+ * standard collation element comparison at the specified collator
+ * strength.
+ * @stable ICU 4.4
+ */
+ USEARCH_STANDARD_ELEMENT_COMPARISON,
+ /**
+ * Value for USEARCH_ELEMENT_COMPARISON;
+ * collation element comparison is modified to effectively provide
+ * behavior between the specified strength and strength - 1. Collation
+ * elements in the pattern that have the base weight for the specified
+ * strength are treated as "wildcards" that match an element with any
+ * other weight at that collation level in the searched text. For
+ * example, with a secondary-strength English collator, a plain 'e' in
+ * the pattern will match a plain e or an e with any diacritic in the
+ * searched text, but an e with diacritic in the pattern will only
+ * match an e with the same diacritic in the searched text.
+ *
+ * This supports "asymmetric search" as described in
+ * <a href="http://www.unicode.org/reports/tr10/#Asymmetric_Search">
+ * UTS #10 Unicode Collation Algorithm</a>.
+ *
+ * @stable ICU 4.4
+ */
+ USEARCH_PATTERN_BASE_WEIGHT_IS_WILDCARD,
+ /**
+ * Value for USEARCH_ELEMENT_COMPARISON.
+ * collation element comparison is modified to effectively provide
+ * behavior between the specified strength and strength - 1. Collation
+ * elements in either the pattern or the searched text that have the
+ * base weight for the specified strength are treated as "wildcards"
+ * that match an element with any other weight at that collation level.
+ * For example, with a secondary-strength English collator, a plain 'e'
+ * in the pattern will match a plain e or an e with any diacritic in the
+ * searched text, but an e with diacritic in the pattern will only
+ * match an e with the same diacritic or a plain e in the searched text.
+ *
+ * This option is similar to "asymmetric search" as described in
+ * <a href="http://www.unicode.org/reports/tr10/#Asymmetric_Search">
+ * UTS #10 Unicode Collation Algorithm</a, but also allows unmarked
+ * characters in the searched text to match marked or unmarked versions of
+ * that character in the pattern.
+ *
+ * @stable ICU 4.4
+ */
+ USEARCH_ANY_BASE_WEIGHT_IS_WILDCARD,
+
+#ifndef U_HIDE_DEPRECATED_API
+ /**
+ * One more than the highest normal USearchAttributeValue value.
+ * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
+ */
USEARCH_ATTRIBUTE_VALUE_COUNT
+#endif /* U_HIDE_DEPRECATED_API */
} USearchAttributeValue;
/* open and close ------------------------------------------------------ */
* @return search iterator data structure, or NULL if there is an error.
* @stable ICU 2.4
*/
-U_CAPI UStringSearch * U_EXPORT2 usearch_open(const UChar *pattern,
+U_STABLE UStringSearch * U_EXPORT2 usearch_open(const UChar *pattern,
int32_t patternlength,
const UChar *text,
int32_t textlength,
* Creating a search iterator data struct using the argument collator language
* rule set. Note, user retains the ownership of this collator, thus the
* responsibility of deletion lies with the user.
+* NOTE: string search cannot be instantiated from a collator that has
+* collate digits as numbers (CODAN) turned on.
* @param pattern for matching
* @param patternlength length of the pattern, -1 for null-termination
* @param text text string
* @return search iterator data structure, or NULL if there is an error.
* @stable ICU 2.4
*/
-U_CAPI UStringSearch * U_EXPORT2 usearch_openFromCollator(
+U_STABLE UStringSearch * U_EXPORT2 usearch_openFromCollator(
const UChar *pattern,
int32_t patternlength,
const UChar *text,
* @param searchiter data struct to clean up
* @stable ICU 2.4
*/
-U_CAPI void U_EXPORT2 usearch_close(UStringSearch *searchiter);
+U_STABLE void U_EXPORT2 usearch_close(UStringSearch *searchiter);
+
+#if U_SHOW_CPLUSPLUS_API
+
+U_NAMESPACE_BEGIN
+
+/**
+ * \class LocalUStringSearchPointer
+ * "Smart pointer" class, closes a UStringSearch via usearch_close().
+ * For most methods see the LocalPointerBase base class.
+ *
+ * @see LocalPointerBase
+ * @see LocalPointer
+ * @stable ICU 4.4
+ */
+U_DEFINE_LOCAL_OPEN_POINTER(LocalUStringSearchPointer, UStringSearch, usearch_close);
+
+U_NAMESPACE_END
+
+#endif // U_SHOW_CPLUSPLUS_API
/* get and set methods -------------------------------------------------- */
* header comments
* @param strsrch search iterator data struct
* @param position position to start next search from. If position is less
-* than or greater than the text range for searching,
+* than or greater than the text range for searching,
* an U_INDEX_OUTOFBOUNDS_ERROR will be returned
* @param status error status if any.
* @stable ICU 2.4
*/
-U_CAPI void U_EXPORT2 usearch_setOffset(UStringSearch *strsrch,
+U_STABLE void U_EXPORT2 usearch_setOffset(UStringSearch *strsrch,
int32_t position,
UErrorCode *status);
* @see #USEARCH_DONE
* @stable ICU 2.4
*/
-U_CAPI int32_t U_EXPORT2 usearch_getOffset(const UStringSearch *strsrch);
+U_STABLE int32_t U_EXPORT2 usearch_getOffset(const UStringSearch *strsrch);
/**
* Sets the text searching attributes located in the enum USearchAttribute
* @see #usearch_getAttribute
* @stable ICU 2.4
*/
-U_CAPI void U_EXPORT2 usearch_setAttribute(UStringSearch *strsrch,
+U_STABLE void U_EXPORT2 usearch_setAttribute(UStringSearch *strsrch,
USearchAttribute attribute,
USearchAttributeValue value,
UErrorCode *status);
* @see #usearch_setAttribute
* @stable ICU 2.4
*/
-U_CAPI USearchAttributeValue U_EXPORT2 usearch_getAttribute(
+U_STABLE USearchAttributeValue U_EXPORT2 usearch_getAttribute(
const UStringSearch *strsrch,
USearchAttribute attribute);
* @see #USEARCH_DONE
* @stable ICU 2.4
*/
-U_CAPI int32_t U_EXPORT2 usearch_getMatchedStart(
+U_STABLE int32_t U_EXPORT2 usearch_getMatchedStart(
const UStringSearch *strsrch);
/**
* @see #USEARCH_DONE
* @stable ICU 2.4
*/
-U_CAPI int32_t U_EXPORT2 usearch_getMatchedLength(
+U_STABLE int32_t U_EXPORT2 usearch_getMatchedLength(
const UStringSearch *strsrch);
/**
* @see #USEARCH_DONE
* @stable ICU 2.4
*/
-U_CAPI int32_t U_EXPORT2 usearch_getMatchedText(const UStringSearch *strsrch,
+U_STABLE int32_t U_EXPORT2 usearch_getMatchedText(const UStringSearch *strsrch,
UChar *result,
int32_t resultCapacity,
UErrorCode *status);
* @see #usearch_getBreakIterator
* @stable ICU 2.4
*/
-U_CAPI void U_EXPORT2 usearch_setBreakIterator(UStringSearch *strsrch,
+U_STABLE void U_EXPORT2 usearch_setBreakIterator(UStringSearch *strsrch,
UBreakIterator *breakiter,
UErrorCode *status);
* @see #usearch_setBreakIterator
* @stable ICU 2.4
*/
-U_CAPI const UBreakIterator * U_EXPORT2 usearch_getBreakIterator(
+U_STABLE const UBreakIterator * U_EXPORT2 usearch_getBreakIterator(
const UStringSearch *strsrch);
#endif
* @see #usearch_getText
* @stable ICU 2.4
*/
-U_CAPI void U_EXPORT2 usearch_setText( UStringSearch *strsrch,
+U_STABLE void U_EXPORT2 usearch_setText( UStringSearch *strsrch,
const UChar *text,
int32_t textlength,
UErrorCode *status);
* @see #usearch_setText
* @stable ICU 2.4
*/
-U_CAPI const UChar * U_EXPORT2 usearch_getText(const UStringSearch *strsrch,
+U_STABLE const UChar * U_EXPORT2 usearch_getText(const UStringSearch *strsrch,
int32_t *length);
/**
* @return collator
* @stable ICU 2.4
*/
-U_CAPI UCollator * U_EXPORT2 usearch_getCollator(
+U_STABLE UCollator * U_EXPORT2 usearch_getCollator(
const UStringSearch *strsrch);
/**
* @param status for errors if it occurs
* @stable ICU 2.4
*/
-U_CAPI void U_EXPORT2 usearch_setCollator( UStringSearch *strsrch,
+U_STABLE void U_EXPORT2 usearch_setCollator( UStringSearch *strsrch,
const UCollator *collator,
UErrorCode *status);
* done to strsrch.
* @stable ICU 2.4
*/
-U_CAPI void U_EXPORT2 usearch_setPattern( UStringSearch *strsrch,
+U_STABLE void U_EXPORT2 usearch_setPattern( UStringSearch *strsrch,
const UChar *pattern,
int32_t patternlength,
UErrorCode *status);
* @return pattern string
* @stable ICU 2.4
*/
-U_CAPI const UChar * U_EXPORT2 usearch_getPattern(
+U_STABLE const UChar * U_EXPORT2 usearch_getPattern(
const UStringSearch *strsrch,
int32_t *length);
* @see #USEARCH_DONE
* @stable ICU 2.4
*/
-U_CAPI int32_t U_EXPORT2 usearch_first(UStringSearch *strsrch,
+U_STABLE int32_t U_EXPORT2 usearch_first(UStringSearch *strsrch,
UErrorCode *status);
/**
-* Returns the first index greater than <tt>position</tt> at which the string
-* text
+* Returns the first index equal or greater than <tt>position</tt> at which
+* the string text
* matches the search pattern. The iterator is adjusted so that its current
* index (as returned by <tt>usearch_getOffset</tt>) is the match position if
* one was found.
* @see #USEARCH_DONE
* @stable ICU 2.4
*/
-U_CAPI int32_t U_EXPORT2 usearch_following(UStringSearch *strsrch,
+U_STABLE int32_t U_EXPORT2 usearch_following(UStringSearch *strsrch,
int32_t position,
UErrorCode *status);
* @see #USEARCH_DONE
* @stable ICU 2.4
*/
-U_CAPI int32_t U_EXPORT2 usearch_last(UStringSearch *strsrch,
+U_STABLE int32_t U_EXPORT2 usearch_last(UStringSearch *strsrch,
UErrorCode *status);
/**
* <p>
* Search positions that may render incorrect results are highlighted in the
* header comments. If position is less than or greater than the text range
-* for searching, an U_INDEX_OUTOFBOUNDS_ERROR will be returned
+* for searching, an U_INDEX_OUTOFBOUNDS_ERROR will be returned.
+* <p>
+* When <tt>USEARCH_OVERLAP</tt> option is off, the last index of the
+* result match is always less than <tt>position</tt>.
+* When <tt>USERARCH_OVERLAP</tt> is on, the result match may span across
+* <tt>position</tt>.
* @param strsrch search iterator data struct
* @param position index position the search is to begin at
* @param status for errors if it occurs
* @see #USEARCH_DONE
* @stable ICU 2.4
*/
-U_CAPI int32_t U_EXPORT2 usearch_preceding(UStringSearch *strsrch,
+U_STABLE int32_t U_EXPORT2 usearch_preceding(UStringSearch *strsrch,
int32_t position,
UErrorCode *status);
* @see #USEARCH_DONE
* @stable ICU 2.4
*/
-U_CAPI int32_t U_EXPORT2 usearch_next(UStringSearch *strsrch,
+U_STABLE int32_t U_EXPORT2 usearch_next(UStringSearch *strsrch,
UErrorCode *status);
/**
* @see #USEARCH_DONE
* @stable ICU 2.4
*/
-U_CAPI int32_t U_EXPORT2 usearch_previous(UStringSearch *strsrch,
+U_STABLE int32_t U_EXPORT2 usearch_previous(UStringSearch *strsrch,
UErrorCode *status);
/**
* @see #usearch_first
* @stable ICU 2.4
*/
-U_CAPI void U_EXPORT2 usearch_reset(UStringSearch *strsrch);
+U_STABLE void U_EXPORT2 usearch_reset(UStringSearch *strsrch);
-#endif /* #if !UCONFIG_NO_COLLATION */
+#ifndef U_HIDE_INTERNAL_API
+/**
+ * Simple forward search for the pattern, starting at a specified index,
+ * and using using a default set search options.
+ *
+ * This is an experimental function, and is not an official part of the
+ * ICU API.
+ *
+ * The collator options, such as UCOL_STRENGTH and UCOL_NORMALIZTION, are honored.
+ *
+ * The UStringSearch options USEARCH_CANONICAL_MATCH, USEARCH_OVERLAP and
+ * any Break Iterator are ignored.
+ *
+ * Matches obey the following constraints:
+ *
+ * Characters at the start or end positions of a match that are ignorable
+ * for collation are not included as part of the match, unless they
+ * are part of a combining sequence, as described below.
+ *
+ * A match will not include a partial combining sequence. Combining
+ * character sequences are considered to be inseperable units,
+ * and either match the pattern completely, or are considered to not match
+ * at all. Thus, for example, an A followed a combining accent mark will
+ * not be found when searching for a plain (unaccented) A. (unless
+ * the collation strength has been set to ignore all accents).
+ *
+ * When beginning a search, the initial starting position, startIdx,
+ * is assumed to be an acceptable match boundary with respect to
+ * combining characters. A combining sequence that spans across the
+ * starting point will not supress a match beginning at startIdx.
+ *
+ * Characters that expand to multiple collation elements
+ * (German sharp-S becoming 'ss', or the composed forms of accented
+ * characters, for example) also must match completely.
+ * Searching for a single 's' in a string containing only a sharp-s will
+ * find no match.
+ *
+ *
+ * @param strsrch the UStringSearch struct, which references both
+ * the text to be searched and the pattern being sought.
+ * @param startIdx The index into the text to begin the search.
+ * @param matchStart An out parameter, the starting index of the matched text.
+ * This parameter may be NULL.
+ * A value of -1 will be returned if no match was found.
+ * @param matchLimit Out parameter, the index of the first position following the matched text.
+ * The matchLimit will be at a suitable position for beginning a subsequent search
+ * in the input text.
+ * This parameter may be NULL.
+ * A value of -1 will be returned if no match was found.
+ *
+ * @param status Report any errors. Note that no match found is not an error.
+ * @return TRUE if a match was found, FALSE otherwise.
+ *
+ * @internal
+ */
+U_INTERNAL UBool U_EXPORT2 usearch_search(UStringSearch *strsrch,
+ int32_t startIdx,
+ int32_t *matchStart,
+ int32_t *matchLimit,
+ UErrorCode *status);
+
+/**
+ * Simple backwards search for the pattern, starting at a specified index,
+ * and using using a default set search options.
+ *
+ * This is an experimental function, and is not an official part of the
+ * ICU API.
+ *
+ * The collator options, such as UCOL_STRENGTH and UCOL_NORMALIZTION, are honored.
+ *
+ * The UStringSearch options USEARCH_CANONICAL_MATCH, USEARCH_OVERLAP and
+ * any Break Iterator are ignored.
+ *
+ * Matches obey the following constraints:
+ *
+ * Characters at the start or end positions of a match that are ignorable
+ * for collation are not included as part of the match, unless they
+ * are part of a combining sequence, as described below.
+ *
+ * A match will not include a partial combining sequence. Combining
+ * character sequences are considered to be inseperable units,
+ * and either match the pattern completely, or are considered to not match
+ * at all. Thus, for example, an A followed a combining accent mark will
+ * not be found when searching for a plain (unaccented) A. (unless
+ * the collation strength has been set to ignore all accents).
+ *
+ * When beginning a search, the initial starting position, startIdx,
+ * is assumed to be an acceptable match boundary with respect to
+ * combining characters. A combining sequence that spans across the
+ * starting point will not supress a match beginning at startIdx.
+ *
+ * Characters that expand to multiple collation elements
+ * (German sharp-S becoming 'ss', or the composed forms of accented
+ * characters, for example) also must match completely.
+ * Searching for a single 's' in a string containing only a sharp-s will
+ * find no match.
+ *
+ *
+ * @param strsrch the UStringSearch struct, which references both
+ * the text to be searched and the pattern being sought.
+ * @param startIdx The index into the text to begin the search.
+ * @param matchStart An out parameter, the starting index of the matched text.
+ * This parameter may be NULL.
+ * A value of -1 will be returned if no match was found.
+ * @param matchLimit Out parameter, the index of the first position following the matched text.
+ * The matchLimit will be at a suitable position for beginning a subsequent search
+ * in the input text.
+ * This parameter may be NULL.
+ * A value of -1 will be returned if no match was found.
+ *
+ * @param status Report any errors. Note that no match found is not an error.
+ * @return TRUE if a match was found, FALSE otherwise.
+ *
+ * @internal
+ */
+U_INTERNAL UBool U_EXPORT2 usearch_searchBackwards(UStringSearch *strsrch,
+ int32_t startIdx,
+ int32_t *matchStart,
+ int32_t *matchLimit,
+ UErrorCode *status);
+#endif /* U_HIDE_INTERNAL_API */
+
+#endif /* #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION */
#endif