michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (C) 2001-2011 IBM and others. All rights reserved. michael@0: ********************************************************************** michael@0: * Date Name Description michael@0: * 06/28/2001 synwee Creation. michael@0: ********************************************************************** michael@0: */ michael@0: #ifndef USEARCH_H michael@0: #define USEARCH_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION michael@0: michael@0: #include "unicode/localpointer.h" michael@0: #include "unicode/ucol.h" michael@0: #include "unicode/ucoleitr.h" michael@0: #include "unicode/ubrk.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C API: StringSearch michael@0: * michael@0: * C Apis for an engine that provides language-sensitive text searching based michael@0: * on the comparison rules defined in a UCollator data struct, michael@0: * see ucol.h. This ensures that language eccentricity can be michael@0: * handled, e.g. for the German collator, characters ß and SS will be matched michael@0: * if case is chosen to be ignored. michael@0: * See the michael@0: * "ICU Collation Design Document" for more information. michael@0: *
michael@0: * The algorithm implemented is a modified form of the Boyer Moore's search. michael@0: * For more information see michael@0: * michael@0: * "Efficient Text Searching in Java", published in Java Report michael@0: * in February, 1999, for further information on the algorithm. michael@0: *
michael@0: * There are 2 match options for selection:
michael@0: * This search has APIs similar to that of other text iteration mechanisms
michael@0: * such as the break iterators in ubrk.h. Using these
michael@0: * APIs, it is easy to scan through text looking for all occurances of
michael@0: * a given pattern. This search iterator allows changing of direction by
michael@0: * calling a reset followed by a next or previous.
michael@0: * Though a direction change can occur without calling reset first,
michael@0: * this operation comes with some speed penalty.
michael@0: * Generally, match results in the forward direction will match the result
michael@0: * matches in the backwards direction in the reverse order
michael@0: *
michael@0: * usearch.h provides APIs to specify the starting position
michael@0: * within the text string to be searched, e.g. usearch_setOffset,
michael@0: * usearch_preceding and usearch_following. Since the
michael@0: * starting position will be set as it is specified, please take note that
michael@0: * there are some dangerous positions which the search may render incorrect
michael@0: * results:
michael@0: *
michael@0: * A breakiterator can be used if only matches at logical breaks are desired.
michael@0: * Using a breakiterator will only give you results that exactly matches the
michael@0: * boundaries given by the breakiterator. For instance the pattern "e" will
michael@0: * not be found in the string "\u00e9" if a character break iterator is used.
michael@0: *
michael@0: * Options are provided to handle overlapping matches.
michael@0: * E.g. In English, overlapping matches produces the result 0 and 2
michael@0: * for the pattern "abab" in the text "ababab", where else mutually
michael@0: * exclusive matches only produce the result of 0.
michael@0: *
michael@0: * Though collator attributes will be taken into consideration while
michael@0: * performing matches, there are no APIs here for setting and getting the
michael@0: * attributes. These attributes can be set by getting the collator
michael@0: * from usearch_getCollator and using the APIs in ucol.h.
michael@0: * Lastly to update String Search to the new collator attributes,
michael@0: * usearch_reset() has to be called.
michael@0: *
michael@0: * Restriction:
michael@0: * Example of use:
michael@0: * Use usearch_getMatchedLength to get the matched string length.
michael@0: * @param strsrch search iterator data struct
michael@0: * @return index to a substring within the text string that is being
michael@0: * searched.
michael@0: * @see #usearch_first
michael@0: * @see #usearch_next
michael@0: * @see #usearch_previous
michael@0: * @see #usearch_last
michael@0: * @see #USEARCH_DONE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE int32_t U_EXPORT2 usearch_getMatchedStart(
michael@0: const UStringSearch *strsrch);
michael@0:
michael@0: /**
michael@0: * Returns the length of text in the string which matches the search pattern.
michael@0: * This call returns a valid result only after a successful call to
michael@0: * usearch_first, usearch_next, usearch_previous,
michael@0: * or usearch_last.
michael@0: * Just after construction, or after a searching method returns
michael@0: * USEARCH_DONE, this method will return 0.
michael@0: * @param strsrch search iterator data struct
michael@0: * @return The length of the match in the string text, or 0 if there is no
michael@0: * match currently.
michael@0: * @see #usearch_first
michael@0: * @see #usearch_next
michael@0: * @see #usearch_previous
michael@0: * @see #usearch_last
michael@0: * @see #USEARCH_DONE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE int32_t U_EXPORT2 usearch_getMatchedLength(
michael@0: const UStringSearch *strsrch);
michael@0:
michael@0: /**
michael@0: * Returns the text that was matched by the most recent call to
michael@0: * usearch_first, usearch_next, usearch_previous,
michael@0: * or usearch_last.
michael@0: * If the iterator is not pointing at a valid match (e.g. just after
michael@0: * construction or after USEARCH_DONE has been returned, returns
michael@0: * an empty string. If result is not large enough to store the matched text,
michael@0: * result will be filled with the partial text and an U_BUFFER_OVERFLOW_ERROR
michael@0: * will be returned in status. result will be null-terminated whenever
michael@0: * possible. If the buffer fits the matched text exactly, a null-termination
michael@0: * is not possible, then a U_STRING_NOT_TERMINATED_ERROR set in status.
michael@0: * Pre-flighting can be either done with length = 0 or the API
michael@0: * usearch_getMatchLength.
michael@0: * @param strsrch search iterator data struct
michael@0: * @param result UChar buffer to store the matched string
michael@0: * @param resultCapacity length of the result buffer
michael@0: * @param status error returned if result is not large enough
michael@0: * @return exact length of the matched text, not counting the null-termination
michael@0: * @see #usearch_first
michael@0: * @see #usearch_next
michael@0: * @see #usearch_previous
michael@0: * @see #usearch_last
michael@0: * @see #USEARCH_DONE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE int32_t U_EXPORT2 usearch_getMatchedText(const UStringSearch *strsrch,
michael@0: UChar *result,
michael@0: int32_t resultCapacity,
michael@0: UErrorCode *status);
michael@0:
michael@0: #if !UCONFIG_NO_BREAK_ITERATION
michael@0:
michael@0: /**
michael@0: * Set the BreakIterator that will be used to restrict the points at which
michael@0: * matches are detected.
michael@0: * @param strsrch search iterator data struct
michael@0: * @param breakiter A BreakIterator that will be used to restrict the points
michael@0: * at which matches are detected. If a match is found, but
michael@0: * the match's start or end index is not a boundary as
michael@0: * determined by the BreakIterator, the match will
michael@0: * be rejected and another will be searched for.
michael@0: * If this parameter is NULL, no break detection is
michael@0: * attempted.
michael@0: * @param status for errors if it occurs
michael@0: * @see #usearch_getBreakIterator
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE void U_EXPORT2 usearch_setBreakIterator(UStringSearch *strsrch,
michael@0: UBreakIterator *breakiter,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Returns the BreakIterator that is used to restrict the points at which
michael@0: * matches are detected. This will be the same object that was passed to the
michael@0: * constructor or to usearch_setBreakIterator. Note that
michael@0: * NULL
michael@0: * is a legal value; it means that break detection should not be attempted.
michael@0: * @param strsrch search iterator data struct
michael@0: * @return break iterator used
michael@0: * @see #usearch_setBreakIterator
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE const UBreakIterator * U_EXPORT2 usearch_getBreakIterator(
michael@0: const UStringSearch *strsrch);
michael@0:
michael@0: #endif
michael@0:
michael@0: /**
michael@0: * Set the string text to be searched. Text iteration will hence begin at the
michael@0: * start of the text string. This method is useful if you want to re-use an
michael@0: * iterator to search for the same pattern within a different body of text.
michael@0: * @param strsrch search iterator data struct
michael@0: * @param text new string to look for match
michael@0: * @param textlength length of the new string, -1 for null-termination
michael@0: * @param status for errors if it occurs. If text is NULL, or textlength is 0
michael@0: * then an U_ILLEGAL_ARGUMENT_ERROR is returned with no change
michael@0: * done to strsrch.
michael@0: * @see #usearch_getText
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE void U_EXPORT2 usearch_setText( UStringSearch *strsrch,
michael@0: const UChar *text,
michael@0: int32_t textlength,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Return the string text to be searched.
michael@0: * @param strsrch search iterator data struct
michael@0: * @param length returned string text length
michael@0: * @return string text
michael@0: * @see #usearch_setText
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE const UChar * U_EXPORT2 usearch_getText(const UStringSearch *strsrch,
michael@0: int32_t *length);
michael@0:
michael@0: /**
michael@0: * Gets the collator used for the language rules.
michael@0: *
michael@0: * Deleting the returned UCollator before calling
michael@0: * usearch_close would cause the string search to fail.
michael@0: * usearch_close will delete the collator if this search owns it.
michael@0: * @param strsrch search iterator data struct
michael@0: * @return collator
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE UCollator * U_EXPORT2 usearch_getCollator(
michael@0: const UStringSearch *strsrch);
michael@0:
michael@0: /**
michael@0: * Sets the collator used for the language rules. User retains the ownership
michael@0: * of this collator, thus the responsibility of deletion lies with the user.
michael@0: * This method causes internal data such as Boyer-Moore shift tables to
michael@0: * be recalculated, but the iterator's position is unchanged.
michael@0: * @param strsrch search iterator data struct
michael@0: * @param collator to be used
michael@0: * @param status for errors if it occurs
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE void U_EXPORT2 usearch_setCollator( UStringSearch *strsrch,
michael@0: const UCollator *collator,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Sets the pattern used for matching.
michael@0: * Internal data like the Boyer Moore table will be recalculated, but the
michael@0: * iterator's position is unchanged.
michael@0: * @param strsrch search iterator data struct
michael@0: * @param pattern string
michael@0: * @param patternlength pattern length, -1 for null-terminated string
michael@0: * @param status for errors if it occurs. If text is NULL, or textlength is 0
michael@0: * then an U_ILLEGAL_ARGUMENT_ERROR is returned with no change
michael@0: * done to strsrch.
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE void U_EXPORT2 usearch_setPattern( UStringSearch *strsrch,
michael@0: const UChar *pattern,
michael@0: int32_t patternlength,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Gets the search pattern
michael@0: * @param strsrch search iterator data struct
michael@0: * @param length return length of the pattern, -1 indicates that the pattern
michael@0: * is null-terminated
michael@0: * @return pattern string
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE const UChar * U_EXPORT2 usearch_getPattern(
michael@0: const UStringSearch *strsrch,
michael@0: int32_t *length);
michael@0:
michael@0: /* methods ------------------------------------------------------------- */
michael@0:
michael@0: /**
michael@0: * Returns the first index at which the string text matches the search
michael@0: * pattern.
michael@0: * The iterator is adjusted so that its current index (as returned by
michael@0: * usearch_getOffset) is the match position if one was found.
michael@0: * If a match is not found, USEARCH_DONE will be returned and
michael@0: * the iterator will be adjusted to the index USEARCH_DONE.
michael@0: * @param strsrch search iterator data struct
michael@0: * @param status for errors if it occurs
michael@0: * @return The character index of the first match, or
michael@0: * USEARCH_DONE if there are no matches.
michael@0: * @see #usearch_getOffset
michael@0: * @see #USEARCH_DONE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE int32_t U_EXPORT2 usearch_first(UStringSearch *strsrch,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Returns the first index equal or greater than position at which
michael@0: * the string text
michael@0: * matches the search pattern. The iterator is adjusted so that its current
michael@0: * index (as returned by usearch_getOffset) is the match position if
michael@0: * one was found.
michael@0: * If a match is not found, USEARCH_DONE will be returned and
michael@0: * the iterator will be adjusted to the index USEARCH_DONE
michael@0: *
michael@0: * Search positions that may render incorrect results are highlighted in the
michael@0: * header comments. If position is less than or greater than the text range
michael@0: * for searching, an U_INDEX_OUTOFBOUNDS_ERROR will be returned
michael@0: * @param strsrch search iterator data struct
michael@0: * @param position to start the search at
michael@0: * @param status for errors if it occurs
michael@0: * @return The character index of the first match following pos,
michael@0: * or USEARCH_DONE if there are no matches.
michael@0: * @see #usearch_getOffset
michael@0: * @see #USEARCH_DONE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE int32_t U_EXPORT2 usearch_following(UStringSearch *strsrch,
michael@0: int32_t position,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Returns the last index in the target text at which it matches the search
michael@0: * pattern. The iterator is adjusted so that its current
michael@0: * index (as returned by usearch_getOffset) is the match position if
michael@0: * one was found.
michael@0: * If a match is not found, USEARCH_DONE will be returned and
michael@0: * the iterator will be adjusted to the index USEARCH_DONE.
michael@0: * @param strsrch search iterator data struct
michael@0: * @param status for errors if it occurs
michael@0: * @return The index of the first match, or USEARCH_DONE if there
michael@0: * are no matches.
michael@0: * @see #usearch_getOffset
michael@0: * @see #USEARCH_DONE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE int32_t U_EXPORT2 usearch_last(UStringSearch *strsrch,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Returns the first index less than position at which the string text
michael@0: * matches the search pattern. The iterator is adjusted so that its current
michael@0: * index (as returned by usearch_getOffset) is the match position if
michael@0: * one was found.
michael@0: * If a match is not found, USEARCH_DONE will be returned and
michael@0: * the iterator will be adjusted to the index USEARCH_DONE
michael@0: *
michael@0: * Search positions that may render incorrect results are highlighted in the
michael@0: * header comments. If position is less than or greater than the text range
michael@0: * for searching, an U_INDEX_OUTOFBOUNDS_ERROR will be returned.
michael@0: *
michael@0: * When USEARCH_OVERLAP option is off, the last index of the
michael@0: * result match is always less than position.
michael@0: * When USERARCH_OVERLAP is on, the result match may span across
michael@0: * position.
michael@0: * @param strsrch search iterator data struct
michael@0: * @param position index position the search is to begin at
michael@0: * @param status for errors if it occurs
michael@0: * @return The character index of the first match preceding pos,
michael@0: * or USEARCH_DONE if there are no matches.
michael@0: * @see #usearch_getOffset
michael@0: * @see #USEARCH_DONE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE int32_t U_EXPORT2 usearch_preceding(UStringSearch *strsrch,
michael@0: int32_t position,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Returns the index of the next point at which the string text matches the
michael@0: * search pattern, starting from the current position.
michael@0: * The iterator is adjusted so that its current
michael@0: * index (as returned by usearch_getOffset) is the match position if
michael@0: * one was found.
michael@0: * If a match is not found, USEARCH_DONE will be returned and
michael@0: * the iterator will be adjusted to the index USEARCH_DONE
michael@0: * @param strsrch search iterator data struct
michael@0: * @param status for errors if it occurs
michael@0: * @return The index of the next match after the current position, or
michael@0: * USEARCH_DONE if there are no more matches.
michael@0: * @see #usearch_first
michael@0: * @see #usearch_getOffset
michael@0: * @see #USEARCH_DONE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE int32_t U_EXPORT2 usearch_next(UStringSearch *strsrch,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Returns the index of the previous point at which the string text matches
michael@0: * the search pattern, starting at the current position.
michael@0: * The iterator is adjusted so that its current
michael@0: * index (as returned by usearch_getOffset) is the match position if
michael@0: * one was found.
michael@0: * If a match is not found, USEARCH_DONE will be returned and
michael@0: * the iterator will be adjusted to the index USEARCH_DONE
michael@0: * @param strsrch search iterator data struct
michael@0: * @param status for errors if it occurs
michael@0: * @return The index of the previous match before the current position,
michael@0: * or USEARCH_DONE if there are no more matches.
michael@0: * @see #usearch_last
michael@0: * @see #usearch_getOffset
michael@0: * @see #USEARCH_DONE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE int32_t U_EXPORT2 usearch_previous(UStringSearch *strsrch,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Reset the iteration.
michael@0: * Search will begin at the start of the text string if a forward iteration
michael@0: * is initiated before a backwards iteration. Otherwise if a backwards
michael@0: * iteration is initiated before a forwards iteration, the search will begin
michael@0: * at the end of the text string.
michael@0: * @param strsrch search iterator data struct
michael@0: * @see #usearch_first
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE void U_EXPORT2 usearch_reset(UStringSearch *strsrch);
michael@0:
michael@0: #ifndef U_HIDE_INTERNAL_API
michael@0: /**
michael@0: * Simple forward search for the pattern, starting at a specified index,
michael@0: * and using using a default set search options.
michael@0: *
michael@0: * This is an experimental function, and is not an official part of the
michael@0: * ICU API.
michael@0: *
michael@0: * The collator options, such as UCOL_STRENGTH and UCOL_NORMALIZTION, are honored.
michael@0: *
michael@0: * The UStringSearch options USEARCH_CANONICAL_MATCH, USEARCH_OVERLAP and
michael@0: * any Break Iterator are ignored.
michael@0: *
michael@0: * Matches obey the following constraints:
michael@0: *
michael@0: * Characters at the start or end positions of a match that are ignorable
michael@0: * for collation are not included as part of the match, unless they
michael@0: * are part of a combining sequence, as described below.
michael@0: *
michael@0: * A match will not include a partial combining sequence. Combining
michael@0: * character sequences are considered to be inseperable units,
michael@0: * and either match the pattern completely, or are considered to not match
michael@0: * at all. Thus, for example, an A followed a combining accent mark will
michael@0: * not be found when searching for a plain (unaccented) A. (unless
michael@0: * the collation strength has been set to ignore all accents).
michael@0: *
michael@0: * When beginning a search, the initial starting position, startIdx,
michael@0: * is assumed to be an acceptable match boundary with respect to
michael@0: * combining characters. A combining sequence that spans across the
michael@0: * starting point will not supress a match beginning at startIdx.
michael@0: *
michael@0: * Characters that expand to multiple collation elements
michael@0: * (German sharp-S becoming 'ss', or the composed forms of accented
michael@0: * characters, for example) also must match completely.
michael@0: * Searching for a single 's' in a string containing only a sharp-s will
michael@0: * find no match.
michael@0: *
michael@0: *
michael@0: * @param strsrch the UStringSearch struct, which references both
michael@0: * the text to be searched and the pattern being sought.
michael@0: * @param startIdx The index into the text to begin the search.
michael@0: * @param matchStart An out parameter, the starting index of the matched text.
michael@0: * This parameter may be NULL.
michael@0: * A value of -1 will be returned if no match was found.
michael@0: * @param matchLimit Out parameter, the index of the first position following the matched text.
michael@0: * The matchLimit will be at a suitable position for beginning a subsequent search
michael@0: * in the input text.
michael@0: * This parameter may be NULL.
michael@0: * A value of -1 will be returned if no match was found.
michael@0: *
michael@0: * @param status Report any errors. Note that no match found is not an error.
michael@0: * @return TRUE if a match was found, FALSE otherwise.
michael@0: *
michael@0: * @internal
michael@0: */
michael@0: U_INTERNAL UBool U_EXPORT2 usearch_search(UStringSearch *strsrch,
michael@0: int32_t startIdx,
michael@0: int32_t *matchStart,
michael@0: int32_t *matchLimit,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Simple backwards search for the pattern, starting at a specified index,
michael@0: * and using using a default set search options.
michael@0: *
michael@0: * This is an experimental function, and is not an official part of the
michael@0: * ICU API.
michael@0: *
michael@0: * The collator options, such as UCOL_STRENGTH and UCOL_NORMALIZTION, are honored.
michael@0: *
michael@0: * The UStringSearch options USEARCH_CANONICAL_MATCH, USEARCH_OVERLAP and
michael@0: * any Break Iterator are ignored.
michael@0: *
michael@0: * Matches obey the following constraints:
michael@0: *
michael@0: * Characters at the start or end positions of a match that are ignorable
michael@0: * for collation are not included as part of the match, unless they
michael@0: * are part of a combining sequence, as described below.
michael@0: *
michael@0: * A match will not include a partial combining sequence. Combining
michael@0: * character sequences are considered to be inseperable units,
michael@0: * and either match the pattern completely, or are considered to not match
michael@0: * at all. Thus, for example, an A followed a combining accent mark will
michael@0: * not be found when searching for a plain (unaccented) A. (unless
michael@0: * the collation strength has been set to ignore all accents).
michael@0: *
michael@0: * When beginning a search, the initial starting position, startIdx,
michael@0: * is assumed to be an acceptable match boundary with respect to
michael@0: * combining characters. A combining sequence that spans across the
michael@0: * starting point will not supress a match beginning at startIdx.
michael@0: *
michael@0: * Characters that expand to multiple collation elements
michael@0: * (German sharp-S becoming 'ss', or the composed forms of accented
michael@0: * characters, for example) also must match completely.
michael@0: * Searching for a single 's' in a string containing only a sharp-s will
michael@0: * find no match.
michael@0: *
michael@0: *
michael@0: * @param strsrch the UStringSearch struct, which references both
michael@0: * the text to be searched and the pattern being sought.
michael@0: * @param startIdx The index into the text to begin the search.
michael@0: * @param matchStart An out parameter, the starting index of the matched text.
michael@0: * This parameter may be NULL.
michael@0: * A value of -1 will be returned if no match was found.
michael@0: * @param matchLimit Out parameter, the index of the first position following the matched text.
michael@0: * The matchLimit will be at a suitable position for beginning a subsequent search
michael@0: * in the input text.
michael@0: * This parameter may be NULL.
michael@0: * A value of -1 will be returned if no match was found.
michael@0: *
michael@0: * @param status Report any errors. Note that no match found is not an error.
michael@0: * @return TRUE if a match was found, FALSE otherwise.
michael@0: *
michael@0: * @internal
michael@0: */
michael@0: U_INTERNAL UBool U_EXPORT2 usearch_searchBackwards(UStringSearch *strsrch,
michael@0: int32_t startIdx,
michael@0: int32_t *matchStart,
michael@0: int32_t *matchLimit,
michael@0: UErrorCode *status);
michael@0: #endif /* U_HIDE_INTERNAL_API */
michael@0:
michael@0: #endif /* #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION */
michael@0:
michael@0: #endif
michael@0: * Let S' be the sub-string of a text string S between the offsets start and
michael@0: * end
michael@0: * A pattern string P matches a text string S at the offsets
michael@0: * option 1. Some canonical equivalent of P matches some canonical equivalent
michael@0: * of S'
michael@0: * option 2. P matches S' and if P starts or ends with a combining mark,
michael@0: * there exists no non-ignorable combining mark before or after S'
michael@0: * in S respectively.
michael@0: *
michael@0: * Option 2. will be the default.
michael@0: *
michael@0: *
michael@0: *
michael@0: * Currently there are no composite characters that consists of a
michael@0: * character with combining class > 0 before a character with combining
michael@0: * class == 0. However, if such a character exists in the future, the
michael@0: * search mechanism does not guarantee the results for option 1.
michael@0: *
michael@0: *
michael@0: *
michael@0: * @stable ICU 2.4
michael@0: */
michael@0:
michael@0: /**
michael@0: * DONE is returned by previous() and next() after all valid matches have
michael@0: * been returned, and by first() and last() if there are no matches at all.
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define USEARCH_DONE -1
michael@0:
michael@0: /**
michael@0: * Data structure for searching
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: struct UStringSearch;
michael@0: /**
michael@0: * Data structure for searching
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: typedef struct UStringSearch UStringSearch;
michael@0:
michael@0: /**
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: typedef enum {
michael@0: /** Option for overlapping matches */
michael@0: USEARCH_OVERLAP,
michael@0: /**
michael@0: * Option for canonical matches. option 1 in header documentation.
michael@0: * The default value will be USEARCH_OFF
michael@0: */
michael@0: USEARCH_CANONICAL_MATCH,
michael@0: /**
michael@0: * Option to control how collation elements are compared.
michael@0: * The default value will be USEARCH_STANDARD_ELEMENT_COMPARISON.
michael@0: * @stable ICU 4.4
michael@0: */
michael@0: USEARCH_ELEMENT_COMPARISON,
michael@0:
michael@0: USEARCH_ATTRIBUTE_COUNT
michael@0: } USearchAttribute;
michael@0:
michael@0: /**
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: typedef enum {
michael@0: /** Default value for any USearchAttribute */
michael@0: USEARCH_DEFAULT = -1,
michael@0: /** Value for USEARCH_OVERLAP and USEARCH_CANONICAL_MATCH */
michael@0: USEARCH_OFF,
michael@0: /** Value for USEARCH_OVERLAP and USEARCH_CANONICAL_MATCH */
michael@0: USEARCH_ON,
michael@0: /**
michael@0: * Value (default) for USEARCH_ELEMENT_COMPARISON;
michael@0: * standard collation element comparison at the specified collator
michael@0: * strength.
michael@0: * @stable ICU 4.4
michael@0: */
michael@0: USEARCH_STANDARD_ELEMENT_COMPARISON,
michael@0: /**
michael@0: * Value for USEARCH_ELEMENT_COMPARISON;
michael@0: * collation element comparison is modified to effectively provide
michael@0: * behavior between the specified strength and strength - 1. Collation
michael@0: * elements in the pattern that have the base weight for the specified
michael@0: * strength are treated as "wildcards" that match an element with any
michael@0: * other weight at that collation level in the searched text. For
michael@0: * example, with a secondary-strength English collator, a plain 'e' in
michael@0: * the pattern will match a plain e or an e with any diacritic in the
michael@0: * searched text, but an e with diacritic in the pattern will only
michael@0: * match an e with the same diacritic in the searched text.
michael@0: * @stable ICU 4.4
michael@0: */
michael@0: USEARCH_PATTERN_BASE_WEIGHT_IS_WILDCARD,
michael@0: /**
michael@0: * Value for USEARCH_ELEMENT_COMPARISON.
michael@0: * collation element comparison is modified to effectively provide
michael@0: * behavior between the specified strength and strength - 1. Collation
michael@0: * elements in either the pattern or the searched text that have the
michael@0: * base weight for the specified strength are treated as "wildcards"
michael@0: * that match an element with any other weight at that collation level.
michael@0: * For example, with a secondary-strength English collator, a plain 'e'
michael@0: * in the pattern will match a plain e or an e with any diacritic in the
michael@0: * searched text, but an e with diacritic in the pattern will only
michael@0: * match an e with the same diacritic or a plain e in the searched text.
michael@0: * @stable ICU 4.4
michael@0: */
michael@0: USEARCH_ANY_BASE_WEIGHT_IS_WILDCARD,
michael@0:
michael@0: USEARCH_ATTRIBUTE_VALUE_COUNT
michael@0: } USearchAttributeValue;
michael@0:
michael@0: /* open and close ------------------------------------------------------ */
michael@0:
michael@0: /**
michael@0: * Creating a search iterator data struct using the argument locale language
michael@0: * rule set. A collator will be created in the process, which will be owned by
michael@0: * this search and will be deleted in usearch_close.
michael@0: * @param pattern for matching
michael@0: * @param patternlength length of the pattern, -1 for null-termination
michael@0: * @param text text string
michael@0: * @param textlength length of the text string, -1 for null-termination
michael@0: * @param locale name of locale for the rules to be used
michael@0: * @param breakiter A BreakIterator that will be used to restrict the points
michael@0: * at which matches are detected. If a match is found, but
michael@0: * the match's start or end index is not a boundary as
michael@0: * determined by the BreakIterator, the match will
michael@0: * be rejected and another will be searched for.
michael@0: * If this parameter is NULL, no break detection is
michael@0: * attempted.
michael@0: * @param status for errors if it occurs. If pattern or text is NULL, or if
michael@0: * patternlength or textlength is 0 then an
michael@0: * U_ILLEGAL_ARGUMENT_ERROR is returned.
michael@0: * @return search iterator data structure, or NULL if there is an error.
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE UStringSearch * U_EXPORT2 usearch_open(const UChar *pattern,
michael@0: int32_t patternlength,
michael@0: const UChar *text,
michael@0: int32_t textlength,
michael@0: const char *locale,
michael@0: UBreakIterator *breakiter,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Creating a search iterator data struct using the argument collator language
michael@0: * rule set. Note, user retains the ownership of this collator, thus the
michael@0: * responsibility of deletion lies with the user.
michael@0: * NOTE: string search cannot be instantiated from a collator that has
michael@0: * collate digits as numbers (CODAN) turned on.
michael@0: * @param pattern for matching
michael@0: * @param patternlength length of the pattern, -1 for null-termination
michael@0: * @param text text string
michael@0: * @param textlength length of the text string, -1 for null-termination
michael@0: * @param collator used for the language rules
michael@0: * @param breakiter A BreakIterator that will be used to restrict the points
michael@0: * at which matches are detected. If a match is found, but
michael@0: * the match's start or end index is not a boundary as
michael@0: * determined by the BreakIterator, the match will
michael@0: * be rejected and another will be searched for.
michael@0: * If this parameter is NULL, no break detection is
michael@0: * attempted.
michael@0: * @param status for errors if it occurs. If collator, pattern or text is NULL,
michael@0: * or if patternlength or textlength is 0 then an
michael@0: * U_ILLEGAL_ARGUMENT_ERROR is returned.
michael@0: * @return search iterator data structure, or NULL if there is an error.
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE UStringSearch * U_EXPORT2 usearch_openFromCollator(
michael@0: const UChar *pattern,
michael@0: int32_t patternlength,
michael@0: const UChar *text,
michael@0: int32_t textlength,
michael@0: const UCollator *collator,
michael@0: UBreakIterator *breakiter,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Destroying and cleaning up the search iterator data struct.
michael@0: * If a collator is created in usearch_open, it will be destroyed here.
michael@0: * @param searchiter data struct to clean up
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE void U_EXPORT2 usearch_close(UStringSearch *searchiter);
michael@0:
michael@0: #if U_SHOW_CPLUSPLUS_API
michael@0:
michael@0: U_NAMESPACE_BEGIN
michael@0:
michael@0: /**
michael@0: * \class LocalUStringSearchPointer
michael@0: * "Smart pointer" class, closes a UStringSearch via usearch_close().
michael@0: * For most methods see the LocalPointerBase base class.
michael@0: *
michael@0: * @see LocalPointerBase
michael@0: * @see LocalPointer
michael@0: * @stable ICU 4.4
michael@0: */
michael@0: U_DEFINE_LOCAL_OPEN_POINTER(LocalUStringSearchPointer, UStringSearch, usearch_close);
michael@0:
michael@0: U_NAMESPACE_END
michael@0:
michael@0: #endif
michael@0:
michael@0: /* get and set methods -------------------------------------------------- */
michael@0:
michael@0: /**
michael@0: * Sets the current position in the text string which the next search will
michael@0: * start from. Clears previous states.
michael@0: * This method takes the argument index and sets the position in the text
michael@0: * string accordingly without checking if the index is pointing to a
michael@0: * valid starting point to begin searching.
michael@0: * Search positions that may render incorrect results are highlighted in the
michael@0: * header comments
michael@0: * @param strsrch search iterator data struct
michael@0: * @param position position to start next search from. If position is less
michael@0: * than or greater than the text range for searching,
michael@0: * an U_INDEX_OUTOFBOUNDS_ERROR will be returned
michael@0: * @param status error status if any.
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE void U_EXPORT2 usearch_setOffset(UStringSearch *strsrch,
michael@0: int32_t position,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Return the current index in the string text being searched.
michael@0: * If the iteration has gone past the end of the text (or past the beginning
michael@0: * for a backwards search), USEARCH_DONE is returned.
michael@0: * @param strsrch search iterator data struct
michael@0: * @see #USEARCH_DONE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE int32_t U_EXPORT2 usearch_getOffset(const UStringSearch *strsrch);
michael@0:
michael@0: /**
michael@0: * Sets the text searching attributes located in the enum USearchAttribute
michael@0: * with values from the enum USearchAttributeValue.
michael@0: * USEARCH_DEFAULT can be used for all attributes for resetting.
michael@0: * @param strsrch search iterator data struct
michael@0: * @param attribute text attribute to be set
michael@0: * @param value text attribute value
michael@0: * @param status for errors if it occurs
michael@0: * @see #usearch_getAttribute
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE void U_EXPORT2 usearch_setAttribute(UStringSearch *strsrch,
michael@0: USearchAttribute attribute,
michael@0: USearchAttributeValue value,
michael@0: UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Gets the text searching attributes.
michael@0: * @param strsrch search iterator data struct
michael@0: * @param attribute text attribute to be retrieve
michael@0: * @return text attribute value
michael@0: * @see #usearch_setAttribute
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: U_STABLE USearchAttributeValue U_EXPORT2 usearch_getAttribute(
michael@0: const UStringSearch *strsrch,
michael@0: USearchAttribute attribute);
michael@0:
michael@0: /**
michael@0: * Returns the index to the match in the text string that was searched.
michael@0: * This call returns a valid result only after a successful call to
michael@0: * usearch_first, usearch_next, usearch_previous,
michael@0: * or usearch_last.
michael@0: * Just after construction, or after a searching method returns
michael@0: * USEARCH_DONE, this method will return USEARCH_DONE.
michael@0: *
michael@0: * char *tgtstr = "The quick brown fox jumped over the lazy fox";
michael@0: * char *patstr = "fox";
michael@0: * UChar target[64];
michael@0: * UChar pattern[16];
michael@0: * UErrorCode status = U_ZERO_ERROR;
michael@0: * u_uastrcpy(target, tgtstr);
michael@0: * u_uastrcpy(pattern, patstr);
michael@0: *
michael@0: * UStringSearch *search = usearch_open(pattern, -1, target, -1, "en_US",
michael@0: * NULL, &status);
michael@0: * if (U_SUCCESS(status)) {
michael@0: * for (int pos = usearch_first(search, &status);
michael@0: * pos != USEARCH_DONE;
michael@0: * pos = usearch_next(search, &status))
michael@0: * {
michael@0: * printf("Found match at %d pos, length is %d\n", pos,
michael@0: * usearch_getMatchLength(search));
michael@0: * }
michael@0: * }
michael@0: *
michael@0: * usearch_close(search);
michael@0: *