| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#ifndef __COLLATIONITERATOR_H__ |
| 15 |
#define __COLLATIONITERATOR_H__ |
| 16 |
|
| 17 |
#include "unicode/utypes.h" |
| 18 |
|
| 19 |
#if !UCONFIG_NO_COLLATION |
| 20 |
|
| 21 |
#include "cmemory.h" |
| 22 |
#include "collation.h" |
| 23 |
#include "collationdata.h" |
| 24 |
|
| 25 |
U_NAMESPACE_BEGIN |
| 26 |
|
| 27 |
class SkippedState; |
| 28 |
class UCharsTrie; |
| 29 |
class UVector32; |
| 30 |
|
| 31 |
|
| 32 |
#define CEBUFFER_INITIAL_CAPACITY 40 |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN |
| 43 |
template class U_I18N_API MaybeStackArray<int64_t, CEBUFFER_INITIAL_CAPACITY>; |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
class U_I18N_API CollationIterator : public UObject { |
| 53 |
private: |
| 54 |
class U_I18N_API CEBuffer { |
| 55 |
private: |
| 56 |
|
| 57 |
static const int32_t INITIAL_CAPACITY = CEBUFFER_INITIAL_CAPACITY; |
| 58 |
public: |
| 59 |
CEBuffer() : length(0) {} |
| 60 |
~CEBuffer(); |
| 61 |
|
| 62 |
inline void append(int64_t ce, UErrorCode &errorCode) { |
| 63 |
if(length < INITIAL_CAPACITY || ensureAppendCapacity(1, errorCode)) { |
| 64 |
buffer[length++] = ce; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
inline void appendUnsafe(int64_t ce) { |
| 69 |
buffer[length++] = ce; |
| 70 |
} |
| 71 |
|
| 72 |
UBool ensureAppendCapacity(int32_t appCap, UErrorCode &errorCode); |
| 73 |
|
| 74 |
inline UBool incLength(UErrorCode &errorCode) { |
| 75 |
|
| 76 |
|
| 77 |
if(length < INITIAL_CAPACITY || ensureAppendCapacity(1, errorCode)) { |
| 78 |
++length; |
| 79 |
return true; |
| 80 |
} else { |
| 81 |
return false; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
inline int64_t set(int32_t i, int64_t ce) { |
| 86 |
return buffer[i] = ce; |
| 87 |
} |
| 88 |
inline int64_t get(int32_t i) const { return buffer[i]; } |
| 89 |
|
| 90 |
const int64_t *getCEs() const { return buffer.getAlias(); } |
| 91 |
|
| 92 |
int32_t length; |
| 93 |
|
| 94 |
private: |
| 95 |
CEBuffer(const CEBuffer &) = delete; |
| 96 |
void operator=(const CEBuffer &) = delete; |
| 97 |
|
| 98 |
MaybeStackArray<int64_t, INITIAL_CAPACITY> buffer; |
| 99 |
}; |
| 100 |
|
| 101 |
public: |
| 102 |
CollationIterator(const CollationData *d, UBool numeric) |
| 103 |
: trie(d->trie), |
| 104 |
data(d), |
| 105 |
cesIndex(0), |
| 106 |
skipped(nullptr), |
| 107 |
numCpFwd(-1), |
| 108 |
isNumeric(numeric) {} |
| 109 |
|
| 110 |
virtual ~CollationIterator(); |
| 111 |
|
| 112 |
virtual bool operator==(const CollationIterator &other) const; |
| 113 |
inline bool operator!=(const CollationIterator &other) const { |
| 114 |
return !operator==(other); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
virtual void resetToOffset(int32_t newOffset) = 0; |
| 123 |
|
| 124 |
virtual int32_t getOffset() const = 0; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
inline int64_t nextCE(UErrorCode &errorCode) { |
| 130 |
if(cesIndex < ceBuffer.length) { |
| 131 |
|
| 132 |
return ceBuffer.get(cesIndex++); |
| 133 |
} |
| 134 |
|
| 135 |
if(!ceBuffer.incLength(errorCode)) { |
| 136 |
return Collation::NO_CE; |
| 137 |
} |
| 138 |
UChar32 c; |
| 139 |
uint32_t ce32 = handleNextCE32(c, errorCode); |
| 140 |
uint32_t t = ce32 & 0xff; |
| 141 |
if(t < Collation::SPECIAL_CE32_LOW_BYTE) { |
| 142 |
|
| 143 |
|
| 144 |
return ceBuffer.set(cesIndex++, |
| 145 |
(static_cast<int64_t>(ce32 & 0xffff0000) << 32) | ((ce32 & 0xff00) << 16) | (t << 8)); |
| 146 |
} |
| 147 |
const CollationData *d; |
| 148 |
|
| 149 |
|
| 150 |
if(t == Collation::SPECIAL_CE32_LOW_BYTE) { |
| 151 |
if(c < 0) { |
| 152 |
return ceBuffer.set(cesIndex++, Collation::NO_CE); |
| 153 |
} |
| 154 |
d = data->base; |
| 155 |
ce32 = d->getCE32(c); |
| 156 |
t = ce32 & 0xff; |
| 157 |
if(t < Collation::SPECIAL_CE32_LOW_BYTE) { |
| 158 |
|
| 159 |
return ceBuffer.set(cesIndex++, |
| 160 |
(static_cast<int64_t>(ce32 & 0xffff0000) << 32) | ((ce32 & 0xff00) << 16) | (t << 8)); |
| 161 |
} |
| 162 |
} else { |
| 163 |
d = data; |
| 164 |
} |
| 165 |
if(t == Collation::LONG_PRIMARY_CE32_LOW_BYTE) { |
| 166 |
|
| 167 |
return ceBuffer.set(cesIndex++, |
| 168 |
(static_cast<int64_t>(ce32 - t) << 32) | Collation::COMMON_SEC_AND_TER_CE); |
| 169 |
} |
| 170 |
return nextCEFromCE32(d, c, ce32, errorCode); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
int32_t fetchCEs(UErrorCode &errorCode); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
void setCurrentCE(int64_t ce) { |
| 183 |
|
| 184 |
ceBuffer.set(cesIndex - 1, ce); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
int64_t previousCE(UVector32 &offsets, UErrorCode &errorCode); |
| 191 |
|
| 192 |
inline int32_t getCEsLength() const { |
| 193 |
return ceBuffer.length; |
| 194 |
} |
| 195 |
|
| 196 |
inline int64_t getCE(int32_t i) const { |
| 197 |
return ceBuffer.get(i); |
| 198 |
} |
| 199 |
|
| 200 |
const int64_t *getCEs() const { |
| 201 |
return ceBuffer.getCEs(); |
| 202 |
} |
| 203 |
|
| 204 |
void clearCEs() { |
| 205 |
cesIndex = ceBuffer.length = 0; |
| 206 |
} |
| 207 |
|
| 208 |
void clearCEsIfNoneRemaining() { |
| 209 |
if(cesIndex == ceBuffer.length) { clearCEs(); } |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
virtual UChar32 nextCodePoint(UErrorCode &errorCode) = 0; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
virtual UChar32 previousCodePoint(UErrorCode &errorCode) = 0; |
| 223 |
|
| 224 |
protected: |
| 225 |
CollationIterator(const CollationIterator &other); |
| 226 |
|
| 227 |
void reset(); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
virtual uint32_t handleNextCE32(UChar32 &c, UErrorCode &errorCode); |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
virtual char16_t handleGetTrailSurrogate(); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
virtual UBool foundNULTerminator(); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
virtual UBool forbidSurrogateCodePoints() const; |
| 259 |
|
| 260 |
virtual void forwardNumCodePoints(int32_t num, UErrorCode &errorCode) = 0; |
| 261 |
|
| 262 |
virtual void backwardNumCodePoints(int32_t num, UErrorCode &errorCode) = 0; |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
virtual uint32_t getDataCE32(UChar32 c) const; |
| 270 |
|
| 271 |
virtual uint32_t getCE32FromBuilderData(uint32_t ce32, UErrorCode &errorCode); |
| 272 |
|
| 273 |
void appendCEsFromCE32(const CollationData *d, UChar32 c, uint32_t ce32, |
| 274 |
UBool forward, UErrorCode &errorCode); |
| 275 |
|
| 276 |
|
| 277 |
const UTrie2 *trie; |
| 278 |
const CollationData *data; |
| 279 |
|
| 280 |
private: |
| 281 |
int64_t nextCEFromCE32(const CollationData *d, UChar32 c, uint32_t ce32, |
| 282 |
UErrorCode &errorCode); |
| 283 |
|
| 284 |
uint32_t getCE32FromPrefix(const CollationData *d, uint32_t ce32, |
| 285 |
UErrorCode &errorCode); |
| 286 |
|
| 287 |
UChar32 nextSkippedCodePoint(UErrorCode &errorCode); |
| 288 |
|
| 289 |
void backwardNumSkipped(int32_t n, UErrorCode &errorCode); |
| 290 |
|
| 291 |
uint32_t nextCE32FromContraction( |
| 292 |
const CollationData *d, uint32_t contractionCE32, |
| 293 |
const char16_t *p, uint32_t ce32, UChar32 c, |
| 294 |
UErrorCode &errorCode); |
| 295 |
|
| 296 |
uint32_t nextCE32FromDiscontiguousContraction( |
| 297 |
const CollationData *d, UCharsTrie &suffixes, uint32_t ce32, |
| 298 |
int32_t lookAhead, UChar32 c, |
| 299 |
UErrorCode &errorCode); |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
int64_t previousCEUnsafe(UChar32 c, UVector32 &offsets, UErrorCode &errorCode); |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
void appendNumericCEs(uint32_t ce32, UBool forward, UErrorCode &errorCode); |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
void appendNumericSegmentCEs(const char *digits, int32_t length, UErrorCode &errorCode); |
| 320 |
|
| 321 |
CEBuffer ceBuffer; |
| 322 |
int32_t cesIndex; |
| 323 |
|
| 324 |
SkippedState *skipped; |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
int32_t numCpFwd; |
| 329 |
|
| 330 |
UBool isNumeric; |
| 331 |
}; |
| 332 |
|
| 333 |
U_NAMESPACE_END |
| 334 |
|
| 335 |
#endif |
| 336 |
#endif |