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) 2010-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * utf16collationiterator.h
10 * created on: 2010oct27
11 * created by: Markus W. Scherer
14 #ifndef __UTF16COLLATIONITERATOR_H__
15 #define __UTF16COLLATIONITERATOR_H__
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_COLLATION
22 #include "collation.h"
23 #include "collationdata.h"
24 #include "collationiterator.h"
25 #include "normalizer2impl.h"
30 * UTF-16 collation element and character iterator.
31 * Handles normalized UTF-16 text inline, with length or NUL-terminated.
32 * Unnormalized text is handled by a subclass.
34 class U_I18N_API UTF16CollationIterator : public CollationIterator {
36 UTF16CollationIterator(const CollationData *d, UBool numeric,
37 const UChar *s, const UChar *p, const UChar *lim)
38 : CollationIterator(d, numeric),
39 start(s), pos(p), limit(lim) {}
41 UTF16CollationIterator(const UTF16CollationIterator &other, const UChar *newText);
43 virtual ~UTF16CollationIterator();
45 virtual UBool operator==(const CollationIterator &other) const;
47 virtual void resetToOffset(int32_t newOffset);
49 virtual int32_t getOffset() const;
51 void setText(const UChar *s, const UChar *lim) {
57 virtual UChar32 nextCodePoint(UErrorCode &errorCode);
59 virtual UChar32 previousCodePoint(UErrorCode &errorCode);
62 // Copy constructor only for subclasses which set the pointers.
63 UTF16CollationIterator(const UTF16CollationIterator &other)
64 : CollationIterator(other),
65 start(NULL), pos(NULL), limit(NULL) {}
67 virtual uint32_t handleNextCE32(UChar32 &c, UErrorCode &errorCode);
69 virtual UChar handleGetTrailSurrogate();
71 virtual UBool foundNULTerminator();
73 virtual void forwardNumCodePoints(int32_t num, UErrorCode &errorCode);
75 virtual void backwardNumCodePoints(int32_t num, UErrorCode &errorCode);
77 // UTF-16 string pointers.
78 // limit can be NULL for NUL-terminated strings.
79 const UChar *start, *pos, *limit;
83 * Incrementally checks the input text for FCD and normalizes where necessary.
85 class U_I18N_API FCDUTF16CollationIterator : public UTF16CollationIterator {
87 FCDUTF16CollationIterator(const CollationData *data, UBool numeric,
88 const UChar *s, const UChar *p, const UChar *lim)
89 : UTF16CollationIterator(data, numeric, s, p, lim),
90 rawStart(s), segmentStart(p), segmentLimit(NULL), rawLimit(lim),
91 nfcImpl(data->nfcImpl),
94 FCDUTF16CollationIterator(const FCDUTF16CollationIterator &other, const UChar *newText);
96 virtual ~FCDUTF16CollationIterator();
98 virtual UBool operator==(const CollationIterator &other) const;
100 virtual void resetToOffset(int32_t newOffset);
102 virtual int32_t getOffset() const;
104 virtual UChar32 nextCodePoint(UErrorCode &errorCode);
106 virtual UChar32 previousCodePoint(UErrorCode &errorCode);
109 virtual uint32_t handleNextCE32(UChar32 &c, UErrorCode &errorCode);
111 virtual UBool foundNULTerminator();
113 virtual void forwardNumCodePoints(int32_t num, UErrorCode &errorCode);
115 virtual void backwardNumCodePoints(int32_t num, UErrorCode &errorCode);
119 * Switches to forward checking if possible.
120 * To be called when checkDir < 0 || (checkDir == 0 && pos == limit).
121 * Returns with checkDir > 0 || (checkDir == 0 && pos != limit).
123 void switchToForward();
126 * Extend the FCD text segment forward or normalize around pos.
127 * To be called when checkDir > 0 && pos != limit.
128 * @return TRUE if success, checkDir == 0 and pos != limit
130 UBool nextSegment(UErrorCode &errorCode);
133 * Switches to backward checking.
134 * To be called when checkDir > 0 || (checkDir == 0 && pos == start).
135 * Returns with checkDir < 0 || (checkDir == 0 && pos != start).
137 void switchToBackward();
140 * Extend the FCD text segment backward or normalize around pos.
141 * To be called when checkDir < 0 && pos != start.
142 * @return TRUE if success, checkDir == 0 and pos != start
144 UBool previousSegment(UErrorCode &errorCode);
146 UBool normalize(const UChar *from, const UChar *to, UErrorCode &errorCode);
148 // Text pointers: The input text is [rawStart, rawLimit[
149 // where rawLimit can be NULL for NUL-terminated text.
153 // The input text [segmentStart..pos[ passes the FCD check.
154 // Moving forward checks incrementally.
155 // segmentLimit is undefined. limit == rawLimit.
158 // The input text [pos..segmentLimit[ passes the FCD check.
159 // Moving backward checks incrementally.
160 // segmentStart is undefined, start == rawStart.
164 // The input text [segmentStart..segmentLimit[ is being processed.
165 // These pointers are at FCD boundaries.
166 // Either this text segment already passes the FCD check
167 // and segmentStart==start<=pos<=limit==segmentLimit,
168 // or the current segment had to be normalized so that
169 // [segmentStart..segmentLimit[ turned into the normalized string,
170 // corresponding to normalized.getBuffer()==start<=pos<=limit==start+normalized.length().
171 const UChar *rawStart;
172 const UChar *segmentStart;
173 const UChar *segmentLimit;
174 // rawLimit==NULL for a NUL-terminated string.
175 const UChar *rawLimit;
177 const Normalizer2Impl &nfcImpl;
178 UnicodeString normalized;
179 // Direction of incremental FCD check. See comments before rawStart.
185 #endif // !UCONFIG_NO_COLLATION
186 #endif // __UTF16COLLATIONITERATOR_H__