Coverage Report

Created: 2026-09-28 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/common/caniter.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) 1996-2015, International Business Machines Corporation and
6
 * others. All Rights Reserved.
7
 *****************************************************************************
8
 */
9
10
#include "unicode/utypes.h"
11
12
#if !UCONFIG_NO_NORMALIZATION
13
14
#include "unicode/caniter.h"
15
#include "unicode/normalizer2.h"
16
#include "unicode/uchar.h"
17
#include "unicode/uniset.h"
18
#include "unicode/usetiter.h"
19
#include "unicode/ustring.h"
20
#include "unicode/utf16.h"
21
#include "cmemory.h"
22
#include "hash.h"
23
#include "normalizer2impl.h"
24
25
/**
26
 * This class allows one to iterate through all the strings that are canonically equivalent to a given
27
 * string. For example, here are some sample results:
28
Results for: {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
29
1: \u0041\u030A\u0064\u0307\u0327
30
 = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
31
2: \u0041\u030A\u0064\u0327\u0307
32
 = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
33
3: \u0041\u030A\u1E0B\u0327
34
 = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
35
4: \u0041\u030A\u1E11\u0307
36
 = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
37
5: \u00C5\u0064\u0307\u0327
38
 = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
39
6: \u00C5\u0064\u0327\u0307
40
 = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
41
7: \u00C5\u1E0B\u0327
42
 = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
43
8: \u00C5\u1E11\u0307
44
 = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
45
9: \u212B\u0064\u0307\u0327
46
 = {ANGSTROM SIGN}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
47
10: \u212B\u0064\u0327\u0307
48
 = {ANGSTROM SIGN}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
49
11: \u212B\u1E0B\u0327
50
 = {ANGSTROM SIGN}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
51
12: \u212B\u1E11\u0307
52
 = {ANGSTROM SIGN}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
53
 *<br>Note: the code is intended for use with small strings, and is not suitable for larger ones,
54
 * since it has not been optimized for that situation.
55
 *@author M. Davis
56
 *@draft
57
 */
58
59
// public
60
61
U_NAMESPACE_BEGIN
62
63
// TODO: add boilerplate methods.
64
65
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CanonicalIterator)
66
67
68
/**
69
 *@param source string to get results for
70
 */
71
CanonicalIterator::CanonicalIterator(const UnicodeString &sourceStr, UErrorCode &status) :
72
0
    pieces(nullptr),
73
0
    pieces_length(0),
74
0
    pieces_lengths(nullptr),
75
0
    current(nullptr),
76
0
    current_length(0),
77
0
    nfd(Normalizer2::getNFDInstance(status)),
78
0
    nfcImpl(Normalizer2Factory::getNFCImpl(status))
79
0
{
80
0
    if(U_SUCCESS(status) && nfcImpl->ensureCanonIterData(status)) {
81
0
      setSource(sourceStr, status);
82
0
    }
83
0
}
84
85
0
CanonicalIterator::~CanonicalIterator() {
86
0
  cleanPieces();
87
0
}
88
89
0
void CanonicalIterator::cleanPieces() {
90
0
    int32_t i = 0;
91
0
    if(pieces != nullptr) {
92
0
        for(i = 0; i < pieces_length; i++) {
93
0
            if(pieces[i] != nullptr) {
94
0
                delete[] pieces[i];
95
0
            }
96
0
        }
97
0
        uprv_free(pieces);
98
0
        pieces = nullptr;
99
0
        pieces_length = 0;
100
0
    }
101
0
    if(pieces_lengths != nullptr) {
102
0
        uprv_free(pieces_lengths);
103
0
        pieces_lengths = nullptr;
104
0
    }
105
0
    if(current != nullptr) {
106
0
        uprv_free(current);
107
0
        current = nullptr;
108
0
        current_length = 0;
109
0
    }
110
0
}
111
112
/**
113
 *@return gets the source: NOTE: it is the NFD form of source
114
 */
115
0
UnicodeString CanonicalIterator::getSource() {
116
0
  return source;
117
0
}
118
119
/**
120
 * Resets the iterator so that one can start again from the beginning.
121
 */
122
0
void CanonicalIterator::reset() {
123
0
    done = false;
124
0
    for (int i = 0; i < current_length; ++i) {
125
0
        current[i] = 0;
126
0
    }
127
0
}
128
129
/**
130
 *@return the next string that is canonically equivalent. The value null is returned when
131
 * the iteration is done.
132
 */
133
0
UnicodeString CanonicalIterator::next() {
134
0
    int32_t i = 0;
135
136
0
    if (done) {
137
0
      buffer.setToBogus();
138
0
      return buffer;
139
0
    }
140
141
    // delete old contents
142
0
    buffer.remove();
143
144
    // construct return value
145
146
0
    for (i = 0; i < pieces_length; ++i) {
147
0
        buffer.append(pieces[i][current[i]]);
148
0
    }
149
    //String result = buffer.toString(); // not needed
150
151
    // find next value for next time
152
153
0
    for (i = current_length - 1; ; --i) {
154
0
        if (i < 0) {
155
0
            done = true;
156
0
            break;
157
0
        }
158
0
        current[i]++;
159
0
        if (current[i] < pieces_lengths[i]) break; // got sequence
160
0
        current[i] = 0;
161
0
    }
162
0
    return buffer;
163
0
}
164
165
/**
166
 *@param set the source string to iterate against. This allows the same iterator to be used
167
 * while changing the source string, saving object creation.
168
 */
169
0
void CanonicalIterator::setSource(const UnicodeString &newSource, UErrorCode &status) {
170
0
    int32_t list_length = 0;
171
0
    UChar32 cp = 0;
172
0
    int32_t start = 0;
173
0
    int32_t i = 0;
174
0
    UnicodeString *list = nullptr;
175
176
0
    nfd->normalize(newSource, source, status);
177
0
    if(U_FAILURE(status)) {
178
0
      return;
179
0
    }
180
0
    done = false;
181
182
0
    cleanPieces();
183
184
    // catch degenerate case
185
0
    if (newSource.length() == 0) {
186
0
        pieces = static_cast<UnicodeString**>(uprv_malloc(sizeof(UnicodeString*)));
187
0
        pieces_lengths = static_cast<int32_t*>(uprv_malloc(1 * sizeof(int32_t)));
188
0
        pieces_length = 1;
189
0
        current = static_cast<int32_t*>(uprv_malloc(1 * sizeof(int32_t)));
190
0
        current_length = 1;
191
0
        if (pieces == nullptr || pieces_lengths == nullptr || current == nullptr) {
192
0
            status = U_MEMORY_ALLOCATION_ERROR;
193
0
            goto CleanPartialInitialization;
194
0
        }
195
0
        current[0] = 0;
196
0
        pieces[0] = new UnicodeString[1];
197
0
        pieces_lengths[0] = 1;
198
0
        if (pieces[0] == nullptr) {
199
0
            status = U_MEMORY_ALLOCATION_ERROR;
200
0
            goto CleanPartialInitialization;
201
0
        }
202
0
        return;
203
0
    }
204
205
206
0
    list = new UnicodeString[source.length()];
207
0
    if (list == nullptr) {
208
0
        status = U_MEMORY_ALLOCATION_ERROR;
209
0
        goto CleanPartialInitialization;
210
0
    }
211
212
    // i should initially be the number of code units at the 
213
    // start of the string
214
0
    i = U16_LENGTH(source.char32At(0));
215
    // int32_t i = 1;
216
    // find the segments
217
    // This code iterates through the source string and 
218
    // extracts segments that end up on a codepoint that
219
    // doesn't start any decompositions. (Analysis is done
220
    // on the NFD form - see above).
221
0
    for (; i < source.length(); i += U16_LENGTH(cp)) {
222
0
        cp = source.char32At(i);
223
0
        if (nfcImpl->isCanonSegmentStarter(cp)) {
224
0
            source.extract(start, i-start, list[list_length++]); // add up to i
225
0
            start = i;
226
0
        }
227
0
    }
228
0
    source.extract(start, i-start, list[list_length++]); // add last one
229
230
231
    // allocate the arrays, and find the strings that are CE to each segment
232
0
    pieces = static_cast<UnicodeString**>(uprv_malloc(list_length * sizeof(UnicodeString*)));
233
0
    pieces_length = list_length;
234
0
    pieces_lengths = static_cast<int32_t*>(uprv_malloc(list_length * sizeof(int32_t)));
235
0
    current = static_cast<int32_t*>(uprv_malloc(list_length * sizeof(int32_t)));
236
0
    current_length = list_length;
237
0
    if (pieces == nullptr || pieces_lengths == nullptr || current == nullptr) {
238
0
        status = U_MEMORY_ALLOCATION_ERROR;
239
0
        goto CleanPartialInitialization;
240
0
    }
241
242
0
    for (i = 0; i < current_length; i++) {
243
0
        current[i] = 0;
244
0
    }
245
    // for each segment, get all the combinations that can produce 
246
    // it after NFD normalization
247
0
    for (i = 0; i < pieces_length; ++i) {
248
        //if (PROGRESS) printf("SEGMENT\n");
249
0
        pieces[i] = getEquivalents(list[i], pieces_lengths[i], status);
250
0
    }
251
252
0
    delete[] list;
253
0
    return;
254
// Common section to cleanup all local variables and reset object variables.
255
0
CleanPartialInitialization:
256
0
    delete[] list;
257
0
    cleanPieces();
258
0
}
259
260
/**
261
 * Dumb recursive implementation of permutation.
262
 * TODO: optimize
263
 * @param source the string to find permutations for
264
 * @return the results in a set.
265
 */
266
0
void U_EXPORT2 CanonicalIterator::permute(UnicodeString &source, UBool skipZeros, Hashtable *result, UErrorCode &status, int32_t depth) {
267
0
    if(U_FAILURE(status)) {
268
0
        return;
269
0
    }
270
    // To avoid infinity loop caused by permute, we limit the depth of recursive
271
    // call to permute and return U_UNSUPPORTED_ERROR.
272
    // We know in some unit test we need at least 4. Set to 8 just in case some
273
    // unforseen use cases.
274
0
    constexpr int32_t kPermuteDepthLimit = 8;
275
0
    if (depth > kPermuteDepthLimit) {
276
0
        status = U_UNSUPPORTED_ERROR;
277
0
        return;
278
0
    }
279
    //if (PROGRESS) printf("Permute: %s\n", UToS(Tr(source)));
280
0
    int32_t i = 0;
281
282
    // optimization:
283
    // if zero or one character, just return a set with it
284
    // we check for length < 2 to keep from counting code points all the time
285
0
    if (source.length() <= 2 && source.countChar32() <= 1) {
286
0
        UnicodeString *toPut = new UnicodeString(source);
287
        /* test for nullptr */
288
0
        if (toPut == nullptr) {
289
0
            status = U_MEMORY_ALLOCATION_ERROR;
290
0
            return;
291
0
        }
292
0
        result->put(source, toPut, status);
293
0
        return;
294
0
    }
295
296
    // otherwise iterate through the string, and recursively permute all the other characters
297
0
    UChar32 cp;
298
0
    Hashtable subpermute(status);
299
0
    if(U_FAILURE(status)) {
300
0
        return;
301
0
    }
302
0
    subpermute.setValueDeleter(uprv_deleteUObject);
303
304
0
    for (i = 0; i < source.length(); i += U16_LENGTH(cp)) {
305
0
        cp = source.char32At(i);
306
0
        const UHashElement *ne = nullptr;
307
0
        int32_t el = UHASH_FIRST;
308
0
        UnicodeString subPermuteString = source;
309
310
        // optimization:
311
        // if the character is canonical combining class zero,
312
        // don't permute it
313
0
        if (skipZeros && i != 0 && u_getCombiningClass(cp) == 0) {
314
            //System.out.println("Skipping " + Utility.hex(UTF16.valueOf(source, i)));
315
0
            continue;
316
0
        }
317
318
0
        subpermute.removeAll();
319
320
        // see what the permutations of the characters before and after this one are
321
        //Hashtable *subpermute = permute(source.substring(0,i) + source.substring(i + UTF16.getCharCount(cp)));
322
0
        permute(subPermuteString.remove(i, U16_LENGTH(cp)), skipZeros, &subpermute, status, depth+1);
323
        /* Test for buffer overflows */
324
0
        if(U_FAILURE(status)) {
325
0
            return;
326
0
        }
327
        // The upper remove is destructive. The question is do we have to make a copy, or we don't care about the contents 
328
        // of source at this point.
329
330
        // prefix this character to all of them
331
0
        ne = subpermute.nextElement(el);
332
0
        while (ne != nullptr) {
333
0
            UnicodeString* permRes = static_cast<UnicodeString*>(ne->value.pointer);
334
0
            UnicodeString *chStr = new UnicodeString(cp);
335
            //test for nullptr
336
0
            if (chStr == nullptr) {
337
0
                status = U_MEMORY_ALLOCATION_ERROR;
338
0
                return;
339
0
            }
340
0
            chStr->append(*permRes); //*((UnicodeString *)(ne->value.pointer));
341
            //if (PROGRESS) printf("  Piece: %s\n", UToS(*chStr));
342
0
            result->put(*chStr, chStr, status);
343
0
            ne = subpermute.nextElement(el);
344
0
        }
345
0
    }
346
    //return result;
347
0
}
348
349
// privates
350
351
// we have a segment, in NFD. Find all the strings that are canonically equivalent to it.
352
0
UnicodeString* CanonicalIterator::getEquivalents(const UnicodeString &segment, int32_t &result_len, UErrorCode &status) {
353
0
    Hashtable result(status);
354
0
    Hashtable permutations(status);
355
0
    Hashtable basic(status);
356
0
    if (U_FAILURE(status)) {
357
0
        return nullptr;
358
0
    }
359
0
    result.setValueDeleter(uprv_deleteUObject);
360
0
    permutations.setValueDeleter(uprv_deleteUObject);
361
0
    basic.setValueDeleter(uprv_deleteUObject);
362
363
0
    char16_t USeg[256];
364
0
    int32_t segLen = segment.extract(USeg, 256, status);
365
0
    getEquivalents2(&basic, USeg, segLen, status);
366
0
    if (U_FAILURE(status)) {
367
0
        return nullptr;
368
0
    }
369
370
    // now get all the permutations
371
    // add only the ones that are canonically equivalent
372
    // TODO: optimize by not permuting any class zero.
373
374
0
    const UHashElement *ne = nullptr;
375
0
    int32_t el = UHASH_FIRST;
376
    //Iterator it = basic.iterator();
377
0
    ne = basic.nextElement(el);
378
    //while (it.hasNext())
379
0
    while (ne != nullptr) {
380
        //String item = (String) it.next();
381
0
        UnicodeString item = *static_cast<UnicodeString*>(ne->value.pointer);
382
383
0
        permutations.removeAll();
384
0
        permute(item, CANITER_SKIP_ZEROES, &permutations, status);
385
0
        const UHashElement *ne2 = nullptr;
386
0
        int32_t el2 = UHASH_FIRST;
387
        //Iterator it2 = permutations.iterator();
388
0
        ne2 = permutations.nextElement(el2);
389
        //while (it2.hasNext())
390
0
        while (ne2 != nullptr) {
391
            //String possible = (String) it2.next();
392
            //UnicodeString *possible = new UnicodeString(*((UnicodeString *)(ne2->value.pointer)));
393
0
            UnicodeString possible(*static_cast<UnicodeString*>(ne2->value.pointer));
394
0
            UnicodeString attempt;
395
0
            nfd->normalize(possible, attempt, status);
396
397
            // TODO: check if operator == is semanticaly the same as attempt.equals(segment)
398
0
            if (attempt==segment) {
399
                //if (PROGRESS) printf("Adding Permutation: %s\n", UToS(Tr(*possible)));
400
                // TODO: use the hashtable just to catch duplicates - store strings directly (somehow).
401
0
                result.put(possible, new UnicodeString(possible), status); //add(possible);
402
0
            } else {
403
                //if (PROGRESS) printf("-Skipping Permutation: %s\n", UToS(Tr(*possible)));
404
0
            }
405
406
0
            ne2 = permutations.nextElement(el2);
407
0
        }
408
0
        ne = basic.nextElement(el);
409
0
    }
410
411
    /* Test for buffer overflows */
412
0
    if(U_FAILURE(status)) {
413
0
        return nullptr;
414
0
    }
415
    // convert into a String[] to clean up storage
416
    //String[] finalResult = new String[result.size()];
417
0
    UnicodeString *finalResult = nullptr;
418
0
    int32_t resultCount;
419
0
    if((resultCount = result.count()) != 0) {
420
0
        finalResult = new UnicodeString[resultCount];
421
0
        if (finalResult == nullptr) {
422
0
            status = U_MEMORY_ALLOCATION_ERROR;
423
0
            return nullptr;
424
0
        }
425
0
    }
426
0
    else {
427
0
        status = U_ILLEGAL_ARGUMENT_ERROR;
428
0
        return nullptr;
429
0
    }
430
    //result.toArray(finalResult);
431
0
    result_len = 0;
432
0
    el = UHASH_FIRST;
433
0
    ne = result.nextElement(el);
434
0
    while(ne != nullptr) {
435
0
        finalResult[result_len++] = *static_cast<UnicodeString*>(ne->value.pointer);
436
0
        ne = result.nextElement(el);
437
0
    }
438
439
440
0
    return finalResult;
441
0
}
442
443
Hashtable* CanonicalIterator::getEquivalents2(Hashtable* fillinResult U_LIFETIME_BOUND,
444
                                              const char16_t* segment,
445
                                              int32_t segLen,
446
0
                                              UErrorCode& status) {
447
0
    if (U_FAILURE(status)) {
448
0
        return nullptr;
449
0
    }
450
451
    //if (PROGRESS) printf("Adding: %s\n", UToS(Tr(segment)));
452
453
0
    UnicodeString toPut(segment, segLen);
454
455
0
    fillinResult->put(toPut, new UnicodeString(toPut), status);
456
457
0
    UnicodeSet starts;
458
459
    // cycle through all the characters
460
0
    UChar32 cp;
461
0
    for (int32_t i = 0; i < segLen; i += U16_LENGTH(cp)) {
462
        // see if any character is at the start of some decomposition
463
0
        U16_GET(segment, 0, i, segLen, cp);
464
0
        if (!nfcImpl->getCanonStartSet(cp, starts)) {
465
0
            continue;
466
0
        }
467
        // if so, see which decompositions match
468
0
        UnicodeSetIterator iter(starts);
469
0
        while (iter.next()) {
470
0
            UChar32 cp2 = iter.getCodepoint();
471
0
            Hashtable remainder(status);
472
0
            remainder.setValueDeleter(uprv_deleteUObject);
473
0
            if (extract(&remainder, cp2, segment, segLen, i, status) == nullptr) {
474
0
                if (U_FAILURE(status)) {
475
0
                    return nullptr;
476
0
                }
477
0
                continue;
478
0
            }
479
480
            // there were some matches, so add all the possibilities to the set.
481
0
            UnicodeString prefix(segment, i);
482
0
            prefix += cp2;
483
484
0
            int32_t el = UHASH_FIRST;
485
0
            const UHashElement *ne = remainder.nextElement(el);
486
0
            while (ne != nullptr) {
487
0
                UnicodeString item = *static_cast<UnicodeString*>(ne->value.pointer);
488
0
                UnicodeString *toAdd = new UnicodeString(prefix);
489
                /* test for nullptr */
490
0
                if (toAdd == nullptr) {
491
0
                    status = U_MEMORY_ALLOCATION_ERROR;
492
0
                    return nullptr;
493
0
                }
494
0
                *toAdd += item;
495
0
                fillinResult->put(*toAdd, toAdd, status);
496
497
                //if (PROGRESS) printf("Adding: %s\n", UToS(Tr(*toAdd)));
498
499
0
                ne = remainder.nextElement(el);
500
0
            }
501
            // ICU-22642 Guards against strings that have so many permutations
502
            // that they would otherwise hang the function.
503
0
            constexpr int32_t kResultLimit = 4096;
504
0
            if (fillinResult->count() > kResultLimit) {
505
0
                status = U_UNSUPPORTED_ERROR;
506
0
                return nullptr;
507
0
            }
508
0
        }
509
0
    }
510
511
    /* Test for buffer overflows */
512
0
    if(U_FAILURE(status)) {
513
0
        return nullptr;
514
0
    }
515
0
    return fillinResult;
516
0
}
517
518
/**
519
 * See if the decomposition of cp2 is at segment starting at segmentPos 
520
 * (with canonical rearrangement!)
521
 * If so, take the remainder, and return the equivalents 
522
 */
523
Hashtable* CanonicalIterator::extract(Hashtable* fillinResult U_LIFETIME_BOUND,
524
                                      UChar32 comp,
525
                                      const char16_t* segment,
526
                                      int32_t segLen,
527
                                      int32_t segmentPos,
528
0
                                      UErrorCode& status) {
529
//Hashtable *CanonicalIterator::extract(UChar32 comp, const UnicodeString &segment, int32_t segLen, int32_t segmentPos, UErrorCode &status) {
530
    //if (PROGRESS) printf(" extract: %s, ", UToS(Tr(UnicodeString(comp))));
531
    //if (PROGRESS) printf("%s, %i\n", UToS(Tr(segment)), segmentPos);
532
533
0
    if (U_FAILURE(status)) {
534
0
        return nullptr;
535
0
    }
536
537
0
    UnicodeString temp(comp);
538
0
    int32_t inputLen=temp.length();
539
0
    UnicodeString decompString;
540
0
    nfd->normalize(temp, decompString, status);
541
0
    if (U_FAILURE(status)) {
542
0
        return nullptr;
543
0
    }
544
0
    if (decompString.isBogus()) {
545
0
        status = U_MEMORY_ALLOCATION_ERROR;
546
0
        return nullptr;
547
0
    }
548
0
    const char16_t *decomp=decompString.getBuffer();
549
0
    int32_t decompLen=decompString.length();
550
551
    // See if it matches the start of segment (at segmentPos)
552
0
    UBool ok = false;
553
0
    UChar32 cp;
554
0
    int32_t decompPos = 0;
555
0
    UChar32 decompCp;
556
0
    U16_NEXT(decomp, decompPos, decompLen, decompCp);
557
558
0
    int32_t i = segmentPos;
559
0
    while(i < segLen) {
560
0
        U16_NEXT(segment, i, segLen, cp);
561
562
0
        if (cp == decompCp) { // if equal, eat another cp from decomp
563
564
            //if (PROGRESS) printf("  matches: %s\n", UToS(Tr(UnicodeString(cp))));
565
566
0
            if (decompPos == decompLen) { // done, have all decomp characters!
567
0
                temp.append(segment+i, segLen-i);
568
0
                ok = true;
569
0
                break;
570
0
            }
571
0
            U16_NEXT(decomp, decompPos, decompLen, decompCp);
572
0
        } else {
573
            //if (PROGRESS) printf("  buffer: %s\n", UToS(Tr(UnicodeString(cp))));
574
575
            // brute force approach
576
0
            temp.append(cp);
577
578
            /* TODO: optimize
579
            // since we know that the classes are monotonically increasing, after zero
580
            // e.g. 0 5 7 9 0 3
581
            // we can do an optimization
582
            // there are only a few cases that work: zero, less, same, greater
583
            // if both classes are the same, we fail
584
            // if the decomp class < the segment class, we fail
585
586
            segClass = getClass(cp);
587
            if (decompClass <= segClass) return null;
588
            */
589
0
        }
590
0
    }
591
0
    if (!ok)
592
0
        return nullptr; // we failed, characters left over
593
594
    //if (PROGRESS) printf("Matches\n");
595
596
0
    if (inputLen == temp.length()) {
597
0
        fillinResult->put(UnicodeString(), new UnicodeString(), status);
598
0
        return fillinResult; // succeed, but no remainder
599
0
    }
600
601
    // brute force approach
602
    // check to make sure result is canonically equivalent
603
0
    UnicodeString trial;
604
0
    nfd->normalize(temp, trial, status);
605
0
    if(U_FAILURE(status) || trial.compare(segment+segmentPos, segLen - segmentPos) != 0) {
606
0
        return nullptr;
607
0
    }
608
609
0
    return getEquivalents2(fillinResult, temp.getBuffer()+inputLen, temp.length()-inputLen, status);
610
0
}
611
612
U_NAMESPACE_END
613
614
#endif /* #if !UCONFIG_NO_NORMALIZATION */