Coverage Report

Created: 2025-10-24 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/common/brkeng.cpp
Line
Count
Source
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
 ************************************************************************************
5
 * Copyright (C) 2006-2016, International Business Machines Corporation
6
 * and others. All Rights Reserved.
7
 ************************************************************************************
8
 */
9
10
#include "unicode/utypes.h"
11
12
#if !UCONFIG_NO_BREAK_ITERATION
13
14
#include "unicode/uchar.h"
15
#include "unicode/uniset.h"
16
#include "unicode/chariter.h"
17
#include "unicode/ures.h"
18
#include "unicode/udata.h"
19
#include "unicode/putil.h"
20
#include "unicode/ustring.h"
21
#include "unicode/uscript.h"
22
#include "unicode/ucharstrie.h"
23
#include "unicode/bytestrie.h"
24
#include "unicode/rbbi.h"
25
26
#include "brkeng.h"
27
#include "cmemory.h"
28
#include "dictbe.h"
29
#include "lstmbe.h"
30
#include "charstr.h"
31
#include "dictionarydata.h"
32
#include "mutex.h"
33
#include "uvector.h"
34
#include "umutex.h"
35
#include "uresimp.h"
36
#include "ubrkimpl.h"
37
38
U_NAMESPACE_BEGIN
39
40
/*
41
 ******************************************************************
42
 */
43
44
1.76k
LanguageBreakEngine::LanguageBreakEngine() {
45
1.76k
}
46
47
1.75k
LanguageBreakEngine::~LanguageBreakEngine() {
48
1.75k
}
49
50
/*
51
 ******************************************************************
52
 */
53
54
3
LanguageBreakFactory::LanguageBreakFactory() {
55
3
}
56
57
0
LanguageBreakFactory::~LanguageBreakFactory() {
58
0
}
59
60
/*
61
 ******************************************************************
62
 */
63
64
1.75k
UnhandledEngine::UnhandledEngine(UErrorCode &status) : fHandled(nullptr) {
65
1.75k
    (void)status;
66
1.75k
}
67
68
1.75k
UnhandledEngine::~UnhandledEngine() {
69
1.75k
    delete fHandled;
70
1.75k
    fHandled = nullptr;
71
1.75k
}
72
73
UBool
74
921k
UnhandledEngine::handles(UChar32 c, const char* locale) const {
75
921k
    (void)locale; // Unused
76
921k
    return fHandled && fHandled->contains(c);
77
921k
}
78
79
int32_t
80
UnhandledEngine::findBreaks( UText *text,
81
                             int32_t startPos,
82
                             int32_t endPos,
83
                             UVector32 &/*foundBreaks*/,
84
                             UBool /* isPhraseBreaking */,
85
921k
                             UErrorCode &status) const {
86
921k
    if (U_FAILURE(status)) return 0;
87
921k
    utext_setNativeIndex(text, startPos);
88
921k
    UChar32 c = utext_current32(text);
89
4.29M
    while (static_cast<int32_t>(utext_getNativeIndex(text)) < endPos && fHandled->contains(c)) {
90
3.36M
        utext_next32(text);            // TODO:  recast loop to work with post-increment operations.
91
3.36M
        c = utext_current32(text);
92
3.36M
    }
93
921k
    return 0;
94
921k
}
95
96
void
97
178k
UnhandledEngine::handleCharacter(UChar32 c) {
98
178k
    if (fHandled == nullptr) {
99
1.75k
        fHandled = new UnicodeSet();
100
1.75k
        if (fHandled == nullptr) {
101
0
            return;
102
0
        }
103
1.75k
    }
104
178k
    if (!fHandled->contains(c)) {
105
178k
        UErrorCode status = U_ZERO_ERROR;
106
        // Apply the entire script of the character.
107
178k
        int32_t script = u_getIntPropertyValue(c, UCHAR_SCRIPT);
108
178k
        fHandled->applyIntPropertyValue(UCHAR_SCRIPT, script, status);
109
178k
    }
110
178k
}
111
112
/*
113
 ******************************************************************
114
 */
115
116
3
ICULanguageBreakFactory::ICULanguageBreakFactory(UErrorCode &/*status*/) {
117
3
    fEngines = nullptr;
118
3
}
119
120
0
ICULanguageBreakFactory::~ICULanguageBreakFactory() {
121
0
    delete fEngines;
122
0
}
123
124
190k
void ICULanguageBreakFactory::ensureEngines(UErrorCode& status) {
125
190k
    static UMutex gBreakEngineMutex;
126
190k
    Mutex m(&gBreakEngineMutex);
127
190k
    if (fEngines == nullptr) {
128
3
        LocalPointer<UStack>  engines(new UStack(uprv_deleteUObject, nullptr, status), status);
129
3
        if (U_SUCCESS(status)) {
130
3
            fEngines = engines.orphan();
131
3
        }
132
3
    }
133
190k
}
134
135
const LanguageBreakEngine *
136
190k
ICULanguageBreakFactory::getEngineFor(UChar32 c, const char* locale) {
137
190k
    const LanguageBreakEngine *lbe = nullptr;
138
190k
    UErrorCode  status = U_ZERO_ERROR;
139
190k
    ensureEngines(status);
140
190k
    if (U_FAILURE(status) ) {
141
        // Note: no way to return error code to caller.
142
0
        return nullptr;
143
0
    }
144
145
190k
    static UMutex gBreakEngineMutex;
146
190k
    Mutex m(&gBreakEngineMutex);
147
190k
    int32_t i = fEngines->size();
148
1.11M
    while (--i >= 0) {
149
938k
        lbe = static_cast<const LanguageBreakEngine*>(fEngines->elementAt(i));
150
938k
        if (lbe != nullptr && lbe->handles(c, locale)) {
151
11.9k
            return lbe;
152
11.9k
        }
153
938k
    }
154
155
    // We didn't find an engine. Create one.
156
178k
    lbe = loadEngineFor(c, locale);
157
178k
    if (lbe != nullptr) {
158
15
        fEngines->push((void *)lbe, status);
159
15
    }
160
178k
    return U_SUCCESS(status) ? lbe : nullptr;
161
190k
}
162
163
const LanguageBreakEngine *
164
178k
ICULanguageBreakFactory::loadEngineFor(UChar32 c, const char*) {
165
178k
    UErrorCode status = U_ZERO_ERROR;
166
178k
    UScriptCode code = uscript_getScript(c, &status);
167
178k
    if (U_SUCCESS(status)) {
168
178k
        const LanguageBreakEngine *engine = nullptr;
169
        // Try to use LSTM first
170
178k
        const LSTMData *data = CreateLSTMDataForScript(code, status);
171
178k
        if (U_SUCCESS(status)) {
172
178k
            if (data != nullptr) {
173
0
                engine = CreateLSTMBreakEngine(code, data, status);
174
0
                if (U_SUCCESS(status) && engine != nullptr) {
175
0
                    return engine;
176
0
                }
177
0
                if (engine != nullptr) {
178
0
                    delete engine;
179
0
                    engine = nullptr;
180
0
                } else {
181
0
                    DeleteLSTMData(data);
182
0
                }
183
0
            }
184
178k
        }
185
178k
        status = U_ZERO_ERROR;  // fallback to dictionary based
186
178k
        DictionaryMatcher *m = loadDictionaryMatcherFor(code);
187
178k
        if (m != nullptr) {
188
15
            switch(code) {
189
3
            case USCRIPT_THAI:
190
3
                engine = new ThaiBreakEngine(m, status);
191
3
                break;
192
3
            case USCRIPT_LAO:
193
3
                engine = new LaoBreakEngine(m, status);
194
3
                break;
195
3
            case USCRIPT_MYANMAR:
196
3
                engine = new BurmeseBreakEngine(m, status);
197
3
                break;
198
3
            case USCRIPT_KHMER:
199
3
                engine = new KhmerBreakEngine(m, status);
200
3
                break;
201
202
0
#if !UCONFIG_NO_NORMALIZATION
203
                // CJK not available w/o normalization
204
0
            case USCRIPT_HANGUL:
205
0
                engine = new CjkBreakEngine(m, kKorean, status);
206
0
                break;
207
208
            // use same BreakEngine and dictionary for both Chinese and Japanese
209
0
            case USCRIPT_HIRAGANA:
210
0
            case USCRIPT_KATAKANA:
211
3
            case USCRIPT_HAN:
212
3
                engine = new CjkBreakEngine(m, kChineseJapanese, status);
213
3
                break;
214
#if 0
215
            // TODO: Have to get some characters with script=common handled
216
            // by CjkBreakEngine (e.g. U+309B). Simply subjecting
217
            // them to CjkBreakEngine does not work. The engine has to
218
            // special-case them.
219
            case USCRIPT_COMMON:
220
            {
221
                UBlockCode block = ublock_getCode(code);
222
                if (block == UBLOCK_HIRAGANA || block == UBLOCK_KATAKANA)
223
                   engine = new CjkBreakEngine(dict, kChineseJapanese, status);
224
                break;
225
            }
226
#endif
227
0
#endif
228
229
0
            default:
230
0
                break;
231
15
            }
232
15
            if (engine == nullptr) {
233
0
                delete m;
234
0
            }
235
15
            else if (U_FAILURE(status)) {
236
0
                delete engine;
237
0
                engine = nullptr;
238
0
            }
239
15
            return engine;
240
15
        }
241
178k
    }
242
178k
    return nullptr;
243
178k
}
244
245
DictionaryMatcher *
246
178k
ICULanguageBreakFactory::loadDictionaryMatcherFor(UScriptCode script) { 
247
178k
    UErrorCode status = U_ZERO_ERROR;
248
    // open root from brkitr tree.
249
178k
    UResourceBundle *b = ures_open(U_ICUDATA_BRKITR, "", &status);
250
178k
    b = ures_getByKeyWithFallback(b, "dictionaries", b, &status);
251
178k
    int32_t dictnlength = 0;
252
178k
    const char16_t *dictfname =
253
178k
        ures_getStringByKeyWithFallback(b, uscript_getShortName(script), &dictnlength, &status);
254
178k
    if (U_FAILURE(status)) {
255
178k
        ures_close(b);
256
178k
        return nullptr;
257
178k
    }
258
15
    CharString dictnbuf;
259
15
    CharString ext;
260
15
    const char16_t *extStart = u_memrchr(dictfname, 0x002e, dictnlength);  // last dot
261
15
    if (extStart != nullptr) {
262
15
        int32_t len = static_cast<int32_t>(extStart - dictfname);
263
15
        ext.appendInvariantChars(UnicodeString(false, extStart + 1, dictnlength - len - 1), status);
264
15
        dictnlength = len;
265
15
    }
266
15
    dictnbuf.appendInvariantChars(UnicodeString(false, dictfname, dictnlength), status);
267
15
    ures_close(b);
268
269
15
    UDataMemory *file = udata_open(U_ICUDATA_BRKITR, ext.data(), dictnbuf.data(), &status);
270
15
    if (U_SUCCESS(status)) {
271
        // build trie
272
15
        const uint8_t* data = static_cast<const uint8_t*>(udata_getMemory(file));
273
15
        const int32_t* indexes = reinterpret_cast<const int32_t*>(data);
274
15
        const int32_t offset = indexes[DictionaryData::IX_STRING_TRIE_OFFSET];
275
15
        const int32_t trieType = indexes[DictionaryData::IX_TRIE_TYPE] & DictionaryData::TRIE_TYPE_MASK;
276
15
        DictionaryMatcher *m = nullptr;
277
15
        if (trieType == DictionaryData::TRIE_TYPE_BYTES) {
278
12
            const int32_t transform = indexes[DictionaryData::IX_TRANSFORM];
279
12
            const char* characters = reinterpret_cast<const char*>(data + offset);
280
12
            m = new BytesDictionaryMatcher(characters, transform, file);
281
12
        }
282
3
        else if (trieType == DictionaryData::TRIE_TYPE_UCHARS) {
283
3
            const char16_t* characters = reinterpret_cast<const char16_t*>(data + offset);
284
3
            m = new UCharsDictionaryMatcher(characters, file);
285
3
        }
286
15
        if (m == nullptr) {
287
            // no matcher exists to take ownership - either we are an invalid 
288
            // type or memory allocation failed
289
0
            udata_close(file);
290
0
        }
291
15
        return m;
292
15
    } else if (dictfname != nullptr) {
293
        // we don't have a dictionary matcher.
294
        // returning nullptr here will cause us to fail to find a dictionary break engine, as expected
295
0
        status = U_ZERO_ERROR;
296
0
        return nullptr;
297
0
    }
298
0
    return nullptr;
299
15
}
300
301
302
void ICULanguageBreakFactory::addExternalEngine(
303
0
        ExternalBreakEngine* external, UErrorCode& status) {
304
0
    LocalPointer<ExternalBreakEngine> engine(external, status);
305
0
    ensureEngines(status);
306
0
    LocalPointer<BreakEngineWrapper> wrapper(
307
0
        new BreakEngineWrapper(engine.orphan(), status), status);
308
0
    static UMutex gBreakEngineMutex;
309
0
    Mutex m(&gBreakEngineMutex);
310
0
    fEngines->push(wrapper.getAlias(), status);
311
0
    wrapper.orphan();
312
0
}
313
314
BreakEngineWrapper::BreakEngineWrapper(
315
0
    ExternalBreakEngine* engine, UErrorCode &status) : delegate(engine, status) {
316
0
}
317
318
0
BreakEngineWrapper::~BreakEngineWrapper() {
319
0
}
320
321
0
UBool BreakEngineWrapper::handles(UChar32 c, const char* locale) const {
322
0
    return delegate->isFor(c, locale);
323
0
}
324
325
int32_t BreakEngineWrapper::findBreaks(
326
    UText *text,
327
    int32_t startPos,
328
    int32_t endPos,
329
    UVector32 &foundBreaks,
330
    UBool /* isPhraseBreaking */,
331
0
    UErrorCode &status) const {
332
0
    if (U_FAILURE(status)) return 0;
333
0
    int32_t result = 0;
334
335
    // Find the span of characters included in the set.
336
    //   The span to break begins at the current position in the text, and
337
    //   extends towards the start or end of the text, depending on 'reverse'.
338
339
0
    utext_setNativeIndex(text, startPos);
340
0
    int32_t start = static_cast<int32_t>(utext_getNativeIndex(text));
341
0
    int32_t current;
342
0
    int32_t rangeStart;
343
0
    int32_t rangeEnd;
344
0
    UChar32 c = utext_current32(text);
345
0
    while ((current = static_cast<int32_t>(utext_getNativeIndex(text))) < endPos && delegate->handles(c)) {
346
0
        utext_next32(text);         // TODO:  recast loop for postincrement
347
0
        c = utext_current32(text);
348
0
    }
349
0
    rangeStart = start;
350
0
    rangeEnd = current;
351
0
    int32_t beforeSize = foundBreaks.size();
352
0
    int32_t additionalCapacity = rangeEnd - rangeStart + 1;
353
    // enlarge to contains (rangeEnd-rangeStart+1) more items
354
0
    foundBreaks.ensureCapacity(beforeSize+additionalCapacity, status);
355
0
    if (U_FAILURE(status)) return 0;
356
0
    foundBreaks.setSize(beforeSize + beforeSize+additionalCapacity);
357
0
    result = delegate->fillBreaks(text, rangeStart, rangeEnd, foundBreaks.getBuffer()+beforeSize,
358
0
                                  additionalCapacity, status);
359
0
    if (U_FAILURE(status)) return 0;
360
0
    foundBreaks.setSize(beforeSize + result);
361
0
    utext_setNativeIndex(text, current);
362
0
    return result;
363
0
}
364
365
U_NAMESPACE_END
366
367
#endif /* #if !UCONFIG_NO_BREAK_ITERATION */