Coverage Report

Created: 2026-09-28 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/common/unicode/bytestrie.h
Line
Count
Source
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-2012, International Business Machines
6
*   Corporation and others.  All Rights Reserved.
7
*******************************************************************************
8
*   file name:  bytestrie.h
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
#ifndef __BYTESTRIE_H__
18
#define __BYTESTRIE_H__
19
20
/**
21
 * \file
22
 * \brief C++ API: Trie for mapping byte sequences to integer values.
23
 */
24
25
#include "unicode/utypes.h"
26
27
#if U_SHOW_CPLUSPLUS_API
28
29
#include "unicode/stringpiece.h"
30
#include "unicode/uobject.h"
31
#include "unicode/ustringtrie.h"
32
33
class BytesTrieTest;
34
35
U_NAMESPACE_BEGIN
36
37
class ByteSink;
38
class BytesTrieBuilder;
39
class CharString;
40
class UVector32;
41
42
/**
43
 * Light-weight, non-const reader class for a BytesTrie.
44
 * Traverses a byte-serialized data structure with minimal state,
45
 * for mapping byte sequences to non-negative integer values.
46
 *
47
 * This class owns the serialized trie data only if it was constructed by
48
 * the builder's build() method.
49
 * The public constructor and the copy constructor only alias the data (only copy the pointer).
50
 * There is no assignment operator.
51
 *
52
 * This class is not intended for public subclassing.
53
 * @stable ICU 4.8
54
 */
55
class U_COMMON_API BytesTrie : public UMemory {
56
public:
57
    /**
58
     * Constructs a BytesTrie reader instance.
59
     *
60
     * The trieBytes must contain a copy of a byte sequence from the BytesTrieBuilder,
61
     * starting with the first byte of that sequence.
62
     * The BytesTrie object will not read more bytes than
63
     * the BytesTrieBuilder generated in the corresponding build() call.
64
     *
65
     * The array is not copied/cloned and must not be modified while
66
     * the BytesTrie object is in use.
67
     *
68
     * @param trieBytes The byte array that contains the serialized trie.
69
     * @stable ICU 4.8
70
     */
71
    BytesTrie(const void* trieBytes U_LIFETIME_BOUND)
72
26.6M
            : ownedArray_(nullptr), bytes_(static_cast<const uint8_t *>(trieBytes)),
73
26.6M
              pos_(bytes_), remainingMatchLength_(-1) {}
74
75
    /**
76
     * Destructor.
77
     * @stable ICU 4.8
78
     */
79
    ~BytesTrie();
80
81
    /**
82
     * Copy constructor, copies the other trie reader object and its state,
83
     * but not the byte array which will be shared. (Shallow copy.)
84
     * @param other Another BytesTrie object.
85
     * @stable ICU 4.8
86
     */
87
    BytesTrie(const BytesTrie &other)
88
82.9k
            : ownedArray_(nullptr), bytes_(other.bytes_),
89
82.9k
              pos_(other.pos_), remainingMatchLength_(other.remainingMatchLength_) {}
90
91
    /**
92
     * Resets this trie to its initial state.
93
     * @return *this
94
     * @stable ICU 4.8
95
     */
96
189
    BytesTrie& reset() U_LIFETIME_BOUND {
97
189
        pos_=bytes_;
98
189
        remainingMatchLength_=-1;
99
189
        return *this;
100
189
    }
101
102
    /**
103
     * Returns the state of this trie as a 64-bit integer.
104
     * The state value is never 0.
105
     *
106
     * @return opaque state value
107
     * @see resetToState64
108
     * @stable ICU 65
109
     */
110
115k
    uint64_t getState64() const {
111
115k
        return (static_cast<uint64_t>(remainingMatchLength_ + 2) << kState64RemainingShift) |
112
115k
            static_cast<uint64_t>(pos_ - bytes_);
113
115k
    }
114
115
    /**
116
     * Resets this trie to the saved state.
117
     * Unlike resetToState(State), the 64-bit state value
118
     * must be from getState64() from the same trie object or
119
     * from one initialized the exact same way.
120
     * Because of no validation, this method is faster.
121
     *
122
     * @param state The opaque trie state value from getState64().
123
     * @return *this
124
     * @see getState64
125
     * @see resetToState
126
     * @see reset
127
     * @stable ICU 65
128
     */
129
79.8k
    BytesTrie& resetToState64(uint64_t state) U_LIFETIME_BOUND {
130
79.8k
        remainingMatchLength_ = static_cast<int32_t>(state >> kState64RemainingShift) - 2;
131
79.8k
        pos_ = bytes_ + (state & kState64PosMask);
132
79.8k
        return *this;
133
79.8k
    }
134
135
    /**
136
     * BytesTrie state object, for saving a trie's current state
137
     * and resetting the trie back to this state later.
138
     * @stable ICU 4.8
139
     */
140
    class State : public UMemory {
141
    public:
142
        /**
143
         * Constructs an empty State.
144
         * @stable ICU 4.8
145
         */
146
0
        State() { bytes=nullptr; }
147
    private:
148
        friend class BytesTrie;
149
150
        const uint8_t *bytes;
151
        const uint8_t *pos;
152
        int32_t remainingMatchLength;
153
    };
154
155
    /**
156
     * Saves the state of this trie.
157
     * @param state The State object to hold the trie's state.
158
     * @return *this
159
     * @see resetToState
160
     * @stable ICU 4.8
161
     */
162
0
    const BytesTrie& saveState(State& state) const U_LIFETIME_BOUND {
163
0
        state.bytes=bytes_;
164
0
        state.pos=pos_;
165
0
        state.remainingMatchLength=remainingMatchLength_;
166
0
        return *this;
167
0
    }
168
169
    /**
170
     * Resets this trie to the saved state.
171
     * If the state object contains no state, or the state of a different trie,
172
     * then this trie remains unchanged.
173
     * @param state The State object which holds a saved trie state.
174
     * @return *this
175
     * @see saveState
176
     * @see reset
177
     * @stable ICU 4.8
178
     */
179
0
    BytesTrie& resetToState(const State& state) U_LIFETIME_BOUND {
180
0
        if(bytes_==state.bytes && bytes_!=nullptr) {
181
0
            pos_=state.pos;
182
0
            remainingMatchLength_=state.remainingMatchLength;
183
0
        }
184
0
        return *this;
185
0
    }
186
187
    /**
188
     * Determines whether the byte sequence so far matches, whether it has a value,
189
     * and whether another input byte can continue a matching byte sequence.
190
     * @return The match/value Result.
191
     * @stable ICU 4.8
192
     */
193
    UStringTrieResult current() const;
194
195
    /**
196
     * Traverses the trie from the initial state for this input byte.
197
     * Equivalent to reset().next(inByte).
198
     * @param inByte Input byte value. Values -0x100..-1 are treated like 0..0xff.
199
     *               Values below -0x100 and above 0xff will never match.
200
     * @return The match/value Result.
201
     * @stable ICU 4.8
202
     */
203
26.4M
    inline UStringTrieResult first(int32_t inByte) {
204
26.4M
        remainingMatchLength_=-1;
205
26.4M
        if(inByte<0) {
206
174k
            inByte+=0x100;
207
174k
        }
208
26.4M
        return nextImpl(bytes_, inByte);
209
26.4M
    }
210
211
    /**
212
     * Traverses the trie from the current state for this input byte.
213
     * @param inByte Input byte value. Values -0x100..-1 are treated like 0..0xff.
214
     *               Values below -0x100 and above 0xff will never match.
215
     * @return The match/value Result.
216
     * @stable ICU 4.8
217
     */
218
    UStringTrieResult next(int32_t inByte);
219
220
    /**
221
     * Traverses the trie from the current state for this byte sequence.
222
     * Equivalent to
223
     * \code
224
     * Result result=current();
225
     * for(each c in s)
226
     *   if(!USTRINGTRIE_HAS_NEXT(result)) return USTRINGTRIE_NO_MATCH;
227
     *   result=next(c);
228
     * return result;
229
     * \endcode
230
     * @param s A string or byte sequence. Can be nullptr if length is 0.
231
     * @param length The length of the byte sequence. Can be -1 if NUL-terminated.
232
     * @return The match/value Result.
233
     * @stable ICU 4.8
234
     */
235
    UStringTrieResult next(const char *s, int32_t length);
236
237
    /**
238
     * Returns a matching byte sequence's value if called immediately after
239
     * current()/first()/next() returned USTRINGTRIE_INTERMEDIATE_VALUE or USTRINGTRIE_FINAL_VALUE.
240
     * getValue() can be called multiple times.
241
     *
242
     * Do not call getValue() after USTRINGTRIE_NO_MATCH or USTRINGTRIE_NO_VALUE!
243
     * @return The value for the byte sequence so far.
244
     * @stable ICU 4.8
245
     */
246
225k
    inline int32_t getValue() const {
247
225k
        const uint8_t *pos=pos_;
248
225k
        int32_t leadByte=*pos++;
249
        // U_ASSERT(leadByte>=kMinValueLead);
250
225k
        return readValue(pos, leadByte>>1);
251
225k
    }
252
253
    /**
254
     * Determines whether all byte sequences reachable from the current state
255
     * map to the same value.
256
     * @param uniqueValue Receives the unique value, if this function returns true.
257
     *                    (output-only)
258
     * @return true if all byte sequences reachable from the current state
259
     *         map to the same value.
260
     * @stable ICU 4.8
261
     */
262
0
    inline UBool hasUniqueValue(int32_t &uniqueValue) const {
263
0
        const uint8_t *pos=pos_;
264
0
        // Skip the rest of a pending linear-match node.
265
0
        return pos!=nullptr && findUniqueValue(pos+remainingMatchLength_+1, false, uniqueValue);
266
0
    }
267
268
    /**
269
     * Finds each byte which continues the byte sequence from the current state.
270
     * That is, each byte b for which it would be next(b)!=USTRINGTRIE_NO_MATCH now.
271
     * @param out Each next byte is appended to this object.
272
     *            (Only uses the out.Append(s, length) method.)
273
     * @return the number of bytes which continue the byte sequence from here
274
     * @stable ICU 4.8
275
     */
276
    int32_t getNextBytes(ByteSink &out) const;
277
278
    /**
279
     * Iterator for all of the (byte sequence, value) pairs in a BytesTrie.
280
     * @stable ICU 4.8
281
     */
282
    class U_COMMON_API Iterator : public UMemory {
283
    public:
284
        /**
285
         * Iterates from the root of a byte-serialized BytesTrie.
286
         * @param trieBytes The trie bytes.
287
         * @param maxStringLength If 0, the iterator returns full strings/byte sequences.
288
         *                        Otherwise, the iterator returns strings with this maximum length.
289
         * @param errorCode Standard ICU error code. Its input value must
290
         *                  pass the U_SUCCESS() test, or else the function returns
291
         *                  immediately. Check for U_FAILURE() on output or use with
292
         *                  function chaining. (See User Guide for details.)
293
         * @stable ICU 4.8
294
         */
295
        Iterator(const void* trieBytes U_LIFETIME_BOUND,
296
                 int32_t maxStringLength,
297
                 UErrorCode& errorCode);
298
299
        /**
300
         * Iterates from the current state of the specified BytesTrie.
301
         * @param trie The trie whose state will be copied for iteration.
302
         * @param maxStringLength If 0, the iterator returns full strings/byte sequences.
303
         *                        Otherwise, the iterator returns strings with this maximum length.
304
         * @param errorCode Standard ICU error code. Its input value must
305
         *                  pass the U_SUCCESS() test, or else the function returns
306
         *                  immediately. Check for U_FAILURE() on output or use with
307
         *                  function chaining. (See User Guide for details.)
308
         * @stable ICU 4.8
309
         */
310
        Iterator(const BytesTrie &trie, int32_t maxStringLength, UErrorCode &errorCode);
311
312
        /**
313
         * Destructor.
314
         * @stable ICU 4.8
315
         */
316
        ~Iterator();
317
318
        /**
319
         * Resets this iterator to its initial state.
320
         * @return *this
321
         * @stable ICU 4.8
322
         */
323
        Iterator& reset() U_LIFETIME_BOUND;
324
325
        /**
326
         * @return true if there are more elements.
327
         * @stable ICU 4.8
328
         */
329
        UBool hasNext() const;
330
331
        /**
332
         * Finds the next (byte sequence, value) pair if there is one.
333
         *
334
         * If the byte sequence is truncated to the maximum length and does not
335
         * have a real value, then the value is set to -1.
336
         * In this case, this "not a real value" is indistinguishable from
337
         * a real value of -1.
338
         * @param errorCode Standard ICU error code. Its input value must
339
         *                  pass the U_SUCCESS() test, or else the function returns
340
         *                  immediately. Check for U_FAILURE() on output or use with
341
         *                  function chaining. (See User Guide for details.)
342
         * @return true if there is another element.
343
         * @stable ICU 4.8
344
         */
345
        UBool next(UErrorCode &errorCode);
346
347
        /**
348
         * @return The NUL-terminated byte sequence for the last successful next().
349
         * @stable ICU 4.8
350
         */
351
        StringPiece getString() const U_LIFETIME_BOUND;
352
        /**
353
         * @return The value for the last successful next().
354
         * @stable ICU 4.8
355
         */
356
0
        int32_t getValue() const { return value_; }
357
358
    private:
359
        UBool truncateAndStop();
360
361
        const uint8_t* branchNext(const uint8_t* pos U_LIFETIME_BOUND,
362
                                  int32_t length,
363
                                  UErrorCode& errorCode);
364
365
        const uint8_t *bytes_;
366
        const uint8_t *pos_;
367
        const uint8_t *initialPos_;
368
        int32_t remainingMatchLength_;
369
        int32_t initialRemainingMatchLength_;
370
371
        CharString *str_;
372
        int32_t maxLength_;
373
        int32_t value_;
374
375
        // The stack stores pairs of integers for backtracking to another
376
        // outbound edge of a branch node.
377
        // The first integer is an offset from bytes_.
378
        // The second integer has the str_->length() from before the node in bits 15..0,
379
        // and the remaining branch length in bits 24..16. (Bits 31..25 are unused.)
380
        // (We could store the remaining branch length minus 1 in bits 23..16 and not use bits 31..24,
381
        // but the code looks more confusing that way.)
382
        UVector32 *stack_;
383
    };
384
385
private:
386
    friend class BytesTrieBuilder;
387
    friend class ::BytesTrieTest;
388
389
    /**
390
     * Constructs a BytesTrie reader instance.
391
     * Unlike the public constructor which just aliases an array,
392
     * this constructor adopts the builder's array.
393
     * This constructor is only called by the builder.
394
     */
395
    BytesTrie(void *adoptBytes, const void *trieBytes)
396
            : ownedArray_(static_cast<uint8_t *>(adoptBytes)),
397
              bytes_(static_cast<const uint8_t *>(trieBytes)),
398
0
              pos_(bytes_), remainingMatchLength_(-1) {}
399
400
    // No assignment operator.
401
    BytesTrie &operator=(const BytesTrie &other) = delete;
402
403
25.7M
    inline void stop() {
404
25.7M
        pos_=nullptr;
405
25.7M
    }
406
407
    // Reads a compact 32-bit integer.
408
    // pos is already after the leadByte, and the lead byte is already shifted right by 1.
409
    static int32_t readValue(const uint8_t *pos, int32_t leadByte);
410
140M
    static inline const uint8_t* skipValue(const uint8_t* pos U_LIFETIME_BOUND, int32_t leadByte) {
411
        // U_ASSERT(leadByte>=kMinValueLead);
412
140M
        if(leadByte>=(kMinTwoByteValueLead<<1)) {
413
75.2M
            if(leadByte<(kMinThreeByteValueLead<<1)) {
414
41.0M
                ++pos;
415
41.0M
            } else if(leadByte<(kFourByteValueLead<<1)) {
416
34.1M
                pos+=2;
417
34.1M
            } else {
418
4.33k
                pos+=3+((leadByte>>1)&1);
419
4.33k
            }
420
75.2M
        }
421
140M
        return pos;
422
140M
    }
423
121M
    static inline const uint8_t* skipValue(const uint8_t* pos U_LIFETIME_BOUND) {
424
121M
        int32_t leadByte=*pos++;
425
121M
        return skipValue(pos, leadByte);
426
121M
    }
427
428
    // Reads a jump delta and jumps.
429
    static const uint8_t* jumpByDelta(const uint8_t* pos U_LIFETIME_BOUND);
430
431
73.7M
    static inline const uint8_t* skipDelta(const uint8_t* pos U_LIFETIME_BOUND) {
432
73.7M
        int32_t delta=*pos++;
433
73.7M
        if(delta>=kMinTwoByteDeltaLead) {
434
66.9M
            if(delta<kMinThreeByteDeltaLead) {
435
26.3M
                ++pos;
436
40.5M
            } else if(delta<kFourByteDeltaLead) {
437
40.5M
                pos+=2;
438
40.5M
            } else {
439
0
                pos+=3+(delta&1);
440
0
            }
441
66.9M
        }
442
73.7M
        return pos;
443
73.7M
    }
444
445
19.7M
    static inline UStringTrieResult valueResult(int32_t node) {
446
19.7M
        return static_cast<UStringTrieResult>(USTRINGTRIE_INTERMEDIATE_VALUE - (node & kValueIsFinal));
447
19.7M
    }
448
449
    // Handles a branch node for both next(byte) and next(string).
450
    UStringTrieResult branchNext(const uint8_t *pos, int32_t length, int32_t inByte);
451
452
    // Requires remainingLength_<0.
453
    UStringTrieResult nextImpl(const uint8_t *pos, int32_t inByte);
454
455
    // Helper functions for hasUniqueValue().
456
    // Recursively finds a unique value (or whether there is not a unique one)
457
    // from a branch.
458
    static const uint8_t* findUniqueValueFromBranch(const uint8_t* pos U_LIFETIME_BOUND,
459
                                                    int32_t length,
460
                                                    UBool haveUniqueValue,
461
                                                    int32_t& uniqueValue);
462
    // Recursively finds a unique value (or whether there is not a unique one)
463
    // starting from a position on a node lead byte.
464
    static UBool findUniqueValue(const uint8_t *pos, UBool haveUniqueValue, int32_t &uniqueValue);
465
466
    // Helper functions for getNextBytes().
467
    // getNextBytes() when pos is on a branch node.
468
    static void getNextBranchBytes(const uint8_t *pos, int32_t length, ByteSink &out);
469
    static void append(ByteSink &out, int c);
470
471
    // BytesTrie data structure
472
    //
473
    // The trie consists of a series of byte-serialized nodes for incremental
474
    // string/byte sequence matching. The root node is at the beginning of the trie data.
475
    //
476
    // Types of nodes are distinguished by their node lead byte ranges.
477
    // After each node, except a final-value node, another node follows to
478
    // encode match values or continue matching further bytes.
479
    //
480
    // Node types:
481
    //  - Value node: Stores a 32-bit integer in a compact, variable-length format.
482
    //    The value is for the string/byte sequence so far.
483
    //    One node bit indicates whether the value is final or whether
484
    //    matching continues with the next node.
485
    //  - Linear-match node: Matches a number of bytes.
486
    //  - Branch node: Branches to other nodes according to the current input byte.
487
    //    The node byte is the length of the branch (number of bytes to select from)
488
    //    minus 1. It is followed by a sub-node:
489
    //    - If the length is at most kMaxBranchLinearSubNodeLength, then
490
    //      there are length-1 (key, value) pairs and then one more comparison byte.
491
    //      If one of the key bytes matches, then the value is either a final value for
492
    //      the string/byte sequence so far, or a "jump" delta to the next node.
493
    //      If the last byte matches, then matching continues with the next node.
494
    //      (Values have the same encoding as value nodes.)
495
    //    - If the length is greater than kMaxBranchLinearSubNodeLength, then
496
    //      there is one byte and one "jump" delta.
497
    //      If the input byte is less than the sub-node byte, then "jump" by delta to
498
    //      the next sub-node which will have a length of length/2.
499
    //      (The delta has its own compact encoding.)
500
    //      Otherwise, skip the "jump" delta to the next sub-node
501
    //      which will have a length of length-length/2.
502
503
    // Node lead byte values.
504
505
    // 00..0f: Branch node. If node!=0 then the length is node+1, otherwise
506
    // the length is one more than the next byte.
507
508
    // For a branch sub-node with at most this many entries, we drop down
509
    // to a linear search.
510
    static const int32_t kMaxBranchLinearSubNodeLength=5;
511
512
    // 10..1f: Linear-match node, match 1..16 bytes and continue reading the next node.
513
    static const int32_t kMinLinearMatch=0x10;
514
    static const int32_t kMaxLinearMatchLength=0x10;
515
516
    // 20..ff: Variable-length value node.
517
    // If odd, the value is final. (Otherwise, intermediate value or jump delta.)
518
    // Then shift-right by 1 bit.
519
    // The remaining lead byte value indicates the number of following bytes (0..4)
520
    // and contains the value's top bits.
521
    static const int32_t kMinValueLead=kMinLinearMatch+kMaxLinearMatchLength;  // 0x20
522
    // It is a final value if bit 0 is set.
523
    static const int32_t kValueIsFinal=1;
524
525
    // Compact value: After testing bit 0, shift right by 1 and then use the following thresholds.
526
    static const int32_t kMinOneByteValueLead=kMinValueLead/2;  // 0x10
527
    static const int32_t kMaxOneByteValue=0x40;  // At least 6 bits in the first byte.
528
529
    static const int32_t kMinTwoByteValueLead=kMinOneByteValueLead+kMaxOneByteValue+1;  // 0x51
530
    static const int32_t kMaxTwoByteValue=0x1aff;
531
532
    static const int32_t kMinThreeByteValueLead=kMinTwoByteValueLead+(kMaxTwoByteValue>>8)+1;  // 0x6c
533
    static const int32_t kFourByteValueLead=0x7e;
534
535
    // A little more than Unicode code points. (0x11ffff)
536
    static const int32_t kMaxThreeByteValue=((kFourByteValueLead-kMinThreeByteValueLead)<<16)-1;
537
538
    static const int32_t kFiveByteValueLead=0x7f;
539
540
    // Compact delta integers.
541
    static const int32_t kMaxOneByteDelta=0xbf;
542
    static const int32_t kMinTwoByteDeltaLead=kMaxOneByteDelta+1;  // 0xc0
543
    static const int32_t kMinThreeByteDeltaLead=0xf0;
544
    static const int32_t kFourByteDeltaLead=0xfe;
545
    static const int32_t kFiveByteDeltaLead=0xff;
546
547
    static const int32_t kMaxTwoByteDelta=((kMinThreeByteDeltaLead-kMinTwoByteDeltaLead)<<8)-1;  // 0x2fff
548
    static const int32_t kMaxThreeByteDelta=((kFourByteDeltaLead-kMinThreeByteDeltaLead)<<16)-1;  // 0xdffff
549
550
    // For getState64():
551
    // The remainingMatchLength_ is -1..14=(kMaxLinearMatchLength=0x10)-2
552
    // so we need at least 5 bits for that.
553
    // We add 2 to store it as a positive value 1..16=kMaxLinearMatchLength.
554
    static constexpr int32_t kState64RemainingShift = 59;
555
    static constexpr uint64_t kState64PosMask = (UINT64_C(1) << kState64RemainingShift) - 1;
556
557
    uint8_t *ownedArray_;
558
559
    // Fixed value referencing the BytesTrie bytes.
560
    const uint8_t *bytes_;
561
562
    // Iterator variables.
563
564
    // Pointer to next trie byte to read. nullptr if no more matches.
565
    const uint8_t *pos_;
566
    // Remaining length of a linear-match node, minus 1. Negative if not in such a node.
567
    int32_t remainingMatchLength_;
568
};
569
570
U_NAMESPACE_END
571
572
#endif /* U_SHOW_CPLUSPLUS_API */
573
574
#endif  // __BYTESTRIE_H__