2 *******************************************************************************
3 * Copyright (C) 1997-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 02/18/97 aliu Converted from OpenClass. Added DONE.
13 * 01/13/2000 helena Added UErrorCode parameter to createXXXInstance methods.
14 *****************************************************************************************
17 // *****************************************************************************
18 // This file was generated from the java source file BreakIterator.java
19 // *****************************************************************************
21 #include "unicode/utypes.h"
23 #if !UCONFIG_NO_BREAK_ITERATION
25 #include "unicode/rbbi.h"
26 #include "unicode/brkiter.h"
27 #include "unicode/udata.h"
28 #include "unicode/ures.h"
29 #include "unicode/ustring.h"
30 #include "unicode/filteredbrk.h"
40 #include "unicode/filteredbrk.h"
42 // *****************************************************************************
43 // class BreakIterator
44 // This class implements methods for finding the location of boundaries in text.
45 // Instances of BreakIterator maintain a current position and scan over text
46 // returning the index of characters where boundaries occur.
47 // *****************************************************************************
51 // -------------------------------------
54 BreakIterator::buildInstance(const Locale
& loc
, const char *type
, int32_t kind
, UErrorCode
&status
)
58 CharString actualLocale
;
60 const UChar
* brkfname
= NULL
;
61 UResourceBundle brkRulesStack
;
62 UResourceBundle brkNameStack
;
63 UResourceBundle
*brkRules
= &brkRulesStack
;
64 UResourceBundle
*brkName
= &brkNameStack
;
65 RuleBasedBreakIterator
*result
= NULL
;
67 if (U_FAILURE(status
))
70 ures_initStackObject(brkRules
);
71 ures_initStackObject(brkName
);
74 UResourceBundle
*b
= ures_openNoDefault(U_ICUDATA_BRKITR
, loc
.getName(), &status
);
76 // Get the "boundaries" array.
77 if (U_SUCCESS(status
)) {
78 brkRules
= ures_getByKeyWithFallback(b
, "boundaries", brkRules
, &status
);
79 // Get the string object naming the rules file
80 brkName
= ures_getByKeyWithFallback(brkRules
, type
, brkName
, &status
);
81 // Get the actual string
82 brkfname
= ures_getString(brkName
, &size
, &status
);
83 U_ASSERT((size_t)size
<sizeof(fnbuff
));
84 if ((size_t)size
>=sizeof(fnbuff
)) {
86 if (U_SUCCESS(status
)) {
87 status
= U_BUFFER_OVERFLOW_ERROR
;
91 // Use the string if we found it
92 if (U_SUCCESS(status
) && brkfname
) {
93 actualLocale
.append(ures_getLocaleInternal(brkName
, &status
), -1, status
);
95 UChar
* extStart
=u_strchr(brkfname
, 0x002e);
98 len
= (int)(extStart
-brkfname
);
99 u_UCharsToChars(extStart
+1, ext
, sizeof(ext
)); // nul terminates the buff
100 u_UCharsToChars(brkfname
, fnbuff
, len
);
102 fnbuff
[len
]=0; // nul terminate
106 ures_close(brkRules
);
109 UDataMemory
* file
= udata_open(U_ICUDATA_BRKITR
, ext
, fnbuff
, &status
);
110 if (U_FAILURE(status
)) {
115 // Create a RuleBasedBreakIterator
116 result
= new RuleBasedBreakIterator(file
, status
);
118 // If there is a result, set the valid locale and actual locale, and the kind
119 if (U_SUCCESS(status
) && result
!= NULL
) {
120 U_LOCALE_BASED(locBased
, *(BreakIterator
*)result
);
121 locBased
.setLocaleIDs(ures_getLocaleByType(b
, ULOC_VALID_LOCALE
, &status
),
122 actualLocale
.data());
123 result
->setBreakType(kind
);
128 if (U_FAILURE(status
) && result
!= NULL
) { // Sometimes redundant check, but simple
133 if (result
== NULL
) {
135 if (U_SUCCESS(status
)) {
136 status
= U_MEMORY_ALLOCATION_ERROR
;
143 // Creates a break iterator for word breaks.
144 BreakIterator
* U_EXPORT2
145 BreakIterator::createWordInstance(const Locale
& key
, UErrorCode
& status
)
147 return createInstance(key
, UBRK_WORD
, status
);
150 // -------------------------------------
152 // Creates a break iterator for line breaks.
153 BreakIterator
* U_EXPORT2
154 BreakIterator::createLineInstance(const Locale
& key
, UErrorCode
& status
)
156 return createInstance(key
, UBRK_LINE
, status
);
159 // -------------------------------------
161 // Creates a break iterator for character breaks.
162 BreakIterator
* U_EXPORT2
163 BreakIterator::createCharacterInstance(const Locale
& key
, UErrorCode
& status
)
165 return createInstance(key
, UBRK_CHARACTER
, status
);
168 // -------------------------------------
170 // Creates a break iterator for sentence breaks.
171 BreakIterator
* U_EXPORT2
172 BreakIterator::createSentenceInstance(const Locale
& key
, UErrorCode
& status
)
174 return createInstance(key
, UBRK_SENTENCE
, status
);
177 // -------------------------------------
179 // Creates a break iterator for title casing breaks.
180 BreakIterator
* U_EXPORT2
181 BreakIterator::createTitleInstance(const Locale
& key
, UErrorCode
& status
)
183 return createInstance(key
, UBRK_TITLE
, status
);
186 // -------------------------------------
188 // Gets all the available locales that has localized text boundary data.
189 const Locale
* U_EXPORT2
190 BreakIterator::getAvailableLocales(int32_t& count
)
192 return Locale::getAvailableLocales(count
);
195 // ------------------------------------------
197 // Default constructor and destructor
199 //-------------------------------------------
201 BreakIterator::BreakIterator()
204 *validLocale
= *actualLocale
= 0;
207 BreakIterator::~BreakIterator()
211 // ------------------------------------------
215 //-------------------------------------------
216 #if !UCONFIG_NO_SERVICE
218 // -------------------------------------
220 class ICUBreakIteratorFactory
: public ICUResourceBundleFactory
{
222 virtual ~ICUBreakIteratorFactory();
224 virtual UObject
* handleCreate(const Locale
& loc
, int32_t kind
, const ICUService
* /*service*/, UErrorCode
& status
) const {
225 return BreakIterator::makeInstance(loc
, kind
, status
);
229 ICUBreakIteratorFactory::~ICUBreakIteratorFactory() {}
231 // -------------------------------------
233 class ICUBreakIteratorService
: public ICULocaleService
{
235 ICUBreakIteratorService()
236 : ICULocaleService(UNICODE_STRING("Break Iterator", 14))
238 UErrorCode status
= U_ZERO_ERROR
;
239 registerFactory(new ICUBreakIteratorFactory(), status
);
242 virtual ~ICUBreakIteratorService();
244 virtual UObject
* cloneInstance(UObject
* instance
) const {
245 return ((BreakIterator
*)instance
)->clone();
248 virtual UObject
* handleDefault(const ICUServiceKey
& key
, UnicodeString
* /*actualID*/, UErrorCode
& status
) const {
249 LocaleKey
& lkey
= (LocaleKey
&)key
;
250 int32_t kind
= lkey
.kind();
252 lkey
.currentLocale(loc
);
253 return BreakIterator::makeInstance(loc
, kind
, status
);
256 virtual UBool
isDefault() const {
257 return countFactories() == 1;
261 ICUBreakIteratorService::~ICUBreakIteratorService() {}
263 // -------------------------------------
265 // defined in ucln_cmn.h
268 static icu::UInitOnce gInitOnce
;
269 static icu::ICULocaleService
* gService
= NULL
;
274 * Release all static memory held by breakiterator.
277 static UBool U_CALLCONV
breakiterator_cleanup(void) {
278 #if !UCONFIG_NO_SERVICE
290 static void U_CALLCONV
292 gService
= new ICUBreakIteratorService();
293 ucln_common_registerCleanup(UCLN_COMMON_BREAKITERATOR
, breakiterator_cleanup
);
296 static ICULocaleService
*
299 umtx_initOnce(gInitOnce
, &initService
);
304 // -------------------------------------
309 return !gInitOnce
.isReset() && getService() != NULL
;
312 // -------------------------------------
314 URegistryKey U_EXPORT2
315 BreakIterator::registerInstance(BreakIterator
* toAdopt
, const Locale
& locale
, UBreakIteratorType kind
, UErrorCode
& status
)
317 ICULocaleService
*service
= getService();
318 if (service
== NULL
) {
319 status
= U_MEMORY_ALLOCATION_ERROR
;
322 return service
->registerInstance(toAdopt
, locale
, kind
, status
);
325 // -------------------------------------
328 BreakIterator::unregister(URegistryKey key
, UErrorCode
& status
)
330 if (U_SUCCESS(status
)) {
332 return gService
->unregister(key
, status
);
334 status
= U_MEMORY_ALLOCATION_ERROR
;
339 // -------------------------------------
341 StringEnumeration
* U_EXPORT2
342 BreakIterator::getAvailableLocales(void)
344 ICULocaleService
*service
= getService();
345 if (service
== NULL
) {
348 return service
->getAvailableLocales();
350 #endif /* UCONFIG_NO_SERVICE */
352 // -------------------------------------
355 BreakIterator::createInstance(const Locale
& loc
, int32_t kind
, UErrorCode
& status
)
357 if (U_FAILURE(status
)) {
361 #if !UCONFIG_NO_SERVICE
363 Locale
actualLoc("");
364 BreakIterator
*result
= (BreakIterator
*)gService
->get(loc
, kind
, &actualLoc
, status
);
365 // TODO: The way the service code works in ICU 2.8 is that if
366 // there is a real registered break iterator, the actualLoc
367 // will be populated, but if the handleDefault path is taken
368 // (because nothing is registered that can handle the
369 // requested locale) then the actualLoc comes back empty. In
370 // that case, the returned object already has its actual/valid
371 // locale data populated (by makeInstance, which is what
372 // handleDefault calls), so we don't touch it. YES, A COMMENT
373 // THIS LONG is a sign of bad code -- so the action item is to
374 // revisit this in ICU 3.0 and clean it up/fix it/remove it.
375 if (U_SUCCESS(status
) && (result
!= NULL
) && *actualLoc
.getName() != 0) {
376 U_LOCALE_BASED(locBased
, *result
);
377 locBased
.setLocaleIDs(actualLoc
.getName(), actualLoc
.getName());
384 return makeInstance(loc
, kind
, status
);
388 // -------------------------------------
389 enum { kKeyValueLenMax
= 32 };
392 BreakIterator::makeInstance(const Locale
& loc
, int32_t kind
, UErrorCode
& status
)
395 if (U_FAILURE(status
)) {
398 char lbType
[kKeyValueLenMax
];
400 BreakIterator
*result
= NULL
;
403 result
= BreakIterator::buildInstance(loc
, "grapheme", kind
, status
);
406 result
= BreakIterator::buildInstance(loc
, "word", kind
, status
);
409 uprv_strcpy(lbType
, "line");
411 char lbKeyValue
[kKeyValueLenMax
] = {0};
412 UErrorCode kvStatus
= U_ZERO_ERROR
;
413 int32_t kLen
= loc
.getKeywordValue("lb", lbKeyValue
, kKeyValueLenMax
, kvStatus
);
414 if (U_SUCCESS(kvStatus
) && kLen
> 0 && (uprv_strcmp(lbKeyValue
,"strict")==0 || uprv_strcmp(lbKeyValue
,"normal")==0 || uprv_strcmp(lbKeyValue
,"loose")==0)) {
415 uprv_strcat(lbType
, "_");
416 uprv_strcat(lbType
, lbKeyValue
);
419 result
= BreakIterator::buildInstance(loc
, lbType
, kind
, status
);
420 if (U_SUCCESS(status
) && result
!= NULL
) {
421 char lwKeyValue
[kKeyValueLenMax
] = {0};
422 UErrorCode kvStatus
= U_ZERO_ERROR
;
423 int32_t kLen
= loc
.getKeywordValue("lw", lwKeyValue
, kKeyValueLenMax
, kvStatus
);
424 result
->setKeepAll(U_SUCCESS(kvStatus
) && kLen
> 0 && uprv_strcmp(lwKeyValue
,"keepall")==0);
428 result
= BreakIterator::buildInstance(loc
, "sentence", kind
, status
);
430 char ssKeyValue
[kKeyValueLenMax
] = {0};
431 UErrorCode kvStatus
= U_ZERO_ERROR
;
432 int32_t kLen
= loc
.getKeywordValue("ss", ssKeyValue
, kKeyValueLenMax
, kvStatus
);
433 if (U_SUCCESS(kvStatus
) && kLen
> 0 && uprv_strcmp(ssKeyValue
,"standard")==0) {
434 FilteredBreakIteratorBuilder
* fbiBuilder
= FilteredBreakIteratorBuilder::createInstance(loc
, kvStatus
);
435 if (U_SUCCESS(kvStatus
)) {
436 result
= fbiBuilder
->build(result
, status
);
443 result
= BreakIterator::buildInstance(loc
, "title", kind
, status
);
446 status
= U_ILLEGAL_ARGUMENT_ERROR
;
449 if (U_FAILURE(status
)) {
457 BreakIterator::getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const {
458 U_LOCALE_BASED(locBased
, *this);
459 return locBased
.getLocale(type
, status
);
463 BreakIterator::getLocaleID(ULocDataLocaleType type
, UErrorCode
& status
) const {
464 U_LOCALE_BASED(locBased
, *this);
465 return locBased
.getLocaleID(type
, status
);
469 // This implementation of getRuleStatus is a do-nothing stub, here to
470 // provide a default implementation for any derived BreakIterator classes that
471 // do not implement it themselves.
472 int32_t BreakIterator::getRuleStatus() const {
476 // This implementation of getRuleStatusVec is a do-nothing stub, here to
477 // provide a default implementation for any derived BreakIterator classes that
478 // do not implement it themselves.
479 int32_t BreakIterator::getRuleStatusVec(int32_t *fillInVec
, int32_t capacity
, UErrorCode
&status
) {
480 if (U_FAILURE(status
)) {
484 status
= U_BUFFER_OVERFLOW_ERROR
;
491 BreakIterator::BreakIterator (const Locale
& valid
, const Locale
& actual
) {
492 U_LOCALE_BASED(locBased
, (*this));
493 locBased
.setLocaleIDs(valid
, actual
);
498 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */