1/*
2 * Copyright (C) 1996-2010, International Business Machines Corporation and Others.
3 * All rights reserved.
4 */
5
6/**
7 * \file
8 * \brief C API: Boyer-Moore StringSearch prototype.
9 * \internal
10 */
11
12#ifndef _BMS_H
13#define _BMS_H
14
15#include "unicode/utypes.h"
16
17#if !UCONFIG_NO_COLLATION && !UCONFIG_NO_BREAK_ITERATION
18
19#include "unicode/ucol.h"
20
21/**
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.
25 *
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.
30 *
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>
34 */
35typedef void UCD;
36
37/**
38 * Open a <code>UCD</code> object.
39 *
40 * @param coll - the collator
41 * @param status - will be set if any errors occur.
42 *
43 * @return the <code>UCD</code> object. You must call
44 *         <code>ucd_close</code> when you are done using the object.
45 *
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>.
48 *
49 * @internal ICU 4.0.1 technology preview
50 */
51U_CAPI UCD * U_EXPORT2
52ucd_open(UCollator *coll, UErrorCode *status);
53
54/**
55 * Release a <code>UCD</code> object.
56 *
57 * @param ucd - the object
58 *
59 * @internal ICU 4.0.1 technology preview
60 */
61U_CAPI void U_EXPORT2
62ucd_close(UCD *ucd);
63
64/**
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
68 * same behavior.
69 *
70 * @param ucd - the <code>UCD</code> object
71 *
72 * @return the <code>UCollator</code> used to create the given
73 *         <code>UCD</code> object.
74 *
75 * @internal ICU 4.0.1 technology preview
76 */
77U_CAPI UCollator * U_EXPORT2
78ucd_getCollator(UCD *ucd);
79
80/**
81 * <code>UCD</code> objects are expensive to compute, and so
82 * may be cached. This routine will free the cached objects and delete
83 * the cache.
84 *
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>
88 * at the same time.
89 *
90 * @internal ICU 4.0.1 technology preview
91 */
92U_CAPI void U_EXPORT2
93ucd_freeCache();
94
95/**
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.
99 *
100 * @internal 4.0.1 technology preview
101 */
102U_CAPI void U_EXPORT2
103ucd_flushCache();
104
105/**
106 * BMS
107 *
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.
111 *
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:
115 *
116 * <pre>
117 * void boyerMooreExample(UCollator *collator, UChar *pattern, int32_t patternLen, UChar *target, int32_t targetLength)
118 * {
119 *     UErrorCode status = U_ZERO_ERROR;
120 *     int32_t offset = 0, start = -1, end = -1;
121 *     UCD *ucd = NULL);
122 *     BMS *bms = NULL;
123 *
124 *     ucd = ucd_open(collator, &status);
125 *     if (U_FAILURE(status)) {
126 *         // could not create a UCD object
127 *         return;
128 *     }
129 *
130 *     BMS *bms = bms_open(ucd, pattern, patternLength, target, targetlength, &status);
131 *     if (U_FAILURE(status)) {
132 *         // could not create a BMS object
133 *         ucd_close(ucd);
134 *         return;
135 *     }
136 *
137 *
138 *     // Find all matches
139 *     while (bms_search(bms, offset, &start, &end)) {
140 *         // process the match between start and end
141 *         ...
142 *
143 *         // advance past the match
144 *         offset = end;
145 *     }
146 *
147 *     // at this point, if offset == 0, there were no matches
148 *     if (offset == 0) {
149 *         // handle the case of no matches
150 *     }
151 *
152 *     bms_close(bms);
153 *     ucd_close(ucd);
154 *
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.
158 *     ucd_flushCache();
159 * }
160 * </pre>
161 *
162 * NOTE: This is a technology preview. The final version of this API may not bear any resenblence to this API.
163 *
164 * Knows linitations:
165 *   1) Backwards searching has not been implemented.
166 *
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.
171 *
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.)
177 *
178 * NOTE: This is a technology preview. The final version of this API may not bear any resenblence to this API.
179 *
180 * @internal ICU 4.0.1 technology preview
181 */
182struct BMS;
183typedef struct BMS BMS; /**< @see BMS */
184
185/**
186 * Construct a <code>MBS</code> object.
187 *
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.
194 *
195 * @return the <code>BMS</code> object.
196 *
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>.
200 *
201 * @internal ICU 4.0.1 technology preview
202 */
203U_CAPI BMS * U_EXPORT2
204bms_open(UCD *ucd,
205         const UChar *pattern, int32_t patternLength,
206         const UChar *target,  int32_t targetLength,
207         UErrorCode  *status);
208
209/**
210 * Close a <code>BMS</code> object and release all the
211 * storage associated with it.
212 *
213 * @param bms - the <code>BMS</code> object to close.
214 * @internal ICU 4.0.1 technology preview
215 */
216U_CAPI void U_EXPORT2
217bms_close(BMS *bms);
218
219/**
220 * Test the pattern to see if it generates any CEs.
221 *
222 * @param bms - the <code>BMS</code> object
223 * @return <code>TRUE</code> if the pattern string did not generate any CEs
224 *
225 * @internal ICU 4.0.1 technology preview
226 */
227U_CAPI UBool U_EXPORT2
228bms_empty(BMS *bms);
229
230/**
231 * Get the <code>UCD</code> object used to create
232 * a given <code>BMS</code> object.
233 *
234 * @param bms - the <code>BMS</code> object
235 *
236 * @return - the <code>UCD</code> object used to create
237 *           the given <code>BMS</code> object.
238 *
239 * @internal ICU 4.0.1 technology preview
240 */
241U_CAPI UCD * U_EXPORT2
242bms_getData(BMS *bms);
243
244/**
245 * Search for the pattern string in the target string.
246 *
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
251 *
252 * @return <code>TRUE</code> if the match succeeds, <code>FALSE</code> otherwise.
253 *
254 * @internal ICU 4.0.1 technology preview
255 */
256U_CAPI UBool U_EXPORT2
257bms_search(BMS *bms, int32_t offset, int32_t *start, int32_t *end);
258
259/**
260 * Set the target string for the match.
261 *
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.
266 *
267 * @internal ICU 4.0.1 technology preview
268 */
269U_CAPI void U_EXPORT2
270bms_setTargetString(BMS *bms, const UChar *target, int32_t targetLength, UErrorCode *status);
271
272#endif
273
274#endif /* _BMS_H */
275