2 *******************************************************************************
3 * Copyright (C) 2010-2014, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
8 * created on: 2010oct27
9 * created by: Markus W. Scherer
12 #ifndef __COLLATIONITERATOR_H__
13 #define __COLLATIONITERATOR_H__
15 #include "unicode/utypes.h"
17 #if !UCONFIG_NO_COLLATION
20 #include "collation.h"
21 #include "collationdata.h"
30 * Collation element iterator and abstract character iterator.
32 * When a method returns a code point value, it must be in 0..10FFFF,
33 * except it can be negative as a sentinel value.
35 class U_I18N_API CollationIterator
: public UObject
{
39 /** Large enough for CEs of most short strings. */
40 static const int32_t INITIAL_CAPACITY
= 40;
42 CEBuffer() : length(0) {}
45 inline void append(int64_t ce
, UErrorCode
&errorCode
) {
46 if(length
< INITIAL_CAPACITY
|| ensureAppendCapacity(1, errorCode
)) {
47 buffer
[length
++] = ce
;
51 inline void appendUnsafe(int64_t ce
) {
52 buffer
[length
++] = ce
;
55 UBool
ensureAppendCapacity(int32_t appCap
, UErrorCode
&errorCode
);
57 inline UBool
incLength(UErrorCode
&errorCode
) {
58 // Use INITIAL_CAPACITY for a very simple fastpath.
59 // (Rather than buffer.getCapacity().)
60 if(length
< INITIAL_CAPACITY
|| ensureAppendCapacity(1, errorCode
)) {
68 inline int64_t set(int32_t i
, int64_t ce
) {
69 return buffer
[i
] = ce
;
71 inline int64_t get(int32_t i
) const { return buffer
[i
]; }
73 const int64_t *getCEs() const { return buffer
.getAlias(); }
78 CEBuffer(const CEBuffer
&);
79 void operator=(const CEBuffer
&);
81 MaybeStackArray
<int64_t, INITIAL_CAPACITY
> buffer
;
85 CollationIterator(const CollationData
*d
, UBool numeric
)
93 virtual ~CollationIterator();
95 virtual UBool
operator==(const CollationIterator
&other
) const;
96 inline UBool
operator!=(const CollationIterator
&other
) const {
97 return !operator==(other
);
101 * Resets the iterator state and sets the position to the specified offset.
102 * Subclasses must implement, and must call the parent class method,
103 * or CollationIterator::reset().
105 virtual void resetToOffset(int32_t newOffset
) = 0;
107 virtual int32_t getOffset() const = 0;
110 * Returns the next collation element.
112 inline int64_t nextCE(UErrorCode
&errorCode
) {
113 if(cesIndex
< ceBuffer
.length
) {
114 // Return the next buffered CE.
115 return ceBuffer
.get(cesIndex
++);
117 // assert cesIndex == ceBuffer.length;
118 if(!ceBuffer
.incLength(errorCode
)) {
119 return Collation::NO_CE
;
122 uint32_t ce32
= handleNextCE32(c
, errorCode
);
123 uint32_t t
= ce32
& 0xff;
124 if(t
< Collation::SPECIAL_CE32_LOW_BYTE
) { // Forced-inline of isSpecialCE32(ce32).
125 // Normal CE from the main data.
126 // Forced-inline of ceFromSimpleCE32(ce32).
127 return ceBuffer
.set(cesIndex
++,
128 ((int64_t)(ce32
& 0xffff0000) << 32) | ((ce32
& 0xff00) << 16) | (t
<< 8));
130 const CollationData
*d
;
131 // The compiler should be able to optimize the previous and the following
132 // comparisons of t with the same constant.
133 if(t
== Collation::SPECIAL_CE32_LOW_BYTE
) {
135 return ceBuffer
.set(cesIndex
++, Collation::NO_CE
);
138 ce32
= d
->getCE32(c
);
140 if(t
< Collation::SPECIAL_CE32_LOW_BYTE
) {
141 // Normal CE from the base data.
142 return ceBuffer
.set(cesIndex
++,
143 ((int64_t)(ce32
& 0xffff0000) << 32) | ((ce32
& 0xff00) << 16) | (t
<< 8));
148 if(t
== Collation::LONG_PRIMARY_CE32_LOW_BYTE
) {
149 // Forced-inline of ceFromLongPrimaryCE32(ce32).
150 return ceBuffer
.set(cesIndex
++,
151 ((int64_t)(ce32
- t
) << 32) | Collation::COMMON_SEC_AND_TER_CE
);
153 return nextCEFromCE32(d
, c
, ce32
, errorCode
);
158 * @return getCEsLength()
160 int32_t fetchCEs(UErrorCode
&errorCode
);
163 * Overwrites the current CE (the last one returned by nextCE()).
165 void setCurrentCE(int64_t ce
) {
166 // assert cesIndex > 0;
167 ceBuffer
.set(cesIndex
- 1, ce
);
171 * Returns the previous collation element.
173 int64_t previousCE(UVector32
&offsets
, UErrorCode
&errorCode
);
175 inline int32_t getCEsLength() const {
176 return ceBuffer
.length
;
179 inline int64_t getCE(int32_t i
) const {
180 return ceBuffer
.get(i
);
183 const int64_t *getCEs() const {
184 return ceBuffer
.getCEs();
188 cesIndex
= ceBuffer
.length
= 0;
191 void clearCEsIfNoneRemaining() {
192 if(cesIndex
== ceBuffer
.length
) { clearCEs(); }
196 * Returns the next code point (with post-increment).
197 * Public for identical-level comparison and for testing.
199 virtual UChar32
nextCodePoint(UErrorCode
&errorCode
) = 0;
202 * Returns the previous code point (with pre-decrement).
203 * Public for identical-level comparison and for testing.
205 virtual UChar32
previousCodePoint(UErrorCode
&errorCode
) = 0;
208 CollationIterator(const CollationIterator
&other
);
213 * Returns the next code point and its local CE32 value.
214 * Returns Collation::FALLBACK_CE32 at the end of the text (c<0)
215 * or when c's CE32 value is to be looked up in the base data (fallback).
217 * The code point is used for fallbacks, context and implicit weights.
218 * It is ignored when the returned CE32 is not special (e.g., FFFD_CE32).
220 virtual uint32_t handleNextCE32(UChar32
&c
, UErrorCode
&errorCode
);
223 * Called when handleNextCE32() returns a LEAD_SURROGATE_TAG for a lead surrogate code unit.
224 * Returns the trail surrogate in that case and advances past it,
225 * if a trail surrogate follows the lead surrogate.
226 * Otherwise returns any other code unit and does not advance.
228 virtual UChar
handleGetTrailSurrogate();
231 * Called when handleNextCE32() returns with c==0, to see whether it is a NUL terminator.
232 * (Not needed in Java.)
234 virtual UBool
foundNULTerminator();
237 * @return FALSE if surrogate code points U+D800..U+DFFF
238 * map to their own implicit primary weights (for UTF-16),
239 * or TRUE if they map to CE(U+FFFD) (for UTF-8)
241 virtual UBool
forbidSurrogateCodePoints() const;
243 virtual void forwardNumCodePoints(int32_t num
, UErrorCode
&errorCode
) = 0;
245 virtual void backwardNumCodePoints(int32_t num
, UErrorCode
&errorCode
) = 0;
248 * Returns the CE32 from the data trie.
249 * Normally the same as data->getCE32(), but overridden in the builder.
250 * Call this only when the faster data->getCE32() cannot be used.
252 virtual uint32_t getDataCE32(UChar32 c
) const;
254 virtual uint32_t getCE32FromBuilderData(uint32_t ce32
, UErrorCode
&errorCode
);
256 void appendCEsFromCE32(const CollationData
*d
, UChar32 c
, uint32_t ce32
,
257 UBool forward
, UErrorCode
&errorCode
);
259 // Main lookup trie of the data object.
261 const CollationData
*data
;
264 int64_t nextCEFromCE32(const CollationData
*d
, UChar32 c
, uint32_t ce32
,
265 UErrorCode
&errorCode
);
267 uint32_t getCE32FromPrefix(const CollationData
*d
, uint32_t ce32
,
268 UErrorCode
&errorCode
);
270 UChar32
nextSkippedCodePoint(UErrorCode
&errorCode
);
272 void backwardNumSkipped(int32_t n
, UErrorCode
&errorCode
);
274 uint32_t nextCE32FromContraction(
275 const CollationData
*d
, uint32_t contractionCE32
,
276 const UChar
*p
, uint32_t ce32
, UChar32 c
,
277 UErrorCode
&errorCode
);
279 uint32_t nextCE32FromDiscontiguousContraction(
280 const CollationData
*d
, UCharsTrie
&suffixes
, uint32_t ce32
,
281 int32_t lookAhead
, UChar32 c
,
282 UErrorCode
&errorCode
);
285 * Returns the previous CE when data->isUnsafeBackward(c, isNumeric).
287 int64_t previousCEUnsafe(UChar32 c
, UVector32
&offsets
, UErrorCode
&errorCode
);
290 * Turns a string of digits (bytes 0..9)
291 * into a sequence of CEs that will sort in numeric order.
293 * Starts from this ce32's digit value and consumes the following/preceding digits.
294 * The digits string must not be empty and must not have leading zeros.
296 void appendNumericCEs(uint32_t ce32
, UBool forward
, UErrorCode
&errorCode
);
299 * Turns 1..254 digits into a sequence of CEs.
300 * Called by appendNumericCEs() for each segment of at most 254 digits.
302 void appendNumericSegmentCEs(const char *digits
, int32_t length
, UErrorCode
&errorCode
);
307 SkippedState
*skipped
;
309 // Number of code points to read forward, or -1.
310 // Used as a forward iteration limit in previousCEUnsafe().
312 // Numeric collation (CollationSettings::NUMERIC).
318 #endif // !UCONFIG_NO_COLLATION
319 #endif // __COLLATIONITERATOR_H__