intl/icu/source/common/ucharstrieiterator.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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-2011, International Business Machines
michael@0 4 * Corporation and others. All Rights Reserved.
michael@0 5 *******************************************************************************
michael@0 6 * file name: ucharstrieiterator.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: 2010nov15
michael@0 12 * created by: Markus W. Scherer
michael@0 13 */
michael@0 14
michael@0 15 #include "unicode/utypes.h"
michael@0 16 #include "unicode/ucharstrie.h"
michael@0 17 #include "unicode/unistr.h"
michael@0 18 #include "uvectr32.h"
michael@0 19
michael@0 20 U_NAMESPACE_BEGIN
michael@0 21
michael@0 22 UCharsTrie::Iterator::Iterator(const UChar *trieUChars, int32_t maxStringLength,
michael@0 23 UErrorCode &errorCode)
michael@0 24 : uchars_(trieUChars),
michael@0 25 pos_(uchars_), initialPos_(uchars_),
michael@0 26 remainingMatchLength_(-1), initialRemainingMatchLength_(-1),
michael@0 27 skipValue_(FALSE),
michael@0 28 maxLength_(maxStringLength), value_(0), stack_(NULL) {
michael@0 29 if(U_FAILURE(errorCode)) {
michael@0 30 return;
michael@0 31 }
michael@0 32 // stack_ is a pointer so that it's easy to turn ucharstrie.h into
michael@0 33 // a public API header for which we would want it to depend only on
michael@0 34 // other public headers.
michael@0 35 // Unlike UCharsTrie itself, its Iterator performs memory allocations anyway
michael@0 36 // via the UnicodeString and UVector32 implementations, so this additional
michael@0 37 // cost is minimal.
michael@0 38 stack_=new UVector32(errorCode);
michael@0 39 if(stack_==NULL) {
michael@0 40 errorCode=U_MEMORY_ALLOCATION_ERROR;
michael@0 41 }
michael@0 42 }
michael@0 43
michael@0 44 UCharsTrie::Iterator::Iterator(const UCharsTrie &trie, int32_t maxStringLength,
michael@0 45 UErrorCode &errorCode)
michael@0 46 : uchars_(trie.uchars_), pos_(trie.pos_), initialPos_(trie.pos_),
michael@0 47 remainingMatchLength_(trie.remainingMatchLength_),
michael@0 48 initialRemainingMatchLength_(trie.remainingMatchLength_),
michael@0 49 skipValue_(FALSE),
michael@0 50 maxLength_(maxStringLength), value_(0), stack_(NULL) {
michael@0 51 if(U_FAILURE(errorCode)) {
michael@0 52 return;
michael@0 53 }
michael@0 54 stack_=new UVector32(errorCode);
michael@0 55 if(U_FAILURE(errorCode)) {
michael@0 56 return;
michael@0 57 }
michael@0 58 if(stack_==NULL) {
michael@0 59 errorCode=U_MEMORY_ALLOCATION_ERROR;
michael@0 60 return;
michael@0 61 }
michael@0 62 int32_t length=remainingMatchLength_; // Actual remaining match length minus 1.
michael@0 63 if(length>=0) {
michael@0 64 // Pending linear-match node, append remaining UChars to str_.
michael@0 65 ++length;
michael@0 66 if(maxLength_>0 && length>maxLength_) {
michael@0 67 length=maxLength_; // This will leave remainingMatchLength>=0 as a signal.
michael@0 68 }
michael@0 69 str_.append(pos_, length);
michael@0 70 pos_+=length;
michael@0 71 remainingMatchLength_-=length;
michael@0 72 }
michael@0 73 }
michael@0 74
michael@0 75 UCharsTrie::Iterator::~Iterator() {
michael@0 76 delete stack_;
michael@0 77 }
michael@0 78
michael@0 79 UCharsTrie::Iterator &
michael@0 80 UCharsTrie::Iterator::reset() {
michael@0 81 pos_=initialPos_;
michael@0 82 remainingMatchLength_=initialRemainingMatchLength_;
michael@0 83 skipValue_=FALSE;
michael@0 84 int32_t length=remainingMatchLength_+1; // Remaining match length.
michael@0 85 if(maxLength_>0 && length>maxLength_) {
michael@0 86 length=maxLength_;
michael@0 87 }
michael@0 88 str_.truncate(length);
michael@0 89 pos_+=length;
michael@0 90 remainingMatchLength_-=length;
michael@0 91 stack_->setSize(0);
michael@0 92 return *this;
michael@0 93 }
michael@0 94
michael@0 95 UBool
michael@0 96 UCharsTrie::Iterator::hasNext() const { return pos_!=NULL || !stack_->isEmpty(); }
michael@0 97
michael@0 98 UBool
michael@0 99 UCharsTrie::Iterator::next(UErrorCode &errorCode) {
michael@0 100 if(U_FAILURE(errorCode)) {
michael@0 101 return FALSE;
michael@0 102 }
michael@0 103 const UChar *pos=pos_;
michael@0 104 if(pos==NULL) {
michael@0 105 if(stack_->isEmpty()) {
michael@0 106 return FALSE;
michael@0 107 }
michael@0 108 // Pop the state off the stack and continue with the next outbound edge of
michael@0 109 // the branch node.
michael@0 110 int32_t stackSize=stack_->size();
michael@0 111 int32_t length=stack_->elementAti(stackSize-1);
michael@0 112 pos=uchars_+stack_->elementAti(stackSize-2);
michael@0 113 stack_->setSize(stackSize-2);
michael@0 114 str_.truncate(length&0xffff);
michael@0 115 length=(int32_t)((uint32_t)length>>16);
michael@0 116 if(length>1) {
michael@0 117 pos=branchNext(pos, length, errorCode);
michael@0 118 if(pos==NULL) {
michael@0 119 return TRUE; // Reached a final value.
michael@0 120 }
michael@0 121 } else {
michael@0 122 str_.append(*pos++);
michael@0 123 }
michael@0 124 }
michael@0 125 if(remainingMatchLength_>=0) {
michael@0 126 // We only get here if we started in a pending linear-match node
michael@0 127 // with more than maxLength remaining units.
michael@0 128 return truncateAndStop();
michael@0 129 }
michael@0 130 for(;;) {
michael@0 131 int32_t node=*pos++;
michael@0 132 if(node>=kMinValueLead) {
michael@0 133 if(skipValue_) {
michael@0 134 pos=skipNodeValue(pos, node);
michael@0 135 node&=kNodeTypeMask;
michael@0 136 skipValue_=FALSE;
michael@0 137 } else {
michael@0 138 // Deliver value for the string so far.
michael@0 139 UBool isFinal=(UBool)(node>>15);
michael@0 140 if(isFinal) {
michael@0 141 value_=readValue(pos, node&0x7fff);
michael@0 142 } else {
michael@0 143 value_=readNodeValue(pos, node);
michael@0 144 }
michael@0 145 if(isFinal || (maxLength_>0 && str_.length()==maxLength_)) {
michael@0 146 pos_=NULL;
michael@0 147 } else {
michael@0 148 // We cannot skip the value right here because it shares its
michael@0 149 // lead unit with a match node which we have to evaluate
michael@0 150 // next time.
michael@0 151 // Instead, keep pos_ on the node lead unit itself.
michael@0 152 pos_=pos-1;
michael@0 153 skipValue_=TRUE;
michael@0 154 }
michael@0 155 return TRUE;
michael@0 156 }
michael@0 157 }
michael@0 158 if(maxLength_>0 && str_.length()==maxLength_) {
michael@0 159 return truncateAndStop();
michael@0 160 }
michael@0 161 if(node<kMinLinearMatch) {
michael@0 162 if(node==0) {
michael@0 163 node=*pos++;
michael@0 164 }
michael@0 165 pos=branchNext(pos, node+1, errorCode);
michael@0 166 if(pos==NULL) {
michael@0 167 return TRUE; // Reached a final value.
michael@0 168 }
michael@0 169 } else {
michael@0 170 // Linear-match node, append length units to str_.
michael@0 171 int32_t length=node-kMinLinearMatch+1;
michael@0 172 if(maxLength_>0 && str_.length()+length>maxLength_) {
michael@0 173 str_.append(pos, maxLength_-str_.length());
michael@0 174 return truncateAndStop();
michael@0 175 }
michael@0 176 str_.append(pos, length);
michael@0 177 pos+=length;
michael@0 178 }
michael@0 179 }
michael@0 180 }
michael@0 181
michael@0 182 // Branch node, needs to take the first outbound edge and push state for the rest.
michael@0 183 const UChar *
michael@0 184 UCharsTrie::Iterator::branchNext(const UChar *pos, int32_t length, UErrorCode &errorCode) {
michael@0 185 while(length>kMaxBranchLinearSubNodeLength) {
michael@0 186 ++pos; // ignore the comparison unit
michael@0 187 // Push state for the greater-or-equal edge.
michael@0 188 stack_->addElement((int32_t)(skipDelta(pos)-uchars_), errorCode);
michael@0 189 stack_->addElement(((length-(length>>1))<<16)|str_.length(), errorCode);
michael@0 190 // Follow the less-than edge.
michael@0 191 length>>=1;
michael@0 192 pos=jumpByDelta(pos);
michael@0 193 }
michael@0 194 // List of key-value pairs where values are either final values or jump deltas.
michael@0 195 // Read the first (key, value) pair.
michael@0 196 UChar trieUnit=*pos++;
michael@0 197 int32_t node=*pos++;
michael@0 198 UBool isFinal=(UBool)(node>>15);
michael@0 199 int32_t value=readValue(pos, node&=0x7fff);
michael@0 200 pos=skipValue(pos, node);
michael@0 201 stack_->addElement((int32_t)(pos-uchars_), errorCode);
michael@0 202 stack_->addElement(((length-1)<<16)|str_.length(), errorCode);
michael@0 203 str_.append(trieUnit);
michael@0 204 if(isFinal) {
michael@0 205 pos_=NULL;
michael@0 206 value_=value;
michael@0 207 return NULL;
michael@0 208 } else {
michael@0 209 return pos+value;
michael@0 210 }
michael@0 211 }
michael@0 212
michael@0 213 U_NAMESPACE_END

mercurial