blob: d0ce489dae269eed7d636ec3b509beec62930d29 [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 <functional>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021#include <memory>
22#include <string>
Adam Lesinskicacb28f2016-10-19 12:18:14 -070023#include <unordered_map>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <vector>
25
Adam Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
27
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028#include "ConfigDescription.h"
29#include "util/BigBuffer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031namespace aapt {
32
33struct Span {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 std::string name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 uint32_t first_char;
36 uint32_t last_char;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037};
38
39struct StyleString {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 std::string str;
41 std::vector<Span> spans;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042};
43
44class StringPool {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 public:
Adam Lesinskib54ef102016-10-21 13:38:42 -070046 class Context {
47 public:
48 enum : uint32_t {
49 kStylePriority = 0u,
50 kHighPriority = 1u,
51 kNormalPriority = 0x7fffffffu,
52 kLowPriority = 0xffffffffu,
53 };
54 uint32_t priority = kNormalPriority;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 ConfigDescription config;
Adam Lesinskib54ef102016-10-21 13:38:42 -070056
57 Context() = default;
58 Context(uint32_t p, const ConfigDescription& c) : priority(p), config(c) {}
59 explicit Context(uint32_t p) : priority(p) {}
60 explicit Context(const ConfigDescription& c)
61 : priority(kNormalPriority), config(c) {}
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 class Entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 class Ref {
67 public:
68 Ref();
69 Ref(const Ref&);
70 ~Ref();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 Ref& operator=(const Ref& rhs);
73 const std::string* operator->() const;
74 const std::string& operator*() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 size_t index() const;
77 const Context& GetContext() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 private:
80 friend class StringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 explicit Ref(Entry* entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 Entry* entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 class StyleEntry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 class StyleRef {
90 public:
91 StyleRef();
92 StyleRef(const StyleRef&);
93 ~StyleRef();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 StyleRef& operator=(const StyleRef& rhs);
96 const StyleEntry* operator->() const;
97 const StyleEntry& operator*() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 size_t index() const;
100 const Context& GetContext() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 private:
103 friend class StringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 explicit StyleRef(StyleEntry* entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 StyleEntry* entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 class Entry {
111 public:
112 std::string value;
113 Context context;
114 size_t index;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 private:
117 friend class StringPool;
118 friend class Ref;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 int ref_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 struct Span {
124 Ref name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 uint32_t first_char;
126 uint32_t last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 class StyleEntry {
130 public:
131 Ref str;
132 std::vector<Span> spans;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 private:
135 friend class StringPool;
136 friend class StyleRef;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 int ref_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 using const_iterator = std::vector<std::unique_ptr<Entry>>::const_iterator;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 static bool FlattenUtf8(BigBuffer* out, const StringPool& pool);
144 static bool FlattenUtf16(BigBuffer* out, const StringPool& pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800145
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 StringPool() = default;
147 StringPool(const StringPool&) = delete;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800148
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 /**
150 * Adds a string to the pool, unless it already exists. Returns
151 * a reference to the string in the pool.
152 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800153 Ref MakeRef(const android::StringPiece& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 /**
156 * Adds a string to the pool, unless it already exists, with a context
157 * object that can be used when sorting the string pool. Returns
158 * a reference to the string in the pool.
159 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800160 Ref MakeRef(const android::StringPiece& str, const Context& context);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 /**
163 * Adds a style to the string pool and returns a reference to it.
164 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 StyleRef MakeRef(const StyleString& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800166
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 /**
168 * Adds a style to the string pool with a context object that
169 * can be used when sorting the string pool. Returns a reference
170 * to the style in the string pool.
171 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 StyleRef MakeRef(const StyleString& str, const Context& context);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800173
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 /**
175 * Adds a style from another string pool. Returns a reference to the
176 * style in the string pool.
177 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 StyleRef MakeRef(const StyleRef& ref);
Adam Lesinski769de982015-04-10 19:43:55 -0700179
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 /**
181 * Moves pool into this one without coalescing strings. When this
182 * function returns, pool will be empty.
183 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 void Merge(StringPool&& pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 * Returns the number of strings in the table.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 */
189 inline size_t size() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800190
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 /**
192 * Reserves space for strings and styles as an optimization.
193 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 void HintWillAdd(size_t string_count, size_t style_count);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800195
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 /**
197 * Sorts the strings according to some comparison function.
198 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 void Sort(const std::function<bool(const Entry&, const Entry&)>& cmp);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800200
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 /**
202 * Removes any strings that have no references.
203 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 void Prune();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800205
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 private:
207 friend const_iterator begin(const StringPool& pool);
208 friend const_iterator end(const StringPool& pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800209
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 static bool Flatten(BigBuffer* out, const StringPool& pool, bool utf8);
Adam Lesinski24aad162015-04-24 19:19:30 -0700211
Adam Lesinskid5083f62017-01-16 15:07:21 -0800212 Ref MakeRefImpl(const android::StringPiece& str, const Context& context, bool unique);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 std::vector<std::unique_ptr<Entry>> strings_;
215 std::vector<std::unique_ptr<StyleEntry>> styles_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800216 std::unordered_multimap<android::StringPiece, Entry*> indexed_strings_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800217};
218
219//
220// Inline implementation
221//
222
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223inline size_t StringPool::size() const { return strings_.size(); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224
225inline StringPool::const_iterator begin(const StringPool& pool) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226 return pool.strings_.begin();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227}
228
229inline StringPool::const_iterator end(const StringPool& pool) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 return pool.strings_.end();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231}
232
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800234
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235#endif // AAPT_STRING_POOL_H