17 #ifndef __BYTESTRIE_H__ 18 #define __BYTESTRIE_H__ 27 #if U_SHOW_CPLUSPLUS_API 36 class BytesTrieBuilder;
70 : ownedArray_(
NULL), bytes_(static_cast<const uint8_t *>(trieBytes)),
71 pos_(bytes_), remainingMatchLength_(-1) {}
86 : ownedArray_(
NULL), bytes_(other.bytes_),
87 pos_(other.pos_), remainingMatchLength_(other.remainingMatchLength_) {}
96 remainingMatchLength_=-1;
100 #ifndef U_HIDE_DRAFT_API 110 return (static_cast<uint64_t>(remainingMatchLength_ + 2) << kState64RemainingShift) |
111 (uint64_t)(pos_ - bytes_);
129 remainingMatchLength_ =
static_cast<int32_t
>(state >> kState64RemainingShift) - 2;
130 pos_ = bytes_ + (state & kState64PosMask);
150 const uint8_t *bytes;
152 int32_t remainingMatchLength;
165 state.remainingMatchLength=remainingMatchLength_;
180 if(bytes_==state.bytes && bytes_!=
NULL) {
182 remainingMatchLength_=state.remainingMatchLength;
204 remainingMatchLength_=-1;
208 return nextImpl(bytes_, inByte);
247 const uint8_t *pos=pos_;
248 int32_t leadByte=*pos++;
250 return readValue(pos, leadByte>>1);
263 const uint8_t *pos=pos_;
265 return pos!=
NULL && findUniqueValue(pos+remainingMatchLength_+1,
FALSE, uniqueValue);
276 int32_t getNextBytes(
ByteSink &out)
const;
327 UBool hasNext()
const;
357 UBool truncateAndStop();
359 const uint8_t *branchNext(
const uint8_t *pos, int32_t length,
UErrorCode &errorCode);
361 const uint8_t *bytes_;
363 const uint8_t *initialPos_;
364 int32_t remainingMatchLength_;
365 int32_t initialRemainingMatchLength_;
390 BytesTrie(
void *adoptBytes,
const void *trieBytes)
391 : ownedArray_(static_cast<uint8_t *>(adoptBytes)),
392 bytes_(static_cast<const uint8_t *>(trieBytes)),
393 pos_(bytes_), remainingMatchLength_(-1) {}
404 static int32_t readValue(
const uint8_t *pos, int32_t leadByte);
405 static inline const uint8_t *skipValue(
const uint8_t *pos, int32_t leadByte) {
407 if(leadByte>=(kMinTwoByteValueLead<<1)) {
408 if(leadByte<(kMinThreeByteValueLead<<1)) {
410 }
else if(leadByte<(kFourByteValueLead<<1)) {
413 pos+=3+((leadByte>>1)&1);
418 static inline const uint8_t *skipValue(
const uint8_t *pos) {
419 int32_t leadByte=*pos++;
420 return skipValue(pos, leadByte);
424 static const uint8_t *jumpByDelta(
const uint8_t *pos);
426 static inline const uint8_t *skipDelta(
const uint8_t *pos) {
427 int32_t delta=*pos++;
428 if(delta>=kMinTwoByteDeltaLead) {
429 if(delta<kMinThreeByteDeltaLead) {
431 }
else if(delta<kFourByteDeltaLead) {
445 UStringTrieResult branchNext(
const uint8_t *pos, int32_t length, int32_t inByte);
453 static const uint8_t *findUniqueValueFromBranch(
const uint8_t *pos, int32_t length,
454 UBool haveUniqueValue, int32_t &uniqueValue);
457 static UBool findUniqueValue(
const uint8_t *pos,
UBool haveUniqueValue, int32_t &uniqueValue);
461 static void getNextBranchBytes(
const uint8_t *pos, int32_t length,
ByteSink &out);
462 static void append(
ByteSink &out,
int c);
503 static const int32_t kMaxBranchLinearSubNodeLength=5;
506 static const int32_t kMinLinearMatch=0x10;
507 static const int32_t kMaxLinearMatchLength=0x10;
514 static const int32_t kMinValueLead=kMinLinearMatch+kMaxLinearMatchLength;
516 static const int32_t kValueIsFinal=1;
519 static const int32_t kMinOneByteValueLead=kMinValueLead/2;
520 static const int32_t kMaxOneByteValue=0x40;
522 static const int32_t kMinTwoByteValueLead=kMinOneByteValueLead+kMaxOneByteValue+1;
523 static const int32_t kMaxTwoByteValue=0x1aff;
525 static const int32_t kMinThreeByteValueLead=kMinTwoByteValueLead+(kMaxTwoByteValue>>8)+1;
526 static const int32_t kFourByteValueLead=0x7e;
529 static const int32_t kMaxThreeByteValue=((kFourByteValueLead-kMinThreeByteValueLead)<<16)-1;
531 static const int32_t kFiveByteValueLead=0x7f;
534 static const int32_t kMaxOneByteDelta=0xbf;
535 static const int32_t kMinTwoByteDeltaLead=kMaxOneByteDelta+1;
536 static const int32_t kMinThreeByteDeltaLead=0xf0;
537 static const int32_t kFourByteDeltaLead=0xfe;
538 static const int32_t kFiveByteDeltaLead=0xff;
540 static const int32_t kMaxTwoByteDelta=((kMinThreeByteDeltaLead-kMinTwoByteDeltaLead)<<8)-1;
541 static const int32_t kMaxThreeByteDelta=((kFourByteDeltaLead-kMinThreeByteDeltaLead)<<16)-1;
547 static constexpr int32_t kState64RemainingShift = 59;
548 static constexpr uint64_t kState64PosMask = (
UINT64_C(1) << kState64RemainingShift) - 1;
550 uint8_t *ownedArray_;
553 const uint8_t *bytes_;
560 int32_t remainingMatchLength_;
567 #endif // __BYTESTRIE_H__ BytesTrie & resetToState64(uint64_t state)
Resets this trie to the saved state.
Builder class for BytesTrie.
BytesTrie state object, for saving a trie's current state and resetting the trie back to this state l...
UStringTrieResult
Return values for BytesTrie::next(), UCharsTrie::next() and similar methods.
BytesTrie(const void *trieBytes)
Constructs a BytesTrie reader instance.
A ByteSink can be filled with bytes.
UBool hasUniqueValue(int32_t &uniqueValue) const
Determines whether all byte sequences reachable from the current state map to the same value...
Light-weight, non-const reader class for a BytesTrie.
BytesTrie & reset()
Resets this trie to its initial state.
C++ API: StringPiece: Read-only byte string wrapper class.
UStringTrieResult first(int32_t inByte)
Traverses the trie from the initial state for this input byte.
int32_t getValue() const
Returns a matching byte sequence's value if called immediately after current()/first()/next() returne...
BytesTrie(const BytesTrie &other)
Copy constructor, copies the other trie reader object and its state, but not the byte array which wil...
#define NULL
Define NULL if necessary, to nullptr for C++ and to ((void *)0) for C.
State()
Constructs an empty State.
Iterator for all of the (byte sequence, value) pairs in a BytesTrie.
C++ API: Common ICU base class UObject.
uint64_t getState64() const
Returns the state of this trie as a 64-bit integer.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
C API: Helper definitions for dictionary trie APIs.
const BytesTrie & saveState(State &state) const
Saves the state of this trie.
Basic definitions for ICU, for both C and C++ APIs.
#define FALSE
The FALSE value of a UBool.
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
The input unit(s) continued a matching string and there is a value for the string so far...
#define UINT64_C(c)
Provides a platform independent way to specify an unsigned 64-bit integer constant.
A string-like object that points to a sized piece of memory.
BytesTrie & resetToState(const State &state)
Resets this trie to the saved state.
UMemory is the common ICU base class.
int8_t UBool
The ICU boolean type.