1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2012-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * utf8collationiterator.cpp
10 * created on: 2012nov12 (from utf16collationiterator.cpp & uitercollationiterator.cpp)
11 * created by: Markus W. Scherer
14 #include "unicode/utypes.h"
16 #if !UCONFIG_NO_COLLATION
18 #include "unicode/utf8.h"
21 #include "collation.h"
22 #include "collationdata.h"
23 #include "collationfcd.h"
24 #include "collationiterator.h"
25 #include "normalizer2impl.h"
27 #include "utf8collationiterator.h"
31 UTF8CollationIterator::~UTF8CollationIterator() {}
34 UTF8CollationIterator::resetToOffset(int32_t newOffset) {
40 UTF8CollationIterator::getOffset() const {
45 UTF8CollationIterator::handleNextCE32(UChar32 &c, UErrorCode & /*errorCode*/) {
48 return Collation::FALLBACK_CE32;
50 // Optimized combination of U8_NEXT_OR_FFFD() and UTRIE2_U8_NEXT32().
53 // ASCII 00..7F; trail bytes 80..BF map to error values.
54 return trie->data32[c];
57 if(c < 0xe0 && pos != length && (t1 = (u8[pos] - 0x80)) <= 0x3f) {
58 // U+0080..U+07FF; 00..7F map to error values.
59 uint32_t ce32 = trie->data32[trie->index[(UTRIE2_UTF8_2B_INDEX_2_OFFSET - 0xc0) + c] + t1];
60 c = ((c & 0x1f) << 6) | t1;
63 } else if(c <= 0xef &&
64 ((pos + 1) < length || length < 0) &&
65 (t1 = (u8[pos] - 0x80)) <= 0x3f && (c != 0xe0 || t1 >= 0x20) &&
66 (t2 = (u8[pos + 1] - 0x80)) <= 0x3f
68 // U+0800..U+FFFF; caller maps surrogates to error values.
69 c = (UChar)((c << 12) | (t1 << 6) | t2);
71 return UTRIE2_GET32_FROM_U16_SINGLE_LEAD(trie, c);
73 // Function call for supplementary code points and error cases.
74 // Illegal byte sequences yield U+FFFD.
75 c = utf8_nextCharSafeBody(u8, &pos, length, c, -3);
76 return data->getCE32(c);
81 UTF8CollationIterator::foundNULTerminator() {
91 UTF8CollationIterator::forbidSurrogateCodePoints() const {
96 UTF8CollationIterator::nextCodePoint(UErrorCode & /*errorCode*/) {
100 if(u8[pos] == 0 && length < 0) {
105 U8_NEXT_OR_FFFD(u8, pos, length, c);
110 UTF8CollationIterator::previousCodePoint(UErrorCode & /*errorCode*/) {
115 U8_PREV_OR_FFFD(u8, 0, pos, c);
120 UTF8CollationIterator::forwardNumCodePoints(int32_t num, UErrorCode & /*errorCode*/) {
121 U8_FWD_N(u8, pos, length, num);
125 UTF8CollationIterator::backwardNumCodePoints(int32_t num, UErrorCode & /*errorCode*/) {
126 U8_BACK_N(u8, 0, pos, num);
129 // FCDUTF8CollationIterator ------------------------------------------------ ***
131 FCDUTF8CollationIterator::~FCDUTF8CollationIterator() {}
134 FCDUTF8CollationIterator::resetToOffset(int32_t newOffset) {
136 start = pos = newOffset;
141 FCDUTF8CollationIterator::getOffset() const {
142 if(state != IN_NORMALIZED) {
144 } else if(pos == 0) {
152 FCDUTF8CollationIterator::handleNextCE32(UChar32 &c, UErrorCode &errorCode) {
154 if(state == CHECK_FWD) {
155 // Combination of UTF8CollationIterator::handleNextCE32() with FCD check fastpath.
158 return Collation::FALLBACK_CE32;
162 // ASCII 00..7F; trail bytes 80..BF map to error values.
163 return trie->data32[c];
166 if(c < 0xe0 && pos != length && (t1 = (u8[pos] - 0x80)) <= 0x3f) {
167 // U+0080..U+07FF; 00..7F map to error values.
168 uint32_t ce32 = trie->data32[trie->index[(UTRIE2_UTF8_2B_INDEX_2_OFFSET - 0xc0) + c] + t1];
169 c = ((c & 0x1f) << 6) | t1;
171 if(CollationFCD::hasTccc(c) && pos != length && nextHasLccc()) {
176 } else if(c <= 0xef &&
177 ((pos + 1) < length || length < 0) &&
178 (t1 = (u8[pos] - 0x80)) <= 0x3f && (c != 0xe0 || t1 >= 0x20) &&
179 (t2 = (u8[pos + 1] - 0x80)) <= 0x3f
181 // U+0800..U+FFFF; caller maps surrogates to error values.
182 c = (UChar)((c << 12) | (t1 << 6) | t2);
184 if(CollationFCD::hasTccc(c) &&
185 (CollationFCD::maybeTibetanCompositeVowel(c) ||
186 (pos != length && nextHasLccc()))) {
189 break; // return CE32(BMP)
192 // Function call for supplementary code points and error cases.
193 // Illegal byte sequences yield U+FFFD.
194 c = utf8_nextCharSafeBody(u8, &pos, length, c, -3);
196 return Collation::FFFD_CE32;
198 U_ASSERT(c > 0xffff);
199 if(CollationFCD::hasTccc(U16_LEAD(c)) && pos != length && nextHasLccc()) {
202 return data->getCE32FromSupplementary(c);
206 if(!nextSegment(errorCode)) {
208 return Collation::FALLBACK_CE32;
211 } else if(state == IN_FCD_SEGMENT && pos != limit) {
212 return UTF8CollationIterator::handleNextCE32(c, errorCode);
213 } else if(state == IN_NORMALIZED && pos != normalized.length()) {
214 c = normalized[pos++];
220 return UTRIE2_GET32_FROM_U16_SINGLE_LEAD(trie, c);
224 FCDUTF8CollationIterator::nextHasLccc() const {
225 U_ASSERT(state == CHECK_FWD && pos != length);
226 // The lowest code point with ccc!=0 is U+0300 which is CC 80 in UTF-8.
227 // CJK U+4000..U+DFFF except U+Axxx are also FCD-inert. (Lead bytes E4..ED except EA.)
229 if(c < 0xcc || (0xe4 <= c && c <= 0xed && c != 0xea)) { return FALSE; }
231 U8_NEXT_OR_FFFD(u8, i, length, c);
232 if(c > 0xffff) { c = U16_LEAD(c); }
233 return CollationFCD::hasLccc(c);
237 FCDUTF8CollationIterator::previousHasTccc() const {
238 U_ASSERT(state == CHECK_BWD && pos != 0);
239 UChar32 c = u8[pos - 1];
240 if(c < 0x80) { return FALSE; }
242 U8_PREV_OR_FFFD(u8, 0, i, c);
243 if(c > 0xffff) { c = U16_LEAD(c); }
244 return CollationFCD::hasTccc(c);
248 FCDUTF8CollationIterator::handleGetTrailSurrogate() {
249 if(state != IN_NORMALIZED) { return 0; }
250 U_ASSERT(pos < normalized.length());
252 if(U16_IS_TRAIL(trail = normalized[pos])) { ++pos; }
257 FCDUTF8CollationIterator::foundNULTerminator() {
258 if(state == CHECK_FWD && length < 0) {
267 FCDUTF8CollationIterator::nextCodePoint(UErrorCode &errorCode) {
270 if(state == CHECK_FWD) {
271 if(pos == length || ((c = u8[pos]) == 0 && length < 0)) {
278 U8_NEXT_OR_FFFD(u8, pos, length, c);
279 if(CollationFCD::hasTccc(c <= 0xffff ? c : U16_LEAD(c)) &&
280 (CollationFCD::maybeTibetanCompositeVowel(c) ||
281 (pos != length && nextHasLccc()))) {
282 // c is not FCD-inert, therefore it is not U+FFFD and it has a valid byte sequence
283 // and we can use U8_LENGTH() rather than a previous-position variable.
285 if(!nextSegment(errorCode)) {
291 } else if(state == IN_FCD_SEGMENT && pos != limit) {
292 U8_NEXT_OR_FFFD(u8, pos, length, c);
294 } else if(state == IN_NORMALIZED && pos != normalized.length()) {
295 c = normalized.char32At(pos);
296 pos += U16_LENGTH(c);
305 FCDUTF8CollationIterator::previousCodePoint(UErrorCode &errorCode) {
308 if(state == CHECK_BWD) {
312 if((c = u8[pos - 1]) < 0x80) {
316 U8_PREV_OR_FFFD(u8, 0, pos, c);
317 if(CollationFCD::hasLccc(c <= 0xffff ? c : U16_LEAD(c)) &&
318 (CollationFCD::maybeTibetanCompositeVowel(c) ||
319 (pos != 0 && previousHasTccc()))) {
320 // c is not FCD-inert, therefore it is not U+FFFD and it has a valid byte sequence
321 // and we can use U8_LENGTH() rather than a previous-position variable.
323 if(!previousSegment(errorCode)) {
329 } else if(state == IN_FCD_SEGMENT && pos != start) {
330 U8_PREV_OR_FFFD(u8, 0, pos, c);
332 } else if(state >= IN_NORMALIZED && pos != 0) {
333 c = normalized.char32At(pos - 1);
334 pos -= U16_LENGTH(c);
343 FCDUTF8CollationIterator::forwardNumCodePoints(int32_t num, UErrorCode &errorCode) {
344 // Specify the class to avoid a virtual-function indirection.
345 // In Java, we would declare this class final.
346 while(num > 0 && FCDUTF8CollationIterator::nextCodePoint(errorCode) >= 0) {
352 FCDUTF8CollationIterator::backwardNumCodePoints(int32_t num, UErrorCode &errorCode) {
353 // Specify the class to avoid a virtual-function indirection.
354 // In Java, we would declare this class final.
355 while(num > 0 && FCDUTF8CollationIterator::previousCodePoint(errorCode) >= 0) {
361 FCDUTF8CollationIterator::switchToForward() {
362 U_ASSERT(state == CHECK_BWD ||
363 (state == IN_FCD_SEGMENT && pos == limit) ||
364 (state == IN_NORMALIZED && pos == normalized.length()));
365 if(state == CHECK_BWD) {
366 // Turn around from backward checking.
369 state = CHECK_FWD; // Check forward.
370 } else { // pos < limit
371 state = IN_FCD_SEGMENT; // Stay in FCD segment.
374 // Reached the end of the FCD segment.
375 if(state == IN_FCD_SEGMENT) {
376 // The input text segment is FCD, extend it forward.
378 // The input text segment needed to be normalized.
379 // Switch to checking forward from it.
387 FCDUTF8CollationIterator::nextSegment(UErrorCode &errorCode) {
388 if(U_FAILURE(errorCode)) { return FALSE; }
389 U_ASSERT(state == CHECK_FWD && pos != length);
390 // The input text [start..pos[ passes the FCD check.
391 int32_t segmentStart = pos;
392 // Collect the characters being checked, in case they need to be normalized.
396 // Fetch the next character and its fcd16 value.
397 int32_t cpStart = pos;
399 U8_NEXT_OR_FFFD(u8, pos, length, c);
400 uint16_t fcd16 = nfcImpl.getFCD16(c);
401 uint8_t leadCC = (uint8_t)(fcd16 >> 8);
402 if(leadCC == 0 && cpStart != segmentStart) {
403 // FCD boundary before this character.
408 if(leadCC != 0 && (prevCC > leadCC || CollationFCD::isFCD16OfTibetanCompositeVowel(fcd16))) {
409 // Fails FCD check. Find the next FCD boundary and normalize.
410 while(pos != length) {
412 U8_NEXT_OR_FFFD(u8, pos, length, c);
413 if(nfcImpl.getFCD16(c) <= 0xff) {
419 if(!normalize(s, errorCode)) { return FALSE; }
420 start = segmentStart;
422 state = IN_NORMALIZED;
426 prevCC = (uint8_t)fcd16;
427 if(pos == length || prevCC == 0) {
428 // FCD boundary after the last character.
434 U_ASSERT(pos != limit);
435 state = IN_FCD_SEGMENT;
440 FCDUTF8CollationIterator::switchToBackward() {
441 U_ASSERT(state == CHECK_FWD ||
442 (state == IN_FCD_SEGMENT && pos == start) ||
443 (state >= IN_NORMALIZED && pos == 0));
444 if(state == CHECK_FWD) {
445 // Turn around from forward checking.
448 state = CHECK_BWD; // Check backward.
449 } else { // pos > start
450 state = IN_FCD_SEGMENT; // Stay in FCD segment.
453 // Reached the start of the FCD segment.
454 if(state == IN_FCD_SEGMENT) {
455 // The input text segment is FCD, extend it backward.
457 // The input text segment needed to be normalized.
458 // Switch to checking backward from it.
466 FCDUTF8CollationIterator::previousSegment(UErrorCode &errorCode) {
467 if(U_FAILURE(errorCode)) { return FALSE; }
468 U_ASSERT(state == CHECK_BWD && pos != 0);
469 // The input text [pos..limit[ passes the FCD check.
470 int32_t segmentLimit = pos;
471 // Collect the characters being checked, in case they need to be normalized.
475 // Fetch the previous character and its fcd16 value.
476 int32_t cpLimit = pos;
478 U8_PREV_OR_FFFD(u8, 0, pos, c);
479 uint16_t fcd16 = nfcImpl.getFCD16(c);
480 uint8_t trailCC = (uint8_t)fcd16;
481 if(trailCC == 0 && cpLimit != segmentLimit) {
482 // FCD boundary after this character.
487 if(trailCC != 0 && ((nextCC != 0 && trailCC > nextCC) ||
488 CollationFCD::isFCD16OfTibetanCompositeVowel(fcd16))) {
489 // Fails FCD check. Find the previous FCD boundary and normalize.
490 while(fcd16 > 0xff && pos != 0) {
492 U8_PREV_OR_FFFD(u8, 0, pos, c);
493 fcd16 = nfcImpl.getFCD16(c);
501 if(!normalize(s, errorCode)) { return FALSE; }
502 limit = segmentLimit;
504 state = IN_NORMALIZED;
505 pos = normalized.length();
508 nextCC = (uint8_t)(fcd16 >> 8);
509 if(pos == 0 || nextCC == 0) {
510 // FCD boundary before the following character.
516 U_ASSERT(pos != start);
517 state = IN_FCD_SEGMENT;
522 FCDUTF8CollationIterator::normalize(const UnicodeString &s, UErrorCode &errorCode) {
523 // NFD without argument checking.
524 U_ASSERT(U_SUCCESS(errorCode));
525 nfcImpl.decompose(s, normalized, errorCode);
526 return U_SUCCESS(errorCode);
531 #endif // !UCONFIG_NO_COLLATION