1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 1997-2015, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
11 * Modification History:
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 *****************************************************************************************
19 // *****************************************************************************
20 // This file was generated from the java source file BreakIterator.java
21 // *****************************************************************************
23 #include "unicode/utypes.h"
25 #if !UCONFIG_NO_BREAK_ITERATION
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"
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 // *****************************************************************************
52 // -------------------------------------
55 BreakIterator::buildInstance(const Locale
& loc
, const char *type
, UErrorCode
&status
)
59 CharString actualLocale
;
61 const UChar
* brkfname
= NULL
;
62 UResourceBundle brkRulesStack
;
63 UResourceBundle brkNameStack
;
64 UResourceBundle
*brkRules
= &brkRulesStack
;
65 UResourceBundle
*brkName
= &brkNameStack
;
66 RuleBasedBreakIterator
*result
= NULL
;
68 if (U_FAILURE(status
))
71 ures_initStackObject(brkRules
);
72 ures_initStackObject(brkName
);
75 UResourceBundle
*b
= ures_openNoDefault(U_ICUDATA_BRKITR
, loc
.getName(), &status
);
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
);
84 U_ASSERT((size_t)size
<sizeof(fnbuff
));
85 if ((size_t)size
>=sizeof(fnbuff
)) {
87 if (U_SUCCESS(status
)) {
88 status
= U_BUFFER_OVERFLOW_ERROR
;
92 // Use the string if we found it
93 if (U_SUCCESS(status
) && brkfname
) {
94 actualLocale
.append(ures_getLocaleInternal(brkName
, &status
), -1, status
);
96 UChar
* extStart
=u_strchr(brkfname
, 0x002e);
99 len
= (int)(extStart
-brkfname
);
100 u_UCharsToChars(extStart
+1, ext
, sizeof(ext
)); // nul terminates the buff
101 u_UCharsToChars(brkfname
, fnbuff
, len
);
103 fnbuff
[len
]=0; // nul terminate
107 ures_close(brkRules
);
110 UDataMemory
* file
= udata_open(U_ICUDATA_BRKITR
, ext
, fnbuff
, &status
);
111 if (U_FAILURE(status
)) {
116 // Create a RuleBasedBreakIterator
117 result
= new RuleBasedBreakIterator(file
, status
);
119 // If there is a result, set the valid locale and actual locale, and the kind
120 if (U_SUCCESS(status
) && result
!= NULL
) {
121 U_LOCALE_BASED(locBased
, *(BreakIterator
*)result
);
122 locBased
.setLocaleIDs(ures_getLocaleByType(b
, ULOC_VALID_LOCALE
, &status
),
123 actualLocale
.data());
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 // Constructors, destructor and assignment operator
199 //-------------------------------------------
201 BreakIterator::BreakIterator()
202 : fLineWordOpts(UBRK_LINEWORD_NORMAL
)
204 *validLocale
= *actualLocale
= 0;
207 BreakIterator::BreakIterator(const BreakIterator
&other
) : UObject(other
) {
208 uprv_strncpy(actualLocale
, other
.actualLocale
, sizeof(actualLocale
));
209 uprv_strncpy(validLocale
, other
.validLocale
, sizeof(validLocale
));
212 BreakIterator
&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
));
220 BreakIterator::~BreakIterator()
224 // ------------------------------------------
228 //-------------------------------------------
229 #if !UCONFIG_NO_SERVICE
231 // -------------------------------------
233 class ICUBreakIteratorFactory
: public ICUResourceBundleFactory
{
235 virtual ~ICUBreakIteratorFactory();
237 virtual UObject
* handleCreate(const Locale
& loc
, int32_t kind
, const ICUService
* /*service*/, UErrorCode
& status
) const {
238 return BreakIterator::makeInstance(loc
, kind
, status
);
242 ICUBreakIteratorFactory::~ICUBreakIteratorFactory() {}
244 // -------------------------------------
246 class ICUBreakIteratorService
: public ICULocaleService
{
248 ICUBreakIteratorService()
249 : ICULocaleService(UNICODE_STRING("Break Iterator", 14))
251 UErrorCode status
= U_ZERO_ERROR
;
252 registerFactory(new ICUBreakIteratorFactory(), status
);
255 virtual ~ICUBreakIteratorService();
257 virtual UObject
* cloneInstance(UObject
* instance
) const {
258 return ((BreakIterator
*)instance
)->clone();
261 virtual UObject
* handleDefault(const ICUServiceKey
& key
, UnicodeString
* /*actualID*/, UErrorCode
& status
) const {
262 LocaleKey
& lkey
= (LocaleKey
&)key
;
263 int32_t kind
= lkey
.kind();
265 lkey
.currentLocale(loc
);
266 return BreakIterator::makeInstance(loc
, kind
, status
);
269 virtual UBool
isDefault() const {
270 return countFactories() == 1;
274 ICUBreakIteratorService::~ICUBreakIteratorService() {}
276 // -------------------------------------
278 // defined in ucln_cmn.h
281 static icu::UInitOnce gInitOnceBrkiter
= U_INITONCE_INITIALIZER
;
282 static icu::ICULocaleService
* gService
= NULL
;
287 * Release all static memory held by breakiterator.
290 static UBool U_CALLCONV
breakiterator_cleanup(void) {
291 #if !UCONFIG_NO_SERVICE
296 gInitOnceBrkiter
.reset();
303 static void U_CALLCONV
305 gService
= new ICUBreakIteratorService();
306 ucln_common_registerCleanup(UCLN_COMMON_BREAKITERATOR
, breakiterator_cleanup
);
309 static ICULocaleService
*
312 umtx_initOnce(gInitOnceBrkiter
, &initService
);
317 // -------------------------------------
322 return !gInitOnceBrkiter
.isReset() && getService() != NULL
;
325 // -------------------------------------
327 URegistryKey U_EXPORT2
328 BreakIterator::registerInstance(BreakIterator
* toAdopt
, const Locale
& locale
, UBreakIteratorType kind
, UErrorCode
& status
)
330 ICULocaleService
*service
= getService();
331 if (service
== NULL
) {
332 status
= U_MEMORY_ALLOCATION_ERROR
;
335 return service
->registerInstance(toAdopt
, locale
, kind
, status
);
338 // -------------------------------------
341 BreakIterator::unregister(URegistryKey key
, UErrorCode
& status
)
343 if (U_SUCCESS(status
)) {
345 return gService
->unregister(key
, status
);
347 status
= U_MEMORY_ALLOCATION_ERROR
;
352 // -------------------------------------
354 StringEnumeration
* U_EXPORT2
355 BreakIterator::getAvailableLocales(void)
357 ICULocaleService
*service
= getService();
358 if (service
== NULL
) {
361 return service
->getAvailableLocales();
363 #endif /* UCONFIG_NO_SERVICE */
365 // -------------------------------------
368 BreakIterator::createInstance(const Locale
& loc
, int32_t kind
, UErrorCode
& status
)
370 if (U_FAILURE(status
)) {
374 #if !UCONFIG_NO_SERVICE
376 Locale
actualLoc("");
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());
397 return makeInstance(loc
, kind
, status
);
401 // -------------------------------------
402 enum { kKeyValueLenMax
= 32 };
405 BreakIterator::makeInstance(const Locale
& loc
, int32_t kind
, UErrorCode
& status
)
408 if (U_FAILURE(status
)) {
411 char lbType
[kKeyValueLenMax
];
413 BreakIterator
*result
= NULL
;
416 result
= BreakIterator::buildInstance(loc
, "grapheme", status
);
419 result
= BreakIterator::buildInstance(loc
, "word", status
);
422 uprv_strcpy(lbType
, "line");
424 char lbKeyValue
[kKeyValueLenMax
] = {0};
425 UErrorCode kvStatus
= U_ZERO_ERROR
;
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)) {
428 uprv_strcat(lbType
, "_");
429 uprv_strcat(lbType
, lbKeyValue
);
432 result
= BreakIterator::buildInstance(loc
, lbType
, status
);
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
);
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
;
445 result
->setLineWordOpts(lineWordOpts
);
449 result
= BreakIterator::buildInstance(loc
, "sentence", status
);
450 #if !UCONFIG_NO_FILTERED_BREAK_ITERATION
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
);
466 result
= BreakIterator::buildInstance(loc
, "title", status
);
469 status
= U_ILLEGAL_ARGUMENT_ERROR
;
472 if (U_FAILURE(status
)) {
480 BreakIterator::getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const {
481 U_LOCALE_BASED(locBased
, *this);
482 return locBased
.getLocale(type
, status
);
486 BreakIterator::getLocaleID(ULocDataLocaleType type
, UErrorCode
& status
) const {
487 U_LOCALE_BASED(locBased
, *this);
488 return locBased
.getLocaleID(type
, status
);
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.
495 int32_t BreakIterator::getRuleStatus() const {
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.
502 int32_t BreakIterator::getRuleStatusVec(int32_t *fillInVec
, int32_t capacity
, UErrorCode
&status
) {
503 if (U_FAILURE(status
)) {
507 status
= U_BUFFER_OVERFLOW_ERROR
;
514 BreakIterator::BreakIterator (const Locale
& valid
, const Locale
& actual
) {
515 U_LOCALE_BASED(locBased
, (*this));
516 locBased
.setLocaleIDs(valid
, actual
);
521 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */