intl/icu/source/common/brkiter.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial