2 ******************************************************************************
3 * Copyright (C) 1996-2010, 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;
35 * This object holds the information needed to do a Collation sensitive Boyer-Moore search. It encapulates
36 * the pattern, the "bad character" and "good suffix" tables, the Collator-based data needed to compute them,
37 * and a reference to the text being searched.
39 * To do a search, you fist need to get a <code>CollData</code> object by calling <code>CollData::open</code>.
40 * Then you construct a <code>BoyerMooreSearch</code> object from the <code>CollData</code> object, the pattern
41 * string and the target string. Then you call the <code>search</code> method. Here's a code sample:
44 * void boyerMooreExample(UCollator *collator, UnicodeString *pattern, UnicodeString *target)
46 * UErrorCode status = U_ZERO_ERROR;
47 * CollData *collData = CollData::open(collator, status);
49 * if (U_FAILURE(status)) {
50 * // could not create a CollData object
54 * BoyerMooreSearch *search = new BoyerMooreSearch(collData, *patternString, target, status);
56 * if (U_FAILURE(status)) {
57 * // could not create a BoyerMooreSearch object
58 * CollData::close(collData);
62 * int32_t offset = 0, start = -1, end = -1;
65 * while (search->search(offset, start, end)) {
66 * // process the match between start and end
68 * // advance past the match
72 * // at this point, if offset == 0, there were no matches
74 * // handle the case of no matches
78 * CollData::close(collData);
80 * // CollData objects are cached, so the call to
81 * // CollData::close doesn't delete the object.
82 * // Call this if you don't need the object any more.
83 * CollData::flushCollDataCache();
87 * NOTE: This is a technology preview. The final version of this API may not bear any resenblence to this API.
90 * 1) Backwards searching has not been implemented.
92 * 2) For Han and Hangul characters, this code ignores any Collation tailorings. In general,
93 * this isn't a problem, but in Korean locals, at strength 1, Hangul characters are tailored
94 * to be equal to Han characters with the same pronounciation. Because this code ignroes
95 * tailorings, searching for a Hangul character will not find a Han character and visa-versa.
97 * 3) In some cases, searching for a pattern that needs to be normalized and ends
98 * in a discontiguous contraction may fail. The only known cases of this are with
99 * the Tibetan script. For example searching for the pattern
100 * "\u0F7F\u0F80\u0F81\u0F82\u0F83\u0F84\u0F85" will fail. (This case is artificial. We've
101 * been unable to find a pratical, real-world example of this failure.)
103 * @internal ICU 4.0.1 technology preview
107 class U_I18N_API BoyerMooreSearch : public UObject
111 * Construct a <code>BoyerMooreSearch</code> object.
113 * @param theData - A <code>CollData</code> object holding the Collator-sensitive data
114 * @param patternString - the string for which to search
115 * @param targetString - the string in which to search or <code>NULL</code> if youu will
116 * set it later by calling <code>setTargetString</code>.
117 * @param status - will be set if any errors occur.
119 * Note: if on return, status is set to an error code,
120 * the only safe thing to do with this object is to call
123 * @internal ICU 4.0.1 technology preview
125 BoyerMooreSearch(CollData *theData, const UnicodeString &patternString, const UnicodeString *targetString, UErrorCode &status);
130 * @internal ICU 4.0.1 technology preview
135 * Test the pattern to see if it generates any CEs.
137 * @return <code>TRUE</code> if the pattern string did not generate any CEs
139 * @internal ICU 4.0.1 technology preview
144 * Search for the pattern string in the target string.
146 * @param offset - the offset in the target string at which to begin the search
147 * @param start - will be set to the starting offset of the match, or -1 if there's no match
148 * @param end - will be set to the ending offset of the match, or -1 if there's no match
150 * @return <code>TRUE</code> if the match succeeds, <code>FALSE</code> otherwise.
152 * @internal ICU 4.0.1 technology preview
154 UBool search(int32_t offset, int32_t &start, int32_t &end);
157 * Set the target string for the match.
159 * @param targetString - the new target string
160 * @param status - will be set if any errors occur.
162 * @internal ICU 4.0.1 technology preview
164 void setTargetString(const UnicodeString *targetString, UErrorCode &status);
166 // **** no longer need these? ****
168 * Return the <code>CollData</code> object used for searching
170 * @return the <code>CollData</code> object used for searching
172 * @internal ICU 4.0.1 technology preview
177 * Return the CEs generated by the pattern string.
179 * @return a <code>CEList</code> object holding the CEs generated by the pattern string.
181 * @internal ICU 4.0.1 technology preview
183 CEList *getPatternCEs();
186 * Return the <code>BadCharacterTable</code> object computed for the pattern string.
188 * @return the <code>BadCharacterTable</code> object.
190 * @internal ICU 4.0.1 technology preview
192 BadCharacterTable *getBadCharacterTable();
195 * Return the <code>GoodSuffixTable</code> object computed for the pattern string.
197 * @return the <code>GoodSuffixTable</code> object computed for the pattern string.
199 * @internal ICU 4.0.1 technology preview
201 GoodSuffixTable *getGoodSuffixTable();
205 * @internal ICU 4.0.1 technology preview
207 virtual UClassID getDynamicClassID() const;
210 * @internal ICU 4.0.1 technology preview
212 static UClassID getStaticClassID();
217 BadCharacterTable *badCharacterTable;
218 GoodSuffixTable *goodSuffixTable;
219 UnicodeString pattern;
225 #endif // #if !UCONFIG_NO_COLLATION
226 #endif // #ifndef B_M_SEARCH_H