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