Coverage Report

Created: 2025-06-24 06:43

/src/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 "utracimp.h"
42
#include "charstr.h"
43
44
// *****************************************************************************
45
// class BreakIterator
46
// This class implements methods for finding the location of boundaries in text.
47
// Instances of BreakIterator maintain a current position and scan over text
48
// returning the index of characters where boundaries occur.
49
// *****************************************************************************
50
51
U_NAMESPACE_BEGIN
52
53
// -------------------------------------
54
55
BreakIterator*
56
BreakIterator::buildInstance(const Locale& loc, const char *type, UErrorCode &status)
57
0
{
58
0
    char fnbuff[256];
59
0
    char ext[4]={'\0'};
60
0
    CharString actualLocale;
61
0
    int32_t size;
62
0
    const UChar* brkfname = NULL;
63
0
    UResourceBundle brkRulesStack;
64
0
    UResourceBundle brkNameStack;
65
0
    UResourceBundle *brkRules = &brkRulesStack;
66
0
    UResourceBundle *brkName  = &brkNameStack;
67
0
    RuleBasedBreakIterator *result = NULL;
68
69
0
    if (U_FAILURE(status))
70
0
        return NULL;
71
72
0
    ures_initStackObject(brkRules);
73
0
    ures_initStackObject(brkName);
74
75
    // Get the locale
76
0
    UResourceBundle *b = ures_openNoDefault(U_ICUDATA_BRKITR, loc.getName(), &status);
77
78
    // Get the "boundaries" array.
79
0
    if (U_SUCCESS(status)) {
80
0
        brkRules = ures_getByKeyWithFallback(b, "boundaries", brkRules, &status);
81
        // Get the string object naming the rules file
82
0
        brkName = ures_getByKeyWithFallback(brkRules, type, brkName, &status);
83
        // Get the actual string
84
0
        brkfname = ures_getString(brkName, &size, &status);
85
0
        U_ASSERT((size_t)size<sizeof(fnbuff));
86
0
        if ((size_t)size>=sizeof(fnbuff)) {
87
0
            size=0;
88
0
            if (U_SUCCESS(status)) {
89
0
                status = U_BUFFER_OVERFLOW_ERROR;
90
0
            }
91
0
        }
92
93
        // Use the string if we found it
94
0
        if (U_SUCCESS(status) && brkfname) {
95
0
            actualLocale.append(ures_getLocaleInternal(brkName, &status), -1, status);
96
97
0
            UChar* extStart=u_strchr(brkfname, 0x002e);
98
0
            int len = 0;
99
0
            if(extStart!=NULL){
100
0
                len = (int)(extStart-brkfname);
101
0
                u_UCharsToChars(extStart+1, ext, sizeof(ext)); // nul terminates the buff
102
0
                u_UCharsToChars(brkfname, fnbuff, len);
103
0
            }
104
0
            fnbuff[len]=0; // nul terminate
105
0
        }
106
0
    }
107
108
0
    ures_close(brkRules);
109
0
    ures_close(brkName);
110
111
0
    UDataMemory* file = udata_open(U_ICUDATA_BRKITR, ext, fnbuff, &status);
112
0
    if (U_FAILURE(status)) {
113
0
        ures_close(b);
114
0
        return NULL;
115
0
    }
116
117
    // Create a RuleBasedBreakIterator
118
0
    result = new RuleBasedBreakIterator(file, status);
119
120
    // If there is a result, set the valid locale and actual locale, and the kind
121
0
    if (U_SUCCESS(status) && result != NULL) {
122
0
        U_LOCALE_BASED(locBased, *(BreakIterator*)result);
123
0
        locBased.setLocaleIDs(ures_getLocaleByType(b, ULOC_VALID_LOCALE, &status), 
124
0
                              actualLocale.data());
125
0
    }
126
127
0
    ures_close(b);
128
129
0
    if (U_FAILURE(status) && result != NULL) {  // Sometimes redundant check, but simple
130
0
        delete result;
131
0
        return NULL;
132
0
    }
133
134
0
    if (result == NULL) {
135
0
        udata_close(file);
136
0
        if (U_SUCCESS(status)) {
137
0
            status = U_MEMORY_ALLOCATION_ERROR;
138
0
        }
139
0
    }
140
141
0
    return result;
142
0
}
143
144
// Creates a break iterator for word breaks.
145
BreakIterator* U_EXPORT2
146
BreakIterator::createWordInstance(const Locale& key, UErrorCode& status)
147
0
{
148
0
    return createInstance(key, UBRK_WORD, status);
149
0
}
150
151
// -------------------------------------
152
153
// Creates a break iterator  for line breaks.
154
BreakIterator* U_EXPORT2
155
BreakIterator::createLineInstance(const Locale& key, UErrorCode& status)
156
0
{
157
0
    return createInstance(key, UBRK_LINE, status);
158
0
}
159
160
// -------------------------------------
161
162
// Creates a break iterator  for character breaks.
163
BreakIterator* U_EXPORT2
164
BreakIterator::createCharacterInstance(const Locale& key, UErrorCode& status)
165
0
{
166
0
    return createInstance(key, UBRK_CHARACTER, status);
167
0
}
168
169
// -------------------------------------
170
171
// Creates a break iterator  for sentence breaks.
172
BreakIterator* U_EXPORT2
173
BreakIterator::createSentenceInstance(const Locale& key, UErrorCode& status)
174
0
{
175
0
    return createInstance(key, UBRK_SENTENCE, status);
176
0
}
177
178
// -------------------------------------
179
180
// Creates a break iterator for title casing breaks.
181
BreakIterator* U_EXPORT2
182
BreakIterator::createTitleInstance(const Locale& key, UErrorCode& status)
183
0
{
184
0
    return createInstance(key, UBRK_TITLE, status);
185
0
}
186
187
// -------------------------------------
188
189
// Gets all the available locales that has localized text boundary data.
190
const Locale* U_EXPORT2
191
BreakIterator::getAvailableLocales(int32_t& count)
192
0
{
193
0
    return Locale::getAvailableLocales(count);
194
0
}
195
196
// ------------------------------------------
197
//
198
// Constructors, destructor and assignment operator
199
//
200
//-------------------------------------------
201
202
BreakIterator::BreakIterator()
203
0
{
204
0
    *validLocale = *actualLocale = 0;
205
0
}
206
207
0
BreakIterator::BreakIterator(const BreakIterator &other) : UObject(other) {
208
0
    uprv_strncpy(actualLocale, other.actualLocale, sizeof(actualLocale));
209
0
    uprv_strncpy(validLocale, other.validLocale, sizeof(validLocale));
210
0
}
211
212
0
BreakIterator &BreakIterator::operator =(const BreakIterator &other) {
213
0
    if (this != &other) {
214
0
        uprv_strncpy(actualLocale, other.actualLocale, sizeof(actualLocale));
215
0
        uprv_strncpy(validLocale, other.validLocale, sizeof(validLocale));
216
0
    }
217
0
    return *this;
218
0
}
219
220
BreakIterator::~BreakIterator()
221
0
{
222
0
}
223
224
// ------------------------------------------
225
//
226
// Registration
227
//
228
//-------------------------------------------
229
#if !UCONFIG_NO_SERVICE
230
231
// -------------------------------------
232
233
class ICUBreakIteratorFactory : public ICUResourceBundleFactory {
234
public:
235
    virtual ~ICUBreakIteratorFactory();
236
protected:
237
0
    virtual UObject* handleCreate(const Locale& loc, int32_t kind, const ICUService* /*service*/, UErrorCode& status) const {
238
0
        return BreakIterator::makeInstance(loc, kind, status);
239
0
    }
240
};
241
242
0
ICUBreakIteratorFactory::~ICUBreakIteratorFactory() {}
243
244
// -------------------------------------
245
246
class ICUBreakIteratorService : public ICULocaleService {
247
public:
248
    ICUBreakIteratorService()
249
0
        : ICULocaleService(UNICODE_STRING("Break Iterator", 14))
250
0
    {
251
0
        UErrorCode status = U_ZERO_ERROR;
252
0
        registerFactory(new ICUBreakIteratorFactory(), status);
253
0
    }
254
255
    virtual ~ICUBreakIteratorService();
256
257
0
    virtual UObject* cloneInstance(UObject* instance) const {
258
0
        return ((BreakIterator*)instance)->clone();
259
0
    }
260
261
0
    virtual UObject* handleDefault(const ICUServiceKey& key, UnicodeString* /*actualID*/, UErrorCode& status) const {
262
0
        LocaleKey& lkey = (LocaleKey&)key;
263
0
        int32_t kind = lkey.kind();
264
0
        Locale loc;
265
0
        lkey.currentLocale(loc);
266
0
        return BreakIterator::makeInstance(loc, kind, status);
267
0
    }
268
269
0
    virtual UBool isDefault() const {
270
0
        return countFactories() == 1;
271
0
    }
272
};
273
274
0
ICUBreakIteratorService::~ICUBreakIteratorService() {}
275
276
// -------------------------------------
277
278
// defined in ucln_cmn.h
279
U_NAMESPACE_END
280
281
static icu::UInitOnce gInitOnceBrkiter = U_INITONCE_INITIALIZER;
282
static icu::ICULocaleService* gService = NULL;
283
284
285
286
/**
287
 * Release all static memory held by breakiterator.
288
 */
289
U_CDECL_BEGIN
290
0
static UBool U_CALLCONV breakiterator_cleanup(void) {
291
0
#if !UCONFIG_NO_SERVICE
292
0
    if (gService) {
293
0
        delete gService;
294
0
        gService = NULL;
295
0
    }
296
0
    gInitOnceBrkiter.reset();
297
0
#endif
298
0
    return TRUE;
299
0
}
300
U_CDECL_END
301
U_NAMESPACE_BEGIN
302
303
static void U_CALLCONV 
304
0
initService(void) {
305
0
    gService = new ICUBreakIteratorService();
306
0
    ucln_common_registerCleanup(UCLN_COMMON_BREAKITERATOR, breakiterator_cleanup);
307
0
}
308
309
static ICULocaleService*
310
getService(void)
311
0
{
312
0
    umtx_initOnce(gInitOnceBrkiter, &initService);
313
0
    return gService;
314
0
}
315
316
317
// -------------------------------------
318
319
static inline UBool
320
hasService(void)
321
0
{
322
0
    return !gInitOnceBrkiter.isReset() && getService() != NULL;
323
0
}
324
325
// -------------------------------------
326
327
URegistryKey U_EXPORT2
328
BreakIterator::registerInstance(BreakIterator* toAdopt, const Locale& locale, UBreakIteratorType kind, UErrorCode& status)
329
0
{
330
0
    ICULocaleService *service = getService();
331
0
    if (service == NULL) {
332
0
        status = U_MEMORY_ALLOCATION_ERROR;
333
0
        return NULL;
334
0
    }
335
0
    return service->registerInstance(toAdopt, locale, kind, status);
336
0
}
337
338
// -------------------------------------
339
340
UBool U_EXPORT2
341
BreakIterator::unregister(URegistryKey key, UErrorCode& status)
342
0
{
343
0
    if (U_SUCCESS(status)) {
344
0
        if (hasService()) {
345
0
            return gService->unregister(key, status);
346
0
        }
347
0
        status = U_MEMORY_ALLOCATION_ERROR;
348
0
    }
349
0
    return FALSE;
350
0
}
351
352
// -------------------------------------
353
354
StringEnumeration* U_EXPORT2
355
BreakIterator::getAvailableLocales(void)
356
0
{
357
0
    ICULocaleService *service = getService();
358
0
    if (service == NULL) {
359
0
        return NULL;
360
0
    }
361
0
    return service->getAvailableLocales();
362
0
}
363
#endif /* UCONFIG_NO_SERVICE */
364
365
// -------------------------------------
366
367
BreakIterator*
368
BreakIterator::createInstance(const Locale& loc, int32_t kind, UErrorCode& status)
369
0
{
370
0
    if (U_FAILURE(status)) {
371
0
        return NULL;
372
0
    }
373
374
0
#if !UCONFIG_NO_SERVICE
375
0
    if (hasService()) {
376
0
        Locale actualLoc("");
377
0
        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
0
        if (U_SUCCESS(status) && (result != NULL) && *actualLoc.getName() != 0) {
389
0
            U_LOCALE_BASED(locBased, *result);
390
0
            locBased.setLocaleIDs(actualLoc.getName(), actualLoc.getName());
391
0
        }
392
0
        return result;
393
0
    }
394
0
    else
395
0
#endif
396
0
    {
397
0
        return makeInstance(loc, kind, status);
398
0
    }
399
0
}
400
401
// -------------------------------------
402
enum { kKeyValueLenMax = 32 };
403
404
BreakIterator*
405
BreakIterator::makeInstance(const Locale& loc, int32_t kind, UErrorCode& status)
406
0
{
407
408
0
    if (U_FAILURE(status)) {
409
0
        return NULL;
410
0
    }
411
0
    char lbType[kKeyValueLenMax];
412
413
0
    BreakIterator *result = NULL;
414
0
    switch (kind) {
415
0
    case UBRK_CHARACTER:
416
0
        {
417
0
            UTRACE_ENTRY(UTRACE_UBRK_CREATE_CHARACTER);
418
0
            result = BreakIterator::buildInstance(loc, "grapheme", status);
419
0
            UTRACE_EXIT_STATUS(status);
420
0
        }
421
0
        break;
422
0
    case UBRK_WORD:
423
0
        {
424
0
            UTRACE_ENTRY(UTRACE_UBRK_CREATE_WORD);
425
0
            result = BreakIterator::buildInstance(loc, "word", status);
426
0
            UTRACE_EXIT_STATUS(status);
427
0
        }
428
0
        break;
429
0
    case UBRK_LINE:
430
0
        {
431
0
            UTRACE_ENTRY(UTRACE_UBRK_CREATE_LINE);
432
0
            uprv_strcpy(lbType, "line");
433
0
            char lbKeyValue[kKeyValueLenMax] = {0};
434
0
            UErrorCode kvStatus = U_ZERO_ERROR;
435
0
            int32_t kLen = loc.getKeywordValue("lb", lbKeyValue, kKeyValueLenMax, kvStatus);
436
0
            if (U_SUCCESS(kvStatus) && kLen > 0 && (uprv_strcmp(lbKeyValue,"strict")==0 || uprv_strcmp(lbKeyValue,"normal")==0 || uprv_strcmp(lbKeyValue,"loose")==0)) {
437
0
                uprv_strcat(lbType, "_");
438
0
                uprv_strcat(lbType, lbKeyValue);
439
0
            }
440
0
            result = BreakIterator::buildInstance(loc, lbType, status);
441
442
0
            UTRACE_DATA1(UTRACE_INFO, "lb=%s", lbKeyValue);
443
0
            UTRACE_EXIT_STATUS(status);
444
0
        }
445
0
        break;
446
0
    case UBRK_SENTENCE:
447
0
        {
448
0
            UTRACE_ENTRY(UTRACE_UBRK_CREATE_SENTENCE);
449
0
            result = BreakIterator::buildInstance(loc, "sentence", status);
450
0
#if !UCONFIG_NO_FILTERED_BREAK_ITERATION
451
0
            char ssKeyValue[kKeyValueLenMax] = {0};
452
0
            UErrorCode kvStatus = U_ZERO_ERROR;
453
0
            int32_t kLen = loc.getKeywordValue("ss", ssKeyValue, kKeyValueLenMax, kvStatus);
454
0
            if (U_SUCCESS(kvStatus) && kLen > 0 && uprv_strcmp(ssKeyValue,"standard")==0) {
455
0
                FilteredBreakIteratorBuilder* fbiBuilder = FilteredBreakIteratorBuilder::createInstance(loc, kvStatus);
456
0
                if (U_SUCCESS(kvStatus)) {
457
0
                    result = fbiBuilder->build(result, status);
458
0
                    delete fbiBuilder;
459
0
                }
460
0
            }
461
0
#endif
462
0
            UTRACE_EXIT_STATUS(status);
463
0
        }
464
0
        break;
465
0
    case UBRK_TITLE:
466
0
        {
467
0
            UTRACE_ENTRY(UTRACE_UBRK_CREATE_TITLE);
468
0
            result = BreakIterator::buildInstance(loc, "title", status);
469
0
            UTRACE_EXIT_STATUS(status);
470
0
        }
471
0
        break;
472
0
    default:
473
0
        status = U_ILLEGAL_ARGUMENT_ERROR;
474
0
    }
475
476
0
    if (U_FAILURE(status)) {
477
0
        return NULL;
478
0
    }
479
480
0
    return result;
481
0
}
482
483
Locale
484
0
BreakIterator::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
485
0
    U_LOCALE_BASED(locBased, *this);
486
0
    return locBased.getLocale(type, status);
487
0
}
488
489
const char *
490
0
BreakIterator::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
491
0
    U_LOCALE_BASED(locBased, *this);
492
0
    return locBased.getLocaleID(type, status);
493
0
}
494
495
496
// This implementation of getRuleStatus is a do-nothing stub, here to
497
// provide a default implementation for any derived BreakIterator classes that
498
// do not implement it themselves.
499
0
int32_t BreakIterator::getRuleStatus() const {
500
0
    return 0;
501
0
}
502
503
// This implementation of getRuleStatusVec is a do-nothing stub, here to
504
// provide a default implementation for any derived BreakIterator classes that
505
// do not implement it themselves.
506
0
int32_t BreakIterator::getRuleStatusVec(int32_t *fillInVec, int32_t capacity, UErrorCode &status) {
507
0
    if (U_FAILURE(status)) {
508
0
        return 0;
509
0
    }
510
0
    if (capacity < 1) {
511
0
        status = U_BUFFER_OVERFLOW_ERROR;
512
0
        return 1;
513
0
    }
514
0
    *fillInVec = 0;
515
0
    return 1;
516
0
}
517
518
0
BreakIterator::BreakIterator (const Locale& valid, const Locale& actual) {
519
0
  U_LOCALE_BASED(locBased, (*this));
520
0
  locBased.setLocaleIDs(valid, actual);
521
0
}
522
523
U_NAMESPACE_END
524
525
#endif /* #if !UCONFIG_NO_BREAK_ITERATION */
526
527
//eof