/src/icu/icu4c/source/common/bytestrie.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ******************************************************************************* |
5 | | * Copyright (C) 2010-2011, International Business Machines |
6 | | * Corporation and others. All Rights Reserved. |
7 | | ******************************************************************************* |
8 | | * file name: bytestrie.cpp |
9 | | * encoding: UTF-8 |
10 | | * tab size: 8 (not used) |
11 | | * indentation:4 |
12 | | * |
13 | | * created on: 2010sep25 |
14 | | * created by: Markus W. Scherer |
15 | | */ |
16 | | |
17 | | #include "unicode/utypes.h" |
18 | | #include "unicode/bytestream.h" |
19 | | #include "unicode/bytestrie.h" |
20 | | #include "unicode/uobject.h" |
21 | | #include "cmemory.h" |
22 | | #include "uassert.h" |
23 | | |
24 | | U_NAMESPACE_BEGIN |
25 | | |
26 | 13.3k | BytesTrie::~BytesTrie() { |
27 | 13.3k | uprv_free(ownedArray_); |
28 | 13.3k | } |
29 | | |
30 | | // lead byte already shifted right by 1. |
31 | | int32_t |
32 | 13.3k | BytesTrie::readValue(const uint8_t *pos, int32_t leadByte) { |
33 | 13.3k | int32_t value; |
34 | 13.3k | if(leadByte<kMinTwoByteValueLead) { |
35 | 0 | value=leadByte-kMinOneByteValueLead; |
36 | 13.3k | } else if(leadByte<kMinThreeByteValueLead) { |
37 | 13.1k | value=((leadByte-kMinTwoByteValueLead)<<8)|*pos; |
38 | 13.1k | } else if(leadByte<kFourByteValueLead) { |
39 | 236 | value=((leadByte-kMinThreeByteValueLead)<<16)|(pos[0]<<8)|pos[1]; |
40 | 236 | } else if(leadByte==kFourByteValueLead) { |
41 | 0 | value=(pos[0]<<16)|(pos[1]<<8)|pos[2]; |
42 | 0 | } else { |
43 | 0 | value=(pos[0]<<24)|(pos[1]<<16)|(pos[2]<<8)|pos[3]; |
44 | 0 | } |
45 | 13.3k | return value; |
46 | 13.3k | } |
47 | | |
48 | | const uint8_t * |
49 | 52.5k | BytesTrie::jumpByDelta(const uint8_t *pos) { |
50 | 52.5k | int32_t delta=*pos++; |
51 | 52.5k | if(delta<kMinTwoByteDeltaLead) { |
52 | | // nothing to do |
53 | 39.9k | } else if(delta<kMinThreeByteDeltaLead) { |
54 | 35.1k | delta=((delta-kMinTwoByteDeltaLead)<<8)|*pos++; |
55 | 35.1k | } else if(delta<kFourByteDeltaLead) { |
56 | 4.71k | delta=((delta-kMinThreeByteDeltaLead)<<16)|(pos[0]<<8)|pos[1]; |
57 | 4.71k | pos+=2; |
58 | 4.71k | } else if(delta==kFourByteDeltaLead) { |
59 | 0 | delta=(pos[0]<<16)|(pos[1]<<8)|pos[2]; |
60 | 0 | pos+=3; |
61 | 0 | } else { |
62 | 0 | delta=(pos[0]<<24)|(pos[1]<<16)|(pos[2]<<8)|pos[3]; |
63 | 0 | pos+=4; |
64 | 0 | } |
65 | 52.5k | return pos+delta; |
66 | 52.5k | } |
67 | | |
68 | | UStringTrieResult |
69 | 0 | BytesTrie::current() const { |
70 | 0 | const uint8_t *pos=pos_; |
71 | 0 | if(pos==nullptr) { |
72 | 0 | return USTRINGTRIE_NO_MATCH; |
73 | 0 | } else { |
74 | 0 | int32_t node; |
75 | 0 | return (remainingMatchLength_<0 && (node=*pos)>=kMinValueLead) ? |
76 | 0 | valueResult(node) : USTRINGTRIE_NO_VALUE; |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | UStringTrieResult |
81 | 28.3k | BytesTrie::branchNext(const uint8_t *pos, int32_t length, int32_t inByte) { |
82 | | // Branch according to the current byte. |
83 | 28.3k | if(length==0) { |
84 | 24.2k | length=*pos++; |
85 | 24.2k | } |
86 | 28.3k | ++length; |
87 | | // The length of the branch is the number of bytes to select from. |
88 | | // The data structure encodes a binary search. |
89 | 101k | while(length>kMaxBranchLinearSubNodeLength) { |
90 | 73.5k | if(inByte<*pos++) { |
91 | 52.5k | length>>=1; |
92 | 52.5k | pos=jumpByDelta(pos); |
93 | 52.5k | } else { |
94 | 20.9k | length=length-(length>>1); |
95 | 20.9k | pos=skipDelta(pos); |
96 | 20.9k | } |
97 | 73.5k | } |
98 | | // Drop down to linear search for the last few bytes. |
99 | | // length>=2 because the loop body above sees length>kMaxBranchLinearSubNodeLength>=3 |
100 | | // and divides length by 2. |
101 | 43.8k | do { |
102 | 43.8k | if(inByte==*pos++) { |
103 | 23.6k | UStringTrieResult result; |
104 | 23.6k | int32_t node=*pos; |
105 | 23.6k | U_ASSERT(node>=kMinValueLead); |
106 | 23.6k | if(node&kValueIsFinal) { |
107 | | // Leave the final value for getValue() to read. |
108 | 11.4k | result=USTRINGTRIE_FINAL_VALUE; |
109 | 12.1k | } else { |
110 | | // Use the non-final value as the jump delta. |
111 | 12.1k | ++pos; |
112 | | // int32_t delta=readValue(pos, node>>1); |
113 | 12.1k | node>>=1; |
114 | 12.1k | int32_t delta; |
115 | 12.1k | if(node<kMinTwoByteValueLead) { |
116 | 1.14k | delta=node-kMinOneByteValueLead; |
117 | 10.9k | } else if(node<kMinThreeByteValueLead) { |
118 | 10.9k | delta=((node-kMinTwoByteValueLead)<<8)|*pos++; |
119 | 10.9k | } else if(node<kFourByteValueLead) { |
120 | 86 | delta=((node-kMinThreeByteValueLead)<<16)|(pos[0]<<8)|pos[1]; |
121 | 86 | pos+=2; |
122 | 86 | } else if(node==kFourByteValueLead) { |
123 | 0 | delta=(pos[0]<<16)|(pos[1]<<8)|pos[2]; |
124 | 0 | pos+=3; |
125 | 0 | } else { |
126 | 0 | delta=(pos[0]<<24)|(pos[1]<<16)|(pos[2]<<8)|pos[3]; |
127 | 0 | pos+=4; |
128 | 0 | } |
129 | | // end readValue() |
130 | 12.1k | pos+=delta; |
131 | 12.1k | node=*pos; |
132 | 12.1k | result= node>=kMinValueLead ? valueResult(node) : USTRINGTRIE_NO_VALUE; |
133 | 12.1k | } |
134 | 23.6k | pos_=pos; |
135 | 23.6k | return result; |
136 | 23.6k | } |
137 | 20.2k | --length; |
138 | 20.2k | pos=skipValue(pos); |
139 | 20.2k | } while(length>1); |
140 | 4.75k | if(inByte==*pos++) { |
141 | 4.46k | pos_=pos; |
142 | 4.46k | int32_t node=*pos; |
143 | 4.46k | return node>=kMinValueLead ? valueResult(node) : USTRINGTRIE_NO_VALUE; |
144 | 4.46k | } else { |
145 | 286 | stop(); |
146 | 286 | return USTRINGTRIE_NO_MATCH; |
147 | 286 | } |
148 | 4.75k | } |
149 | | |
150 | | UStringTrieResult |
151 | 28.6k | BytesTrie::nextImpl(const uint8_t *pos, int32_t inByte) { |
152 | 28.8k | for(;;) { |
153 | 28.8k | int32_t node=*pos++; |
154 | 28.8k | if(node<kMinLinearMatch) { |
155 | 28.3k | return branchNext(pos, node, inByte); |
156 | 28.3k | } else if(node<kMinValueLead) { |
157 | | // Match the first of length+1 bytes. |
158 | 280 | int32_t length=node-kMinLinearMatch; // Actual match length minus 1. |
159 | 280 | if(inByte==*pos++) { |
160 | 263 | remainingMatchLength_=--length; |
161 | 263 | pos_=pos; |
162 | 263 | return (length<0 && (node=*pos)>=kMinValueLead) ? |
163 | 186 | valueResult(node) : USTRINGTRIE_NO_VALUE; |
164 | 263 | } else { |
165 | | // No match. |
166 | 17 | break; |
167 | 17 | } |
168 | 280 | } else if(node&kValueIsFinal) { |
169 | | // No further matching bytes. |
170 | 0 | break; |
171 | 212 | } else { |
172 | | // Skip intermediate value. |
173 | 212 | pos=skipValue(pos, node); |
174 | | // The next node must not also be a value node. |
175 | 212 | U_ASSERT(*pos<kMinValueLead); |
176 | 212 | } |
177 | 28.8k | } |
178 | 17 | stop(); |
179 | 17 | return USTRINGTRIE_NO_MATCH; |
180 | 28.6k | } |
181 | | |
182 | | UStringTrieResult |
183 | 28.9k | BytesTrie::next(int32_t inByte) { |
184 | 28.9k | const uint8_t *pos=pos_; |
185 | 28.9k | if(pos==nullptr) { |
186 | 0 | return USTRINGTRIE_NO_MATCH; |
187 | 0 | } |
188 | 28.9k | if(inByte<0) { |
189 | 0 | inByte+=0x100; |
190 | 0 | } |
191 | 28.9k | int32_t length=remainingMatchLength_; // Actual remaining match length minus 1. |
192 | 28.9k | if(length>=0) { |
193 | | // Remaining part of a linear-match node. |
194 | 334 | if(inByte==*pos++) { |
195 | 325 | remainingMatchLength_=--length; |
196 | 325 | pos_=pos; |
197 | 325 | int32_t node; |
198 | 325 | return (length<0 && (node=*pos)>=kMinValueLead) ? |
199 | 186 | valueResult(node) : USTRINGTRIE_NO_VALUE; |
200 | 325 | } else { |
201 | 9 | stop(); |
202 | 9 | return USTRINGTRIE_NO_MATCH; |
203 | 9 | } |
204 | 334 | } |
205 | 28.6k | return nextImpl(pos, inByte); |
206 | 28.9k | } |
207 | | |
208 | | UStringTrieResult |
209 | 0 | BytesTrie::next(const char *s, int32_t sLength) { |
210 | 0 | if(sLength<0 ? *s==0 : sLength==0) { |
211 | | // Empty input. |
212 | 0 | return current(); |
213 | 0 | } |
214 | 0 | const uint8_t *pos=pos_; |
215 | 0 | if(pos==nullptr) { |
216 | 0 | return USTRINGTRIE_NO_MATCH; |
217 | 0 | } |
218 | 0 | int32_t length=remainingMatchLength_; // Actual remaining match length minus 1. |
219 | 0 | for(;;) { |
220 | | // Fetch the next input byte, if there is one. |
221 | | // Continue a linear-match node without rechecking sLength<0. |
222 | 0 | int32_t inByte; |
223 | 0 | if(sLength<0) { |
224 | 0 | for(;;) { |
225 | 0 | if((inByte=*s++)==0) { |
226 | 0 | remainingMatchLength_=length; |
227 | 0 | pos_=pos; |
228 | 0 | int32_t node; |
229 | 0 | return (length<0 && (node=*pos)>=kMinValueLead) ? |
230 | 0 | valueResult(node) : USTRINGTRIE_NO_VALUE; |
231 | 0 | } |
232 | 0 | if(length<0) { |
233 | 0 | remainingMatchLength_=length; |
234 | 0 | break; |
235 | 0 | } |
236 | 0 | if(inByte!=*pos) { |
237 | 0 | stop(); |
238 | 0 | return USTRINGTRIE_NO_MATCH; |
239 | 0 | } |
240 | 0 | ++pos; |
241 | 0 | --length; |
242 | 0 | } |
243 | 0 | } else { |
244 | 0 | for(;;) { |
245 | 0 | if(sLength==0) { |
246 | 0 | remainingMatchLength_=length; |
247 | 0 | pos_=pos; |
248 | 0 | int32_t node; |
249 | 0 | return (length<0 && (node=*pos)>=kMinValueLead) ? |
250 | 0 | valueResult(node) : USTRINGTRIE_NO_VALUE; |
251 | 0 | } |
252 | 0 | inByte=*s++; |
253 | 0 | --sLength; |
254 | 0 | if(length<0) { |
255 | 0 | remainingMatchLength_=length; |
256 | 0 | break; |
257 | 0 | } |
258 | 0 | if(inByte!=*pos) { |
259 | 0 | stop(); |
260 | 0 | return USTRINGTRIE_NO_MATCH; |
261 | 0 | } |
262 | 0 | ++pos; |
263 | 0 | --length; |
264 | 0 | } |
265 | 0 | } |
266 | 0 | for(;;) { |
267 | 0 | int32_t node=*pos++; |
268 | 0 | if(node<kMinLinearMatch) { |
269 | 0 | UStringTrieResult result=branchNext(pos, node, inByte); |
270 | 0 | if(result==USTRINGTRIE_NO_MATCH) { |
271 | 0 | return USTRINGTRIE_NO_MATCH; |
272 | 0 | } |
273 | | // Fetch the next input byte, if there is one. |
274 | 0 | if(sLength<0) { |
275 | 0 | if((inByte=*s++)==0) { |
276 | 0 | return result; |
277 | 0 | } |
278 | 0 | } else { |
279 | 0 | if(sLength==0) { |
280 | 0 | return result; |
281 | 0 | } |
282 | 0 | inByte=*s++; |
283 | 0 | --sLength; |
284 | 0 | } |
285 | 0 | if(result==USTRINGTRIE_FINAL_VALUE) { |
286 | | // No further matching bytes. |
287 | 0 | stop(); |
288 | 0 | return USTRINGTRIE_NO_MATCH; |
289 | 0 | } |
290 | 0 | pos=pos_; // branchNext() advanced pos and wrote it to pos_ . |
291 | 0 | } else if(node<kMinValueLead) { |
292 | | // Match length+1 bytes. |
293 | 0 | length=node-kMinLinearMatch; // Actual match length minus 1. |
294 | 0 | if(inByte!=*pos) { |
295 | 0 | stop(); |
296 | 0 | return USTRINGTRIE_NO_MATCH; |
297 | 0 | } |
298 | 0 | ++pos; |
299 | 0 | --length; |
300 | 0 | break; |
301 | 0 | } else if(node&kValueIsFinal) { |
302 | | // No further matching bytes. |
303 | 0 | stop(); |
304 | 0 | return USTRINGTRIE_NO_MATCH; |
305 | 0 | } else { |
306 | | // Skip intermediate value. |
307 | 0 | pos=skipValue(pos, node); |
308 | | // The next node must not also be a value node. |
309 | 0 | U_ASSERT(*pos<kMinValueLead); |
310 | 0 | } |
311 | 0 | } |
312 | 0 | } |
313 | 0 | } |
314 | | |
315 | | const uint8_t * |
316 | | BytesTrie::findUniqueValueFromBranch(const uint8_t *pos, int32_t length, |
317 | 0 | UBool haveUniqueValue, int32_t &uniqueValue) { |
318 | 0 | while(length>kMaxBranchLinearSubNodeLength) { |
319 | 0 | ++pos; // ignore the comparison byte |
320 | 0 | if(nullptr==findUniqueValueFromBranch(jumpByDelta(pos), length>>1, haveUniqueValue, uniqueValue)) { |
321 | 0 | return nullptr; |
322 | 0 | } |
323 | 0 | length=length-(length>>1); |
324 | 0 | pos=skipDelta(pos); |
325 | 0 | } |
326 | 0 | do { |
327 | 0 | ++pos; // ignore a comparison byte |
328 | | // handle its value |
329 | 0 | int32_t node=*pos++; |
330 | 0 | UBool isFinal = static_cast<UBool>(node & kValueIsFinal); |
331 | 0 | int32_t value=readValue(pos, node>>1); |
332 | 0 | pos=skipValue(pos, node); |
333 | 0 | if(isFinal) { |
334 | 0 | if(haveUniqueValue) { |
335 | 0 | if(value!=uniqueValue) { |
336 | 0 | return nullptr; |
337 | 0 | } |
338 | 0 | } else { |
339 | 0 | uniqueValue=value; |
340 | 0 | haveUniqueValue=true; |
341 | 0 | } |
342 | 0 | } else { |
343 | 0 | if(!findUniqueValue(pos+value, haveUniqueValue, uniqueValue)) { |
344 | 0 | return nullptr; |
345 | 0 | } |
346 | 0 | haveUniqueValue=true; |
347 | 0 | } |
348 | 0 | } while(--length>1); |
349 | 0 | return pos+1; // ignore the last comparison byte |
350 | 0 | } |
351 | | |
352 | | UBool |
353 | 0 | BytesTrie::findUniqueValue(const uint8_t *pos, UBool haveUniqueValue, int32_t &uniqueValue) { |
354 | 0 | for(;;) { |
355 | 0 | int32_t node=*pos++; |
356 | 0 | if(node<kMinLinearMatch) { |
357 | 0 | if(node==0) { |
358 | 0 | node=*pos++; |
359 | 0 | } |
360 | 0 | pos=findUniqueValueFromBranch(pos, node+1, haveUniqueValue, uniqueValue); |
361 | 0 | if(pos==nullptr) { |
362 | 0 | return false; |
363 | 0 | } |
364 | 0 | haveUniqueValue=true; |
365 | 0 | } else if(node<kMinValueLead) { |
366 | | // linear-match node |
367 | 0 | pos+=node-kMinLinearMatch+1; // Ignore the match bytes. |
368 | 0 | } else { |
369 | 0 | UBool isFinal = static_cast<UBool>(node & kValueIsFinal); |
370 | 0 | int32_t value=readValue(pos, node>>1); |
371 | 0 | if(haveUniqueValue) { |
372 | 0 | if(value!=uniqueValue) { |
373 | 0 | return false; |
374 | 0 | } |
375 | 0 | } else { |
376 | 0 | uniqueValue=value; |
377 | 0 | haveUniqueValue=true; |
378 | 0 | } |
379 | 0 | if(isFinal) { |
380 | 0 | return true; |
381 | 0 | } |
382 | 0 | pos=skipValue(pos, node); |
383 | 0 | } |
384 | 0 | } |
385 | 0 | } |
386 | | |
387 | | int32_t |
388 | 0 | BytesTrie::getNextBytes(ByteSink &out) const { |
389 | 0 | const uint8_t *pos=pos_; |
390 | 0 | if(pos==nullptr) { |
391 | 0 | return 0; |
392 | 0 | } |
393 | 0 | if(remainingMatchLength_>=0) { |
394 | 0 | append(out, *pos); // Next byte of a pending linear-match node. |
395 | 0 | return 1; |
396 | 0 | } |
397 | 0 | int32_t node=*pos++; |
398 | 0 | if(node>=kMinValueLead) { |
399 | 0 | if(node&kValueIsFinal) { |
400 | 0 | return 0; |
401 | 0 | } else { |
402 | 0 | pos=skipValue(pos, node); |
403 | 0 | node=*pos++; |
404 | 0 | U_ASSERT(node<kMinValueLead); |
405 | 0 | } |
406 | 0 | } |
407 | 0 | if(node<kMinLinearMatch) { |
408 | 0 | if(node==0) { |
409 | 0 | node=*pos++; |
410 | 0 | } |
411 | 0 | getNextBranchBytes(pos, ++node, out); |
412 | 0 | return node; |
413 | 0 | } else { |
414 | | // First byte of the linear-match node. |
415 | 0 | append(out, *pos); |
416 | 0 | return 1; |
417 | 0 | } |
418 | 0 | } |
419 | | |
420 | | void |
421 | 0 | BytesTrie::getNextBranchBytes(const uint8_t *pos, int32_t length, ByteSink &out) { |
422 | 0 | while(length>kMaxBranchLinearSubNodeLength) { |
423 | 0 | ++pos; // ignore the comparison byte |
424 | 0 | getNextBranchBytes(jumpByDelta(pos), length>>1, out); |
425 | 0 | length=length-(length>>1); |
426 | 0 | pos=skipDelta(pos); |
427 | 0 | } |
428 | 0 | do { |
429 | 0 | append(out, *pos++); |
430 | 0 | pos=skipValue(pos); |
431 | 0 | } while(--length>1); |
432 | 0 | append(out, *pos); |
433 | 0 | } |
434 | | |
435 | | void |
436 | 0 | BytesTrie::append(ByteSink &out, int c) { |
437 | 0 | char ch = static_cast<char>(c); |
438 | 0 | out.Append(&ch, 1); |
439 | 0 | } |
440 | | |
441 | | U_NAMESPACE_END |