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 | |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 45 | // A StringPool for storing the value of String and StyledString resources. |
| 46 | // Styles and Strings are stored separately, since the runtime variant of this |
| 47 | // class -- ResStringPool -- requires that styled strings *always* appear first, since their |
| 48 | // style data is stored as an array indexed by the same indices as the main string pool array. |
| 49 | // Otherwise, the style data array would have to be sparse and take up more space. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 50 | class StringPool { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | public: |
Adam Lesinski | 8a0b238 | 2017-10-18 15:07:33 -0700 | [diff] [blame^] | 52 | using size_type = size_t; |
| 53 | |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 54 | class Context { |
| 55 | public: |
| 56 | enum : uint32_t { |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 57 | kHighPriority = 1u, |
| 58 | kNormalPriority = 0x7fffffffu, |
| 59 | kLowPriority = 0xffffffffu, |
| 60 | }; |
| 61 | uint32_t priority = kNormalPriority; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | ConfigDescription config; |
Adam Lesinski | b54ef10 | 2016-10-21 13:38:42 -0700 | [diff] [blame] | 63 | |
| 64 | Context() = default; |
| 65 | Context(uint32_t p, const ConfigDescription& c) : priority(p), config(c) {} |
| 66 | explicit Context(uint32_t p) : priority(p) {} |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 67 | explicit Context(const ConfigDescription& c) : priority(kNormalPriority), config(c) { |
| 68 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | }; |
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 | class Entry; |
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 | class Ref { |
| 74 | public: |
| 75 | Ref(); |
| 76 | Ref(const Ref&); |
| 77 | ~Ref(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | Ref& operator=(const Ref& rhs); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 80 | bool operator==(const Ref& rhs) const; |
| 81 | bool operator!=(const Ref& rhs) const; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | const std::string* operator->() const; |
| 83 | const std::string& operator*() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | size_t index() const; |
| 86 | const Context& GetContext() const; |
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 | private: |
| 89 | friend class StringPool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 90 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | explicit Ref(Entry* entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 92 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | Entry* entry_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 94 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 95 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | class StyleEntry; |
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 | class StyleRef { |
| 99 | public: |
| 100 | StyleRef(); |
| 101 | StyleRef(const StyleRef&); |
| 102 | ~StyleRef(); |
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 | StyleRef& operator=(const StyleRef& rhs); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 105 | bool operator==(const StyleRef& rhs) const; |
| 106 | bool operator!=(const StyleRef& rhs) const; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | const StyleEntry* operator->() const; |
| 108 | const StyleEntry& operator*() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 109 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | size_t index() const; |
| 111 | const Context& GetContext() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 112 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | private: |
| 114 | friend class StringPool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 115 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 116 | explicit StyleRef(StyleEntry* entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 117 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 118 | StyleEntry* entry_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | }; |
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 | class Entry { |
| 122 | public: |
| 123 | std::string value; |
| 124 | Context context; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 125 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | private: |
| 127 | friend class StringPool; |
| 128 | friend class Ref; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 129 | |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 130 | size_t index_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 131 | int ref_; |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 132 | const StringPool* pool_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 133 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 134 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 135 | struct Span { |
| 136 | Ref name; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 137 | uint32_t first_char; |
| 138 | uint32_t last_char; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 140 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | class StyleEntry { |
| 142 | public: |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 143 | std::string value; |
| 144 | Context context; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | std::vector<Span> spans; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 146 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | private: |
| 148 | friend class StringPool; |
| 149 | friend class StyleRef; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 150 | |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 151 | size_t index_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 152 | int ref_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 154 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | static bool FlattenUtf8(BigBuffer* out, const StringPool& pool); |
| 156 | static bool FlattenUtf16(BigBuffer* out, const StringPool& pool); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 157 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | StringPool() = default; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 159 | StringPool(StringPool&&) = default; |
| 160 | StringPool& operator=(StringPool&&) = default; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 161 | |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 162 | // Adds a string to the pool, unless it already exists. Returns a reference to the string in the |
| 163 | // pool. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 164 | Ref MakeRef(const android::StringPiece& str); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 165 | |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 166 | // Adds a string to the pool, unless it already exists, with a context object that can be used |
| 167 | // when sorting the string pool. Returns a reference to the string in the pool. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 168 | Ref MakeRef(const android::StringPiece& str, const Context& context); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 169 | |
Adam Lesinski | 8a0b238 | 2017-10-18 15:07:33 -0700 | [diff] [blame^] | 170 | // Adds a string from another string pool. Returns a reference to the string in the string pool. |
| 171 | Ref MakeRef(const Ref& ref); |
| 172 | |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 173 | // Adds a style to the string pool and returns a reference to it. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 174 | StyleRef MakeRef(const StyleString& str); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 175 | |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 176 | // Adds a style to the string pool with a context object that can be used when sorting the string |
| 177 | // pool. Returns a reference to the style in the string pool. |
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 | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 180 | // Adds a style from another string pool. Returns a reference to the style in the string pool. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 181 | StyleRef MakeRef(const StyleRef& ref); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 182 | |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 183 | // Moves pool into this one without coalescing strings. When this function returns, pool will be |
| 184 | // empty. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 185 | void Merge(StringPool&& pool); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 186 | |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 187 | inline const std::vector<std::unique_ptr<Entry>>& strings() const { |
| 188 | return strings_; |
| 189 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 190 | |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 191 | // Returns the number of strings in the table. |
| 192 | inline size_t size() const { |
| 193 | return styles_.size() + strings_.size(); |
| 194 | } |
| 195 | |
| 196 | // Reserves space for strings and styles as an optimization. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 197 | void HintWillAdd(size_t string_count, size_t style_count); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 198 | |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 199 | // Sorts the strings according to their Context using some comparison function. |
| 200 | // Equal Contexts are further sorted by string value, lexicographically. |
| 201 | // If no comparison function is provided, values are only sorted lexicographically. |
| 202 | void Sort(const std::function<int(const Context&, const Context&)>& cmp = nullptr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 203 | |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 204 | // Removes any strings that have no references. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 205 | void Prune(); |
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 | private: |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 208 | DISALLOW_COPY_AND_ASSIGN(StringPool); |
| 209 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | static bool Flatten(BigBuffer* out, const StringPool& pool, bool utf8); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 211 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 212 | Ref MakeRefImpl(const android::StringPiece& str, const Context& context, bool unique); |
Adam Lesinski | 5b6ee11 | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 213 | void ReAssignIndices(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 214 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | std::vector<std::unique_ptr<Entry>> strings_; |
| 216 | std::vector<std::unique_ptr<StyleEntry>> styles_; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 217 | std::unordered_multimap<android::StringPiece, Entry*> indexed_strings_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 220 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 221 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 222 | #endif // AAPT_STRING_POOL_H |