2 * Copyright (C) 1996-2010, International Business Machines Corporation and Others.
8 * \brief C API: Boyer-Moore StringSearch prototype.
15 #include "unicode/utypes.h"
17 #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION
19 #include "unicode/ucol.h"
22 * A <code>UCD</code> object holds the Collator-specific data needed to
23 * compute the length of the shortest string that can
24 * generate a partcular list of CEs.
26 * <code>UCD</code> objects are quite expensive to compute. Because
27 * of this, they are cached. When you call <code>ucd_open</code> it
28 * returns a reference counted cached object. When you call <code>ucd_close</code>
29 * the reference count on the object is decremented but the object is not deleted.
31 * If you do not need to reuse any unreferenced objects in the cache, you can call
32 * <code>ucd_flushCCache</code>. If you no longer need any <code>UCD</code>
33 * objects, you can call <code>ucd_freeCache</code>
38 * Open a <code>UCD</code> object.
40 * @param coll - the collator
41 * @param status - will be set if any errors occur.
43 * @return the <code>UCD</code> object. You must call
44 * <code>ucd_close</code> when you are done using the object.
46 * Note: if on return status is set to an error, the only safe
47 * thing to do with the returned object is to call <code>ucd_close</code>.
49 * @internal ICU 4.0.1 technology preview
51 U_CAPI UCD * U_EXPORT2
52 ucd_open(UCollator *coll, UErrorCode *status);
55 * Release a <code>UCD</code> object.
57 * @param ucd - the object
59 * @internal ICU 4.0.1 technology preview
65 * Get the <code>UCollator</code> object used to create a <code>UCD</code> object.
66 * The <code>UCollator</code> object returned may not be the exact
67 * object that was used to create this object, but it will have the
70 * @param ucd - the <code>UCD</code> object
72 * @return the <code>UCollator</code> used to create the given
73 * <code>UCD</code> object.
75 * @internal ICU 4.0.1 technology preview
77 U_CAPI UCollator * U_EXPORT2
78 ucd_getCollator(UCD *ucd);
81 * <code>UCD</code> objects are expensive to compute, and so
82 * may be cached. This routine will free the cached objects and delete
85 * WARNING: Don't call this until you are have called <code>close</code>
86 * for each <code>UCD</code> object that you have used. also,
87 * DO NOT call this if another thread may be calling <code>ucd_flushCache</code>
90 * @internal ICU 4.0.1 technology preview
96 * <code>UCD</code> objects are expensive to compute, and so
97 * may be cached. This routine will remove any unused <code>UCD</code>
98 * objects from the cache.
100 * @internal 4.0.1 technology preview
102 U_CAPI void U_EXPORT2
108 * This object holds the information needed to do a Collation sensitive Boyer-Moore search. It encapulates
109 * the pattern, the "bad character" and "good suffix" tables, the Collator-based data needed to compute them,
110 * and a reference to the text being searched.
112 * To do a search, you first need to get a <code>UCD</code> object by calling <code>ucd_open</code>.
113 * Then you construct a <code>BMS</code> object from the <code>UCD</code> object, the pattern
114 * string and the target string. Then you call the <code>search</code> method. Here's a code sample:
117 * void boyerMooreExample(UCollator *collator, UChar *pattern, int32_t patternLen, UChar *target, int32_t targetLength)
119 * UErrorCode status = U_ZERO_ERROR;
120 * int32_t offset = 0, start = -1, end = -1;
124 * ucd = ucd_open(collator, &status);
125 * if (U_FAILURE(status)) {
126 * // could not create a UCD object
130 * BMS *bms = bms_open(ucd, pattern, patternLength, target, targetlength, &status);
131 * if (U_FAILURE(status)) {
132 * // could not create a BMS object
138 * // Find all matches
139 * while (bms_search(bms, offset, &start, &end)) {
140 * // process the match between start and end
143 * // advance past the match
147 * // at this point, if offset == 0, there were no matches
149 * // handle the case of no matches
155 * // UCD objects are cached, so the call to
156 * // ucd_close doesn't delete the object.
157 * // Call this if you don't need the object any more.
162 * NOTE: This is a technology preview. The final version of this API may not bear any resenblence to this API.
165 * 1) Backwards searching has not been implemented.
167 * 2) For Han and Hangul characters, this code ignores any Collation tailorings. In general,
168 * this isn't a problem, but in Korean locals, at strength 1, Hangul characters are tailored
169 * to be equal to Han characters with the same pronounciation. Because this code ignroes
170 * tailorings, searching for a Hangul character will not find a Han character and visa-versa.
172 * 3) In some cases, searching for a pattern that needs to be normalized and ends
173 * in a discontiguous contraction may fail. The only known cases of this are with
174 * the Tibetan script. For example searching for the pattern
175 * "\u0F7F\u0F80\u0F81\u0F82\u0F83\u0F84\u0F85" will fail. (This case is artificial. We've
176 * been unable to find a pratical, real-world example of this failure.)
178 * NOTE: This is a technology preview. The final version of this API may not bear any resenblence to this API.
180 * @internal ICU 4.0.1 technology preview
183 typedef struct BMS BMS; /**< @see BMS */
186 * Construct a <code>MBS</code> object.
188 * @param ucd - A <code>UCD</code> object holding the Collator-sensitive data
189 * @param pattern - the string for which to search
190 * @param patternLength - the length of the string for which to search
191 * @param target - the string in which to search
192 * @param targetLength - the length of the string in which to search
193 * @param status - will be set if any errors occur.
195 * @return the <code>BMS</code> object.
197 * Note: if on return status is set to an error, the only safe
198 * thing to do with the returned object is to call
199 * <code>bms_close</code>.
201 * @internal ICU 4.0.1 technology preview
203 U_CAPI BMS * U_EXPORT2
205 const UChar *pattern, int32_t patternLength,
206 const UChar *target, int32_t targetLength,
210 * Close a <code>BMS</code> object and release all the
211 * storage associated with it.
213 * @param bms - the <code>BMS</code> object to close.
214 * @internal ICU 4.0.1 technology preview
216 U_CAPI void U_EXPORT2
220 * Test the pattern to see if it generates any CEs.
222 * @param bms - the <code>BMS</code> object
223 * @return <code>TRUE</code> if the pattern string did not generate any CEs
225 * @internal ICU 4.0.1 technology preview
227 U_CAPI UBool U_EXPORT2
231 * Get the <code>UCD</code> object used to create
232 * a given <code>BMS</code> object.
234 * @param bms - the <code>BMS</code> object
236 * @return - the <code>UCD</code> object used to create
237 * the given <code>BMS</code> object.
239 * @internal ICU 4.0.1 technology preview
241 U_CAPI UCD * U_EXPORT2
242 bms_getData(BMS *bms);
245 * Search for the pattern string in the target string.
247 * @param bms - the <code>BMS</code> object
248 * @param offset - the offset in the target string at which to begin the search
249 * @param start - will be set to the starting offset of the match, or -1 if there's no match
250 * @param end - will be set to the ending offset of the match, or -1 if there's no match
252 * @return <code>TRUE</code> if the match succeeds, <code>FALSE</code> otherwise.
254 * @internal ICU 4.0.1 technology preview
256 U_CAPI UBool U_EXPORT2
257 bms_search(BMS *bms, int32_t offset, int32_t *start, int32_t *end);
260 * Set the target string for the match.
262 * @param bms - the <code>BMS</code> object
263 * @param target - the new target string
264 * @param targetLength - the length of the new target string
265 * @param status - will be set if any errors occur.
267 * @internal ICU 4.0.1 technology preview
269 U_CAPI void U_EXPORT2
270 bms_setTargetString(BMS *bms, const UChar *target, int32_t targetLength, UErrorCode *status);