2 ******************************************************************************
3 * Copyright (C) 1996-2011, International Business Machines *
4 * Corporation and others. All Rights Reserved. *
5 ******************************************************************************
10 * \brief C++ API: Boyer-Moore StringSearch technology preview
11 * \internal ICU 4.0.1 technology preview
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION
21 #include "unicode/uobject.h"
22 #include "unicode/ucol.h"
24 #include "unicode/colldata.h"
28 class BadCharacterTable
;
29 class GoodSuffixTable
;
32 #ifndef U_HIDE_INTERNAL_API
36 * This object holds the information needed to do a Collation sensitive Boyer-Moore search. It encapulates
37 * the pattern, the "bad character" and "good suffix" tables, the Collator-based data needed to compute them,
38 * and a reference to the text being searched.
40 * To do a search, you fist need to get a <code>CollData</code> object by calling <code>CollData::open</code>.
41 * Then you construct a <code>BoyerMooreSearch</code> object from the <code>CollData</code> object, the pattern
42 * string and the target string. Then you call the <code>search</code> method. Here's a code sample:
45 * void boyerMooreExample(UCollator *collator, UnicodeString *pattern, UnicodeString *target)
47 * UErrorCode status = U_ZERO_ERROR;
48 * CollData *collData = CollData::open(collator, status);
50 * if (U_FAILURE(status)) {
51 * // could not create a CollData object
55 * BoyerMooreSearch *search = new BoyerMooreSearch(collData, *patternString, target, status);
57 * if (U_FAILURE(status)) {
58 * // could not create a BoyerMooreSearch object
59 * CollData::close(collData);
63 * int32_t offset = 0, start = -1, end = -1;
66 * while (search->search(offset, start, end)) {
67 * // process the match between start and end
69 * // advance past the match
73 * // at this point, if offset == 0, there were no matches
75 * // handle the case of no matches
79 * CollData::close(collData);
81 * // CollData objects are cached, so the call to
82 * // CollData::close doesn't delete the object.
83 * // Call this if you don't need the object any more.
84 * CollData::flushCollDataCache();
88 * NOTE: This is a technology preview. The final version of this API may not bear any resenblence to this API.
91 * 1) Backwards searching has not been implemented.
93 * 2) For Han and Hangul characters, this code ignores any Collation tailorings. In general,
94 * this isn't a problem, but in Korean locals, at strength 1, Hangul characters are tailored
95 * to be equal to Han characters with the same pronounciation. Because this code ignroes
96 * tailorings, searching for a Hangul character will not find a Han character and visa-versa.
98 * 3) In some cases, searching for a pattern that needs to be normalized and ends
99 * in a discontiguous contraction may fail. The only known cases of this are with
100 * the Tibetan script. For example searching for the pattern
101 * "\u0F7F\u0F80\u0F81\u0F82\u0F83\u0F84\u0F85" will fail. (This case is artificial. We've
102 * been unable to find a pratical, real-world example of this failure.)
104 * @internal ICU 4.0.1 technology preview
108 class U_I18N_API BoyerMooreSearch
: public UObject
112 * Construct a <code>BoyerMooreSearch</code> object.
114 * @param theData - A <code>CollData</code> object holding the Collator-sensitive data
115 * @param patternString - the string for which to search
116 * @param targetString - the string in which to search or <code>NULL</code> if youu will
117 * set it later by calling <code>setTargetString</code>.
118 * @param status - will be set if any errors occur.
120 * Note: if on return, status is set to an error code,
121 * the only safe thing to do with this object is to call
124 * @internal ICU 4.0.1 technology preview
126 BoyerMooreSearch(CollData
*theData
, const UnicodeString
&patternString
, const UnicodeString
*targetString
, UErrorCode
&status
);
131 * @internal ICU 4.0.1 technology preview
136 * Test the pattern to see if it generates any CEs.
138 * @return <code>TRUE</code> if the pattern string did not generate any CEs
140 * @internal ICU 4.0.1 technology preview
145 * Search for the pattern string in the target string.
147 * @param offset - the offset in the target string at which to begin the search
148 * @param start - will be set to the starting offset of the match, or -1 if there's no match
149 * @param end - will be set to the ending offset of the match, or -1 if there's no match
151 * @return <code>TRUE</code> if the match succeeds, <code>FALSE</code> otherwise.
153 * @internal ICU 4.0.1 technology preview
155 UBool
search(int32_t offset
, int32_t &start
, int32_t &end
);
158 * Set the target string for the match.
160 * @param targetString - the new target string
161 * @param status - will be set if any errors occur.
163 * @internal ICU 4.0.1 technology preview
165 void setTargetString(const UnicodeString
*targetString
, UErrorCode
&status
);
167 // **** no longer need these? ****
169 * Return the <code>CollData</code> object used for searching
171 * @return the <code>CollData</code> object used for searching
173 * @internal ICU 4.0.1 technology preview
178 * Return the CEs generated by the pattern string.
180 * @return a <code>CEList</code> object holding the CEs generated by the pattern string.
182 * @internal ICU 4.0.1 technology preview
184 CEList
*getPatternCEs();
187 * Return the <code>BadCharacterTable</code> object computed for the pattern string.
189 * @return the <code>BadCharacterTable</code> object.
191 * @internal ICU 4.0.1 technology preview
193 BadCharacterTable
*getBadCharacterTable();
196 * Return the <code>GoodSuffixTable</code> object computed for the pattern string.
198 * @return the <code>GoodSuffixTable</code> object computed for the pattern string.
200 * @internal ICU 4.0.1 technology preview
202 GoodSuffixTable
*getGoodSuffixTable();
206 * @internal ICU 4.0.1 technology preview
208 virtual UClassID
getDynamicClassID() const;
211 * @internal ICU 4.0.1 technology preview
213 static UClassID
getStaticClassID();
218 BadCharacterTable
*badCharacterTable
;
219 GoodSuffixTable
*goodSuffixTable
;
220 UnicodeString pattern
;
223 #endif /* U_HIDE_INTERNAL_API */
227 #endif // #if !UCONFIG_NO_COLLATION
228 #endif // #ifndef B_M_SEARCH_H