Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * Copyright (C) 2010-2012, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ******************************************************************************* |
michael@0 | 6 | * file name: ucharstrie.h |
michael@0 | 7 | * encoding: US-ASCII |
michael@0 | 8 | * tab size: 8 (not used) |
michael@0 | 9 | * indentation:4 |
michael@0 | 10 | * |
michael@0 | 11 | * created on: 2010nov14 |
michael@0 | 12 | * created by: Markus W. Scherer |
michael@0 | 13 | */ |
michael@0 | 14 | |
michael@0 | 15 | #ifndef __UCHARSTRIE_H__ |
michael@0 | 16 | #define __UCHARSTRIE_H__ |
michael@0 | 17 | |
michael@0 | 18 | /** |
michael@0 | 19 | * \file |
michael@0 | 20 | * \brief C++ API: Trie for mapping Unicode strings (or 16-bit-unit sequences) |
michael@0 | 21 | * to integer values. |
michael@0 | 22 | */ |
michael@0 | 23 | |
michael@0 | 24 | #include "unicode/utypes.h" |
michael@0 | 25 | #include "unicode/unistr.h" |
michael@0 | 26 | #include "unicode/uobject.h" |
michael@0 | 27 | #include "unicode/ustringtrie.h" |
michael@0 | 28 | |
michael@0 | 29 | U_NAMESPACE_BEGIN |
michael@0 | 30 | |
michael@0 | 31 | class Appendable; |
michael@0 | 32 | class UCharsTrieBuilder; |
michael@0 | 33 | class UVector32; |
michael@0 | 34 | |
michael@0 | 35 | /** |
michael@0 | 36 | * Light-weight, non-const reader class for a UCharsTrie. |
michael@0 | 37 | * Traverses a UChar-serialized data structure with minimal state, |
michael@0 | 38 | * for mapping strings (16-bit-unit sequences) to non-negative integer values. |
michael@0 | 39 | * |
michael@0 | 40 | * This class owns the serialized trie data only if it was constructed by |
michael@0 | 41 | * the builder's build() method. |
michael@0 | 42 | * The public constructor and the copy constructor only alias the data (only copy the pointer). |
michael@0 | 43 | * There is no assignment operator. |
michael@0 | 44 | * |
michael@0 | 45 | * This class is not intended for public subclassing. |
michael@0 | 46 | * @stable ICU 4.8 |
michael@0 | 47 | */ |
michael@0 | 48 | class U_COMMON_API UCharsTrie : public UMemory { |
michael@0 | 49 | public: |
michael@0 | 50 | /** |
michael@0 | 51 | * Constructs a UCharsTrie reader instance. |
michael@0 | 52 | * |
michael@0 | 53 | * The trieUChars must contain a copy of a UChar sequence from the UCharsTrieBuilder, |
michael@0 | 54 | * starting with the first UChar of that sequence. |
michael@0 | 55 | * The UCharsTrie object will not read more UChars than |
michael@0 | 56 | * the UCharsTrieBuilder generated in the corresponding build() call. |
michael@0 | 57 | * |
michael@0 | 58 | * The array is not copied/cloned and must not be modified while |
michael@0 | 59 | * the UCharsTrie object is in use. |
michael@0 | 60 | * |
michael@0 | 61 | * @param trieUChars The UChar array that contains the serialized trie. |
michael@0 | 62 | * @stable ICU 4.8 |
michael@0 | 63 | */ |
michael@0 | 64 | UCharsTrie(const UChar *trieUChars) |
michael@0 | 65 | : ownedArray_(NULL), uchars_(trieUChars), |
michael@0 | 66 | pos_(uchars_), remainingMatchLength_(-1) {} |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * Destructor. |
michael@0 | 70 | * @stable ICU 4.8 |
michael@0 | 71 | */ |
michael@0 | 72 | ~UCharsTrie(); |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Copy constructor, copies the other trie reader object and its state, |
michael@0 | 76 | * but not the UChar array which will be shared. (Shallow copy.) |
michael@0 | 77 | * @param other Another UCharsTrie object. |
michael@0 | 78 | * @stable ICU 4.8 |
michael@0 | 79 | */ |
michael@0 | 80 | UCharsTrie(const UCharsTrie &other) |
michael@0 | 81 | : ownedArray_(NULL), uchars_(other.uchars_), |
michael@0 | 82 | pos_(other.pos_), remainingMatchLength_(other.remainingMatchLength_) {} |
michael@0 | 83 | |
michael@0 | 84 | /** |
michael@0 | 85 | * Resets this trie to its initial state. |
michael@0 | 86 | * @return *this |
michael@0 | 87 | * @stable ICU 4.8 |
michael@0 | 88 | */ |
michael@0 | 89 | UCharsTrie &reset() { |
michael@0 | 90 | pos_=uchars_; |
michael@0 | 91 | remainingMatchLength_=-1; |
michael@0 | 92 | return *this; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | /** |
michael@0 | 96 | * UCharsTrie state object, for saving a trie's current state |
michael@0 | 97 | * and resetting the trie back to this state later. |
michael@0 | 98 | * @stable ICU 4.8 |
michael@0 | 99 | */ |
michael@0 | 100 | class State : public UMemory { |
michael@0 | 101 | public: |
michael@0 | 102 | /** |
michael@0 | 103 | * Constructs an empty State. |
michael@0 | 104 | * @stable ICU 4.8 |
michael@0 | 105 | */ |
michael@0 | 106 | State() { uchars=NULL; } |
michael@0 | 107 | private: |
michael@0 | 108 | friend class UCharsTrie; |
michael@0 | 109 | |
michael@0 | 110 | const UChar *uchars; |
michael@0 | 111 | const UChar *pos; |
michael@0 | 112 | int32_t remainingMatchLength; |
michael@0 | 113 | }; |
michael@0 | 114 | |
michael@0 | 115 | /** |
michael@0 | 116 | * Saves the state of this trie. |
michael@0 | 117 | * @param state The State object to hold the trie's state. |
michael@0 | 118 | * @return *this |
michael@0 | 119 | * @see resetToState |
michael@0 | 120 | * @stable ICU 4.8 |
michael@0 | 121 | */ |
michael@0 | 122 | const UCharsTrie &saveState(State &state) const { |
michael@0 | 123 | state.uchars=uchars_; |
michael@0 | 124 | state.pos=pos_; |
michael@0 | 125 | state.remainingMatchLength=remainingMatchLength_; |
michael@0 | 126 | return *this; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | /** |
michael@0 | 130 | * Resets this trie to the saved state. |
michael@0 | 131 | * If the state object contains no state, or the state of a different trie, |
michael@0 | 132 | * then this trie remains unchanged. |
michael@0 | 133 | * @param state The State object which holds a saved trie state. |
michael@0 | 134 | * @return *this |
michael@0 | 135 | * @see saveState |
michael@0 | 136 | * @see reset |
michael@0 | 137 | * @stable ICU 4.8 |
michael@0 | 138 | */ |
michael@0 | 139 | UCharsTrie &resetToState(const State &state) { |
michael@0 | 140 | if(uchars_==state.uchars && uchars_!=NULL) { |
michael@0 | 141 | pos_=state.pos; |
michael@0 | 142 | remainingMatchLength_=state.remainingMatchLength; |
michael@0 | 143 | } |
michael@0 | 144 | return *this; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * Determines whether the string so far matches, whether it has a value, |
michael@0 | 149 | * and whether another input UChar can continue a matching string. |
michael@0 | 150 | * @return The match/value Result. |
michael@0 | 151 | * @stable ICU 4.8 |
michael@0 | 152 | */ |
michael@0 | 153 | UStringTrieResult current() const; |
michael@0 | 154 | |
michael@0 | 155 | /** |
michael@0 | 156 | * Traverses the trie from the initial state for this input UChar. |
michael@0 | 157 | * Equivalent to reset().next(uchar). |
michael@0 | 158 | * @param uchar Input char value. Values below 0 and above 0xffff will never match. |
michael@0 | 159 | * @return The match/value Result. |
michael@0 | 160 | * @stable ICU 4.8 |
michael@0 | 161 | */ |
michael@0 | 162 | inline UStringTrieResult first(int32_t uchar) { |
michael@0 | 163 | remainingMatchLength_=-1; |
michael@0 | 164 | return nextImpl(uchars_, uchar); |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | /** |
michael@0 | 168 | * Traverses the trie from the initial state for the |
michael@0 | 169 | * one or two UTF-16 code units for this input code point. |
michael@0 | 170 | * Equivalent to reset().nextForCodePoint(cp). |
michael@0 | 171 | * @param cp A Unicode code point 0..0x10ffff. |
michael@0 | 172 | * @return The match/value Result. |
michael@0 | 173 | * @stable ICU 4.8 |
michael@0 | 174 | */ |
michael@0 | 175 | UStringTrieResult firstForCodePoint(UChar32 cp); |
michael@0 | 176 | |
michael@0 | 177 | /** |
michael@0 | 178 | * Traverses the trie from the current state for this input UChar. |
michael@0 | 179 | * @param uchar Input char value. Values below 0 and above 0xffff will never match. |
michael@0 | 180 | * @return The match/value Result. |
michael@0 | 181 | * @stable ICU 4.8 |
michael@0 | 182 | */ |
michael@0 | 183 | UStringTrieResult next(int32_t uchar); |
michael@0 | 184 | |
michael@0 | 185 | /** |
michael@0 | 186 | * Traverses the trie from the current state for the |
michael@0 | 187 | * one or two UTF-16 code units for this input code point. |
michael@0 | 188 | * @param cp A Unicode code point 0..0x10ffff. |
michael@0 | 189 | * @return The match/value Result. |
michael@0 | 190 | * @stable ICU 4.8 |
michael@0 | 191 | */ |
michael@0 | 192 | UStringTrieResult nextForCodePoint(UChar32 cp); |
michael@0 | 193 | |
michael@0 | 194 | /** |
michael@0 | 195 | * Traverses the trie from the current state for this string. |
michael@0 | 196 | * Equivalent to |
michael@0 | 197 | * \code |
michael@0 | 198 | * Result result=current(); |
michael@0 | 199 | * for(each c in s) |
michael@0 | 200 | * if(!USTRINGTRIE_HAS_NEXT(result)) return USTRINGTRIE_NO_MATCH; |
michael@0 | 201 | * result=next(c); |
michael@0 | 202 | * return result; |
michael@0 | 203 | * \endcode |
michael@0 | 204 | * @param s A string. Can be NULL if length is 0. |
michael@0 | 205 | * @param length The length of the string. Can be -1 if NUL-terminated. |
michael@0 | 206 | * @return The match/value Result. |
michael@0 | 207 | * @stable ICU 4.8 |
michael@0 | 208 | */ |
michael@0 | 209 | UStringTrieResult next(const UChar *s, int32_t length); |
michael@0 | 210 | |
michael@0 | 211 | /** |
michael@0 | 212 | * Returns a matching string's value if called immediately after |
michael@0 | 213 | * current()/first()/next() returned USTRINGTRIE_INTERMEDIATE_VALUE or USTRINGTRIE_FINAL_VALUE. |
michael@0 | 214 | * getValue() can be called multiple times. |
michael@0 | 215 | * |
michael@0 | 216 | * Do not call getValue() after USTRINGTRIE_NO_MATCH or USTRINGTRIE_NO_VALUE! |
michael@0 | 217 | * @return The value for the string so far. |
michael@0 | 218 | * @stable ICU 4.8 |
michael@0 | 219 | */ |
michael@0 | 220 | inline int32_t getValue() const { |
michael@0 | 221 | const UChar *pos=pos_; |
michael@0 | 222 | int32_t leadUnit=*pos++; |
michael@0 | 223 | // U_ASSERT(leadUnit>=kMinValueLead); |
michael@0 | 224 | return leadUnit&kValueIsFinal ? |
michael@0 | 225 | readValue(pos, leadUnit&0x7fff) : readNodeValue(pos, leadUnit); |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | /** |
michael@0 | 229 | * Determines whether all strings reachable from the current state |
michael@0 | 230 | * map to the same value. |
michael@0 | 231 | * @param uniqueValue Receives the unique value, if this function returns TRUE. |
michael@0 | 232 | * (output-only) |
michael@0 | 233 | * @return TRUE if all strings reachable from the current state |
michael@0 | 234 | * map to the same value. |
michael@0 | 235 | * @stable ICU 4.8 |
michael@0 | 236 | */ |
michael@0 | 237 | inline UBool hasUniqueValue(int32_t &uniqueValue) const { |
michael@0 | 238 | const UChar *pos=pos_; |
michael@0 | 239 | // Skip the rest of a pending linear-match node. |
michael@0 | 240 | return pos!=NULL && findUniqueValue(pos+remainingMatchLength_+1, FALSE, uniqueValue); |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | /** |
michael@0 | 244 | * Finds each UChar which continues the string from the current state. |
michael@0 | 245 | * That is, each UChar c for which it would be next(c)!=USTRINGTRIE_NO_MATCH now. |
michael@0 | 246 | * @param out Each next UChar is appended to this object. |
michael@0 | 247 | * @return the number of UChars which continue the string from here |
michael@0 | 248 | * @stable ICU 4.8 |
michael@0 | 249 | */ |
michael@0 | 250 | int32_t getNextUChars(Appendable &out) const; |
michael@0 | 251 | |
michael@0 | 252 | /** |
michael@0 | 253 | * Iterator for all of the (string, value) pairs in a UCharsTrie. |
michael@0 | 254 | * @stable ICU 4.8 |
michael@0 | 255 | */ |
michael@0 | 256 | class U_COMMON_API Iterator : public UMemory { |
michael@0 | 257 | public: |
michael@0 | 258 | /** |
michael@0 | 259 | * Iterates from the root of a UChar-serialized UCharsTrie. |
michael@0 | 260 | * @param trieUChars The trie UChars. |
michael@0 | 261 | * @param maxStringLength If 0, the iterator returns full strings. |
michael@0 | 262 | * Otherwise, the iterator returns strings with this maximum length. |
michael@0 | 263 | * @param errorCode Standard ICU error code. Its input value must |
michael@0 | 264 | * pass the U_SUCCESS() test, or else the function returns |
michael@0 | 265 | * immediately. Check for U_FAILURE() on output or use with |
michael@0 | 266 | * function chaining. (See User Guide for details.) |
michael@0 | 267 | * @stable ICU 4.8 |
michael@0 | 268 | */ |
michael@0 | 269 | Iterator(const UChar *trieUChars, int32_t maxStringLength, UErrorCode &errorCode); |
michael@0 | 270 | |
michael@0 | 271 | /** |
michael@0 | 272 | * Iterates from the current state of the specified UCharsTrie. |
michael@0 | 273 | * @param trie The trie whose state will be copied for iteration. |
michael@0 | 274 | * @param maxStringLength If 0, the iterator returns full strings. |
michael@0 | 275 | * Otherwise, the iterator returns strings with this maximum length. |
michael@0 | 276 | * @param errorCode Standard ICU error code. Its input value must |
michael@0 | 277 | * pass the U_SUCCESS() test, or else the function returns |
michael@0 | 278 | * immediately. Check for U_FAILURE() on output or use with |
michael@0 | 279 | * function chaining. (See User Guide for details.) |
michael@0 | 280 | * @stable ICU 4.8 |
michael@0 | 281 | */ |
michael@0 | 282 | Iterator(const UCharsTrie &trie, int32_t maxStringLength, UErrorCode &errorCode); |
michael@0 | 283 | |
michael@0 | 284 | /** |
michael@0 | 285 | * Destructor. |
michael@0 | 286 | * @stable ICU 4.8 |
michael@0 | 287 | */ |
michael@0 | 288 | ~Iterator(); |
michael@0 | 289 | |
michael@0 | 290 | /** |
michael@0 | 291 | * Resets this iterator to its initial state. |
michael@0 | 292 | * @return *this |
michael@0 | 293 | * @stable ICU 4.8 |
michael@0 | 294 | */ |
michael@0 | 295 | Iterator &reset(); |
michael@0 | 296 | |
michael@0 | 297 | /** |
michael@0 | 298 | * @return TRUE if there are more elements. |
michael@0 | 299 | * @stable ICU 4.8 |
michael@0 | 300 | */ |
michael@0 | 301 | UBool hasNext() const; |
michael@0 | 302 | |
michael@0 | 303 | /** |
michael@0 | 304 | * Finds the next (string, value) pair if there is one. |
michael@0 | 305 | * |
michael@0 | 306 | * If the string is truncated to the maximum length and does not |
michael@0 | 307 | * have a real value, then the value is set to -1. |
michael@0 | 308 | * In this case, this "not a real value" is indistinguishable from |
michael@0 | 309 | * a real value of -1. |
michael@0 | 310 | * @param errorCode Standard ICU error code. Its input value must |
michael@0 | 311 | * pass the U_SUCCESS() test, or else the function returns |
michael@0 | 312 | * immediately. Check for U_FAILURE() on output or use with |
michael@0 | 313 | * function chaining. (See User Guide for details.) |
michael@0 | 314 | * @return TRUE if there is another element. |
michael@0 | 315 | * @stable ICU 4.8 |
michael@0 | 316 | */ |
michael@0 | 317 | UBool next(UErrorCode &errorCode); |
michael@0 | 318 | |
michael@0 | 319 | /** |
michael@0 | 320 | * @return The string for the last successful next(). |
michael@0 | 321 | * @stable ICU 4.8 |
michael@0 | 322 | */ |
michael@0 | 323 | const UnicodeString &getString() const { return str_; } |
michael@0 | 324 | /** |
michael@0 | 325 | * @return The value for the last successful next(). |
michael@0 | 326 | * @stable ICU 4.8 |
michael@0 | 327 | */ |
michael@0 | 328 | int32_t getValue() const { return value_; } |
michael@0 | 329 | |
michael@0 | 330 | private: |
michael@0 | 331 | UBool truncateAndStop() { |
michael@0 | 332 | pos_=NULL; |
michael@0 | 333 | value_=-1; // no real value for str |
michael@0 | 334 | return TRUE; |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | const UChar *branchNext(const UChar *pos, int32_t length, UErrorCode &errorCode); |
michael@0 | 338 | |
michael@0 | 339 | const UChar *uchars_; |
michael@0 | 340 | const UChar *pos_; |
michael@0 | 341 | const UChar *initialPos_; |
michael@0 | 342 | int32_t remainingMatchLength_; |
michael@0 | 343 | int32_t initialRemainingMatchLength_; |
michael@0 | 344 | UBool skipValue_; // Skip intermediate value which was already delivered. |
michael@0 | 345 | |
michael@0 | 346 | UnicodeString str_; |
michael@0 | 347 | int32_t maxLength_; |
michael@0 | 348 | int32_t value_; |
michael@0 | 349 | |
michael@0 | 350 | // The stack stores pairs of integers for backtracking to another |
michael@0 | 351 | // outbound edge of a branch node. |
michael@0 | 352 | // The first integer is an offset from uchars_. |
michael@0 | 353 | // The second integer has the str_.length() from before the node in bits 15..0, |
michael@0 | 354 | // and the remaining branch length in bits 31..16. |
michael@0 | 355 | // (We could store the remaining branch length minus 1 in bits 30..16 and not use the sign bit, |
michael@0 | 356 | // but the code looks more confusing that way.) |
michael@0 | 357 | UVector32 *stack_; |
michael@0 | 358 | }; |
michael@0 | 359 | |
michael@0 | 360 | private: |
michael@0 | 361 | friend class UCharsTrieBuilder; |
michael@0 | 362 | |
michael@0 | 363 | /** |
michael@0 | 364 | * Constructs a UCharsTrie reader instance. |
michael@0 | 365 | * Unlike the public constructor which just aliases an array, |
michael@0 | 366 | * this constructor adopts the builder's array. |
michael@0 | 367 | * This constructor is only called by the builder. |
michael@0 | 368 | */ |
michael@0 | 369 | UCharsTrie(UChar *adoptUChars, const UChar *trieUChars) |
michael@0 | 370 | : ownedArray_(adoptUChars), uchars_(trieUChars), |
michael@0 | 371 | pos_(uchars_), remainingMatchLength_(-1) {} |
michael@0 | 372 | |
michael@0 | 373 | // No assignment operator. |
michael@0 | 374 | UCharsTrie &operator=(const UCharsTrie &other); |
michael@0 | 375 | |
michael@0 | 376 | inline void stop() { |
michael@0 | 377 | pos_=NULL; |
michael@0 | 378 | } |
michael@0 | 379 | |
michael@0 | 380 | // Reads a compact 32-bit integer. |
michael@0 | 381 | // pos is already after the leadUnit, and the lead unit has bit 15 reset. |
michael@0 | 382 | static inline int32_t readValue(const UChar *pos, int32_t leadUnit) { |
michael@0 | 383 | int32_t value; |
michael@0 | 384 | if(leadUnit<kMinTwoUnitValueLead) { |
michael@0 | 385 | value=leadUnit; |
michael@0 | 386 | } else if(leadUnit<kThreeUnitValueLead) { |
michael@0 | 387 | value=((leadUnit-kMinTwoUnitValueLead)<<16)|*pos; |
michael@0 | 388 | } else { |
michael@0 | 389 | value=(pos[0]<<16)|pos[1]; |
michael@0 | 390 | } |
michael@0 | 391 | return value; |
michael@0 | 392 | } |
michael@0 | 393 | static inline const UChar *skipValue(const UChar *pos, int32_t leadUnit) { |
michael@0 | 394 | if(leadUnit>=kMinTwoUnitValueLead) { |
michael@0 | 395 | if(leadUnit<kThreeUnitValueLead) { |
michael@0 | 396 | ++pos; |
michael@0 | 397 | } else { |
michael@0 | 398 | pos+=2; |
michael@0 | 399 | } |
michael@0 | 400 | } |
michael@0 | 401 | return pos; |
michael@0 | 402 | } |
michael@0 | 403 | static inline const UChar *skipValue(const UChar *pos) { |
michael@0 | 404 | int32_t leadUnit=*pos++; |
michael@0 | 405 | return skipValue(pos, leadUnit&0x7fff); |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | static inline int32_t readNodeValue(const UChar *pos, int32_t leadUnit) { |
michael@0 | 409 | // U_ASSERT(kMinValueLead<=leadUnit && leadUnit<kValueIsFinal); |
michael@0 | 410 | int32_t value; |
michael@0 | 411 | if(leadUnit<kMinTwoUnitNodeValueLead) { |
michael@0 | 412 | value=(leadUnit>>6)-1; |
michael@0 | 413 | } else if(leadUnit<kThreeUnitNodeValueLead) { |
michael@0 | 414 | value=(((leadUnit&0x7fc0)-kMinTwoUnitNodeValueLead)<<10)|*pos; |
michael@0 | 415 | } else { |
michael@0 | 416 | value=(pos[0]<<16)|pos[1]; |
michael@0 | 417 | } |
michael@0 | 418 | return value; |
michael@0 | 419 | } |
michael@0 | 420 | static inline const UChar *skipNodeValue(const UChar *pos, int32_t leadUnit) { |
michael@0 | 421 | // U_ASSERT(kMinValueLead<=leadUnit && leadUnit<kValueIsFinal); |
michael@0 | 422 | if(leadUnit>=kMinTwoUnitNodeValueLead) { |
michael@0 | 423 | if(leadUnit<kThreeUnitNodeValueLead) { |
michael@0 | 424 | ++pos; |
michael@0 | 425 | } else { |
michael@0 | 426 | pos+=2; |
michael@0 | 427 | } |
michael@0 | 428 | } |
michael@0 | 429 | return pos; |
michael@0 | 430 | } |
michael@0 | 431 | |
michael@0 | 432 | static inline const UChar *jumpByDelta(const UChar *pos) { |
michael@0 | 433 | int32_t delta=*pos++; |
michael@0 | 434 | if(delta>=kMinTwoUnitDeltaLead) { |
michael@0 | 435 | if(delta==kThreeUnitDeltaLead) { |
michael@0 | 436 | delta=(pos[0]<<16)|pos[1]; |
michael@0 | 437 | pos+=2; |
michael@0 | 438 | } else { |
michael@0 | 439 | delta=((delta-kMinTwoUnitDeltaLead)<<16)|*pos++; |
michael@0 | 440 | } |
michael@0 | 441 | } |
michael@0 | 442 | return pos+delta; |
michael@0 | 443 | } |
michael@0 | 444 | |
michael@0 | 445 | static const UChar *skipDelta(const UChar *pos) { |
michael@0 | 446 | int32_t delta=*pos++; |
michael@0 | 447 | if(delta>=kMinTwoUnitDeltaLead) { |
michael@0 | 448 | if(delta==kThreeUnitDeltaLead) { |
michael@0 | 449 | pos+=2; |
michael@0 | 450 | } else { |
michael@0 | 451 | ++pos; |
michael@0 | 452 | } |
michael@0 | 453 | } |
michael@0 | 454 | return pos; |
michael@0 | 455 | } |
michael@0 | 456 | |
michael@0 | 457 | static inline UStringTrieResult valueResult(int32_t node) { |
michael@0 | 458 | return (UStringTrieResult)(USTRINGTRIE_INTERMEDIATE_VALUE-(node>>15)); |
michael@0 | 459 | } |
michael@0 | 460 | |
michael@0 | 461 | // Handles a branch node for both next(uchar) and next(string). |
michael@0 | 462 | UStringTrieResult branchNext(const UChar *pos, int32_t length, int32_t uchar); |
michael@0 | 463 | |
michael@0 | 464 | // Requires remainingLength_<0. |
michael@0 | 465 | UStringTrieResult nextImpl(const UChar *pos, int32_t uchar); |
michael@0 | 466 | |
michael@0 | 467 | // Helper functions for hasUniqueValue(). |
michael@0 | 468 | // Recursively finds a unique value (or whether there is not a unique one) |
michael@0 | 469 | // from a branch. |
michael@0 | 470 | static const UChar *findUniqueValueFromBranch(const UChar *pos, int32_t length, |
michael@0 | 471 | UBool haveUniqueValue, int32_t &uniqueValue); |
michael@0 | 472 | // Recursively finds a unique value (or whether there is not a unique one) |
michael@0 | 473 | // starting from a position on a node lead unit. |
michael@0 | 474 | static UBool findUniqueValue(const UChar *pos, UBool haveUniqueValue, int32_t &uniqueValue); |
michael@0 | 475 | |
michael@0 | 476 | // Helper functions for getNextUChars(). |
michael@0 | 477 | // getNextUChars() when pos is on a branch node. |
michael@0 | 478 | static void getNextBranchUChars(const UChar *pos, int32_t length, Appendable &out); |
michael@0 | 479 | |
michael@0 | 480 | // UCharsTrie data structure |
michael@0 | 481 | // |
michael@0 | 482 | // The trie consists of a series of UChar-serialized nodes for incremental |
michael@0 | 483 | // Unicode string/UChar sequence matching. (UChar=16-bit unsigned integer) |
michael@0 | 484 | // The root node is at the beginning of the trie data. |
michael@0 | 485 | // |
michael@0 | 486 | // Types of nodes are distinguished by their node lead unit ranges. |
michael@0 | 487 | // After each node, except a final-value node, another node follows to |
michael@0 | 488 | // encode match values or continue matching further units. |
michael@0 | 489 | // |
michael@0 | 490 | // Node types: |
michael@0 | 491 | // - Final-value node: Stores a 32-bit integer in a compact, variable-length format. |
michael@0 | 492 | // The value is for the string/UChar sequence so far. |
michael@0 | 493 | // - Match node, optionally with an intermediate value in a different compact format. |
michael@0 | 494 | // The value, if present, is for the string/UChar sequence so far. |
michael@0 | 495 | // |
michael@0 | 496 | // Aside from the value, which uses the node lead unit's high bits: |
michael@0 | 497 | // |
michael@0 | 498 | // - Linear-match node: Matches a number of units. |
michael@0 | 499 | // - Branch node: Branches to other nodes according to the current input unit. |
michael@0 | 500 | // The node unit is the length of the branch (number of units to select from) |
michael@0 | 501 | // minus 1. It is followed by a sub-node: |
michael@0 | 502 | // - If the length is at most kMaxBranchLinearSubNodeLength, then |
michael@0 | 503 | // there are length-1 (key, value) pairs and then one more comparison unit. |
michael@0 | 504 | // If one of the key units matches, then the value is either a final value for |
michael@0 | 505 | // the string so far, or a "jump" delta to the next node. |
michael@0 | 506 | // If the last unit matches, then matching continues with the next node. |
michael@0 | 507 | // (Values have the same encoding as final-value nodes.) |
michael@0 | 508 | // - If the length is greater than kMaxBranchLinearSubNodeLength, then |
michael@0 | 509 | // there is one unit and one "jump" delta. |
michael@0 | 510 | // If the input unit is less than the sub-node unit, then "jump" by delta to |
michael@0 | 511 | // the next sub-node which will have a length of length/2. |
michael@0 | 512 | // (The delta has its own compact encoding.) |
michael@0 | 513 | // Otherwise, skip the "jump" delta to the next sub-node |
michael@0 | 514 | // which will have a length of length-length/2. |
michael@0 | 515 | |
michael@0 | 516 | // Match-node lead unit values, after masking off intermediate-value bits: |
michael@0 | 517 | |
michael@0 | 518 | // 0000..002f: Branch node. If node!=0 then the length is node+1, otherwise |
michael@0 | 519 | // the length is one more than the next unit. |
michael@0 | 520 | |
michael@0 | 521 | // For a branch sub-node with at most this many entries, we drop down |
michael@0 | 522 | // to a linear search. |
michael@0 | 523 | static const int32_t kMaxBranchLinearSubNodeLength=5; |
michael@0 | 524 | |
michael@0 | 525 | // 0030..003f: Linear-match node, match 1..16 units and continue reading the next node. |
michael@0 | 526 | static const int32_t kMinLinearMatch=0x30; |
michael@0 | 527 | static const int32_t kMaxLinearMatchLength=0x10; |
michael@0 | 528 | |
michael@0 | 529 | // Match-node lead unit bits 14..6 for the optional intermediate value. |
michael@0 | 530 | // If these bits are 0, then there is no intermediate value. |
michael@0 | 531 | // Otherwise, see the *NodeValue* constants below. |
michael@0 | 532 | static const int32_t kMinValueLead=kMinLinearMatch+kMaxLinearMatchLength; // 0x0040 |
michael@0 | 533 | static const int32_t kNodeTypeMask=kMinValueLead-1; // 0x003f |
michael@0 | 534 | |
michael@0 | 535 | // A final-value node has bit 15 set. |
michael@0 | 536 | static const int32_t kValueIsFinal=0x8000; |
michael@0 | 537 | |
michael@0 | 538 | // Compact value: After testing and masking off bit 15, use the following thresholds. |
michael@0 | 539 | static const int32_t kMaxOneUnitValue=0x3fff; |
michael@0 | 540 | |
michael@0 | 541 | static const int32_t kMinTwoUnitValueLead=kMaxOneUnitValue+1; // 0x4000 |
michael@0 | 542 | static const int32_t kThreeUnitValueLead=0x7fff; |
michael@0 | 543 | |
michael@0 | 544 | static const int32_t kMaxTwoUnitValue=((kThreeUnitValueLead-kMinTwoUnitValueLead)<<16)-1; // 0x3ffeffff |
michael@0 | 545 | |
michael@0 | 546 | // Compact intermediate-value integer, lead unit shared with a branch or linear-match node. |
michael@0 | 547 | static const int32_t kMaxOneUnitNodeValue=0xff; |
michael@0 | 548 | static const int32_t kMinTwoUnitNodeValueLead=kMinValueLead+((kMaxOneUnitNodeValue+1)<<6); // 0x4040 |
michael@0 | 549 | static const int32_t kThreeUnitNodeValueLead=0x7fc0; |
michael@0 | 550 | |
michael@0 | 551 | static const int32_t kMaxTwoUnitNodeValue= |
michael@0 | 552 | ((kThreeUnitNodeValueLead-kMinTwoUnitNodeValueLead)<<10)-1; // 0xfdffff |
michael@0 | 553 | |
michael@0 | 554 | // Compact delta integers. |
michael@0 | 555 | static const int32_t kMaxOneUnitDelta=0xfbff; |
michael@0 | 556 | static const int32_t kMinTwoUnitDeltaLead=kMaxOneUnitDelta+1; // 0xfc00 |
michael@0 | 557 | static const int32_t kThreeUnitDeltaLead=0xffff; |
michael@0 | 558 | |
michael@0 | 559 | static const int32_t kMaxTwoUnitDelta=((kThreeUnitDeltaLead-kMinTwoUnitDeltaLead)<<16)-1; // 0x03feffff |
michael@0 | 560 | |
michael@0 | 561 | UChar *ownedArray_; |
michael@0 | 562 | |
michael@0 | 563 | // Fixed value referencing the UCharsTrie words. |
michael@0 | 564 | const UChar *uchars_; |
michael@0 | 565 | |
michael@0 | 566 | // Iterator variables. |
michael@0 | 567 | |
michael@0 | 568 | // Pointer to next trie unit to read. NULL if no more matches. |
michael@0 | 569 | const UChar *pos_; |
michael@0 | 570 | // Remaining length of a linear-match node, minus 1. Negative if not in such a node. |
michael@0 | 571 | int32_t remainingMatchLength_; |
michael@0 | 572 | }; |
michael@0 | 573 | |
michael@0 | 574 | U_NAMESPACE_END |
michael@0 | 575 | |
michael@0 | 576 | #endif // __UCHARSTRIE_H__ |