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/ucharstriebuilder.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-2016, International Business Machines
6
*   Corporation and others.  All Rights Reserved.
7
*******************************************************************************
8
*   file name:  ucharstriebuilder.h
9
*   encoding:   UTF-8
10
*   tab size:   8 (not used)
11
*   indentation:4
12
*
13
*   created on: 2010nov14
14
*   created by: Markus W. Scherer
15
*/
16
17
#ifndef __UCHARSTRIEBUILDER_H__
18
#define __UCHARSTRIEBUILDER_H__
19
20
#include "unicode/utypes.h"
21
22
#if U_SHOW_CPLUSPLUS_API
23
24
#include "unicode/stringtriebuilder.h"
25
#include "unicode/ucharstrie.h"
26
#include "unicode/unistr.h"
27
28
/**
29
 * \file
30
 * \brief C++ API: Builder for icu::UCharsTrie
31
 */
32
33
U_NAMESPACE_BEGIN
34
35
class UCharsTrieElement;
36
37
/**
38
 * Builder class for UCharsTrie.
39
 *
40
 * This class is not intended for public subclassing.
41
 * @stable ICU 4.8
42
 */
43
class U_COMMON_API UCharsTrieBuilder : public StringTrieBuilder {
44
public:
45
    /**
46
     * Constructs an empty builder.
47
     * @param errorCode Standard ICU error code.
48
     * @stable ICU 4.8
49
     */
50
    UCharsTrieBuilder(UErrorCode &errorCode);
51
52
    /**
53
     * Destructor.
54
     * @stable ICU 4.8
55
     */
56
    virtual ~UCharsTrieBuilder();
57
58
    /**
59
     * Adds a (string, value) pair.
60
     * The string must be unique.
61
     * The string contents will be copied; the builder does not keep
62
     * a reference to the input UnicodeString or its buffer.
63
     * @param s The input string.
64
     * @param value The value associated with this string.
65
     * @param errorCode Standard ICU error code. Its input value must
66
     *                  pass the U_SUCCESS() test, or else the function returns
67
     *                  immediately. Check for U_FAILURE() on output or use with
68
     *                  function chaining. (See User Guide for details.)
69
     * @return *this
70
     * @stable ICU 4.8
71
     */
72
    UCharsTrieBuilder& add(const UnicodeString& s,
73
                           int32_t value,
74
                           UErrorCode& errorCode) U_LIFETIME_BOUND;
75
76
    /**
77
     * Builds a UCharsTrie for the add()ed data.
78
     * Once built, no further data can be add()ed until clear() is called.
79
     *
80
     * A UCharsTrie cannot be empty. At least one (string, value) pair
81
     * must have been add()ed.
82
     *
83
     * This method passes ownership of the builder's internal result array to the new trie object.
84
     * Another call to any build() variant will re-serialize the trie.
85
     * After clear() has been called, a new array will be used as well.
86
     * @param buildOption Build option, see UStringTrieBuildOption.
87
     * @param errorCode Standard ICU error code. Its input value must
88
     *                  pass the U_SUCCESS() test, or else the function returns
89
     *                  immediately. Check for U_FAILURE() on output or use with
90
     *                  function chaining. (See User Guide for details.)
91
     * @return A new UCharsTrie for the add()ed data.
92
     * @stable ICU 4.8
93
     */
94
    UCharsTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
95
96
    /**
97
     * Builds a UCharsTrie for the add()ed data and char16_t-serializes it.
98
     * Once built, no further data can be add()ed until clear() is called.
99
     *
100
     * A UCharsTrie cannot be empty. At least one (string, value) pair
101
     * must have been add()ed.
102
     *
103
     * Multiple calls to buildUnicodeString() set the UnicodeStrings to the
104
     * builder's same char16_t array, without rebuilding.
105
     * If buildUnicodeString() is called after build(), the trie will be
106
     * re-serialized into a new array (because build() passes on ownership).
107
     * If build() is called after buildUnicodeString(), the trie object returned
108
     * by build() will become the owner of the underlying data for the
109
     * previously returned UnicodeString.
110
     * After clear() has been called, a new array will be used as well.
111
     * @param buildOption Build option, see UStringTrieBuildOption.
112
     * @param result A UnicodeString which will be set to the char16_t-serialized
113
     *               UCharsTrie for the add()ed data.
114
     * @param errorCode Standard ICU error code. Its input value must
115
     *                  pass the U_SUCCESS() test, or else the function returns
116
     *                  immediately. Check for U_FAILURE() on output or use with
117
     *                  function chaining. (See User Guide for details.)
118
     * @return result
119
     * @stable ICU 4.8
120
     */
121
    UnicodeString& buildUnicodeString(UStringTrieBuildOption buildOption,
122
                                      UnicodeString& result U_LIFETIME_BOUND,
123
                                      UErrorCode& errorCode) U_LIFETIME_BOUND;
124
125
    /**
126
     * Removes all (string, value) pairs.
127
     * New data can then be add()ed and a new trie can be built.
128
     * @return *this
129
     * @stable ICU 4.8
130
     */
131
0
    UCharsTrieBuilder& clear() U_LIFETIME_BOUND {
132
0
        strings.remove();
133
0
        elementsLength=0;
134
0
        ucharsLength=0;
135
0
        return *this;
136
0
    }
137
138
private:
139
    UCharsTrieBuilder(const UCharsTrieBuilder &other) = delete;  // no copy constructor
140
    UCharsTrieBuilder &operator=(const UCharsTrieBuilder &other) = delete;  // no assignment operator
141
142
    void buildUChars(UStringTrieBuildOption buildOption, UErrorCode &errorCode);
143
144
    virtual int32_t getElementStringLength(int32_t i) const override;
145
    virtual char16_t getElementUnit(int32_t i, int32_t unitIndex) const override;
146
    virtual int32_t getElementValue(int32_t i) const override;
147
148
    virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const override;
149
150
    virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const override;
151
    virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const override;
152
    virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, char16_t unit) const override;
153
154
0
    virtual UBool matchNodesCanHaveValues() const override { return true; }
155
156
3.41k
    virtual int32_t getMaxBranchLinearSubNodeLength() const override { return UCharsTrie::kMaxBranchLinearSubNodeLength; }
157
6.39k
    virtual int32_t getMinLinearMatch() const override { return UCharsTrie::kMinLinearMatch; }
158
4.49k
    virtual int32_t getMaxLinearMatchLength() const override { return UCharsTrie::kMaxLinearMatchLength; }
159
160
    class UCTLinearMatchNode : public LinearMatchNode {
161
    public:
162
        UCTLinearMatchNode(const char16_t *units, int32_t len, Node *nextNode);
163
        virtual bool operator==(const Node &other) const override;
164
        virtual void write(StringTrieBuilder &builder) override;
165
    private:
166
        const char16_t *s;
167
    };
168
169
    virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length,
170
                                        Node *nextNode) const override;
171
172
    UBool ensureCapacity(int32_t length);
173
    virtual int32_t write(int32_t unit) override;
174
    int32_t write(const char16_t *s, int32_t length);
175
    virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length) override;
176
    virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal) override;
177
    virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node) override;
178
    virtual int32_t writeDeltaTo(int32_t jumpTarget) override;
179
180
    UnicodeString strings;
181
    UCharsTrieElement *elements;
182
    int32_t elementsCapacity;
183
    int32_t elementsLength;
184
185
    // char16_t serialization of the trie.
186
    // Grows from the back: ucharsLength measures from the end of the buffer!
187
    char16_t *uchars;
188
    int32_t ucharsCapacity;
189
    int32_t ucharsLength;
190
};
191
192
U_NAMESPACE_END
193
194
#endif /* U_SHOW_CPLUSPLUS_API */
195
196
#endif  // __UCHARSTRIEBUILDER_H__