blob: 3c1f3dc3a1bb2db0bf405d9aa50d5ea1e72f3438 [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 Lesinski8a0b2382017-10-18 15:07:33 -070052 using size_type = size_t;
53
Adam Lesinskib54ef102016-10-21 13:38:42 -070054 class Context {
55 public:
56 enum : uint32_t {
Adam Lesinskib54ef102016-10-21 13:38:42 -070057 kHighPriority = 1u,
58 kNormalPriority = 0x7fffffffu,
59 kLowPriority = 0xffffffffu,
60 };
61 uint32_t priority = kNormalPriority;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 ConfigDescription config;
Adam Lesinskib54ef102016-10-21 13:38:42 -070063
64 Context() = default;
65 Context(uint32_t p, const ConfigDescription& c) : priority(p), config(c) {}
66 explicit Context(uint32_t p) : priority(p) {}
Adam Lesinski5b6ee112017-07-28 17:10:35 -070067 explicit Context(const ConfigDescription& c) : priority(kNormalPriority), config(c) {
68 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 class Entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 class Ref {
74 public:
75 Ref();
76 Ref(const Ref&);
77 ~Ref();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 Ref& operator=(const Ref& rhs);
Adam Lesinski75421622017-01-06 15:20:04 -080080 bool operator==(const Ref& rhs) const;
81 bool operator!=(const Ref& rhs) const;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 const std::string* operator->() const;
83 const std::string& operator*() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 size_t index() const;
86 const Context& GetContext() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 private:
89 friend class StringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 explicit Ref(Entry* entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 Entry* entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 class StyleEntry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 class StyleRef {
99 public:
100 StyleRef();
101 StyleRef(const StyleRef&);
102 ~StyleRef();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 StyleRef& operator=(const StyleRef& rhs);
Adam Lesinski75421622017-01-06 15:20:04 -0800105 bool operator==(const StyleRef& rhs) const;
106 bool operator!=(const StyleRef& rhs) const;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 const StyleEntry* operator->() const;
108 const StyleEntry& operator*() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 size_t index() const;
111 const Context& GetContext() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 private:
114 friend class StringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 explicit StyleRef(StyleEntry* entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 StyleEntry* entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 class Entry {
122 public:
123 std::string value;
124 Context context;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 private:
127 friend class StringPool;
128 friend class Ref;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700130 size_t index_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 int ref_;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700132 const StringPool* pool_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 struct Span {
136 Ref name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 uint32_t first_char;
138 uint32_t last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 class StyleEntry {
142 public:
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700143 std::string value;
144 Context context;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 std::vector<Span> spans;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 private:
148 friend class StringPool;
149 friend class StyleRef;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700151 size_t index_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 int ref_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 static bool FlattenUtf8(BigBuffer* out, const StringPool& pool);
156 static bool FlattenUtf16(BigBuffer* out, const StringPool& pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800157
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 StringPool() = default;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700159 StringPool(StringPool&&) = default;
160 StringPool& operator=(StringPool&&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700162 // Adds a string to the pool, unless it already exists. Returns a reference to the string in the
163 // pool.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800164 Ref MakeRef(const android::StringPiece& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800165
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700166 // 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 Lesinskid5083f62017-01-16 15:07:21 -0800168 Ref MakeRef(const android::StringPiece& str, const Context& context);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800169
Adam Lesinski8a0b2382017-10-18 15:07:33 -0700170 // 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 Lesinski5b6ee112017-07-28 17:10:35 -0700173 // Adds a style to the string pool and returns a reference to it.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 StyleRef MakeRef(const StyleString& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800175
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700176 // 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 Lesinskice5e56e2016-10-21 17:56:45 -0700178 StyleRef MakeRef(const StyleString& str, const Context& context);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800179
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700180 // Adds a style from another string pool. Returns a reference to the style in the string pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 StyleRef MakeRef(const StyleRef& ref);
Adam Lesinski769de982015-04-10 19:43:55 -0700182
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700183 // Moves pool into this one without coalescing strings. When this function returns, pool will be
184 // empty.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 void Merge(StringPool&& pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700187 inline const std::vector<std::unique_ptr<Entry>>& strings() const {
188 return strings_;
189 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800190
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700191 // 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 Lesinskice5e56e2016-10-21 17:56:45 -0700197 void HintWillAdd(size_t string_count, size_t style_count);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800198
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700199 // 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 Lesinski6f6ceb72014-11-14 14:48:12 -0800203
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700204 // Removes any strings that have no references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 void Prune();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 private:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700208 DISALLOW_COPY_AND_ASSIGN(StringPool);
209
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 Lesinski5b6ee112017-07-28 17:10:35 -0700213 void ReAssignIndices();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800214
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 std::vector<std::unique_ptr<Entry>> strings_;
216 std::vector<std::unique_ptr<StyleEntry>> styles_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800217 std::unordered_multimap<android::StringPiece, Entry*> indexed_strings_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800218};
219
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800221
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222#endif // AAPT_STRING_POOL_H