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