/src/mozilla-central/intl/icu/source/common/brkiter.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ******************************************************************************* |
5 | | * Copyright (C) 1997-2015, International Business Machines Corporation and |
6 | | * others. All Rights Reserved. |
7 | | ******************************************************************************* |
8 | | * |
9 | | * File brkiter.cpp |
10 | | * |
11 | | * Modification History: |
12 | | * |
13 | | * Date Name Description |
14 | | * 02/18/97 aliu Converted from OpenClass. Added DONE. |
15 | | * 01/13/2000 helena Added UErrorCode parameter to createXXXInstance methods. |
16 | | ***************************************************************************************** |
17 | | */ |
18 | | |
19 | | // ***************************************************************************** |
20 | | // This file was generated from the java source file BreakIterator.java |
21 | | // ***************************************************************************** |
22 | | |
23 | | #include "unicode/utypes.h" |
24 | | |
25 | | #if !UCONFIG_NO_BREAK_ITERATION |
26 | | |
27 | | #include "unicode/rbbi.h" |
28 | | #include "unicode/brkiter.h" |
29 | | #include "unicode/udata.h" |
30 | | #include "unicode/ures.h" |
31 | | #include "unicode/ustring.h" |
32 | | #include "unicode/filteredbrk.h" |
33 | | #include "ucln_cmn.h" |
34 | | #include "cstring.h" |
35 | | #include "umutex.h" |
36 | | #include "servloc.h" |
37 | | #include "locbased.h" |
38 | | #include "uresimp.h" |
39 | | #include "uassert.h" |
40 | | #include "ubrkimpl.h" |
41 | | #include "charstr.h" |
42 | | |
43 | | // ***************************************************************************** |
44 | | // class BreakIterator |
45 | | // This class implements methods for finding the location of boundaries in text. |
46 | | // Instances of BreakIterator maintain a current position and scan over text |
47 | | // returning the index of characters where boundaries occur. |
48 | | // ***************************************************************************** |
49 | | |
50 | | U_NAMESPACE_BEGIN |
51 | | |
52 | | // ------------------------------------- |
53 | | |
54 | | BreakIterator* |
55 | | BreakIterator::buildInstance(const Locale& loc, const char *type, UErrorCode &status) |
56 | 0 | { |
57 | 0 | char fnbuff[256]; |
58 | 0 | char ext[4]={'\0'}; |
59 | 0 | CharString actualLocale; |
60 | 0 | int32_t size; |
61 | 0 | const UChar* brkfname = NULL; |
62 | 0 | UResourceBundle brkRulesStack; |
63 | 0 | UResourceBundle brkNameStack; |
64 | 0 | UResourceBundle *brkRules = &brkRulesStack; |
65 | 0 | UResourceBundle *brkName = &brkNameStack; |
66 | 0 | RuleBasedBreakIterator *result = NULL; |
67 | 0 |
|
68 | 0 | if (U_FAILURE(status)) |
69 | 0 | return NULL; |
70 | 0 | |
71 | 0 | ures_initStackObject(brkRules); |
72 | 0 | ures_initStackObject(brkName); |
73 | 0 |
|
74 | 0 | // Get the locale |
75 | 0 | UResourceBundle *b = ures_openNoDefault(U_ICUDATA_BRKITR, loc.getName(), &status); |
76 | 0 |
|
77 | 0 | // Get the "boundaries" array. |
78 | 0 | if (U_SUCCESS(status)) { |
79 | 0 | brkRules = ures_getByKeyWithFallback(b, "boundaries", brkRules, &status); |
80 | 0 | // Get the string object naming the rules file |
81 | 0 | brkName = ures_getByKeyWithFallback(brkRules, type, brkName, &status); |
82 | 0 | // Get the actual string |
83 | 0 | brkfname = ures_getString(brkName, &size, &status); |
84 | 0 | U_ASSERT((size_t)size<sizeof(fnbuff)); |
85 | 0 | if ((size_t)size>=sizeof(fnbuff)) { |
86 | 0 | size=0; |
87 | 0 | if (U_SUCCESS(status)) { |
88 | 0 | status = U_BUFFER_OVERFLOW_ERROR; |
89 | 0 | } |
90 | 0 | } |
91 | 0 |
|
92 | 0 | // Use the string if we found it |
93 | 0 | if (U_SUCCESS(status) && brkfname) { |
94 | 0 | actualLocale.append(ures_getLocaleInternal(brkName, &status), -1, status); |
95 | 0 |
|
96 | 0 | UChar* extStart=u_strchr(brkfname, 0x002e); |
97 | 0 | int len = 0; |
98 | 0 | if(extStart!=NULL){ |
99 | 0 | len = (int)(extStart-brkfname); |
100 | 0 | u_UCharsToChars(extStart+1, ext, sizeof(ext)); // nul terminates the buff |
101 | 0 | u_UCharsToChars(brkfname, fnbuff, len); |
102 | 0 | } |
103 | 0 | fnbuff[len]=0; // nul terminate |
104 | 0 | } |
105 | 0 | } |
106 | 0 |
|
107 | 0 | ures_close(brkRules); |
108 | 0 | ures_close(brkName); |
109 | 0 |
|
110 | 0 | UDataMemory* file = udata_open(U_ICUDATA_BRKITR, ext, fnbuff, &status); |
111 | 0 | if (U_FAILURE(status)) { |
112 | 0 | ures_close(b); |
113 | 0 | return NULL; |
114 | 0 | } |
115 | 0 |
|
116 | 0 | // Create a RuleBasedBreakIterator |
117 | 0 | result = new RuleBasedBreakIterator(file, status); |
118 | 0 |
|
119 | 0 | // If there is a result, set the valid locale and actual locale, and the kind |
120 | 0 | if (U_SUCCESS(status) && result != NULL) { |
121 | 0 | U_LOCALE_BASED(locBased, *(BreakIterator*)result); |
122 | 0 | locBased.setLocaleIDs(ures_getLocaleByType(b, ULOC_VALID_LOCALE, &status), |
123 | 0 | actualLocale.data()); |
124 | 0 | } |
125 | 0 |
|
126 | 0 | ures_close(b); |
127 | 0 |
|
128 | 0 | if (U_FAILURE(status) && result != NULL) { // Sometimes redundant check, but simple |
129 | 0 | delete result; |
130 | 0 | return NULL; |
131 | 0 | } |
132 | 0 | |
133 | 0 | if (result == NULL) { |
134 | 0 | udata_close(file); |
135 | 0 | if (U_SUCCESS(status)) { |
136 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
137 | 0 | } |
138 | 0 | } |
139 | 0 |
|
140 | 0 | return result; |
141 | 0 | } |
142 | | |
143 | | // Creates a break iterator for word breaks. |
144 | | BreakIterator* U_EXPORT2 |
145 | | BreakIterator::createWordInstance(const Locale& key, UErrorCode& status) |
146 | 0 | { |
147 | 0 | return createInstance(key, UBRK_WORD, status); |
148 | 0 | } |
149 | | |
150 | | // ------------------------------------- |
151 | | |
152 | | // Creates a break iterator for line breaks. |
153 | | BreakIterator* U_EXPORT2 |
154 | | BreakIterator::createLineInstance(const Locale& key, UErrorCode& status) |
155 | 0 | { |
156 | 0 | return createInstance(key, UBRK_LINE, status); |
157 | 0 | } |
158 | | |
159 | | // ------------------------------------- |
160 | | |
161 | | // Creates a break iterator for character breaks. |
162 | | BreakIterator* U_EXPORT2 |
163 | | BreakIterator::createCharacterInstance(const Locale& key, UErrorCode& status) |
164 | 0 | { |
165 | 0 | return createInstance(key, UBRK_CHARACTER, status); |
166 | 0 | } |
167 | | |
168 | | // ------------------------------------- |
169 | | |
170 | | // Creates a break iterator for sentence breaks. |
171 | | BreakIterator* U_EXPORT2 |
172 | | BreakIterator::createSentenceInstance(const Locale& key, UErrorCode& status) |
173 | 0 | { |
174 | 0 | return createInstance(key, UBRK_SENTENCE, status); |
175 | 0 | } |
176 | | |
177 | | // ------------------------------------- |
178 | | |
179 | | // Creates a break iterator for title casing breaks. |
180 | | BreakIterator* U_EXPORT2 |
181 | | BreakIterator::createTitleInstance(const Locale& key, UErrorCode& status) |
182 | 0 | { |
183 | 0 | return createInstance(key, UBRK_TITLE, status); |
184 | 0 | } |
185 | | |
186 | | // ------------------------------------- |
187 | | |
188 | | // Gets all the available locales that has localized text boundary data. |
189 | | const Locale* U_EXPORT2 |
190 | | BreakIterator::getAvailableLocales(int32_t& count) |
191 | 0 | { |
192 | 0 | return Locale::getAvailableLocales(count); |
193 | 0 | } |
194 | | |
195 | | // ------------------------------------------ |
196 | | // |
197 | | // Constructors, destructor and assignment operator |
198 | | // |
199 | | //------------------------------------------- |
200 | | |
201 | | BreakIterator::BreakIterator() |
202 | 0 | { |
203 | 0 | *validLocale = *actualLocale = 0; |
204 | 0 | } |
205 | | |
206 | 0 | BreakIterator::BreakIterator(const BreakIterator &other) : UObject(other) { |
207 | 0 | uprv_strncpy(actualLocale, other.actualLocale, sizeof(actualLocale)); |
208 | 0 | uprv_strncpy(validLocale, other.validLocale, sizeof(validLocale)); |
209 | 0 | } |
210 | | |
211 | 0 | BreakIterator &BreakIterator::operator =(const BreakIterator &other) { |
212 | 0 | if (this != &other) { |
213 | 0 | uprv_strncpy(actualLocale, other.actualLocale, sizeof(actualLocale)); |
214 | 0 | uprv_strncpy(validLocale, other.validLocale, sizeof(validLocale)); |
215 | 0 | } |
216 | 0 | return *this; |
217 | 0 | } |
218 | | |
219 | | BreakIterator::~BreakIterator() |
220 | 0 | { |
221 | 0 | } |
222 | | |
223 | | // ------------------------------------------ |
224 | | // |
225 | | // Registration |
226 | | // |
227 | | //------------------------------------------- |
228 | | #if !UCONFIG_NO_SERVICE |
229 | | |
230 | | // ------------------------------------- |
231 | | |
232 | | class ICUBreakIteratorFactory : public ICUResourceBundleFactory { |
233 | | public: |
234 | | virtual ~ICUBreakIteratorFactory(); |
235 | | protected: |
236 | 0 | virtual UObject* handleCreate(const Locale& loc, int32_t kind, const ICUService* /*service*/, UErrorCode& status) const { |
237 | 0 | return BreakIterator::makeInstance(loc, kind, status); |
238 | 0 | } |
239 | | }; |
240 | | |
241 | 0 | ICUBreakIteratorFactory::~ICUBreakIteratorFactory() {} |
242 | | |
243 | | // ------------------------------------- |
244 | | |
245 | | class ICUBreakIteratorService : public ICULocaleService { |
246 | | public: |
247 | | ICUBreakIteratorService() |
248 | | : ICULocaleService(UNICODE_STRING("Break Iterator", 14)) |
249 | 0 | { |
250 | 0 | UErrorCode status = U_ZERO_ERROR; |
251 | 0 | registerFactory(new ICUBreakIteratorFactory(), status); |
252 | 0 | } |
253 | | |
254 | | virtual ~ICUBreakIteratorService(); |
255 | | |
256 | 0 | virtual UObject* cloneInstance(UObject* instance) const { |
257 | 0 | return ((BreakIterator*)instance)->clone(); |
258 | 0 | } |
259 | | |
260 | 0 | virtual UObject* handleDefault(const ICUServiceKey& key, UnicodeString* /*actualID*/, UErrorCode& status) const { |
261 | 0 | LocaleKey& lkey = (LocaleKey&)key; |
262 | 0 | int32_t kind = lkey.kind(); |
263 | 0 | Locale loc; |
264 | 0 | lkey.currentLocale(loc); |
265 | 0 | return BreakIterator::makeInstance(loc, kind, status); |
266 | 0 | } |
267 | | |
268 | 0 | virtual UBool isDefault() const { |
269 | 0 | return countFactories() == 1; |
270 | 0 | } |
271 | | }; |
272 | | |
273 | 0 | ICUBreakIteratorService::~ICUBreakIteratorService() {} |
274 | | |
275 | | // ------------------------------------- |
276 | | |
277 | | // defined in ucln_cmn.h |
278 | | U_NAMESPACE_END |
279 | | |
280 | | static icu::UInitOnce gInitOnceBrkiter; |
281 | | static icu::ICULocaleService* gService = NULL; |
282 | | |
283 | | |
284 | | |
285 | | /** |
286 | | * Release all static memory held by breakiterator. |
287 | | */ |
288 | | U_CDECL_BEGIN |
289 | 0 | static UBool U_CALLCONV breakiterator_cleanup(void) { |
290 | 0 | #if !UCONFIG_NO_SERVICE |
291 | 0 | if (gService) { |
292 | 0 | delete gService; |
293 | 0 | gService = NULL; |
294 | 0 | } |
295 | 0 | gInitOnceBrkiter.reset(); |
296 | 0 | #endif |
297 | 0 | return TRUE; |
298 | 0 | } |
299 | | U_CDECL_END |
300 | | U_NAMESPACE_BEGIN |
301 | | |
302 | | static void U_CALLCONV |
303 | 0 | initService(void) { |
304 | 0 | gService = new ICUBreakIteratorService(); |
305 | 0 | ucln_common_registerCleanup(UCLN_COMMON_BREAKITERATOR, breakiterator_cleanup); |
306 | 0 | } |
307 | | |
308 | | static ICULocaleService* |
309 | | getService(void) |
310 | 0 | { |
311 | 0 | umtx_initOnce(gInitOnceBrkiter, &initService); |
312 | 0 | return gService; |
313 | 0 | } |
314 | | |
315 | | |
316 | | // ------------------------------------- |
317 | | |
318 | | static inline UBool |
319 | | hasService(void) |
320 | 0 | { |
321 | 0 | return !gInitOnceBrkiter.isReset() && getService() != NULL; |
322 | 0 | } |
323 | | |
324 | | // ------------------------------------- |
325 | | |
326 | | URegistryKey U_EXPORT2 |
327 | | BreakIterator::registerInstance(BreakIterator* toAdopt, const Locale& locale, UBreakIteratorType kind, UErrorCode& status) |
328 | 0 | { |
329 | 0 | ICULocaleService *service = getService(); |
330 | 0 | if (service == NULL) { |
331 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
332 | 0 | return NULL; |
333 | 0 | } |
334 | 0 | return service->registerInstance(toAdopt, locale, kind, status); |
335 | 0 | } |
336 | | |
337 | | // ------------------------------------- |
338 | | |
339 | | UBool U_EXPORT2 |
340 | | BreakIterator::unregister(URegistryKey key, UErrorCode& status) |
341 | 0 | { |
342 | 0 | if (U_SUCCESS(status)) { |
343 | 0 | if (hasService()) { |
344 | 0 | return gService->unregister(key, status); |
345 | 0 | } |
346 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
347 | 0 | } |
348 | 0 | return FALSE; |
349 | 0 | } |
350 | | |
351 | | // ------------------------------------- |
352 | | |
353 | | StringEnumeration* U_EXPORT2 |
354 | | BreakIterator::getAvailableLocales(void) |
355 | 0 | { |
356 | 0 | ICULocaleService *service = getService(); |
357 | 0 | if (service == NULL) { |
358 | 0 | return NULL; |
359 | 0 | } |
360 | 0 | return service->getAvailableLocales(); |
361 | 0 | } |
362 | | #endif /* UCONFIG_NO_SERVICE */ |
363 | | |
364 | | // ------------------------------------- |
365 | | |
366 | | BreakIterator* |
367 | | BreakIterator::createInstance(const Locale& loc, int32_t kind, UErrorCode& status) |
368 | 0 | { |
369 | 0 | if (U_FAILURE(status)) { |
370 | 0 | return NULL; |
371 | 0 | } |
372 | 0 | |
373 | 0 | #if !UCONFIG_NO_SERVICE |
374 | 0 | if (hasService()) { |
375 | 0 | Locale actualLoc(""); |
376 | 0 | BreakIterator *result = (BreakIterator*)gService->get(loc, kind, &actualLoc, status); |
377 | 0 | // TODO: The way the service code works in ICU 2.8 is that if |
378 | 0 | // there is a real registered break iterator, the actualLoc |
379 | 0 | // will be populated, but if the handleDefault path is taken |
380 | 0 | // (because nothing is registered that can handle the |
381 | 0 | // requested locale) then the actualLoc comes back empty. In |
382 | 0 | // that case, the returned object already has its actual/valid |
383 | 0 | // locale data populated (by makeInstance, which is what |
384 | 0 | // handleDefault calls), so we don't touch it. YES, A COMMENT |
385 | 0 | // THIS LONG is a sign of bad code -- so the action item is to |
386 | 0 | // revisit this in ICU 3.0 and clean it up/fix it/remove it. |
387 | 0 | if (U_SUCCESS(status) && (result != NULL) && *actualLoc.getName() != 0) { |
388 | 0 | U_LOCALE_BASED(locBased, *result); |
389 | 0 | locBased.setLocaleIDs(actualLoc.getName(), actualLoc.getName()); |
390 | 0 | } |
391 | 0 | return result; |
392 | 0 | } |
393 | 0 | else |
394 | 0 | #endif |
395 | 0 | { |
396 | 0 | return makeInstance(loc, kind, status); |
397 | 0 | } |
398 | 0 | } |
399 | | |
400 | | // ------------------------------------- |
401 | | enum { kKeyValueLenMax = 32 }; |
402 | | |
403 | | BreakIterator* |
404 | | BreakIterator::makeInstance(const Locale& loc, int32_t kind, UErrorCode& status) |
405 | 0 | { |
406 | 0 |
|
407 | 0 | if (U_FAILURE(status)) { |
408 | 0 | return NULL; |
409 | 0 | } |
410 | 0 | char lbType[kKeyValueLenMax]; |
411 | 0 |
|
412 | 0 | BreakIterator *result = NULL; |
413 | 0 | switch (kind) { |
414 | 0 | case UBRK_CHARACTER: |
415 | 0 | result = BreakIterator::buildInstance(loc, "grapheme", status); |
416 | 0 | break; |
417 | 0 | case UBRK_WORD: |
418 | 0 | result = BreakIterator::buildInstance(loc, "word", status); |
419 | 0 | break; |
420 | 0 | case UBRK_LINE: |
421 | 0 | uprv_strcpy(lbType, "line"); |
422 | 0 | { |
423 | 0 | char lbKeyValue[kKeyValueLenMax] = {0}; |
424 | 0 | UErrorCode kvStatus = U_ZERO_ERROR; |
425 | 0 | int32_t kLen = loc.getKeywordValue("lb", lbKeyValue, kKeyValueLenMax, kvStatus); |
426 | 0 | if (U_SUCCESS(kvStatus) && kLen > 0 && (uprv_strcmp(lbKeyValue,"strict")==0 || uprv_strcmp(lbKeyValue,"normal")==0 || uprv_strcmp(lbKeyValue,"loose")==0)) { |
427 | 0 | uprv_strcat(lbType, "_"); |
428 | 0 | uprv_strcat(lbType, lbKeyValue); |
429 | 0 | } |
430 | 0 | } |
431 | 0 | result = BreakIterator::buildInstance(loc, lbType, status); |
432 | 0 | break; |
433 | 0 | case UBRK_SENTENCE: |
434 | 0 | result = BreakIterator::buildInstance(loc, "sentence", status); |
435 | 0 | #if !UCONFIG_NO_FILTERED_BREAK_ITERATION |
436 | 0 | { |
437 | 0 | char ssKeyValue[kKeyValueLenMax] = {0}; |
438 | 0 | UErrorCode kvStatus = U_ZERO_ERROR; |
439 | 0 | int32_t kLen = loc.getKeywordValue("ss", ssKeyValue, kKeyValueLenMax, kvStatus); |
440 | 0 | if (U_SUCCESS(kvStatus) && kLen > 0 && uprv_strcmp(ssKeyValue,"standard")==0) { |
441 | 0 | FilteredBreakIteratorBuilder* fbiBuilder = FilteredBreakIteratorBuilder::createInstance(loc, kvStatus); |
442 | 0 | if (U_SUCCESS(kvStatus)) { |
443 | 0 | result = fbiBuilder->build(result, status); |
444 | 0 | delete fbiBuilder; |
445 | 0 | } |
446 | 0 | } |
447 | 0 | } |
448 | 0 | #endif |
449 | 0 | break; |
450 | 0 | case UBRK_TITLE: |
451 | 0 | result = BreakIterator::buildInstance(loc, "title", status); |
452 | 0 | break; |
453 | 0 | default: |
454 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
455 | 0 | } |
456 | 0 |
|
457 | 0 | if (U_FAILURE(status)) { |
458 | 0 | return NULL; |
459 | 0 | } |
460 | 0 | |
461 | 0 | return result; |
462 | 0 | } |
463 | | |
464 | | Locale |
465 | 0 | BreakIterator::getLocale(ULocDataLocaleType type, UErrorCode& status) const { |
466 | 0 | U_LOCALE_BASED(locBased, *this); |
467 | 0 | return locBased.getLocale(type, status); |
468 | 0 | } |
469 | | |
470 | | const char * |
471 | 0 | BreakIterator::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const { |
472 | 0 | U_LOCALE_BASED(locBased, *this); |
473 | 0 | return locBased.getLocaleID(type, status); |
474 | 0 | } |
475 | | |
476 | | |
477 | | // This implementation of getRuleStatus is a do-nothing stub, here to |
478 | | // provide a default implementation for any derived BreakIterator classes that |
479 | | // do not implement it themselves. |
480 | 0 | int32_t BreakIterator::getRuleStatus() const { |
481 | 0 | return 0; |
482 | 0 | } |
483 | | |
484 | | // This implementation of getRuleStatusVec is a do-nothing stub, here to |
485 | | // provide a default implementation for any derived BreakIterator classes that |
486 | | // do not implement it themselves. |
487 | 0 | int32_t BreakIterator::getRuleStatusVec(int32_t *fillInVec, int32_t capacity, UErrorCode &status) { |
488 | 0 | if (U_FAILURE(status)) { |
489 | 0 | return 0; |
490 | 0 | } |
491 | 0 | if (capacity < 1) { |
492 | 0 | status = U_BUFFER_OVERFLOW_ERROR; |
493 | 0 | return 1; |
494 | 0 | } |
495 | 0 | *fillInVec = 0; |
496 | 0 | return 1; |
497 | 0 | } |
498 | | |
499 | 0 | BreakIterator::BreakIterator (const Locale& valid, const Locale& actual) { |
500 | 0 | U_LOCALE_BASED(locBased, (*this)); |
501 | 0 | locBased.setLocaleIDs(valid, actual); |
502 | 0 | } |
503 | | |
504 | | U_NAMESPACE_END |
505 | | |
506 | | #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ |
507 | | |
508 | | //eof |