1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2010-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * utf16collationiterator.cpp
10 * created on: 2010oct27
11 * created by: Markus W. Scherer
14 #include "unicode/utypes.h"
16 #if !UCONFIG_NO_COLLATION
20 #include "collation.h"
21 #include "collationdata.h"
22 #include "collationfcd.h"
23 #include "collationiterator.h"
24 #include "normalizer2impl.h"
26 #include "utf16collationiterator.h"
30 UTF16CollationIterator::UTF16CollationIterator(const UTF16CollationIterator
&other
,
32 : CollationIterator(other
),
34 pos(newText
+ (other
.pos
- other
.start
)),
35 limit(other
.limit
== NULL
? NULL
: newText
+ (other
.limit
- other
.start
)) {
38 UTF16CollationIterator::~UTF16CollationIterator() {}
41 UTF16CollationIterator::operator==(const CollationIterator
&other
) const {
42 if(!CollationIterator::operator==(other
)) { return FALSE
; }
43 const UTF16CollationIterator
&o
= static_cast<const UTF16CollationIterator
&>(other
);
44 // Compare the iterator state but not the text: Assume that the caller does that.
45 return (pos
- start
) == (o
.pos
- o
.start
);
49 UTF16CollationIterator::resetToOffset(int32_t newOffset
) {
51 pos
= start
+ newOffset
;
55 UTF16CollationIterator::getOffset() const {
56 return (int32_t)(pos
- start
);
60 UTF16CollationIterator::handleNextCE32(UChar32
&c
, UErrorCode
& /*errorCode*/) {
63 return Collation::FALLBACK_CE32
;
66 return UTRIE2_GET32_FROM_U16_SINGLE_LEAD(trie
, c
);
70 UTF16CollationIterator::handleGetTrailSurrogate() {
71 if(pos
== limit
) { return 0; }
73 if(U16_IS_TRAIL(trail
= *pos
)) { ++pos
; }
78 UTF16CollationIterator::foundNULTerminator() {
88 UTF16CollationIterator::nextCodePoint(UErrorCode
& /*errorCode*/) {
93 if(c
== 0 && limit
== NULL
) {
99 if(U16_IS_LEAD(c
) && pos
!= limit
&& U16_IS_TRAIL(trail
= *pos
)) {
101 return U16_GET_SUPPLEMENTARY(c
, trail
);
108 UTF16CollationIterator::previousCodePoint(UErrorCode
& /*errorCode*/) {
114 if(U16_IS_TRAIL(c
) && pos
!= start
&& U16_IS_LEAD(lead
= *(pos
- 1))) {
116 return U16_GET_SUPPLEMENTARY(lead
, c
);
123 UTF16CollationIterator::forwardNumCodePoints(int32_t num
, UErrorCode
& /*errorCode*/) {
124 while(num
> 0 && pos
!= limit
) {
126 if(c
== 0 && limit
== NULL
) {
132 if(U16_IS_LEAD(c
) && pos
!= limit
&& U16_IS_TRAIL(*pos
)) {
139 UTF16CollationIterator::backwardNumCodePoints(int32_t num
, UErrorCode
& /*errorCode*/) {
140 while(num
> 0 && pos
!= start
) {
143 if(U16_IS_TRAIL(c
) && pos
!= start
&& U16_IS_LEAD(*(pos
-1))) {
149 // FCDUTF16CollationIterator ----------------------------------------------- ***
151 FCDUTF16CollationIterator::FCDUTF16CollationIterator(const FCDUTF16CollationIterator
&other
,
152 const UChar
*newText
)
153 : UTF16CollationIterator(other
),
155 segmentStart(newText
+ (other
.segmentStart
- other
.rawStart
)),
156 segmentLimit(other
.segmentLimit
== NULL
? NULL
: newText
+ (other
.segmentLimit
- other
.rawStart
)),
157 rawLimit(other
.rawLimit
== NULL
? NULL
: newText
+ (other
.rawLimit
- other
.rawStart
)),
158 nfcImpl(other
.nfcImpl
),
159 normalized(other
.normalized
),
160 checkDir(other
.checkDir
) {
161 if(checkDir
!= 0 || other
.start
== other
.segmentStart
) {
162 start
= newText
+ (other
.start
- other
.rawStart
);
163 pos
= newText
+ (other
.pos
- other
.rawStart
);
164 limit
= other
.limit
== NULL
? NULL
: newText
+ (other
.limit
- other
.rawStart
);
166 start
= normalized
.getBuffer();
167 pos
= start
+ (other
.pos
- other
.start
);
168 limit
= start
+ normalized
.length();
172 FCDUTF16CollationIterator::~FCDUTF16CollationIterator() {}
175 FCDUTF16CollationIterator::operator==(const CollationIterator
&other
) const {
176 // Skip the UTF16CollationIterator and call its parent.
177 if(!CollationIterator::operator==(other
)) { return FALSE
; }
178 const FCDUTF16CollationIterator
&o
= static_cast<const FCDUTF16CollationIterator
&>(other
);
179 // Compare the iterator state but not the text: Assume that the caller does that.
180 if(checkDir
!= o
.checkDir
) { return FALSE
; }
181 if(checkDir
== 0 && (start
== segmentStart
) != (o
.start
== o
.segmentStart
)) { return FALSE
; }
182 if(checkDir
!= 0 || start
== segmentStart
) {
183 return (pos
- rawStart
) == (o
.pos
- o
.rawStart
);
185 return (segmentStart
- rawStart
) == (o
.segmentStart
- o
.rawStart
) &&
186 (pos
- start
) == (o
.pos
- o
.start
);
191 FCDUTF16CollationIterator::resetToOffset(int32_t newOffset
) {
193 start
= segmentStart
= pos
= rawStart
+ newOffset
;
199 FCDUTF16CollationIterator::getOffset() const {
200 if(checkDir
!= 0 || start
== segmentStart
) {
201 return (int32_t)(pos
- rawStart
);
202 } else if(pos
== start
) {
203 return (int32_t)(segmentStart
- rawStart
);
205 return (int32_t)(segmentLimit
- rawStart
);
210 FCDUTF16CollationIterator::handleNextCE32(UChar32
&c
, UErrorCode
&errorCode
) {
215 return Collation::FALLBACK_CE32
;
218 if(CollationFCD::hasTccc(c
)) {
219 if(CollationFCD::maybeTibetanCompositeVowel(c
) ||
220 (pos
!= limit
&& CollationFCD::hasLccc(*pos
))) {
222 if(!nextSegment(errorCode
)) {
224 return Collation::FALLBACK_CE32
;
230 } else if(checkDir
== 0 && pos
!= limit
) {
237 return UTRIE2_GET32_FROM_U16_SINGLE_LEAD(trie
, c
);
241 FCDUTF16CollationIterator::foundNULTerminator() {
243 limit
= rawLimit
= --pos
;
251 FCDUTF16CollationIterator::nextCodePoint(UErrorCode
&errorCode
) {
259 if(CollationFCD::hasTccc(c
)) {
260 if(CollationFCD::maybeTibetanCompositeVowel(c
) ||
261 (pos
!= limit
&& CollationFCD::hasLccc(*pos
))) {
263 if(!nextSegment(errorCode
)) {
268 } else if(c
== 0 && limit
== NULL
) {
269 limit
= rawLimit
= --pos
;
273 } else if(checkDir
== 0 && pos
!= limit
) {
281 if(U16_IS_LEAD(c
) && pos
!= limit
&& U16_IS_TRAIL(trail
= *pos
)) {
283 return U16_GET_SUPPLEMENTARY(c
, trail
);
290 FCDUTF16CollationIterator::previousCodePoint(UErrorCode
&errorCode
) {
298 if(CollationFCD::hasLccc(c
)) {
299 if(CollationFCD::maybeTibetanCompositeVowel(c
) ||
300 (pos
!= start
&& CollationFCD::hasTccc(*(pos
- 1)))) {
302 if(!previousSegment(errorCode
)) {
309 } else if(checkDir
== 0 && pos
!= start
) {
317 if(U16_IS_TRAIL(c
) && pos
!= start
&& U16_IS_LEAD(lead
= *(pos
- 1))) {
319 return U16_GET_SUPPLEMENTARY(lead
, c
);
326 FCDUTF16CollationIterator::forwardNumCodePoints(int32_t num
, UErrorCode
&errorCode
) {
327 // Specify the class to avoid a virtual-function indirection.
328 // In Java, we would declare this class final.
329 while(num
> 0 && FCDUTF16CollationIterator::nextCodePoint(errorCode
) >= 0) {
335 FCDUTF16CollationIterator::backwardNumCodePoints(int32_t num
, UErrorCode
&errorCode
) {
336 // Specify the class to avoid a virtual-function indirection.
337 // In Java, we would declare this class final.
338 while(num
> 0 && FCDUTF16CollationIterator::previousCodePoint(errorCode
) >= 0) {
344 FCDUTF16CollationIterator::switchToForward() {
345 U_ASSERT(checkDir
< 0 || (checkDir
== 0 && pos
== limit
));
347 // Turn around from backward checking.
348 start
= segmentStart
= pos
;
349 if(pos
== segmentLimit
) {
351 checkDir
= 1; // Check forward.
352 } else { // pos < segmentLimit
353 checkDir
= 0; // Stay in FCD segment.
356 // Reached the end of the FCD segment.
357 if(start
== segmentStart
) {
358 // The input text segment is FCD, extend it forward.
360 // The input text segment needed to be normalized.
361 // Switch to checking forward from it.
362 pos
= start
= segmentStart
= segmentLimit
;
363 // Note: If this segment is at the end of the input text,
364 // then it might help to return FALSE to indicate that, so that
365 // we do not have to re-check and normalize when we turn around and go backwards.
366 // However, that would complicate the call sites for an optimization of an unusual case.
374 FCDUTF16CollationIterator::nextSegment(UErrorCode
&errorCode
) {
375 if(U_FAILURE(errorCode
)) { return FALSE
; }
376 U_ASSERT(checkDir
> 0 && pos
!= limit
);
377 // The input text [segmentStart..pos[ passes the FCD check.
378 const UChar
*p
= pos
;
381 // Fetch the next character's fcd16 value.
383 uint16_t fcd16
= nfcImpl
.nextFCD16(p
, rawLimit
);
384 uint8_t leadCC
= (uint8_t)(fcd16
>> 8);
385 if(leadCC
== 0 && q
!= pos
) {
386 // FCD boundary before the [q, p[ character.
387 limit
= segmentLimit
= q
;
390 if(leadCC
!= 0 && (prevCC
> leadCC
|| CollationFCD::isFCD16OfTibetanCompositeVowel(fcd16
))) {
391 // Fails FCD check. Find the next FCD boundary and normalize.
394 } while(p
!= rawLimit
&& nfcImpl
.nextFCD16(p
, rawLimit
) > 0xff);
395 if(!normalize(pos
, q
, errorCode
)) { return FALSE
; }
399 prevCC
= (uint8_t)fcd16
;
400 if(p
== rawLimit
|| prevCC
== 0) {
401 // FCD boundary after the last character.
402 limit
= segmentLimit
= p
;
406 U_ASSERT(pos
!= limit
);
412 FCDUTF16CollationIterator::switchToBackward() {
413 U_ASSERT(checkDir
> 0 || (checkDir
== 0 && pos
== start
));
415 // Turn around from forward checking.
416 limit
= segmentLimit
= pos
;
417 if(pos
== segmentStart
) {
419 checkDir
= -1; // Check backward.
420 } else { // pos > segmentStart
421 checkDir
= 0; // Stay in FCD segment.
424 // Reached the start of the FCD segment.
425 if(start
== segmentStart
) {
426 // The input text segment is FCD, extend it backward.
428 // The input text segment needed to be normalized.
429 // Switch to checking backward from it.
430 pos
= limit
= segmentLimit
= segmentStart
;
438 FCDUTF16CollationIterator::previousSegment(UErrorCode
&errorCode
) {
439 if(U_FAILURE(errorCode
)) { return FALSE
; }
440 U_ASSERT(checkDir
< 0 && pos
!= start
);
441 // The input text [pos..segmentLimit[ passes the FCD check.
442 const UChar
*p
= pos
;
445 // Fetch the previous character's fcd16 value.
447 uint16_t fcd16
= nfcImpl
.previousFCD16(rawStart
, p
);
448 uint8_t trailCC
= (uint8_t)fcd16
;
449 if(trailCC
== 0 && q
!= pos
) {
450 // FCD boundary after the [p, q[ character.
451 start
= segmentStart
= q
;
454 if(trailCC
!= 0 && ((nextCC
!= 0 && trailCC
> nextCC
) ||
455 CollationFCD::isFCD16OfTibetanCompositeVowel(fcd16
))) {
456 // Fails FCD check. Find the previous FCD boundary and normalize.
459 } while(fcd16
> 0xff && p
!= rawStart
&&
460 (fcd16
= nfcImpl
.previousFCD16(rawStart
, p
)) != 0);
461 if(!normalize(q
, pos
, errorCode
)) { return FALSE
; }
465 nextCC
= (uint8_t)(fcd16
>> 8);
466 if(p
== rawStart
|| nextCC
== 0) {
467 // FCD boundary before the following character.
468 start
= segmentStart
= p
;
472 U_ASSERT(pos
!= start
);
478 FCDUTF16CollationIterator::normalize(const UChar
*from
, const UChar
*to
, UErrorCode
&errorCode
) {
479 // NFD without argument checking.
480 U_ASSERT(U_SUCCESS(errorCode
));
481 nfcImpl
.decompose(from
, to
, normalized
, (int32_t)(to
- from
), errorCode
);
482 if(U_FAILURE(errorCode
)) { return FALSE
; }
483 // Switch collation processing into the FCD buffer
484 // with the result of normalizing [segmentStart, segmentLimit[.
487 start
= normalized
.getBuffer();
488 limit
= start
+ normalized
.length();
494 #endif // !UCONFIG_NO_COLLATION