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 <functional> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <string> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 23 | #include <unordered_map> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 26 | #include "android-base/macros.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 27 | #include "androidfw/StringPiece.h" |
| 28 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 29 | #include "ConfigDescription.h" |
| 30 | #include "util/BigBuffer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 31 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
| 34 | struct Span { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | std::string name; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | uint32_t first_char; |
| 37 | uint32_t last_char; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | struct StyleString { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | std::string str; |
| 42 | std::vector<Span> spans; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | class StringPool { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 46 | public: |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 47 | class Context { |
| 48 | public: |
| 49 | enum : uint32_t { |
| 50 | kStylePriority = 0u, |
| 51 | kHighPriority = 1u, |
| 52 | kNormalPriority = 0x7fffffffu, |
| 53 | kLowPriority = 0xffffffffu, |
| 54 | }; |
| 55 | uint32_t priority = kNormalPriority; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | ConfigDescription config; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 57 | |
| 58 | Context() = default; |
| 59 | Context(uint32_t p, const ConfigDescription& c) : priority(p), config(c) {} |
| 60 | explicit Context(uint32_t p) : priority(p) {} |
| 61 | explicit Context(const ConfigDescription& c) |
| 62 | : priority(kNormalPriority), config(c) {} |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | }; |
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 Entry; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 66 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | class Ref { |
| 68 | public: |
| 69 | Ref(); |
| 70 | Ref(const Ref&); |
| 71 | ~Ref(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 72 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | Ref& operator=(const Ref& rhs); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 74 | bool operator==(const Ref& rhs) const; |
| 75 | bool operator!=(const Ref& rhs) const; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | const std::string* operator->() const; |
| 77 | const std::string& operator*() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | size_t index() const; |
| 80 | const Context& GetContext() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 81 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | private: |
| 83 | friend class StringPool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | explicit Ref(Entry* entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 86 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | Entry* entry_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 89 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 90 | class StyleEntry; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 91 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | class StyleRef { |
| 93 | public: |
| 94 | StyleRef(); |
| 95 | StyleRef(const StyleRef&); |
| 96 | ~StyleRef(); |
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 | StyleRef& operator=(const StyleRef& rhs); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 99 | bool operator==(const StyleRef& rhs) const; |
| 100 | bool operator!=(const StyleRef& rhs) const; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | const StyleEntry* operator->() const; |
| 102 | const StyleEntry& operator*() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 103 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | size_t index() const; |
| 105 | const Context& GetContext() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 106 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | private: |
| 108 | friend class StringPool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 109 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | explicit StyleRef(StyleEntry* entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 111 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | StyleEntry* entry_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | }; |
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 | class Entry { |
| 116 | public: |
| 117 | std::string value; |
| 118 | Context context; |
| 119 | size_t index; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 120 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | private: |
| 122 | friend class StringPool; |
| 123 | friend class Ref; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 124 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 125 | int ref_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 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 | struct Span { |
| 129 | Ref name; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | uint32_t first_char; |
| 131 | uint32_t last_char; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 133 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | class StyleEntry { |
| 135 | public: |
| 136 | Ref str; |
| 137 | std::vector<Span> spans; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 138 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | private: |
| 140 | friend class StringPool; |
| 141 | friend class StyleRef; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 142 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 143 | int ref_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 145 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 146 | using const_iterator = std::vector<std::unique_ptr<Entry>>::const_iterator; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 147 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | static bool FlattenUtf8(BigBuffer* out, const StringPool& pool); |
| 149 | static bool FlattenUtf16(BigBuffer* out, const StringPool& pool); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 150 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 151 | StringPool() = default; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 152 | StringPool(StringPool&&) = default; |
| 153 | StringPool& operator=(StringPool&&) = default; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 154 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | /** |
| 156 | * Adds a string to the pool, unless it already exists. Returns |
| 157 | * a reference to the string in the pool. |
| 158 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 159 | Ref MakeRef(const android::StringPiece& str); |
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 string to the pool, unless it already exists, with a context |
| 163 | * object that can be used when sorting the string pool. Returns |
| 164 | * a reference to the string in the pool. |
| 165 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 166 | Ref MakeRef(const android::StringPiece& str, const Context& context); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 167 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Adds a style to the string pool and returns a reference to it. |
| 170 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 171 | StyleRef MakeRef(const StyleString& str); |
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 to the string pool with a context object that |
| 175 | * can be used when sorting the string pool. Returns a reference |
| 176 | * to the style in the string pool. |
| 177 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 178 | StyleRef MakeRef(const StyleString& str, const Context& context); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 179 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 180 | /** |
| 181 | * Adds a style from another string pool. Returns a reference to the |
| 182 | * style in the string pool. |
| 183 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | StyleRef MakeRef(const StyleRef& ref); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 185 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Moves pool into this one without coalescing strings. When this |
| 188 | * function returns, pool will be empty. |
| 189 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 190 | void Merge(StringPool&& pool); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 191 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | /** |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | * Returns the number of strings in the table. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 194 | */ |
| 195 | inline size_t size() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 196 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 197 | /** |
| 198 | * Reserves space for strings and styles as an optimization. |
| 199 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 200 | void HintWillAdd(size_t string_count, size_t style_count); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 201 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 202 | /** |
| 203 | * Sorts the strings according to some comparison function. |
| 204 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 205 | void Sort(const std::function<bool(const Entry&, const Entry&)>& cmp); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 206 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 207 | /** |
| 208 | * Removes any strings that have no references. |
| 209 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | void Prune(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 211 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 212 | private: |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 213 | DISALLOW_COPY_AND_ASSIGN(StringPool); |
| 214 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | friend const_iterator begin(const StringPool& pool); |
| 216 | friend const_iterator end(const StringPool& pool); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 217 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 218 | static bool Flatten(BigBuffer* out, const StringPool& pool, bool utf8); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 219 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 220 | Ref MakeRefImpl(const android::StringPiece& str, const Context& context, bool unique); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 221 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 222 | std::vector<std::unique_ptr<Entry>> strings_; |
| 223 | std::vector<std::unique_ptr<StyleEntry>> styles_; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 224 | std::unordered_multimap<android::StringPiece, Entry*> indexed_strings_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | // |
| 228 | // Inline implementation |
| 229 | // |
| 230 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 231 | inline size_t StringPool::size() const { return strings_.size(); } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 232 | |
| 233 | inline StringPool::const_iterator begin(const StringPool& pool) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 234 | return pool.strings_.begin(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | inline StringPool::const_iterator end(const StringPool& pool) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 238 | return pool.strings_.end(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 241 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 242 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 243 | #endif // AAPT_STRING_POOL_H |