Coverage Report

Created: 2026-06-23 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/common/dictbe.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 <utility>
11
12
#include "unicode/utypes.h"
13
14
#if !UCONFIG_NO_BREAK_ITERATION
15
16
#include "brkeng.h"
17
#include "dictbe.h"
18
#include "unicode/uniset.h"
19
#include "unicode/chariter.h"
20
#include "unicode/resbund.h"
21
#include "unicode/ubrk.h"
22
#include "unicode/usetiter.h"
23
#include "ubrkimpl.h"
24
#include "utracimp.h"
25
#include "uvectr32.h"
26
#include "uvector.h"
27
#include "uassert.h"
28
#include "unicode/normlzr.h"
29
#include "cmemory.h"
30
#include "dictionarydata.h"
31
32
U_NAMESPACE_BEGIN
33
34
/*
35
 ******************************************************************
36
 */
37
38
15
DictionaryBreakEngine::DictionaryBreakEngine() {
39
15
}
40
41
0
DictionaryBreakEngine::~DictionaryBreakEngine() {
42
0
}
43
44
UBool
45
14.6M
DictionaryBreakEngine::handles(UChar32 c, const char*) const {
46
14.6M
    return fSet.contains(c);
47
14.6M
}
48
49
int32_t
50
DictionaryBreakEngine::findBreaks( UText *text,
51
                                 int32_t startPos,
52
                                 int32_t endPos,
53
                                 UVector32 &foundBreaks,
54
                                 UBool isPhraseBreaking,
55
5.79M
                                 UErrorCode& status) const {
56
5.79M
    if (U_FAILURE(status)) return 0;
57
5.79M
    int32_t result = 0;
58
59
    // Find the span of characters included in the set.
60
    //   The span to break begins at the current position in the text, and
61
    //   extends towards the start or end of the text, depending on 'reverse'.
62
63
5.79M
    utext_setNativeIndex(text, startPos);
64
5.79M
    int32_t start = static_cast<int32_t>(utext_getNativeIndex(text));
65
5.79M
    int32_t current;
66
5.79M
    int32_t rangeStart;
67
5.79M
    int32_t rangeEnd;
68
5.79M
    UChar32 c = utext_current32(text);
69
154M
    while ((current = static_cast<int32_t>(utext_getNativeIndex(text))) < endPos && fSet.contains(c)) {
70
148M
        utext_next32(text);         // TODO:  recast loop for postincrement
71
148M
        c = utext_current32(text);
72
148M
    }
73
5.79M
    rangeStart = start;
74
5.79M
    rangeEnd = current;
75
5.79M
    result = divideUpDictionaryRange(text, rangeStart, rangeEnd, foundBreaks, isPhraseBreaking, status);
76
5.79M
    utext_setNativeIndex(text, current);
77
    
78
5.79M
    return result;
79
5.79M
}
80
81
void
82
15
DictionaryBreakEngine::setCharacters( const UnicodeSet &set ) {
83
15
    fSet = set;
84
    // Compact for caching
85
15
    fSet.compact();
86
15
}
87
88
/*
89
 ******************************************************************
90
 * PossibleWord
91
 */
92
93
// Helper class for improving readability of the Thai/Lao/Khmer word break
94
// algorithm. The implementation is completely inline.
95
96
// List size, limited by the maximum number of words in the dictionary
97
// that form a nested sequence.
98
static const int32_t POSSIBLE_WORD_LIST_MAX = 20;
99
100
class PossibleWord {
101
private:
102
    // list of word candidate lengths, in increasing length order
103
    // TODO: bytes would be sufficient for word lengths.
104
    int32_t   count;      // Count of candidates
105
    int32_t   prefix;     // The longest match with a dictionary word
106
    int32_t   offset;     // Offset in the text of these candidates
107
    int32_t   mark;       // The preferred candidate's offset
108
    int32_t   current;    // The candidate we're currently looking at
109
    int32_t   cuLengths[POSSIBLE_WORD_LIST_MAX];   // Word Lengths, in code units.
110
    int32_t   cpLengths[POSSIBLE_WORD_LIST_MAX];   // Word Lengths, in code points.
111
112
public:
113
783k
    PossibleWord() : count(0), prefix(0), offset(-1), mark(0), current(0) {}
114
783k
    ~PossibleWord() {}
115
  
116
    // Fill the list of candidates if needed, select the longest, and return the number found
117
    int32_t   candidates( UText *text, DictionaryMatcher *dict, int32_t rangeEnd );
118
  
119
    // Select the currently marked candidate, point after it in the text, and invalidate self
120
    int32_t   acceptMarked( UText *text );
121
  
122
    // Back up from the current candidate to the next shorter one; return true if that exists
123
    // and point the text after it
124
    UBool     backUp( UText *text );
125
  
126
    // Return the longest prefix this candidate location shares with a dictionary word
127
    // Return value is in code points.
128
6.44M
    int32_t   longestPrefix() { return prefix; }
129
  
130
    // Mark the current candidate as the one we like
131
953k
    void      markCurrent() { mark = current; }
132
    
133
    // Get length in code points of the marked word.
134
23.6M
    int32_t   markedCPLength() { return cpLengths[mark]; }
135
};
136
137
138
73.2M
int32_t PossibleWord::candidates( UText *text, DictionaryMatcher *dict, int32_t rangeEnd ) {
139
    // TODO: If getIndex is too slow, use offset < 0 and add discardAll()
140
73.2M
    int32_t start = static_cast<int32_t>(utext_getNativeIndex(text));
141
73.2M
    if (start != offset) {
142
32.7M
        offset = start;
143
32.7M
        count = dict->matches(text, rangeEnd-start, UPRV_LENGTHOF(cuLengths), cuLengths, cpLengths, nullptr, &prefix);
144
        // Dictionary leaves text after longest prefix, not longest word. Back up.
145
32.7M
        if (count <= 0) {
146
8.80M
            utext_setNativeIndex(text, start);
147
8.80M
        }
148
32.7M
    }
149
73.2M
    if (count > 0) {
150
49.7M
        utext_setNativeIndex(text, start+cuLengths[count-1]);
151
49.7M
    }
152
73.2M
    current = count-1;
153
73.2M
    mark = current;
154
73.2M
    return count;
155
73.2M
}
156
157
int32_t
158
23.6M
PossibleWord::acceptMarked( UText *text ) {
159
23.6M
    utext_setNativeIndex(text, offset + cuLengths[mark]);
160
23.6M
    return cuLengths[mark];
161
23.6M
}
162
163
164
UBool
165
1.60M
PossibleWord::backUp( UText *text ) {
166
1.60M
    if (current > 0) {
167
1.04M
        utext_setNativeIndex(text, offset + cuLengths[--current]);
168
1.04M
        return true;
169
1.04M
    }
170
560k
    return false;
171
1.60M
}
172
173
/*
174
 ******************************************************************
175
 * ThaiBreakEngine
176
 */
177
178
// How many words in a row are "good enough"?
179
static const int32_t THAI_LOOKAHEAD = 3;
180
181
// Will not combine a non-word with a preceding dictionary word longer than this
182
static const int32_t THAI_ROOT_COMBINE_THRESHOLD = 3;
183
184
// Will not combine a non-word that shares at least this much prefix with a
185
// dictionary word, with a preceding word
186
static const int32_t THAI_PREFIX_COMBINE_THRESHOLD = 3;
187
188
// Elision character
189
static const int32_t THAI_PAIYANNOI = 0x0E2F;
190
191
// Repeat character
192
static const int32_t THAI_MAIYAMOK = 0x0E46;
193
194
// Minimum word size
195
static const int32_t THAI_MIN_WORD = 2;
196
197
// Minimum number of characters for two words
198
static const int32_t THAI_MIN_WORD_SPAN = THAI_MIN_WORD * 2;
199
200
ThaiBreakEngine::ThaiBreakEngine(DictionaryMatcher *adoptDictionary, UErrorCode &status)
201
3
    : DictionaryBreakEngine(),
202
3
      fDictionary(adoptDictionary)
203
3
{
204
3
    UTRACE_ENTRY(UTRACE_UBRK_CREATE_BREAK_ENGINE);
205
3
    UTRACE_DATA1(UTRACE_INFO, "dictbe=%s", "Thai");
206
3
    UnicodeSet thaiWordSet(UnicodeString(u"[[:Thai:]&[:LineBreak=SA:]]"), status);
207
3
    if (U_SUCCESS(status)) {
208
3
        setCharacters(thaiWordSet);
209
3
    }
210
3
    fMarkSet.applyPattern(UnicodeString(u"[[:Thai:]&[:LineBreak=SA:]&[:M:]]"), status);
211
3
    fMarkSet.add(0x0020);
212
3
    fEndWordSet = thaiWordSet;
213
3
    fEndWordSet.remove(0x0E31);             // MAI HAN-AKAT
214
3
    fEndWordSet.remove(0x0E40, 0x0E44);     // SARA E through SARA AI MAIMALAI
215
3
    fBeginWordSet.add(0x0E01, 0x0E2E);      // KO KAI through HO NOKHUK
216
3
    fBeginWordSet.add(0x0E40, 0x0E44);      // SARA E through SARA AI MAIMALAI
217
3
    fSuffixSet.add(THAI_PAIYANNOI);
218
3
    fSuffixSet.add(THAI_MAIYAMOK);
219
220
    // Compact for caching.
221
3
    fMarkSet.compact();
222
3
    fEndWordSet.compact();
223
3
    fBeginWordSet.compact();
224
3
    fSuffixSet.compact();
225
3
    UTRACE_EXIT_STATUS(status);
226
3
}
227
228
0
ThaiBreakEngine::~ThaiBreakEngine() {
229
0
    delete fDictionary;
230
0
}
231
232
int32_t
233
ThaiBreakEngine::divideUpDictionaryRange( UText *text,
234
                                                int32_t rangeStart,
235
                                                int32_t rangeEnd,
236
                                                UVector32 &foundBreaks,
237
                                                UBool /* isPhraseBreaking */,
238
236k
                                                UErrorCode& status) const {
239
236k
    if (U_FAILURE(status)) return 0;
240
236k
    utext_setNativeIndex(text, rangeStart);
241
236k
    utext_moveIndex32(text, THAI_MIN_WORD_SPAN);
242
236k
    if (utext_getNativeIndex(text) >= rangeEnd) {
243
152k
        return 0;       // Not enough characters for two words
244
152k
    }
245
83.5k
    utext_setNativeIndex(text, rangeStart);
246
247
248
83.5k
    uint32_t wordsFound = 0;
249
83.5k
    int32_t cpWordLength = 0;    // Word Length in Code Points.
250
83.5k
    int32_t cuWordLength = 0;    // Word length in code units (UText native indexing)
251
83.5k
    int32_t current;
252
83.5k
    PossibleWord words[THAI_LOOKAHEAD];
253
    
254
83.5k
    utext_setNativeIndex(text, rangeStart);
255
    
256
3.84M
    while (U_SUCCESS(status) && (current = static_cast<int32_t>(utext_getNativeIndex(text))) < rangeEnd) {
257
3.75M
        cpWordLength = 0;
258
3.75M
        cuWordLength = 0;
259
260
        // Look for candidate words at the current position
261
3.75M
        int32_t candidates = words[wordsFound%THAI_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
262
        
263
        // If we found exactly one, use that
264
3.75M
        if (candidates == 1) {
265
1.83M
            cuWordLength = words[wordsFound % THAI_LOOKAHEAD].acceptMarked(text);
266
1.83M
            cpWordLength = words[wordsFound % THAI_LOOKAHEAD].markedCPLength();
267
1.83M
            wordsFound += 1;
268
1.83M
        }
269
        // If there was more than one, see which one can take us forward the most words
270
1.92M
        else if (candidates > 1) {
271
            // If we're already at the end of the range, we're done
272
153k
            if (static_cast<int32_t>(utext_getNativeIndex(text)) >= rangeEnd) {
273
12.3k
                goto foundBest;
274
12.3k
            }
275
270k
            do {
276
270k
                if (words[(wordsFound + 1) % THAI_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) > 0) {
277
                    // Followed by another dictionary word; mark first word as a good candidate
278
53.4k
                    words[wordsFound%THAI_LOOKAHEAD].markCurrent();
279
                    
280
                    // If we're already at the end of the range, we're done
281
53.4k
                    if (static_cast<int32_t>(utext_getNativeIndex(text)) >= rangeEnd) {
282
13.8k
                        goto foundBest;
283
13.8k
                    }
284
                    
285
                    // See if any of the possible second words is followed by a third word
286
61.5k
                    do {
287
                        // If we find a third word, stop right away
288
61.5k
                        if (words[(wordsFound + 2) % THAI_LOOKAHEAD].candidates(text, fDictionary, rangeEnd)) {
289
33.6k
                            words[wordsFound % THAI_LOOKAHEAD].markCurrent();
290
33.6k
                            goto foundBest;
291
33.6k
                        }
292
61.5k
                    }
293
39.5k
                    while (words[(wordsFound + 1) % THAI_LOOKAHEAD].backUp(text));
294
39.5k
                }
295
270k
            }
296
222k
            while (words[wordsFound % THAI_LOOKAHEAD].backUp(text));
297
153k
foundBest:
298
            // Set UText position to after the accepted word.
299
153k
            cuWordLength = words[wordsFound % THAI_LOOKAHEAD].acceptMarked(text);
300
153k
            cpWordLength = words[wordsFound % THAI_LOOKAHEAD].markedCPLength();
301
153k
            wordsFound += 1;
302
153k
        }
303
        
304
        // We come here after having either found a word or not. We look ahead to the
305
        // next word. If it's not a dictionary word, we will combine it with the word we
306
        // just found (if there is one), but only if the preceding word does not exceed
307
        // the threshold.
308
        // The text iterator should now be positioned at the end of the word we found.
309
        
310
3.75M
        UChar32 uc = 0;
311
3.75M
        if (static_cast<int32_t>(utext_getNativeIndex(text)) < rangeEnd && cpWordLength < THAI_ROOT_COMBINE_THRESHOLD) {
312
            // if it is a dictionary word, do nothing. If it isn't, then if there is
313
            // no preceding word, or the non-word shares less than the minimum threshold
314
            // of characters with a dictionary word, then scan to resynchronize
315
3.59M
            if (words[wordsFound % THAI_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) <= 0
316
3.46M
                  && (cuWordLength == 0
317
1.78M
                      || words[wordsFound%THAI_LOOKAHEAD].longestPrefix() < THAI_PREFIX_COMBINE_THRESHOLD)) {
318
                // Look for a plausible word boundary
319
1.78M
                int32_t remaining = rangeEnd - (current+cuWordLength);
320
1.78M
                UChar32 pc;
321
1.78M
                int32_t chars = 0;
322
2.45M
                for (;;) {
323
2.45M
                    int32_t pcIndex = static_cast<int32_t>(utext_getNativeIndex(text));
324
2.45M
                    pc = utext_next32(text);
325
2.45M
                    int32_t pcSize = static_cast<int32_t>(utext_getNativeIndex(text)) - pcIndex;
326
2.45M
                    chars += pcSize;
327
2.45M
                    remaining -= pcSize;
328
2.45M
                    if (remaining <= 0) {
329
23.1k
                        break;
330
23.1k
                    }
331
2.43M
                    uc = utext_current32(text);
332
2.43M
                    if (fEndWordSet.contains(pc) && fBeginWordSet.contains(uc)) {
333
                        // Maybe. See if it's in the dictionary.
334
                        // NOTE: In the original Apple code, checked that the next
335
                        // two characters after uc were not 0x0E4C THANTHAKHAT before
336
                        // checking the dictionary. That is just a performance filter,
337
                        // but it's not clear it's faster than checking the trie.
338
2.36M
                        int32_t num_candidates = words[(wordsFound + 1) % THAI_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
339
2.36M
                        utext_setNativeIndex(text, current + cuWordLength + chars);
340
2.36M
                        if (num_candidates > 0) {
341
1.76M
                            break;
342
1.76M
                        }
343
2.36M
                    }
344
2.43M
                }
345
                
346
                // Bump the word count if there wasn't already one
347
1.78M
                if (cuWordLength <= 0) {
348
1.77M
                    wordsFound += 1;
349
1.77M
                }
350
                
351
                // Update the length with the passed-over characters
352
1.78M
                cuWordLength += chars;
353
1.78M
            }
354
1.81M
            else {
355
                // Back up to where we were for next iteration
356
1.81M
                utext_setNativeIndex(text, current+cuWordLength);
357
1.81M
            }
358
3.59M
        }
359
360
        // Never stop before a combining mark.
361
3.75M
        int32_t currPos;
362
3.75M
        while ((currPos = static_cast<int32_t>(utext_getNativeIndex(text))) < rangeEnd && fMarkSet.contains(utext_current32(text))) {
363
1.64k
            utext_next32(text);
364
1.64k
            cuWordLength += static_cast<int32_t>(utext_getNativeIndex(text)) - currPos;
365
1.64k
        }
366
367
        // Look ahead for possible suffixes if a dictionary word does not follow.
368
        // We do this in code rather than using a rule so that the heuristic
369
        // resynch continues to function. For example, one of the suffix characters
370
        // could be a typo in the middle of a word.
371
3.75M
        if (static_cast<int32_t>(utext_getNativeIndex(text)) < rangeEnd && cuWordLength > 0) {
372
3.69M
            if (words[wordsFound%THAI_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) <= 0
373
1.76M
                && fSuffixSet.contains(uc = utext_current32(text))) {
374
76.0k
                if (uc == THAI_PAIYANNOI) {
375
65.9k
                    if (!fSuffixSet.contains(utext_previous32(text))) {
376
                        // Skip over previous end and PAIYANNOI
377
51.5k
                        utext_next32(text);
378
51.5k
                        int32_t paiyannoiIndex = static_cast<int32_t>(utext_getNativeIndex(text));
379
51.5k
                        utext_next32(text);
380
51.5k
                        cuWordLength += static_cast<int32_t>(utext_getNativeIndex(text)) - paiyannoiIndex; // Add PAIYANNOI to word
381
51.5k
                        uc = utext_current32(text);     // Fetch next character
382
51.5k
                    }
383
14.4k
                    else {
384
                        // Restore prior position
385
14.4k
                        utext_next32(text);
386
14.4k
                    }
387
65.9k
                }
388
76.0k
                if (uc == THAI_MAIYAMOK) {
389
41.1k
                    if (utext_previous32(text) != THAI_MAIYAMOK) {
390
                        // Skip over previous end and MAIYAMOK
391
40.0k
                        utext_next32(text);
392
40.0k
                        int32_t maiyamokIndex = static_cast<int32_t>(utext_getNativeIndex(text));
393
40.0k
                        utext_next32(text);
394
40.0k
                        cuWordLength += static_cast<int32_t>(utext_getNativeIndex(text)) - maiyamokIndex; // Add MAIYAMOK to word
395
40.0k
                    }
396
1.05k
                    else {
397
                        // Restore prior position
398
1.05k
                        utext_next32(text);
399
1.05k
                    }
400
41.1k
                }
401
76.0k
            }
402
3.62M
            else {
403
3.62M
                utext_setNativeIndex(text, current+cuWordLength);
404
3.62M
            }
405
3.69M
        }
406
407
        // Did we find a word on this iteration? If so, push it on the break stack
408
3.75M
        if (cuWordLength > 0) {
409
3.75M
            foundBreaks.push((current+cuWordLength), status);
410
3.75M
        }
411
3.75M
    }
412
413
    // Don't return a break for the end of the dictionary range if there is one there.
414
83.5k
    if (foundBreaks.peeki() >= rangeEnd) {
415
83.5k
        (void) foundBreaks.popi();
416
83.5k
        wordsFound -= 1;
417
83.5k
    }
418
419
83.5k
    return wordsFound;
420
83.5k
}
421
422
/*
423
 ******************************************************************
424
 * LaoBreakEngine
425
 */
426
427
// How many words in a row are "good enough"?
428
static const int32_t LAO_LOOKAHEAD = 3;
429
430
// Will not combine a non-word with a preceding dictionary word longer than this
431
static const int32_t LAO_ROOT_COMBINE_THRESHOLD = 3;
432
433
// Will not combine a non-word that shares at least this much prefix with a
434
// dictionary word, with a preceding word
435
static const int32_t LAO_PREFIX_COMBINE_THRESHOLD = 3;
436
437
// Minimum word size
438
static const int32_t LAO_MIN_WORD = 2;
439
440
// Minimum number of characters for two words
441
static const int32_t LAO_MIN_WORD_SPAN = LAO_MIN_WORD * 2;
442
443
LaoBreakEngine::LaoBreakEngine(DictionaryMatcher *adoptDictionary, UErrorCode &status)
444
3
    : DictionaryBreakEngine(),
445
3
      fDictionary(adoptDictionary)
446
3
{
447
3
    UTRACE_ENTRY(UTRACE_UBRK_CREATE_BREAK_ENGINE);
448
3
    UTRACE_DATA1(UTRACE_INFO, "dictbe=%s", "Laoo");
449
3
    UnicodeSet laoWordSet(UnicodeString(u"[[:Laoo:]&[:LineBreak=SA:]]"), status);
450
3
    if (U_SUCCESS(status)) {
451
3
        setCharacters(laoWordSet);
452
3
    }
453
3
    fMarkSet.applyPattern(UnicodeString(u"[[:Laoo:]&[:LineBreak=SA:]&[:M:]]"), status);
454
3
    fMarkSet.add(0x0020);
455
3
    fEndWordSet = laoWordSet;
456
3
    fEndWordSet.remove(0x0EC0, 0x0EC4);     // prefix vowels
457
3
    fBeginWordSet.add(0x0E81, 0x0EAE);      // basic consonants (including holes for corresponding Thai characters)
458
3
    fBeginWordSet.add(0x0EDC, 0x0EDD);      // digraph consonants (no Thai equivalent)
459
3
    fBeginWordSet.add(0x0EC0, 0x0EC4);      // prefix vowels
460
461
    // Compact for caching.
462
3
    fMarkSet.compact();
463
3
    fEndWordSet.compact();
464
3
    fBeginWordSet.compact();
465
3
    UTRACE_EXIT_STATUS(status);
466
3
}
467
468
0
LaoBreakEngine::~LaoBreakEngine() {
469
0
    delete fDictionary;
470
0
}
471
472
int32_t
473
LaoBreakEngine::divideUpDictionaryRange( UText *text,
474
                                                int32_t rangeStart,
475
                                                int32_t rangeEnd,
476
                                                UVector32 &foundBreaks,
477
                                                UBool /* isPhraseBreaking */,
478
76.3k
                                                UErrorCode& status) const {
479
76.3k
    if (U_FAILURE(status)) return 0;
480
76.3k
    if ((rangeEnd - rangeStart) < LAO_MIN_WORD_SPAN) {
481
34.1k
        return 0;       // Not enough characters for two words
482
34.1k
    }
483
484
42.1k
    uint32_t wordsFound = 0;
485
42.1k
    int32_t cpWordLength = 0;
486
42.1k
    int32_t cuWordLength = 0;
487
42.1k
    int32_t current;
488
42.1k
    PossibleWord words[LAO_LOOKAHEAD];
489
490
42.1k
    utext_setNativeIndex(text, rangeStart);
491
492
5.63M
    while (U_SUCCESS(status) && (current = static_cast<int32_t>(utext_getNativeIndex(text))) < rangeEnd) {
493
5.59M
        cuWordLength = 0;
494
5.59M
        cpWordLength = 0;
495
496
        // Look for candidate words at the current position
497
5.59M
        int32_t candidates = words[wordsFound%LAO_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
498
        
499
        // If we found exactly one, use that
500
5.59M
        if (candidates == 1) {
501
2.60M
            cuWordLength = words[wordsFound % LAO_LOOKAHEAD].acceptMarked(text);
502
2.60M
            cpWordLength = words[wordsFound % LAO_LOOKAHEAD].markedCPLength();
503
2.60M
            wordsFound += 1;
504
2.60M
        }
505
        // If there was more than one, see which one can take us forward the most words
506
2.99M
        else if (candidates > 1) {
507
            // If we're already at the end of the range, we're done
508
346k
            if (utext_getNativeIndex(text) >= rangeEnd) {
509
10.4k
                goto foundBest;
510
10.4k
            }
511
526k
            do {
512
526k
                if (words[(wordsFound + 1) % LAO_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) > 0) {
513
                    // Followed by another dictionary word; mark first word as a good candidate
514
263k
                    words[wordsFound%LAO_LOOKAHEAD].markCurrent();
515
                    
516
                    // If we're already at the end of the range, we're done
517
263k
                    if (static_cast<int32_t>(utext_getNativeIndex(text)) >= rangeEnd) {
518
9.27k
                        goto foundBest;
519
9.27k
                    }
520
                    
521
                    // See if any of the possible second words is followed by a third word
522
370k
                    do {
523
                        // If we find a third word, stop right away
524
370k
                        if (words[(wordsFound + 2) % LAO_LOOKAHEAD].candidates(text, fDictionary, rangeEnd)) {
525
186k
                            words[wordsFound % LAO_LOOKAHEAD].markCurrent();
526
186k
                            goto foundBest;
527
186k
                        }
528
370k
                    }
529
254k
                    while (words[(wordsFound + 1) % LAO_LOOKAHEAD].backUp(text));
530
254k
                }
531
526k
            }
532
336k
            while (words[wordsFound % LAO_LOOKAHEAD].backUp(text));
533
346k
foundBest:
534
346k
            cuWordLength = words[wordsFound % LAO_LOOKAHEAD].acceptMarked(text);
535
346k
            cpWordLength = words[wordsFound % LAO_LOOKAHEAD].markedCPLength();
536
346k
            wordsFound += 1;
537
346k
        }
538
        
539
        // We come here after having either found a word or not. We look ahead to the
540
        // next word. If it's not a dictionary word, we will combine it with the word we
541
        // just found (if there is one), but only if the preceding word does not exceed
542
        // the threshold.
543
        // The text iterator should now be positioned at the end of the word we found.
544
5.59M
        if (static_cast<int32_t>(utext_getNativeIndex(text)) < rangeEnd && cpWordLength < LAO_ROOT_COMBINE_THRESHOLD) {
545
            // if it is a dictionary word, do nothing. If it isn't, then if there is
546
            // no preceding word, or the non-word shares less than the minimum threshold
547
            // of characters with a dictionary word, then scan to resynchronize
548
5.32M
            if (words[wordsFound % LAO_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) <= 0
549
5.20M
                  && (cuWordLength == 0
550
2.65M
                      || words[wordsFound%LAO_LOOKAHEAD].longestPrefix() < LAO_PREFIX_COMBINE_THRESHOLD)) {
551
                // Look for a plausible word boundary
552
2.65M
                int32_t remaining = rangeEnd - (current + cuWordLength);
553
2.65M
                UChar32 pc;
554
2.65M
                UChar32 uc;
555
2.65M
                int32_t chars = 0;
556
2.74M
                for (;;) {
557
2.74M
                    int32_t pcIndex = static_cast<int32_t>(utext_getNativeIndex(text));
558
2.74M
                    pc = utext_next32(text);
559
2.74M
                    int32_t pcSize = static_cast<int32_t>(utext_getNativeIndex(text)) - pcIndex;
560
2.74M
                    chars += pcSize;
561
2.74M
                    remaining -= pcSize;
562
2.74M
                    if (remaining <= 0) {
563
17.6k
                        break;
564
17.6k
                    }
565
2.72M
                    uc = utext_current32(text);
566
2.72M
                    if (fEndWordSet.contains(pc) && fBeginWordSet.contains(uc)) {
567
                        // Maybe. See if it's in the dictionary.
568
                        // TODO: this looks iffy; compare with old code.
569
2.65M
                        int32_t num_candidates = words[(wordsFound + 1) % LAO_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
570
2.65M
                        utext_setNativeIndex(text, current + cuWordLength + chars);
571
2.65M
                        if (num_candidates > 0) {
572
2.63M
                            break;
573
2.63M
                        }
574
2.65M
                    }
575
2.72M
                }
576
                
577
                // Bump the word count if there wasn't already one
578
2.65M
                if (cuWordLength <= 0) {
579
2.64M
                    wordsFound += 1;
580
2.64M
                }
581
                
582
                // Update the length with the passed-over characters
583
2.65M
                cuWordLength += chars;
584
2.65M
            }
585
2.67M
            else {
586
                // Back up to where we were for next iteration
587
2.67M
                utext_setNativeIndex(text, current + cuWordLength);
588
2.67M
            }
589
5.32M
        }
590
        
591
        // Never stop before a combining mark.
592
5.59M
        int32_t currPos;
593
5.62M
        while ((currPos = static_cast<int32_t>(utext_getNativeIndex(text))) < rangeEnd && fMarkSet.contains(utext_current32(text))) {
594
32.2k
            utext_next32(text);
595
32.2k
            cuWordLength += static_cast<int32_t>(utext_getNativeIndex(text)) - currPos;
596
32.2k
        }
597
        
598
        // Look ahead for possible suffixes if a dictionary word does not follow.
599
        // We do this in code rather than using a rule so that the heuristic
600
        // resynch continues to function. For example, one of the suffix characters
601
        // could be a typo in the middle of a word.
602
        // NOT CURRENTLY APPLICABLE TO LAO
603
604
        // Did we find a word on this iteration? If so, push it on the break stack
605
5.59M
        if (cuWordLength > 0) {
606
5.59M
            foundBreaks.push((current+cuWordLength), status);
607
5.59M
        }
608
5.59M
    }
609
610
    // Don't return a break for the end of the dictionary range if there is one there.
611
42.1k
    if (foundBreaks.peeki() >= rangeEnd) {
612
42.1k
        (void) foundBreaks.popi();
613
42.1k
        wordsFound -= 1;
614
42.1k
    }
615
616
42.1k
    return wordsFound;
617
42.1k
}
618
619
/*
620
 ******************************************************************
621
 * BurmeseBreakEngine
622
 */
623
624
// How many words in a row are "good enough"?
625
static const int32_t BURMESE_LOOKAHEAD = 3;
626
627
// Will not combine a non-word with a preceding dictionary word longer than this
628
static const int32_t BURMESE_ROOT_COMBINE_THRESHOLD = 3;
629
630
// Will not combine a non-word that shares at least this much prefix with a
631
// dictionary word, with a preceding word
632
static const int32_t BURMESE_PREFIX_COMBINE_THRESHOLD = 3;
633
634
// Minimum word size
635
static const int32_t BURMESE_MIN_WORD = 2;
636
637
// Minimum number of characters for two words
638
static const int32_t BURMESE_MIN_WORD_SPAN = BURMESE_MIN_WORD * 2;
639
640
BurmeseBreakEngine::BurmeseBreakEngine(DictionaryMatcher *adoptDictionary, UErrorCode &status)
641
3
    : DictionaryBreakEngine(),
642
3
      fDictionary(adoptDictionary)
643
3
{
644
3
    UTRACE_ENTRY(UTRACE_UBRK_CREATE_BREAK_ENGINE);
645
3
    UTRACE_DATA1(UTRACE_INFO, "dictbe=%s", "Mymr");
646
3
    fBeginWordSet.add(0x1000, 0x102A);      // basic consonants and independent vowels
647
3
    fEndWordSet.applyPattern(UnicodeString(u"[[:Mymr:]&[:LineBreak=SA:]]"), status);
648
3
    fMarkSet.applyPattern(UnicodeString(u"[[:Mymr:]&[:LineBreak=SA:]&[:M:]]"), status);
649
3
    fMarkSet.add(0x0020);
650
3
    if (U_SUCCESS(status)) {
651
3
        setCharacters(fEndWordSet);
652
3
    }
653
654
    // Compact for caching.
655
3
    fMarkSet.compact();
656
3
    fEndWordSet.compact();
657
3
    fBeginWordSet.compact();
658
3
    UTRACE_EXIT_STATUS(status);
659
3
}
660
661
0
BurmeseBreakEngine::~BurmeseBreakEngine() {
662
0
    delete fDictionary;
663
0
}
664
665
int32_t
666
BurmeseBreakEngine::divideUpDictionaryRange( UText *text,
667
                                                int32_t rangeStart,
668
                                                int32_t rangeEnd,
669
                                                UVector32 &foundBreaks,
670
                                                UBool /* isPhraseBreaking */,
671
171k
                                                UErrorCode& status ) const {
672
171k
    if (U_FAILURE(status)) return 0;
673
171k
    if ((rangeEnd - rangeStart) < BURMESE_MIN_WORD_SPAN) {
674
93.2k
        return 0;       // Not enough characters for two words
675
93.2k
    }
676
677
78.0k
    uint32_t wordsFound = 0;
678
78.0k
    int32_t cpWordLength = 0;
679
78.0k
    int32_t cuWordLength = 0;
680
78.0k
    int32_t current;
681
78.0k
    PossibleWord words[BURMESE_LOOKAHEAD];
682
683
78.0k
    utext_setNativeIndex(text, rangeStart);
684
685
10.3M
    while (U_SUCCESS(status) && (current = static_cast<int32_t>(utext_getNativeIndex(text))) < rangeEnd) {
686
10.3M
        cuWordLength = 0;
687
10.3M
        cpWordLength = 0;
688
689
        // Look for candidate words at the current position
690
10.3M
        int32_t candidates = words[wordsFound%BURMESE_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
691
        
692
        // If we found exactly one, use that
693
10.3M
        if (candidates == 1) {
694
10.1M
            cuWordLength = words[wordsFound % BURMESE_LOOKAHEAD].acceptMarked(text);
695
10.1M
            cpWordLength = words[wordsFound % BURMESE_LOOKAHEAD].markedCPLength();
696
10.1M
            wordsFound += 1;
697
10.1M
        }
698
        // If there was more than one, see which one can take us forward the most words
699
175k
        else if (candidates > 1) {
700
            // If we're already at the end of the range, we're done
701
158k
            if (utext_getNativeIndex(text) >= rangeEnd) {
702
9.57k
                goto foundBest;
703
9.57k
            }
704
322k
            do {
705
322k
                if (words[(wordsFound + 1) % BURMESE_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) > 0) {
706
                    // Followed by another dictionary word; mark first word as a good candidate
707
134k
                    words[wordsFound%BURMESE_LOOKAHEAD].markCurrent();
708
                    
709
                    // If we're already at the end of the range, we're done
710
134k
                    if (static_cast<int32_t>(utext_getNativeIndex(text)) >= rangeEnd) {
711
7.65k
                        goto foundBest;
712
7.65k
                    }
713
                    
714
                    // See if any of the possible second words is followed by a third word
715
269k
                    do {
716
                        // If we find a third word, stop right away
717
269k
                        if (words[(wordsFound + 2) % BURMESE_LOOKAHEAD].candidates(text, fDictionary, rangeEnd)) {
718
78.9k
                            words[wordsFound % BURMESE_LOOKAHEAD].markCurrent();
719
78.9k
                            goto foundBest;
720
78.9k
                        }
721
269k
                    }
722
190k
                    while (words[(wordsFound + 1) % BURMESE_LOOKAHEAD].backUp(text));
723
126k
                }
724
322k
            }
725
236k
            while (words[wordsFound % BURMESE_LOOKAHEAD].backUp(text));
726
158k
foundBest:
727
158k
            cuWordLength = words[wordsFound % BURMESE_LOOKAHEAD].acceptMarked(text);
728
158k
            cpWordLength = words[wordsFound % BURMESE_LOOKAHEAD].markedCPLength();
729
158k
            wordsFound += 1;
730
158k
        }
731
        
732
        // We come here after having either found a word or not. We look ahead to the
733
        // next word. If it's not a dictionary word, we will combine it with the word we
734
        // just found (if there is one), but only if the preceding word does not exceed
735
        // the threshold.
736
        // The text iterator should now be positioned at the end of the word we found.
737
10.3M
        if (static_cast<int32_t>(utext_getNativeIndex(text)) < rangeEnd && cpWordLength < BURMESE_ROOT_COMBINE_THRESHOLD) {
738
            // if it is a dictionary word, do nothing. If it isn't, then if there is
739
            // no preceding word, or the non-word shares less than the minimum threshold
740
            // of characters with a dictionary word, then scan to resynchronize
741
10.2M
            if (words[wordsFound % BURMESE_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) <= 0
742
73.5k
                  && (cuWordLength == 0
743
69.5k
                      || words[wordsFound%BURMESE_LOOKAHEAD].longestPrefix() < BURMESE_PREFIX_COMBINE_THRESHOLD)) {
744
                // Look for a plausible word boundary
745
69.5k
                int32_t remaining = rangeEnd - (current + cuWordLength);
746
69.5k
                UChar32 pc;
747
69.5k
                UChar32 uc;
748
69.5k
                int32_t chars = 0;
749
107k
                for (;;) {
750
107k
                    int32_t pcIndex = static_cast<int32_t>(utext_getNativeIndex(text));
751
107k
                    pc = utext_next32(text);
752
107k
                    int32_t pcSize = static_cast<int32_t>(utext_getNativeIndex(text)) - pcIndex;
753
107k
                    chars += pcSize;
754
107k
                    remaining -= pcSize;
755
107k
                    if (remaining <= 0) {
756
26.8k
                        break;
757
26.8k
                    }
758
80.9k
                    uc = utext_current32(text);
759
80.9k
                    if (fEndWordSet.contains(pc) && fBeginWordSet.contains(uc)) {
760
                        // Maybe. See if it's in the dictionary.
761
                        // TODO: this looks iffy; compare with old code.
762
65.4k
                        int32_t num_candidates = words[(wordsFound + 1) % BURMESE_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
763
65.4k
                        utext_setNativeIndex(text, current + cuWordLength + chars);
764
65.4k
                        if (num_candidates > 0) {
765
42.6k
                            break;
766
42.6k
                        }
767
65.4k
                    }
768
80.9k
                }
769
                
770
                // Bump the word count if there wasn't already one
771
69.5k
                if (cuWordLength <= 0) {
772
17.2k
                    wordsFound += 1;
773
17.2k
                }
774
                
775
                // Update the length with the passed-over characters
776
69.5k
                cuWordLength += chars;
777
69.5k
            }
778
10.1M
            else {
779
                // Back up to where we were for next iteration
780
10.1M
                utext_setNativeIndex(text, current + cuWordLength);
781
10.1M
            }
782
10.2M
        }
783
        
784
        // Never stop before a combining mark.
785
10.3M
        int32_t currPos;
786
10.3M
        while ((currPos = static_cast<int32_t>(utext_getNativeIndex(text))) < rangeEnd && fMarkSet.contains(utext_current32(text))) {
787
11.6k
            utext_next32(text);
788
11.6k
            cuWordLength += static_cast<int32_t>(utext_getNativeIndex(text)) - currPos;
789
11.6k
        }
790
        
791
        // Look ahead for possible suffixes if a dictionary word does not follow.
792
        // We do this in code rather than using a rule so that the heuristic
793
        // resynch continues to function. For example, one of the suffix characters
794
        // could be a typo in the middle of a word.
795
        // NOT CURRENTLY APPLICABLE TO BURMESE
796
797
        // Did we find a word on this iteration? If so, push it on the break stack
798
10.3M
        if (cuWordLength > 0) {
799
10.3M
            foundBreaks.push((current+cuWordLength), status);
800
10.3M
        }
801
10.3M
    }
802
803
    // Don't return a break for the end of the dictionary range if there is one there.
804
78.0k
    if (foundBreaks.peeki() >= rangeEnd) {
805
78.0k
        (void) foundBreaks.popi();
806
78.0k
        wordsFound -= 1;
807
78.0k
    }
808
809
78.0k
    return wordsFound;
810
78.0k
}
811
812
/*
813
 ******************************************************************
814
 * KhmerBreakEngine
815
 */
816
817
// How many words in a row are "good enough"?
818
static const int32_t KHMER_LOOKAHEAD = 3;
819
820
// Will not combine a non-word with a preceding dictionary word longer than this
821
static const int32_t KHMER_ROOT_COMBINE_THRESHOLD = 3;
822
823
// Will not combine a non-word that shares at least this much prefix with a
824
// dictionary word, with a preceding word
825
static const int32_t KHMER_PREFIX_COMBINE_THRESHOLD = 3;
826
827
// Minimum word size
828
static const int32_t KHMER_MIN_WORD = 2;
829
830
// Minimum number of characters for two words
831
static const int32_t KHMER_MIN_WORD_SPAN = KHMER_MIN_WORD * 2;
832
833
KhmerBreakEngine::KhmerBreakEngine(DictionaryMatcher *adoptDictionary, UErrorCode &status)
834
3
    : DictionaryBreakEngine(),
835
3
      fDictionary(adoptDictionary)
836
3
{
837
3
    UTRACE_ENTRY(UTRACE_UBRK_CREATE_BREAK_ENGINE);
838
3
    UTRACE_DATA1(UTRACE_INFO, "dictbe=%s", "Khmr");
839
3
    UnicodeSet khmerWordSet(UnicodeString(u"[[:Khmr:]&[:LineBreak=SA:]]"), status);
840
3
    if (U_SUCCESS(status)) {
841
3
        setCharacters(khmerWordSet);
842
3
    }
843
3
    fMarkSet.applyPattern(UnicodeString(u"[[:Khmr:]&[:LineBreak=SA:]&[:M:]]"), status);
844
3
    fMarkSet.add(0x0020);
845
3
    fEndWordSet = khmerWordSet;
846
3
    fBeginWordSet.add(0x1780, 0x17B3);
847
    //fBeginWordSet.add(0x17A3, 0x17A4);      // deprecated vowels
848
    //fEndWordSet.remove(0x17A5, 0x17A9);     // Khmer independent vowels that can't end a word
849
    //fEndWordSet.remove(0x17B2);             // Khmer independent vowel that can't end a word
850
3
    fEndWordSet.remove(0x17D2);             // KHMER SIGN COENG that combines some following characters
851
    //fEndWordSet.remove(0x17B6, 0x17C5);     // Remove dependent vowels
852
//    fEndWordSet.remove(0x0E31);             // MAI HAN-AKAT
853
//    fEndWordSet.remove(0x0E40, 0x0E44);     // SARA E through SARA AI MAIMALAI
854
//    fBeginWordSet.add(0x0E01, 0x0E2E);      // KO KAI through HO NOKHUK
855
//    fBeginWordSet.add(0x0E40, 0x0E44);      // SARA E through SARA AI MAIMALAI
856
//    fSuffixSet.add(THAI_PAIYANNOI);
857
//    fSuffixSet.add(THAI_MAIYAMOK);
858
859
    // Compact for caching.
860
3
    fMarkSet.compact();
861
3
    fEndWordSet.compact();
862
3
    fBeginWordSet.compact();
863
//    fSuffixSet.compact();
864
3
    UTRACE_EXIT_STATUS(status);
865
3
}
866
867
0
KhmerBreakEngine::~KhmerBreakEngine() {
868
0
    delete fDictionary;
869
0
}
870
871
int32_t
872
KhmerBreakEngine::divideUpDictionaryRange( UText *text,
873
                                                int32_t rangeStart,
874
                                                int32_t rangeEnd,
875
                                                UVector32 &foundBreaks,
876
                                                UBool /* isPhraseBreaking */,
877
103k
                                                UErrorCode& status ) const {
878
103k
    if (U_FAILURE(status)) return 0;
879
103k
    if ((rangeEnd - rangeStart) < KHMER_MIN_WORD_SPAN) {
880
45.8k
        return 0;       // Not enough characters for two words
881
45.8k
    }
882
883
57.2k
    uint32_t wordsFound = 0;
884
57.2k
    int32_t cpWordLength = 0;
885
57.2k
    int32_t cuWordLength = 0;
886
57.2k
    int32_t current;
887
57.2k
    PossibleWord words[KHMER_LOOKAHEAD];
888
889
57.2k
    utext_setNativeIndex(text, rangeStart);
890
891
10.6M
    while (U_SUCCESS(status) && (current = static_cast<int32_t>(utext_getNativeIndex(text))) < rangeEnd) {
892
10.5M
        cuWordLength = 0;
893
10.5M
        cpWordLength = 0;
894
895
        // Look for candidate words at the current position
896
10.5M
        int32_t candidates = words[wordsFound%KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
897
898
        // If we found exactly one, use that
899
10.5M
        if (candidates == 1) {
900
8.28M
            cuWordLength = words[wordsFound % KHMER_LOOKAHEAD].acceptMarked(text);
901
8.28M
            cpWordLength = words[wordsFound % KHMER_LOOKAHEAD].markedCPLength();
902
8.28M
            wordsFound += 1;
903
8.28M
        }
904
905
        // If there was more than one, see which one can take us forward the most words
906
2.29M
        else if (candidates > 1) {
907
            // If we're already at the end of the range, we're done
908
158k
            if (static_cast<int32_t>(utext_getNativeIndex(text)) >= rangeEnd) {
909
11.2k
                goto foundBest;
910
11.2k
            }
911
299k
            do {
912
299k
                if (words[(wordsFound + 1) % KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) > 0) {
913
                    // Followed by another dictionary word; mark first word as a good candidate
914
144k
                    words[wordsFound % KHMER_LOOKAHEAD].markCurrent();
915
916
                    // If we're already at the end of the range, we're done
917
144k
                    if (static_cast<int32_t>(utext_getNativeIndex(text)) >= rangeEnd) {
918
15.5k
                        goto foundBest;
919
15.5k
                    }
920
921
                    // See if any of the possible second words is followed by a third word
922
251k
                    do {
923
                        // If we find a third word, stop right away
924
251k
                        if (words[(wordsFound + 2) % KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd)) {
925
59.2k
                            words[wordsFound % KHMER_LOOKAHEAD].markCurrent();
926
59.2k
                            goto foundBest;
927
59.2k
                        }
928
251k
                    }
929
192k
                    while (words[(wordsFound + 1) % KHMER_LOOKAHEAD].backUp(text));
930
128k
                }
931
299k
            }
932
224k
            while (words[wordsFound % KHMER_LOOKAHEAD].backUp(text));
933
158k
foundBest:
934
158k
            cuWordLength = words[wordsFound % KHMER_LOOKAHEAD].acceptMarked(text);
935
158k
            cpWordLength = words[wordsFound % KHMER_LOOKAHEAD].markedCPLength();
936
158k
            wordsFound += 1;
937
158k
        }
938
939
        // We come here after having either found a word or not. We look ahead to the
940
        // next word. If it's not a dictionary word, we will combine it with the word we
941
        // just found (if there is one), but only if the preceding word does not exceed
942
        // the threshold.
943
        // The text iterator should now be positioned at the end of the word we found.
944
10.5M
        if (static_cast<int32_t>(utext_getNativeIndex(text)) < rangeEnd && cpWordLength < KHMER_ROOT_COMBINE_THRESHOLD) {
945
            // if it is a dictionary word, do nothing. If it isn't, then if there is
946
            // no preceding word, or the non-word shares less than the minimum threshold
947
            // of characters with a dictionary word, then scan to resynchronize
948
10.5M
            if (words[wordsFound % KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) <= 0
949
4.27M
                  && (cuWordLength == 0
950
2.15M
                      || words[wordsFound % KHMER_LOOKAHEAD].longestPrefix() < KHMER_PREFIX_COMBINE_THRESHOLD)) {
951
                // Look for a plausible word boundary
952
2.15M
                int32_t remaining = rangeEnd - (current+cuWordLength);
953
2.15M
                UChar32 pc;
954
2.15M
                UChar32 uc;
955
2.15M
                int32_t chars = 0;
956
2.20M
                for (;;) {
957
2.20M
                    int32_t pcIndex = static_cast<int32_t>(utext_getNativeIndex(text));
958
2.20M
                    pc = utext_next32(text);
959
2.20M
                    int32_t pcSize = static_cast<int32_t>(utext_getNativeIndex(text)) - pcIndex;
960
2.20M
                    chars += pcSize;
961
2.20M
                    remaining -= pcSize;
962
2.20M
                    if (remaining <= 0) {
963
15.1k
                        break;
964
15.1k
                    }
965
2.18M
                    uc = utext_current32(text);
966
2.18M
                    if (fEndWordSet.contains(pc) && fBeginWordSet.contains(uc)) {
967
                        // Maybe. See if it's in the dictionary.
968
2.17M
                        int32_t num_candidates = words[(wordsFound + 1) % KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd);
969
2.17M
                        utext_setNativeIndex(text, current+cuWordLength+chars);
970
2.17M
                        if (num_candidates > 0) {
971
2.14M
                            break;
972
2.14M
                        }
973
2.17M
                    }
974
2.18M
                }
975
976
                // Bump the word count if there wasn't already one
977
2.15M
                if (cuWordLength <= 0) {
978
2.13M
                    wordsFound += 1;
979
2.13M
                }
980
981
                // Update the length with the passed-over characters
982
2.15M
                cuWordLength += chars;
983
2.15M
            }
984
8.35M
            else {
985
                // Back up to where we were for next iteration
986
8.35M
                utext_setNativeIndex(text, current+cuWordLength);
987
8.35M
            }
988
10.5M
        }
989
990
        // Never stop before a combining mark.
991
10.5M
        int32_t currPos;
992
10.5M
        while ((currPos = static_cast<int32_t>(utext_getNativeIndex(text))) < rangeEnd && fMarkSet.contains(utext_current32(text))) {
993
6.94k
            utext_next32(text);
994
6.94k
            cuWordLength += static_cast<int32_t>(utext_getNativeIndex(text)) - currPos;
995
6.94k
        }
996
997
        // Look ahead for possible suffixes if a dictionary word does not follow.
998
        // We do this in code rather than using a rule so that the heuristic
999
        // resynch continues to function. For example, one of the suffix characters
1000
        // could be a typo in the middle of a word.
1001
//        if ((int32_t)utext_getNativeIndex(text) < rangeEnd && wordLength > 0) {
1002
//            if (words[wordsFound%KHMER_LOOKAHEAD].candidates(text, fDictionary, rangeEnd) <= 0
1003
//                && fSuffixSet.contains(uc = utext_current32(text))) {
1004
//                if (uc == KHMER_PAIYANNOI) {
1005
//                    if (!fSuffixSet.contains(utext_previous32(text))) {
1006
//                        // Skip over previous end and PAIYANNOI
1007
//                        utext_next32(text);
1008
//                        utext_next32(text);
1009
//                        wordLength += 1;            // Add PAIYANNOI to word
1010
//                        uc = utext_current32(text);     // Fetch next character
1011
//                    }
1012
//                    else {
1013
//                        // Restore prior position
1014
//                        utext_next32(text);
1015
//                    }
1016
//                }
1017
//                if (uc == KHMER_MAIYAMOK) {
1018
//                    if (utext_previous32(text) != KHMER_MAIYAMOK) {
1019
//                        // Skip over previous end and MAIYAMOK
1020
//                        utext_next32(text);
1021
//                        utext_next32(text);
1022
//                        wordLength += 1;            // Add MAIYAMOK to word
1023
//                    }
1024
//                    else {
1025
//                        // Restore prior position
1026
//                        utext_next32(text);
1027
//                    }
1028
//                }
1029
//            }
1030
//            else {
1031
//                utext_setNativeIndex(text, current+wordLength);
1032
//            }
1033
//        }
1034
1035
        // Did we find a word on this iteration? If so, push it on the break stack
1036
10.5M
        if (cuWordLength > 0) {
1037
10.5M
            foundBreaks.push((current+cuWordLength), status);
1038
10.5M
        }
1039
10.5M
    }
1040
    
1041
    // Don't return a break for the end of the dictionary range if there is one there.
1042
57.2k
    if (foundBreaks.peeki() >= rangeEnd) {
1043
57.2k
        (void) foundBreaks.popi();
1044
57.2k
        wordsFound -= 1;
1045
57.2k
    }
1046
1047
57.2k
    return wordsFound;
1048
57.2k
}
1049
1050
#if !UCONFIG_NO_NORMALIZATION
1051
/*
1052
 ******************************************************************
1053
 * CjkBreakEngine
1054
 */
1055
static const uint32_t kuint32max = 0xFFFFFFFF;
1056
CjkBreakEngine::CjkBreakEngine(DictionaryMatcher *adoptDictionary, LanguageType type, UErrorCode &status)
1057
3
: DictionaryBreakEngine(), fDictionary(adoptDictionary), isCj(false) {
1058
3
    UTRACE_ENTRY(UTRACE_UBRK_CREATE_BREAK_ENGINE);
1059
3
    UTRACE_DATA1(UTRACE_INFO, "dictbe=%s", "Hani");
1060
3
    fMlBreakEngine = nullptr;
1061
3
    nfkcNorm2 = Normalizer2::getNFKCInstance(status);
1062
    // Korean dictionary only includes Hangul syllables
1063
3
    fHangulWordSet.applyPattern(UnicodeString(u"[\\uac00-\\ud7a3]"), status);
1064
3
    fHangulWordSet.compact();
1065
    // Digits, open puncutation and Alphabetic characters.
1066
3
    fDigitOrOpenPunctuationOrAlphabetSet.applyPattern(
1067
3
        UnicodeString(u"[[:Nd:][:Pi:][:Ps:][:Alphabetic:]]"), status);
1068
3
    fDigitOrOpenPunctuationOrAlphabetSet.compact();
1069
3
    fClosePunctuationSet.applyPattern(UnicodeString(u"[[:Pc:][:Pd:][:Pe:][:Pf:][:Po:]]"), status);
1070
3
    fClosePunctuationSet.compact();
1071
1072
    // handle Korean and Japanese/Chinese using different dictionaries
1073
3
    if (type == kKorean) {
1074
0
        if (U_SUCCESS(status)) {
1075
0
            setCharacters(fHangulWordSet);
1076
0
        }
1077
3
    } else { // Chinese and Japanese
1078
3
        UnicodeSet cjSet(UnicodeString(u"[[:Han:][:Hiragana:][:Katakana:]\\u30fc\\uff70\\uff9e\\uff9f]"), status);
1079
3
        isCj = true;
1080
3
        if (U_SUCCESS(status)) {
1081
3
            setCharacters(cjSet);
1082
#if UCONFIG_USE_ML_PHRASE_BREAKING
1083
            fMlBreakEngine = new MlBreakEngine(fDigitOrOpenPunctuationOrAlphabetSet,
1084
                                               fClosePunctuationSet, status);
1085
            if (fMlBreakEngine == nullptr) {
1086
                status = U_MEMORY_ALLOCATION_ERROR;
1087
            }
1088
#else
1089
3
            initJapanesePhraseParameter(status);
1090
3
#endif
1091
3
        }
1092
3
    }
1093
3
    UTRACE_EXIT_STATUS(status);
1094
3
}
1095
1096
0
CjkBreakEngine::~CjkBreakEngine(){
1097
0
    delete fDictionary;
1098
0
    delete fMlBreakEngine;
1099
0
}
1100
1101
// The katakanaCost values below are based on the length frequencies of all
1102
// katakana phrases in the dictionary
1103
static const int32_t kMaxKatakanaLength = 8;
1104
static const int32_t kMaxKatakanaGroupLength = 20;
1105
static const uint32_t maxSnlp = 255;
1106
1107
7.86M
static inline uint32_t getKatakanaCost(int32_t wordLength){
1108
    //TODO: fill array with actual values from dictionary!
1109
7.86M
    static const uint32_t katakanaCost[kMaxKatakanaLength + 1]
1110
7.86M
                                       = {8192, 984, 408, 240, 204, 252, 300, 372, 480};
1111
7.86M
    return (wordLength > kMaxKatakanaLength) ? 8192 : katakanaCost[wordLength];
1112
7.86M
}
1113
1114
302M
static inline bool isKatakana(UChar32 value) {
1115
302M
    return (value >= 0x30A1 && value <= 0x30FE && value != 0x30FB) ||
1116
83.0M
            (value >= 0xFF66 && value <= 0xFF9f);
1117
302M
}
1118
1119
// Function for accessing internal utext flags.
1120
//   Replicates an internal UText function.
1121
1122
5.20M
static inline int32_t utext_i32_flag(int32_t bitIndex) {
1123
5.20M
    return static_cast<int32_t>(1) << bitIndex;
1124
5.20M
}
1125
       
1126
/*
1127
 * @param text A UText representing the text
1128
 * @param rangeStart The start of the range of dictionary characters
1129
 * @param rangeEnd The end of the range of dictionary characters
1130
 * @param foundBreaks vector<int32> to receive the break positions
1131
 * @return The number of breaks found
1132
 */
1133
int32_t 
1134
CjkBreakEngine::divideUpDictionaryRange( UText *inText,
1135
        int32_t rangeStart,
1136
        int32_t rangeEnd,
1137
        UVector32 &foundBreaks,
1138
        UBool isPhraseBreaking,
1139
5.20M
        UErrorCode& status) const {
1140
5.20M
    if (U_FAILURE(status)) return 0;
1141
5.20M
    if (rangeStart >= rangeEnd) {
1142
0
        return 0;
1143
0
    }
1144
1145
    // UnicodeString version of input UText, NFKC normalized if necessary.
1146
5.20M
    UnicodeString inString;
1147
1148
    // inputMap[inStringIndex] = corresponding native index from UText inText.
1149
    // If nullptr then mapping is 1:1
1150
5.20M
    LocalPointer<UVector32>     inputMap;
1151
1152
    // if UText has the input string as one contiguous UTF-16 chunk
1153
5.20M
    if ((inText->providerProperties & utext_i32_flag(UTEXT_PROVIDER_STABLE_CHUNKS)) &&
1154
4.99M
         inText->chunkNativeStart <= rangeStart &&
1155
4.99M
         inText->chunkNativeLimit >= rangeEnd   &&
1156
4.99M
         inText->nativeIndexingLimit >= rangeEnd - inText->chunkNativeStart) {
1157
1158
        // Input UText is in one contiguous UTF-16 chunk.
1159
        // Use Read-only aliasing UnicodeString.
1160
4.99M
        inString.setTo(false,
1161
4.99M
                       inText->chunkContents + rangeStart - inText->chunkNativeStart,
1162
4.99M
                       rangeEnd - rangeStart);
1163
4.99M
    } else {
1164
        // Copy the text from the original inText (UText) to inString (UnicodeString).
1165
        // Create a map from UnicodeString indices -> UText offsets.
1166
218k
        utext_setNativeIndex(inText, rangeStart);
1167
218k
        int32_t limit = rangeEnd;
1168
218k
        U_ASSERT(limit <= utext_nativeLength(inText));
1169
218k
        if (limit > utext_nativeLength(inText)) {
1170
0
            limit = static_cast<int32_t>(utext_nativeLength(inText));
1171
0
        }
1172
218k
        inputMap.adoptInsteadAndCheckErrorCode(new UVector32(status), status);
1173
218k
        if (U_FAILURE(status)) {
1174
0
            return 0;
1175
0
        }
1176
16.4M
        while (utext_getNativeIndex(inText) < limit) {
1177
16.2M
            int32_t nativePosition = static_cast<int32_t>(utext_getNativeIndex(inText));
1178
16.2M
            UChar32 c = utext_next32(inText);
1179
16.2M
            U_ASSERT(c != U_SENTINEL);
1180
16.2M
            inString.append(c);
1181
36.3M
            while (inputMap->size() < inString.length()) {
1182
20.1M
                inputMap->addElement(nativePosition, status);
1183
20.1M
            }
1184
16.2M
        }
1185
218k
        inputMap->addElement(limit, status);
1186
218k
    }
1187
1188
1189
5.20M
    if (!nfkcNorm2->isNormalized(inString, status)) {
1190
608k
        UnicodeString normalizedInput;
1191
        //  normalizedMap[normalizedInput position] ==  original UText position.
1192
608k
        LocalPointer<UVector32> normalizedMap(new UVector32(status), status);
1193
608k
        if (U_FAILURE(status)) {
1194
0
            return 0;
1195
0
        }
1196
        
1197
608k
        UnicodeString fragment;
1198
608k
        UnicodeString normalizedFragment;
1199
57.1M
        for (int32_t srcI = 0; srcI < inString.length();) {  // Once per normalization chunk
1200
56.4M
            fragment.remove();
1201
56.4M
            int32_t fragmentStartI = srcI;
1202
56.4M
            UChar32 c = inString.char32At(srcI);
1203
64.3M
            for (;;) {
1204
64.3M
                fragment.append(c);
1205
64.3M
                srcI = inString.moveIndex32(srcI, 1);
1206
64.3M
                if (srcI == inString.length()) {
1207
608k
                    break;
1208
608k
                }
1209
63.7M
                c = inString.char32At(srcI);
1210
63.7M
                if (nfkcNorm2->hasBoundaryBefore(c)) {
1211
55.8M
                    break;
1212
55.8M
                }
1213
63.7M
            }
1214
56.4M
            nfkcNorm2->normalize(fragment, normalizedFragment, status);
1215
56.4M
            normalizedInput.append(normalizedFragment);
1216
1217
            // Map every position in the normalized chunk to the start of the chunk
1218
            //   in the original input.
1219
56.4M
            int32_t fragmentOriginalStart = inputMap.isValid() ?
1220
44.2M
                    inputMap->elementAti(fragmentStartI) : fragmentStartI+rangeStart;
1221
280M
            while (normalizedMap->size() < normalizedInput.length()) {
1222
223M
                normalizedMap->addElement(fragmentOriginalStart, status);
1223
223M
                if (U_FAILURE(status)) {
1224
0
                    break;
1225
0
                }
1226
223M
            }
1227
56.4M
        }
1228
608k
        U_ASSERT(normalizedMap->size() == normalizedInput.length());
1229
608k
        int32_t nativeEnd = inputMap.isValid() ?
1230
481k
                inputMap->elementAti(inString.length()) : inString.length()+rangeStart;
1231
608k
        normalizedMap->addElement(nativeEnd, status);
1232
1233
608k
        inputMap = std::move(normalizedMap);
1234
608k
        inString = std::move(normalizedInput);
1235
608k
    }
1236
1237
5.20M
    int32_t numCodePts = inString.countChar32();
1238
5.20M
    if (numCodePts != inString.length()) {
1239
        // There are supplementary characters in the input.
1240
        // The dictionary will produce boundary positions in terms of code point indexes,
1241
        //   not in terms of code unit string indexes.
1242
        // Use the inputMap mechanism to take care of this in addition to indexing differences
1243
        //    from normalization and/or UTF-8 input.
1244
136k
        UBool hadExistingMap = inputMap.isValid();
1245
136k
        if (!hadExistingMap) {
1246
24.8k
            inputMap.adoptInsteadAndCheckErrorCode(new UVector32(status), status);
1247
24.8k
            if (U_FAILURE(status)) {
1248
0
                return 0;
1249
0
            }
1250
24.8k
        }
1251
136k
        int32_t cpIdx = 0;
1252
53.3M
        for (int32_t cuIdx = 0; ; cuIdx = inString.moveIndex32(cuIdx, 1)) {
1253
53.3M
            U_ASSERT(cuIdx >= cpIdx);
1254
53.3M
            if (hadExistingMap) {
1255
48.0M
                inputMap->setElementAt(inputMap->elementAti(cuIdx), cpIdx);
1256
48.0M
            } else {
1257
5.33M
                inputMap->addElement(cuIdx+rangeStart, status);
1258
5.33M
            }
1259
53.3M
            cpIdx++;
1260
53.3M
            if (cuIdx == inString.length()) {
1261
136k
               break;
1262
136k
            }
1263
53.3M
        }
1264
136k
    }
1265
1266
#if UCONFIG_USE_ML_PHRASE_BREAKING
1267
    // PhraseBreaking is supported in ja and ko; MlBreakEngine only supports ja.
1268
    if (isPhraseBreaking && isCj) {
1269
        return fMlBreakEngine->divideUpRange(inText, rangeStart, rangeEnd, foundBreaks, inString,
1270
                                             inputMap, status);
1271
    }
1272
#endif
1273
1274
    // bestSnlp[i] is the snlp of the best segmentation of the first i
1275
    // code points in the range to be matched.
1276
5.20M
    UVector32 bestSnlp(numCodePts + 1, status);
1277
5.20M
    bestSnlp.addElement(0, status);
1278
275M
    for(int32_t i = 1; i <= numCodePts; i++) {
1279
269M
        bestSnlp.addElement(kuint32max, status);
1280
269M
    }
1281
1282
1283
    // prev[i] is the index of the last CJK code point in the previous word in 
1284
    // the best segmentation of the first i characters.
1285
5.20M
    UVector32 prev(numCodePts + 1, status);
1286
280M
    for(int32_t i = 0; i <= numCodePts; i++){
1287
275M
        prev.addElement(-1, status);
1288
275M
    }
1289
1290
5.20M
    const int32_t maxWordSize = 20;
1291
5.20M
    UVector32 values(numCodePts, status);
1292
5.20M
    values.setSize(numCodePts);
1293
5.20M
    UVector32 lengths(numCodePts, status);
1294
5.20M
    lengths.setSize(numCodePts);
1295
1296
5.20M
    UText fu = UTEXT_INITIALIZER;
1297
5.20M
    utext_openUnicodeString(&fu, &inString, &status);
1298
1299
    // Dynamic programming to find the best segmentation.
1300
1301
    // In outer loop, i  is the code point index,
1302
    //                ix is the corresponding string (code unit) index.
1303
    //    They differ when the string contains supplementary characters.
1304
5.20M
    int32_t ix = 0;
1305
5.20M
    bool is_prev_katakana = false;
1306
275M
    for (int32_t i = 0;  i < numCodePts;  ++i, ix = inString.moveIndex32(ix, 1)) {
1307
269M
        if (static_cast<uint32_t>(bestSnlp.elementAti(i)) == kuint32max) {
1308
0
            continue;
1309
0
        }
1310
1311
269M
        int32_t count;
1312
269M
        utext_setNativeIndex(&fu, ix);
1313
269M
        count = fDictionary->matches(&fu, maxWordSize, numCodePts,
1314
269M
                             nullptr, lengths.getBuffer(), values.getBuffer(), nullptr);
1315
                             // Note: lengths is filled with code point lengths
1316
                             //       The nullptr parameter is the ignored code unit lengths.
1317
1318
        // if there are no single character matches found in the dictionary 
1319
        // starting with this character, treat character as a 1-character word 
1320
        // with the highest value possible, i.e. the least likely to occur.
1321
        // Exclude Korean characters from this treatment, as they should be left
1322
        // together by default.
1323
269M
        if ((count == 0 || lengths.elementAti(0) != 1) &&
1324
192M
                !fHangulWordSet.contains(inString.char32At(ix))) {
1325
192M
            values.setElementAt(maxSnlp, count);   // 255
1326
192M
            lengths.setElementAt(1, count++);
1327
192M
        }
1328
1329
682M
        for (int32_t j = 0; j < count; j++) {
1330
412M
            uint32_t newSnlp = static_cast<uint32_t>(bestSnlp.elementAti(i)) + static_cast<uint32_t>(values.elementAti(j));
1331
412M
            int32_t ln_j_i = lengths.elementAti(j) + i;
1332
412M
            if (newSnlp < static_cast<uint32_t>(bestSnlp.elementAti(ln_j_i))) {
1333
288M
                bestSnlp.setElementAt(newSnlp, ln_j_i);
1334
288M
                prev.setElementAt(i, ln_j_i);
1335
288M
            }
1336
412M
        }
1337
1338
        // In Japanese,
1339
        // Katakana word in single character is pretty rare. So we apply
1340
        // the following heuristic to Katakana: any continuous run of Katakana
1341
        // characters is considered a candidate word with a default cost
1342
        // specified in the katakanaCost table according to its length.
1343
1344
269M
        bool is_katakana = isKatakana(inString.char32At(ix));
1345
269M
        int32_t katakanaRunLength = 1;
1346
269M
        if (!is_prev_katakana && is_katakana) {
1347
8.02M
            int32_t j = inString.moveIndex32(ix, 1);
1348
            // Find the end of the continuous run of Katakana characters
1349
33.0M
            while (j < inString.length() && katakanaRunLength < kMaxKatakanaGroupLength &&
1350
32.6M
                    isKatakana(inString.char32At(j))) {
1351
25.0M
                j = inString.moveIndex32(j, 1);
1352
25.0M
                katakanaRunLength++;
1353
25.0M
            }
1354
8.02M
            if (katakanaRunLength < kMaxKatakanaGroupLength) {
1355
7.86M
                uint32_t newSnlp = bestSnlp.elementAti(i) + getKatakanaCost(katakanaRunLength);
1356
7.86M
                if (newSnlp < static_cast<uint32_t>(bestSnlp.elementAti(i + katakanaRunLength))) {
1357
138k
                    bestSnlp.setElementAt(newSnlp, i+katakanaRunLength);
1358
138k
                    prev.setElementAt(i, i+katakanaRunLength);  // prev[j] = i;
1359
138k
                }
1360
7.86M
            }
1361
8.02M
        }
1362
269M
        is_prev_katakana = is_katakana;
1363
269M
    }
1364
5.20M
    utext_close(&fu);
1365
1366
    // Start pushing the optimal offset index into t_boundary (t for tentative).
1367
    // prev[numCodePts] is guaranteed to be meaningful.
1368
    // We'll first push in the reverse order, i.e.,
1369
    // t_boundary[0] = numCodePts, and afterwards do a swap.
1370
5.20M
    UVector32 t_boundary(numCodePts+1, status);
1371
1372
5.20M
    int32_t numBreaks = 0;
1373
    // No segmentation found, set boundary to end of range
1374
5.20M
    if (static_cast<uint32_t>(bestSnlp.elementAti(numCodePts)) == kuint32max) {
1375
0
        t_boundary.addElement(numCodePts, status);
1376
0
        numBreaks++;
1377
5.20M
    } else if (isPhraseBreaking) {
1378
0
        t_boundary.addElement(numCodePts, status);
1379
0
        if(U_SUCCESS(status)) {
1380
0
            numBreaks++;
1381
0
            int32_t prevIdx = numCodePts;
1382
1383
0
            int32_t codeUnitIdx = -1;
1384
0
            int32_t prevCodeUnitIdx = -1;
1385
0
            int32_t length = -1;
1386
0
            for (int32_t i = prev.elementAti(numCodePts); i > 0; i = prev.elementAti(i)) {
1387
0
                codeUnitIdx = inString.moveIndex32(0, i);
1388
0
                prevCodeUnitIdx = inString.moveIndex32(0, prevIdx);
1389
                // Calculate the length by using the code unit.
1390
0
                length = prevCodeUnitIdx - codeUnitIdx;
1391
0
                prevIdx = i;
1392
                // Keep the breakpoint if the pattern is not in the fSkipSet and continuous Katakana
1393
                // characters don't occur.
1394
0
                if (!fSkipSet.containsKey(inString.tempSubString(codeUnitIdx, length))
1395
0
                    && (!isKatakana(inString.char32At(inString.moveIndex32(codeUnitIdx, -1)))
1396
0
                           || !isKatakana(inString.char32At(codeUnitIdx)))) {
1397
0
                    t_boundary.addElement(i, status);
1398
0
                    numBreaks++;
1399
0
                }
1400
0
            }
1401
0
        }
1402
5.20M
    } else {
1403
138M
        for (int32_t i = numCodePts; i > 0; i = prev.elementAti(i)) {
1404
133M
            t_boundary.addElement(i, status);
1405
133M
            numBreaks++;
1406
133M
        }
1407
5.20M
        U_ASSERT(prev.elementAti(t_boundary.elementAti(numBreaks - 1)) == 0);
1408
5.20M
    }
1409
1410
    // Add a break for the start of the dictionary range if there is not one
1411
    // there already.
1412
5.20M
    if (foundBreaks.size() == 0 || foundBreaks.peeki() < rangeStart) {
1413
5.20M
        t_boundary.addElement(0, status);
1414
5.20M
        numBreaks++;
1415
5.20M
    }
1416
1417
    // Now that we're done, convert positions in t_boundary[] (indices in 
1418
    // the normalized input string) back to indices in the original input UText
1419
    // while reversing t_boundary and pushing values to foundBreaks.
1420
5.20M
    int32_t prevCPPos = -1;
1421
5.20M
    int32_t prevUTextPos = -1;
1422
5.20M
    int32_t correctedNumBreaks = 0;
1423
143M
    for (int32_t i = numBreaks - 1; i >= 0; i--) {
1424
138M
        int32_t cpPos = t_boundary.elementAti(i);
1425
138M
        U_ASSERT(cpPos > prevCPPos);
1426
138M
        int32_t utextPos =  inputMap.isValid() ? inputMap->elementAti(cpPos) : cpPos + rangeStart;
1427
138M
        U_ASSERT(utextPos >= prevUTextPos);
1428
138M
        if (utextPos > prevUTextPos) {
1429
            // Boundaries are added to foundBreaks output in ascending order.
1430
110M
            U_ASSERT(foundBreaks.size() == 0 || foundBreaks.peeki() < utextPos);
1431
            // In phrase breaking, there has to be a breakpoint between Cj character and close
1432
            // punctuation.
1433
            // E.g.[携帯電話]正しい選択 -> [携帯▁電話]▁正しい▁選択 -> breakpoint between ] and 正
1434
110M
            if (utextPos != rangeStart
1435
5.20M
                || (isPhraseBreaking && utextPos > 0
1436
105M
                       && fClosePunctuationSet.contains(utext_char32At(inText, utextPos - 1)))) {
1437
105M
                foundBreaks.push(utextPos, status);
1438
105M
                correctedNumBreaks++;
1439
105M
            }
1440
110M
        } else {
1441
            // Normalization expanded the input text, the dictionary found a boundary
1442
            // within the expansion, giving two boundaries with the same index in the
1443
            // original text. Ignore the second. See ticket #12918.
1444
27.8M
            --numBreaks;
1445
27.8M
        }
1446
138M
        prevCPPos = cpPos;
1447
138M
        prevUTextPos = utextPos;
1448
138M
    }
1449
5.20M
    (void)prevCPPos; // suppress compiler warnings about unused variable
1450
1451
5.20M
    UChar32 nextChar = utext_char32At(inText, rangeEnd);
1452
5.20M
    if (!foundBreaks.isEmpty() && foundBreaks.peeki() == rangeEnd) {
1453
        // In phrase breaking, there has to be a breakpoint between Cj character and
1454
        // the number/open punctuation.
1455
        // E.g. る文字「そうだ、京都」->る▁文字▁「そうだ、▁京都」-> breakpoint between 字 and「
1456
        // E.g. 乗車率90%程度だろうか -> 乗車▁率▁90%▁程度だろうか -> breakpoint between 率 and 9
1457
        // E.g. しかもロゴがUnicode! -> しかも▁ロゴが▁Unicode!-> breakpoint between が and U
1458
5.20M
        if (isPhraseBreaking) {
1459
0
            if (!fDigitOrOpenPunctuationOrAlphabetSet.contains(nextChar)) {
1460
0
                foundBreaks.popi();
1461
0
                correctedNumBreaks--;
1462
0
            }
1463
5.20M
        } else {
1464
5.20M
            foundBreaks.popi();
1465
5.20M
            correctedNumBreaks--;
1466
5.20M
        }
1467
5.20M
    }
1468
1469
    // inString goes out of scope
1470
    // inputMap goes out of scope
1471
5.20M
    return correctedNumBreaks;
1472
5.20M
}
1473
1474
3
void CjkBreakEngine::initJapanesePhraseParameter(UErrorCode& error) {
1475
3
    loadJapaneseExtensions(error);
1476
3
    loadHiragana(error);
1477
3
}
1478
1479
3
void CjkBreakEngine::loadJapaneseExtensions(UErrorCode& error) {
1480
3
    const char* tag = "extensions";
1481
3
    ResourceBundle ja(U_ICUDATA_BRKITR, "ja", error);
1482
3
    if (U_SUCCESS(error)) {
1483
3
        ResourceBundle bundle = ja.get(tag, error);
1484
675
        while (U_SUCCESS(error) && bundle.hasNext()) {
1485
672
            fSkipSet.puti(bundle.getNextString(error), 1, error);
1486
672
        }
1487
3
    }
1488
3
}
1489
1490
3
void CjkBreakEngine::loadHiragana(UErrorCode& error) {
1491
3
    UnicodeSet hiraganaWordSet(UnicodeString(u"[:Hiragana:]"), error);
1492
3
    hiraganaWordSet.compact();
1493
3
    UnicodeSetIterator iterator(hiraganaWordSet);
1494
1.14k
    while (iterator.next()) {
1495
1.14k
        fSkipSet.puti(UnicodeString(iterator.getCodepoint()), 1, error);
1496
1.14k
    }
1497
3
}
1498
#endif
1499
1500
U_NAMESPACE_END
1501
1502
#endif /* #if !UCONFIG_NO_BREAK_ITERATION */
1503