blob: 3457e0b4185927830a5d037c140c14288d674bc5 [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"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020027#include "androidfw/ConfigDescription.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080028#include "androidfw/StringPiece.h"
29
Ryan Mitchella15c2a82018-03-26 11:05:31 -070030#include "Diagnostics.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031#include "util/BigBuffer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033namespace aapt {
34
35struct Span {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 std::string name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 uint32_t first_char;
38 uint32_t last_char;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070039
40 bool operator==(const Span& right) const {
41 return name == right.name && first_char == right.first_char && last_char == right.last_char;
42 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043};
44
45struct StyleString {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 std::string str;
47 std::vector<Span> spans;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048};
49
Adam Lesinski5b6ee112017-07-28 17:10:35 -070050// A StringPool for storing the value of String and StyledString resources.
51// Styles and Strings are stored separately, since the runtime variant of this
52// class -- ResStringPool -- requires that styled strings *always* appear first, since their
53// style data is stored as an array indexed by the same indices as the main string pool array.
54// Otherwise, the style data array would have to be sparse and take up more space.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055class StringPool {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 public:
Adam Lesinski8a0b2382017-10-18 15:07:33 -070057 using size_type = size_t;
58
Adam Lesinskib54ef102016-10-21 13:38:42 -070059 class Context {
60 public:
61 enum : uint32_t {
Adam Lesinskib54ef102016-10-21 13:38:42 -070062 kHighPriority = 1u,
63 kNormalPriority = 0x7fffffffu,
64 kLowPriority = 0xffffffffu,
65 };
66 uint32_t priority = kNormalPriority;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020067 android::ConfigDescription config;
Adam Lesinskib54ef102016-10-21 13:38:42 -070068
69 Context() = default;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020070 Context(uint32_t p, const android::ConfigDescription& c) : priority(p), config(c) {}
Adam Lesinskib54ef102016-10-21 13:38:42 -070071 explicit Context(uint32_t p) : priority(p) {}
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020072 explicit Context(const android::ConfigDescription& c) : priority(kNormalPriority), config(c) {
Adam Lesinski5b6ee112017-07-28 17:10:35 -070073 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 class Entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 class Ref {
79 public:
80 Ref();
81 Ref(const Ref&);
82 ~Ref();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 Ref& operator=(const Ref& rhs);
Adam Lesinski75421622017-01-06 15:20:04 -080085 bool operator==(const Ref& rhs) const;
86 bool operator!=(const Ref& rhs) const;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 const std::string* operator->() const;
88 const std::string& operator*() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 size_t index() const;
91 const Context& GetContext() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 private:
94 friend class StringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 explicit Ref(Entry* entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 Entry* entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 class StyleEntry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 class StyleRef {
104 public:
105 StyleRef();
106 StyleRef(const StyleRef&);
107 ~StyleRef();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 StyleRef& operator=(const StyleRef& rhs);
Adam Lesinski75421622017-01-06 15:20:04 -0800110 bool operator==(const StyleRef& rhs) const;
111 bool operator!=(const StyleRef& rhs) const;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 const StyleEntry* operator->() const;
113 const StyleEntry& operator*() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 size_t index() const;
116 const Context& GetContext() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800117
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 private:
119 friend class StringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 explicit StyleRef(StyleEntry* entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 StyleEntry* entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 class Entry {
127 public:
128 std::string value;
129 Context context;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 private:
132 friend class StringPool;
133 friend class Ref;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700135 size_t index_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 int ref_;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700137 const StringPool* pool_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 struct Span {
141 Ref name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 uint32_t first_char;
143 uint32_t last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800145
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 class StyleEntry {
147 public:
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700148 std::string value;
149 Context context;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 std::vector<Span> spans;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800151
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 private:
153 friend class StringPool;
154 friend class StyleRef;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700156 size_t index_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 int ref_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800159
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700160 static bool FlattenUtf8(BigBuffer* out, const StringPool& pool, IDiagnostics* diag);
161 static bool FlattenUtf16(BigBuffer* out, const StringPool& pool, IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 StringPool() = default;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700164 StringPool(StringPool&&) = default;
165 StringPool& operator=(StringPool&&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800166
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700167 // Adds a string to the pool, unless it already exists. Returns a reference to the string in the
168 // pool.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800169 Ref MakeRef(const android::StringPiece& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800170
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700171 // Adds a string to the pool, unless it already exists, with a context object that can be used
172 // when sorting the string pool. Returns a reference to the string in the pool.
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000173 Ref MakeRef(const android::StringPiece& str, const Context& context);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174
Adam Lesinski8a0b2382017-10-18 15:07:33 -0700175 // Adds a string from another string pool. Returns a reference to the string in the string pool.
176 Ref MakeRef(const Ref& ref);
177
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700178 // Adds a style to the string pool and returns a reference to it.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 StyleRef MakeRef(const StyleString& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800180
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700181 // Adds a style to the string pool with a context object that can be used when sorting the string
182 // pool. Returns a reference to the style in the string pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700183 StyleRef MakeRef(const StyleString& str, const Context& context);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800184
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700185 // Adds a style from another string pool. Returns a reference to the style in the string pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 StyleRef MakeRef(const StyleRef& ref);
Adam Lesinski769de982015-04-10 19:43:55 -0700187
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700188 // Moves pool into this one without coalescing strings. When this function returns, pool will be
189 // empty.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 void Merge(StringPool&& pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800191
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700192 inline const std::vector<std::unique_ptr<Entry>>& strings() const {
193 return strings_;
194 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800195
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700196 // Returns the number of strings in the table.
197 inline size_t size() const {
198 return styles_.size() + strings_.size();
199 }
200
201 // Reserves space for strings and styles as an optimization.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 void HintWillAdd(size_t string_count, size_t style_count);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800203
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700204 // Sorts the strings according to their Context using some comparison function.
205 // Equal Contexts are further sorted by string value, lexicographically.
206 // If no comparison function is provided, values are only sorted lexicographically.
207 void Sort(const std::function<int(const Context&, const Context&)>& cmp = nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800208
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700209 // Removes any strings that have no references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 void Prune();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800211
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 private:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700213 DISALLOW_COPY_AND_ASSIGN(StringPool);
214
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700215 static bool Flatten(BigBuffer* out, const StringPool& pool, bool utf8, IDiagnostics* diag);
Adam Lesinski24aad162015-04-24 19:19:30 -0700216
Ryan Mitchell90b7a082019-02-15 17:39:58 +0000217 Ref MakeRefImpl(const android::StringPiece& str, const Context& context, bool unique);
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700218 void ReAssignIndices();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700220 std::vector<std::unique_ptr<Entry>> strings_;
221 std::vector<std::unique_ptr<StyleEntry>> styles_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800222 std::unordered_multimap<android::StringPiece, Entry*> indexed_strings_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800223};
224
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227#endif // AAPT_STRING_POOL_H