blob: 6e0d646cae1251b40f18bf8ae308256afd698830 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinski6f6ceb72014-11-14 14:48:12 -080020#include "ConfigDescription.h"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070021#include "util/BigBuffer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022#include "util/StringPiece.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023
24#include <functional>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025#include <memory>
26#include <string>
Adam Lesinskicacb28f2016-10-19 12:18:14 -070027#include <unordered_map>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028#include <vector>
29
30namespace aapt {
31
32struct Span {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 std::string name;
34 uint32_t firstChar;
35 uint32_t lastChar;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036};
37
38struct StyleString {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 std::string str;
40 std::vector<Span> spans;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041};
42
43class StringPool {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 public:
Adam Lesinskib54ef102016-10-21 13:38:42 -070045 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 Lesinskicacb28f2016-10-19 12:18:14 -070054 ConfigDescription config;
Adam Lesinskib54ef102016-10-21 13:38:42 -070055
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 Lesinskicacb28f2016-10-19 12:18:14 -070061 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 class Entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 class Ref {
66 public:
67 Ref();
68 Ref(const Ref&);
69 ~Ref();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 Ref& operator=(const Ref& rhs);
72 const std::string* operator->() const;
73 const std::string& operator*() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 size_t getIndex() const;
76 const Context& getContext() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 private:
79 friend class StringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 explicit Ref(Entry* entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 Entry* mEntry;
84 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 class StyleEntry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 class StyleRef {
89 public:
90 StyleRef();
91 StyleRef(const StyleRef&);
92 ~StyleRef();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 StyleRef& operator=(const StyleRef& rhs);
95 const StyleEntry* operator->() const;
96 const StyleEntry& operator*() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 size_t getIndex() const;
99 const Context& getContext() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 private:
102 friend class StringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 explicit StyleRef(StyleEntry* entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 StyleEntry* mEntry;
107 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 class Entry {
110 public:
111 std::string value;
112 Context context;
113 size_t index;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 private:
116 friend class StringPool;
117 friend class Ref;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 int ref;
120 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 struct Span {
123 Ref name;
124 uint32_t firstChar;
125 uint32_t lastChar;
126 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 class StyleEntry {
129 public:
130 Ref str;
131 std::vector<Span> spans;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 private:
134 friend class StringPool;
135 friend class StyleRef;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 int ref;
138 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 using const_iterator = std::vector<std::unique_ptr<Entry>>::const_iterator;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 static bool flattenUtf8(BigBuffer* out, const StringPool& pool);
143 static bool flattenUtf16(BigBuffer* out, const StringPool& pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 StringPool() = default;
146 StringPool(const StringPool&) = delete;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800147
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 /**
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 Lesinski6f6ceb72014-11-14 14:48:12 -0800153
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 /**
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 Lesinski6f6ceb72014-11-14 14:48:12 -0800160
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 /**
162 * Adds a style to the string pool and returns a reference to it.
163 */
164 StyleRef makeRef(const StyleString& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800165
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 /**
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 Lesinski6f6ceb72014-11-14 14:48:12 -0800172
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 /**
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 Lesinski769de982015-04-10 19:43:55 -0700178
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 /**
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 Lesinski6f6ceb72014-11-14 14:48:12 -0800184
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 /**
186 * Retuns the number of strings in the table.
187 */
188 inline size_t size() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800189
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 /**
191 * Reserves space for strings and styles as an optimization.
192 */
193 void hintWillAdd(size_t stringCount, size_t styleCount);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800194
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 /**
196 * Sorts the strings according to some comparison function.
197 */
198 void sort(const std::function<bool(const Entry&, const Entry&)>& cmp);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 /**
201 * Removes any strings that have no references.
202 */
203 void prune();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 private:
206 friend const_iterator begin(const StringPool& pool);
207 friend const_iterator end(const StringPool& pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800208
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 static bool flatten(BigBuffer* out, const StringPool& pool, bool utf8);
Adam Lesinski24aad162015-04-24 19:19:30 -0700210
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 Ref makeRefImpl(const StringPiece& str, const Context& context, bool unique);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 std::vector<std::unique_ptr<Entry>> mStrings;
214 std::vector<std::unique_ptr<StyleEntry>> mStyles;
215 std::unordered_multimap<StringPiece, Entry*> mIndexedStrings;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800216};
217
218//
219// Inline implementation
220//
221
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222inline size_t StringPool::size() const { return mStrings.size(); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223
224inline StringPool::const_iterator begin(const StringPool& pool) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 return pool.mStrings.begin();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226}
227
228inline StringPool::const_iterator end(const StringPool& pool) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 return pool.mStrings.end();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800230}
231
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800233
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234#endif // AAPT_STRING_POOL_H