Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef AAPT_STRING_POOL_H |
| 18 | #define AAPT_STRING_POOL_H |
| 19 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include "ConfigDescription.h" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 21 | #include "util/BigBuffer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | #include "util/StringPiece.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | |
| 24 | #include <functional> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 25 | #include <memory> |
| 26 | #include <string> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 27 | #include <unordered_map> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
| 30 | namespace aapt { |
| 31 | |
| 32 | struct Span { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 33 | std::string name; |
| 34 | uint32_t firstChar; |
| 35 | uint32_t lastChar; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | struct StyleString { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 39 | std::string str; |
| 40 | std::vector<Span> spans; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class StringPool { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 44 | public: |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 45 | class Context { |
| 46 | public: |
| 47 | enum : uint32_t { |
| 48 | kStylePriority = 0u, |
| 49 | kHighPriority = 1u, |
| 50 | kNormalPriority = 0x7fffffffu, |
| 51 | kLowPriority = 0xffffffffu, |
| 52 | }; |
| 53 | uint32_t priority = kNormalPriority; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | ConfigDescription config; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 55 | |
| 56 | Context() = default; |
| 57 | Context(uint32_t p, const ConfigDescription& c) : priority(p), config(c) {} |
| 58 | explicit Context(uint32_t p) : priority(p) {} |
| 59 | explicit Context(const ConfigDescription& c) |
| 60 | : priority(kNormalPriority), config(c) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 62 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | class Entry; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 64 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | class Ref { |
| 66 | public: |
| 67 | Ref(); |
| 68 | Ref(const Ref&); |
| 69 | ~Ref(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 70 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | Ref& operator=(const Ref& rhs); |
| 72 | const std::string* operator->() const; |
| 73 | const std::string& operator*() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 75 | size_t getIndex() const; |
| 76 | const Context& getContext() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 77 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | private: |
| 79 | friend class StringPool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 80 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 81 | explicit Ref(Entry* entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 82 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | Entry* mEntry; |
| 84 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 85 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | class StyleEntry; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 87 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | class StyleRef { |
| 89 | public: |
| 90 | StyleRef(); |
| 91 | StyleRef(const StyleRef&); |
| 92 | ~StyleRef(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 93 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 94 | StyleRef& operator=(const StyleRef& rhs); |
| 95 | const StyleEntry* operator->() const; |
| 96 | const StyleEntry& operator*() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 97 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 98 | size_t getIndex() const; |
| 99 | const Context& getContext() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 100 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | private: |
| 102 | friend class StringPool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 103 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | explicit StyleRef(StyleEntry* entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 105 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | StyleEntry* mEntry; |
| 107 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 108 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | class Entry { |
| 110 | public: |
| 111 | std::string value; |
| 112 | Context context; |
| 113 | size_t index; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 114 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | private: |
| 116 | friend class StringPool; |
| 117 | friend class Ref; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 118 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | int ref; |
| 120 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 121 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 122 | struct Span { |
| 123 | Ref name; |
| 124 | uint32_t firstChar; |
| 125 | uint32_t lastChar; |
| 126 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 127 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 128 | class StyleEntry { |
| 129 | public: |
| 130 | Ref str; |
| 131 | std::vector<Span> spans; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 132 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 133 | private: |
| 134 | friend class StringPool; |
| 135 | friend class StyleRef; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 136 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | int ref; |
| 138 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 139 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | using const_iterator = std::vector<std::unique_ptr<Entry>>::const_iterator; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 141 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 142 | static bool flattenUtf8(BigBuffer* out, const StringPool& pool); |
| 143 | static bool flattenUtf16(BigBuffer* out, const StringPool& pool); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 144 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | StringPool() = default; |
| 146 | StringPool(const StringPool&) = delete; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 147 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Adds a string to the pool, unless it already exists. Returns |
| 150 | * a reference to the string in the pool. |
| 151 | */ |
| 152 | Ref makeRef(const StringPiece& str); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 153 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | /** |
| 155 | * Adds a string to the pool, unless it already exists, with a context |
| 156 | * object that can be used when sorting the string pool. Returns |
| 157 | * a reference to the string in the pool. |
| 158 | */ |
| 159 | Ref makeRef(const StringPiece& str, const Context& context); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 160 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | /** |
| 162 | * Adds a style to the string pool and returns a reference to it. |
| 163 | */ |
| 164 | StyleRef makeRef(const StyleString& str); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 165 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Adds a style to the string pool with a context object that |
| 168 | * can be used when sorting the string pool. Returns a reference |
| 169 | * to the style in the string pool. |
| 170 | */ |
| 171 | StyleRef makeRef(const StyleString& str, const Context& context); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 172 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Adds a style from another string pool. Returns a reference to the |
| 175 | * style in the string pool. |
| 176 | */ |
| 177 | StyleRef makeRef(const StyleRef& ref); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 178 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | /** |
| 180 | * Moves pool into this one without coalescing strings. When this |
| 181 | * function returns, pool will be empty. |
| 182 | */ |
| 183 | void merge(StringPool&& pool); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 184 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 185 | /** |
| 186 | * Retuns the number of strings in the table. |
| 187 | */ |
| 188 | inline size_t size() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 189 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Reserves space for strings and styles as an optimization. |
| 192 | */ |
| 193 | void hintWillAdd(size_t stringCount, size_t styleCount); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 194 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 195 | /** |
| 196 | * Sorts the strings according to some comparison function. |
| 197 | */ |
| 198 | void sort(const std::function<bool(const Entry&, const Entry&)>& cmp); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 199 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 200 | /** |
| 201 | * Removes any strings that have no references. |
| 202 | */ |
| 203 | void prune(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 204 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | private: |
| 206 | friend const_iterator begin(const StringPool& pool); |
| 207 | friend const_iterator end(const StringPool& pool); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 208 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | static bool flatten(BigBuffer* out, const StringPool& pool, bool utf8); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 210 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 211 | Ref makeRefImpl(const StringPiece& str, const Context& context, bool unique); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 212 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | std::vector<std::unique_ptr<Entry>> mStrings; |
| 214 | std::vector<std::unique_ptr<StyleEntry>> mStyles; |
| 215 | std::unordered_multimap<StringPiece, Entry*> mIndexedStrings; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | // |
| 219 | // Inline implementation |
| 220 | // |
| 221 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 222 | inline size_t StringPool::size() const { return mStrings.size(); } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 223 | |
| 224 | inline StringPool::const_iterator begin(const StringPool& pool) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 225 | return pool.mStrings.begin(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | inline StringPool::const_iterator end(const StringPool& pool) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | return pool.mStrings.end(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 232 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 233 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 234 | #endif // AAPT_STRING_POOL_H |