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