Coverage Report

Created: 2026-06-07 06:21

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