blob: 8350d0d09108f5cc3ff02665b82a8d7860e1b402 [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 Lesinskid0f492d2017-04-03 18:12:45 -070026#include "android-base/macros.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080027#include "androidfw/StringPiece.h"
28
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029#include "ConfigDescription.h"
30#include "util/BigBuffer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032namespace aapt {
33
34struct Span {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 std::string name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 uint32_t first_char;
37 uint32_t last_char;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038};
39
40struct StyleString {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 std::string str;
42 std::vector<Span> spans;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043};
44
Adam Lesinski5b6ee112017-07-28 17:10:35 -070045// 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 Lesinski6f6ceb72014-11-14 14:48:12 -080050class StringPool {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 public:
Adam Lesinskib54ef102016-10-21 13:38:42 -070052 class Context {
53 public:
54 enum : uint32_t {
Adam Lesinskib54ef102016-10-21 13:38:42 -070055 kHighPriority = 1u,
56 kNormalPriority = 0x7fffffffu,
57 kLowPriority = 0xffffffffu,
58 };
59 uint32_t priority = kNormalPriority;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 ConfigDescription config;
Adam Lesinskib54ef102016-10-21 13:38:42 -070061
62 Context() = default;
63 Context(uint32_t p, const ConfigDescription& c) : priority(p), config(c) {}
64 explicit Context(uint32_t p) : priority(p) {}
Adam Lesinski5b6ee112017-07-28 17:10:35 -070065 explicit Context(const ConfigDescription& c) : priority(kNormalPriority), config(c) {
66 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 class Entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 class Ref {
72 public:
73 Ref();
74 Ref(const Ref&);
75 ~Ref();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 Ref& operator=(const Ref& rhs);
Adam Lesinski75421622017-01-06 15:20:04 -080078 bool operator==(const Ref& rhs) const;
79 bool operator!=(const Ref& rhs) const;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 const std::string* operator->() const;
81 const std::string& operator*() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 size_t index() const;
84 const Context& GetContext() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 private:
87 friend class StringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 explicit Ref(Entry* entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 Entry* entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 class StyleEntry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 class StyleRef {
97 public:
98 StyleRef();
99 StyleRef(const StyleRef&);
100 ~StyleRef();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 StyleRef& operator=(const StyleRef& rhs);
Adam Lesinski75421622017-01-06 15:20:04 -0800103 bool operator==(const StyleRef& rhs) const;
104 bool operator!=(const StyleRef& rhs) const;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 const StyleEntry* operator->() const;
106 const StyleEntry& operator*() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 size_t index() const;
109 const Context& GetContext() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800110
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 private:
112 friend class StringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 explicit StyleRef(StyleEntry* entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 StyleEntry* entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 class Entry {
120 public:
121 std::string value;
122 Context context;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 private:
125 friend class StringPool;
126 friend class Ref;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700128 size_t index_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 int ref_;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700130 const StringPool* pool_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 struct Span {
134 Ref name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 uint32_t first_char;
136 uint32_t last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 class StyleEntry {
140 public:
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700141 std::string value;
142 Context context;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 std::vector<Span> spans;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 private:
146 friend class StringPool;
147 friend class StyleRef;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800148
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700149 size_t index_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 int ref_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 static bool FlattenUtf8(BigBuffer* out, const StringPool& pool);
154 static bool FlattenUtf16(BigBuffer* out, const StringPool& pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 StringPool() = default;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700157 StringPool(StringPool&&) = default;
158 StringPool& operator=(StringPool&&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800159
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700160 // Adds a string to the pool, unless it already exists. Returns a reference to the string in the
161 // pool.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800162 Ref MakeRef(const android::StringPiece& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800163
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700164 // Adds a string to the pool, unless it already exists, with a context object that can be used
165 // when sorting the string pool. Returns a reference to the string in the pool.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800166 Ref MakeRef(const android::StringPiece& str, const Context& context);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800167
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700168 // Adds a style to the string pool and returns a reference to it.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 StyleRef MakeRef(const StyleString& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800170
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700171 // Adds a style to the string pool with a context object that can be used when sorting the string
172 // pool. Returns a reference to the style in the string pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 StyleRef MakeRef(const StyleString& str, const Context& context);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700175 // Adds a style from another string pool. Returns a reference to the style in the string pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 StyleRef MakeRef(const StyleRef& ref);
Adam Lesinski769de982015-04-10 19:43:55 -0700177
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700178 // Moves pool into this one without coalescing strings. When this function returns, pool will be
179 // empty.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 void Merge(StringPool&& pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800181
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700182 inline const std::vector<std::unique_ptr<Entry>>& strings() const {
183 return strings_;
184 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700186 // Returns the number of strings in the table.
187 inline size_t size() const {
188 return styles_.size() + strings_.size();
189 }
190
191 // Reserves space for strings and styles as an optimization.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 void HintWillAdd(size_t string_count, size_t style_count);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700194 // Sorts the strings according to their Context using some comparison function.
195 // Equal Contexts are further sorted by string value, lexicographically.
196 // If no comparison function is provided, values are only sorted lexicographically.
197 void Sort(const std::function<int(const Context&, const Context&)>& cmp = nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800198
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700199 // Removes any strings that have no references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 void Prune();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800201
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 private:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700203 DISALLOW_COPY_AND_ASSIGN(StringPool);
204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 static bool Flatten(BigBuffer* out, const StringPool& pool, bool utf8);
Adam Lesinski24aad162015-04-24 19:19:30 -0700206
Adam Lesinskid5083f62017-01-16 15:07:21 -0800207 Ref MakeRefImpl(const android::StringPiece& str, const Context& context, bool unique);
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700208 void ReAssignIndices();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800209
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 std::vector<std::unique_ptr<Entry>> strings_;
211 std::vector<std::unique_ptr<StyleEntry>> styles_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800212 std::unordered_multimap<android::StringPiece, Entry*> indexed_strings_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800213};
214
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800216
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217#endif // AAPT_STRING_POOL_H