Top Description Inners Fields Constructors Methods
com.ibm.icu.text

public final Class StringSearch

extends SearchIterator
Class Inheritance
Imports
java.text.CharacterIterator, .StringCharacterIterator, java.util.Locale, com.ibm.icu.util.ICUException, .ULocale

StringSearch is a SearchIterator that provides language-sensitive text searching based on the comparison rules defined in a RuleBasedCollator object. StringSearch ensures that language eccentricity can be handled, e.g. for the German collator, characters ß and SS will be matched if case is chosen to be ignored. See the "ICU Collation Design Document" for more information.

There are 2 match options for selection:
Let S' be the sub-string of a text string S between the offsets start and end [start, end].
A pattern string P matches a text string S at the offsets [start, end] if

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?
          in S respectively.
Option 2. is the default.

This search has APIs similar to that of other text iteration mechanisms such as the break iterators in BreakIterator. Using these APIs, it is easy to scan through text looking for all occurrences of a given pattern. This search iterator allows changing of direction by calling a reset followed by a next or previous. Though a direction change can occur without calling reset first, this operation comes with some speed penalty. Match results in the forward direction will match the result matches in the backwards direction in the reverse order

SearchIterator provides APIs to specify the starting position within the text string to be searched, e.g. setIndex, preceding and following. Since the starting position will be set as it is specified, please take note that there are some danger points at which the search may render incorrect results:

A BreakIterator can be used if only matches at logical breaks are desired. Using a BreakIterator will only give you results that exactly matches the boundaries given by the BreakIterator. For instance the pattern "e" will not be found in the string "é" if a character break iterator is used.

Options are provided to handle overlapping matches. E.g. In English, overlapping matches produces the result 0 and 2 for the pattern "abab" in the text "ababab", where mutually exclusive matches only produces the result of 0.

Options are also provided to implement "asymmetric search" as described in UTS #10 Unicode Collation Algorithm, specifically the ElementComparisonType values.

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 from getCollator and using the APIs in RuleBasedCollator. Lastly to update StringSearch to the new collator attributes, reset has to be called.

Restriction:
Currently there are no composite characters that consists of a character with combining class > 0 before a character with combining class == 0. However, if such a character exists in the future, StringSearch does not guarantee the results for option 1.

Consult the SearchIterator documentation for information on and examples of how to use instances of this class to implement text searching.

Note, StringSearch is not to be subclassed.

Author
Laura Werner, synwee
ICU Status
Stable since ICU 2.0.
See Also
SearchIterator, RuleBasedCollator

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
StringSearch.CEBuffer

CEBuffer A circular buffer of CEs from the text being searched

private static class
StringSearch.CEI

Java port of ICU4C CEI (usearch.cpp) CEI Collation Element + source text index.

private static class
StringSearch.CollationPCE

Java port of ICU4C UCollationPCE (usrchimp.h)

private static class
StringSearch.Match

An object used for receiving matched index in search() and searchBackwards().

private static class
StringSearch.Pattern

Java port of ICU4C struct UPattern (usrchimp.h)

Field Summary

Modifier and TypeField and Description
private static int
private static int
private static final int
private static final int
private static final int
private static final int
pack-priv int
private RuleBasedCollator
private static final int
private Normalizer2
private StringSearch.Pattern
private static final int
private static final int
private int
private static final int
private CollationElementIterator
private StringSearch.CollationPCE
private boolean
private CollationElementIterator
pack-priv int
Inherited from com.ibm.icu.text.SearchIterator:
breakIteratorDONEmatchLengthsearch_targetText

Constructor Summary

AccessConstructor and Description
public
StringSearch(String
text to look for.
pattern
,
CharacterIterator
target text to search for pattern.
target
,
RuleBasedCollator
RuleBasedCollator that defines the language rules
collator
,
BreakIterator
A BreakIterator that is used to determine the boundaries of a logical match. This argument can be null.
breakiter
)

Initializes the iterator to use the language-specific rules defined in the argument collator to search for argument pattern in the argument target text.

public
StringSearch(String
text to look for.
pattern
,
CharacterIterator
target text to search for pattern.
target
,
RuleBasedCollator
RuleBasedCollator that defines the language rules
collator
)

Initializes the iterator to use the language-specific rules defined in the argument collator to search for argument pattern in the argument target text.

public
StringSearch(String
text to look for.
pattern
,
CharacterIterator
target text to search for pattern.
target
,
Locale
locale to use for language and break iterator rules
locale
)

Initializes the iterator to use the language-specific rules and break iterator rules defined in the argument locale to search for argument pattern in the argument target text.

public
StringSearch(String
text to look for.
pattern
,
CharacterIterator
target text to search for pattern.
target
,
ULocale
locale to use for language and break iterator rules
locale
)

Initializes the iterator to use the language-specific rules and break iterator rules defined in the argument locale to search for argument pattern in the argument target text.

public
StringSearch(String
text to look for.
pattern
,
String
target text to search for pattern.
target
)

Initializes the iterator to use the language-specific rules and break iterator rules defined in the default locale to search for argument pattern in the argument target text.

Method Summary

Modifier and TypeMethod and Description
private static int[]

Returns:

new destination array, destination if there was no new allocation
addToIntArray
(int[]
target array
destination
,
int
destination offset to add value
offset
,
int
to be added
value
,
int
incremental size expected
increments
)

Direct port of ICU4C static int32_t * addTouint32_tArray(...) in usearch.cpp (except not taking destination buffer size and status param).

private static long[]

Returns:

new destination array, destination if there was no new allocation
addToLongArray
(long[]
target array
destination
,
int
destination offset to add value
offset
,
int
target array size
destinationlength
,
long
to be added
value
,
int
incremental size expected
increments
)

Direct port of ICU4C static int64_t * addTouint64_tArray(...) in usearch.cpp.

private boolean

Returns:

true if identical match is found
checkIdentical
(int
offset of possible match
start
,
int
offset of possible match
end
)

Checks for identical match

private static int
codePointAt(CharacterIterator iter, int index)

private static int
private static int
compareCE64s(long targCE, long patCE, SearchIterator.ElementComparisonType compareType)

private int

Returns:

the modified collation element
getCE
(int sourcece)

Getting the modified collation elements taking into account the collation attributes.

public RuleBasedCollator

Returns:

RuleBasedCollator used by this StringSearch
getCollator
()

Gets the RuleBasedCollator used for the language rules.

public int
getIndex()

Implements abstract com.ibm.icu.text.SearchIterator.getIndex.

Return the current index in the text being searched.
private static int

Returns:

collation element mask
getMask
(int
collation strength
strength
)

Getting the mask for collation strength

public String

Returns:

the pattern searched for
getPattern
()

Returns the pattern for which StringSearch is searching for.

private static final String

Returns:

substring from text starting at start and length length
getString
(CharacterIterator
CharacterIterator
text
,
int
start offset
start
,
int
of substring
length
)

Gets a substring out of a CharacterIterator Java porting note: Not available in ICU4C

protected int
handleNext(int
The index in the target text at which the search should start.
position
)

Implements abstract com.ibm.icu.text.SearchIterator.handleNext.

Abstract method which subclasses override to provide the mechanism for finding the next match in the target text.
private boolean
private boolean
private boolean
protected int
handlePrevious(int
The index in the target text at which the search should start.
position
)

Implements abstract com.ibm.icu.text.SearchIterator.handlePrevious.

Abstract method which subclasses override to provide the mechanism for finding the previous match in the target text.
private boolean
private boolean
private boolean
private void
private int
private int

Returns:

total number of expansions
initializePatternCETable
()

Initializing the ce table for a pattern.

private int

Returns:

total number of expansions
initializePatternPCETable
()

Initializing the pce table for a pattern.

private boolean
private boolean
isBreakBoundary(int index)

public boolean

Returns:

true if canonical matches is set, false otherwise
isCanonical
()

Determines whether canonical matches (option 1, as described in the class documentation) is set.

private static final boolean

Returns:

true if offset is out of bounds, false otherwise
isOutOfBounds
(int
offset of the first character in the range
textstart
,
int
limit offset of the text string range
textlimit
,
int
to test
offset
)

Checks if the offset runs out of the text string range

private int
nextBoundaryAfter(int startIndex)

public void
reset()

Overrides com.ibm.icu.text.SearchIterator.reset.

Resets the iteration.
private boolean
search(int startIdx, StringSearch.Match m)

private boolean
public void
setCanonical(boolean
flag indicator if canonical matches are allowed
allowCanonical
)

Set the canonical match mode.

public void
setCollator(RuleBasedCollator
to use for this StringSearch
collator
)

Sets the RuleBasedCollator to be used for language-specific searching.

public void
setIndex(int
position from which to start the next search
position
)

Overrides com.ibm.icu.text.SearchIterator.setIndex.

Sets the position in the target text at which the next search will start.

protected void
setMatchNotFound()

Overrides com.ibm.icu.text.SearchIterator.setMatchNotFound.

Deprecated This API is ICU internal only.
public void
setPattern(String
for searching
pattern
)

Set the pattern to search for.

public void
setTarget(CharacterIterator
new text iterator to look for match,
text
)

Overrides com.ibm.icu.text.SearchIterator.setTarget.

Set the target text to be searched.
Inherited from com.ibm.icu.text.SearchIterator:
firstfollowinggetBreakIteratorgetElementComparisonTypegetMatchedTextgetMatchLengthgetMatchStartgetTargetisOverlappinglastnextprecedingprevioussetBreakIteratorsetElementComparisonTypesetMatchLengthsetOverlapping