blob: 1006ca970dc5b47c8d9b2c65767df65abe81cd52 [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 Kongstad5c541f62018-06-20 08:46:41 +020027#include "androidfw/ConfigDescription.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080028#include "androidfw/StringPiece.h"
29
Ryan Mitchell70414f22018-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;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039};
40
41struct StyleString {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 std::string str;
43 std::vector<Span> spans;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044};
45
Adam Lesinski5b6ee112017-07-28 17:10:35 -070046// A StringPool for storing the value of String and StyledString resources.
47// Styles and Strings are stored separately, since the runtime variant of this
48// class -- ResStringPool -- requires that styled strings *always* appear first, since their
49// style data is stored as an array indexed by the same indices as the main string pool array.
50// Otherwise, the style data array would have to be sparse and take up more space.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051class StringPool {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 public:
Adam Lesinski8a0b2382017-10-18 15:07:33 -070053 using size_type = size_t;
54
Adam Lesinskib54ef102016-10-21 13:38:42 -070055 class Context {
56 public:
57 enum : uint32_t {
Adam Lesinskib54ef102016-10-21 13:38:42 -070058 kHighPriority = 1u,
59 kNormalPriority = 0x7fffffffu,
60 kLowPriority = 0xffffffffu,
61 };
62 uint32_t priority = kNormalPriority;
Mårten Kongstad5c541f62018-06-20 08:46:41 +020063 android::ConfigDescription config;
Adam Lesinskib54ef102016-10-21 13:38:42 -070064
65 Context() = default;
Mårten Kongstad5c541f62018-06-20 08:46:41 +020066 Context(uint32_t p, const android::ConfigDescription& c) : priority(p), config(c) {}
Adam Lesinskib54ef102016-10-21 13:38:42 -070067 explicit Context(uint32_t p) : priority(p) {}
Mårten Kongstad5c541f62018-06-20 08:46:41 +020068 explicit Context(const android::ConfigDescription& c) : priority(kNormalPriority), config(c) {
Adam Lesinski5b6ee112017-07-28 17:10:35 -070069 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 class Entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 class Ref {
75 public:
76 Ref();
77 Ref(const Ref&);
78 ~Ref();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 Ref& operator=(const Ref& rhs);
Adam Lesinski75421622017-01-06 15:20:04 -080081 bool operator==(const Ref& rhs) const;
82 bool operator!=(const Ref& rhs) const;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 const std::string* operator->() const;
84 const std::string& operator*() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 size_t index() const;
87 const Context& GetContext() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 private:
90 friend class StringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 explicit Ref(Entry* entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 Entry* entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 class StyleEntry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 class StyleRef {
100 public:
101 StyleRef();
102 StyleRef(const StyleRef&);
103 ~StyleRef();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 StyleRef& operator=(const StyleRef& rhs);
Adam Lesinski75421622017-01-06 15:20:04 -0800106 bool operator==(const StyleRef& rhs) const;
107 bool operator!=(const StyleRef& rhs) const;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 const StyleEntry* operator->() const;
109 const StyleEntry& operator*() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800110
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 size_t index() const;
112 const Context& GetContext() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 private:
115 friend class StringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 explicit StyleRef(StyleEntry* entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 StyleEntry* entry_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 class Entry {
123 public:
124 std::string value;
125 Context context;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 private:
128 friend class StringPool;
129 friend class Ref;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700131 size_t index_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 int ref_;
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700133 const StringPool* pool_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 struct Span {
137 Ref name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 uint32_t first_char;
139 uint32_t last_char;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 class StyleEntry {
143 public:
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700144 std::string value;
145 Context context;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 std::vector<Span> spans;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800147
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 private:
149 friend class StringPool;
150 friend class StyleRef;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800151
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700152 size_t index_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 int ref_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155
Ryan Mitchell70414f22018-03-26 11:05:31 -0700156 static bool FlattenUtf8(BigBuffer* out, const StringPool& pool, IDiagnostics* diag);
157 static bool FlattenUtf16(BigBuffer* out, const StringPool& pool, IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800158
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 StringPool() = default;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700160 StringPool(StringPool&&) = default;
161 StringPool& operator=(StringPool&&) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700163 // Adds a string to the pool, unless it already exists. Returns a reference to the string in the
164 // pool.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800165 Ref MakeRef(const android::StringPiece& str);
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, with a context object that can be used
168 // when sorting the string pool. Returns a reference to the string in the pool.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800169 Ref MakeRef(const android::StringPiece& str, const Context& context);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800170
Adam Lesinski8a0b2382017-10-18 15:07:33 -0700171 // Adds a string from another string pool. Returns a reference to the string in the string pool.
172 Ref MakeRef(const Ref& ref);
173
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700174 // Adds a style to the string pool and returns a reference to it.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 StyleRef MakeRef(const StyleString& str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800176
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700177 // Adds a style to the string pool with a context object that can be used when sorting the string
178 // pool. Returns a reference to the style in the string pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 StyleRef MakeRef(const StyleString& str, const Context& context);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800180
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700181 // Adds a style from another string pool. Returns a reference to the style in the string pool.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 StyleRef MakeRef(const StyleRef& ref);
Adam Lesinski769de982015-04-10 19:43:55 -0700183
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700184 // Moves pool into this one without coalescing strings. When this function returns, pool will be
185 // empty.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 void Merge(StringPool&& pool);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700188 inline const std::vector<std::unique_ptr<Entry>>& strings() const {
189 return strings_;
190 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800191
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700192 // Returns the number of strings in the table.
193 inline size_t size() const {
194 return styles_.size() + strings_.size();
195 }
196
197 // Reserves space for strings and styles as an optimization.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 void HintWillAdd(size_t string_count, size_t style_count);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700200 // Sorts the strings according to their Context using some comparison function.
201 // Equal Contexts are further sorted by string value, lexicographically.
202 // If no comparison function is provided, values are only sorted lexicographically.
203 void Sort(const std::function<int(const Context&, const Context&)>& cmp = nullptr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700205 // Removes any strings that have no references.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 void Prune();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 private:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700209 DISALLOW_COPY_AND_ASSIGN(StringPool);
210
Ryan Mitchell70414f22018-03-26 11:05:31 -0700211 static bool Flatten(BigBuffer* out, const StringPool& pool, bool utf8, IDiagnostics* diag);
Adam Lesinski24aad162015-04-24 19:19:30 -0700212
Adam Lesinskid5083f62017-01-16 15:07:21 -0800213 Ref MakeRefImpl(const android::StringPiece& str, const Context& context, bool unique);
Adam Lesinski5b6ee112017-07-28 17:10:35 -0700214 void ReAssignIndices();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800215
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 std::vector<std::unique_ptr<Entry>> strings_;
217 std::vector<std::unique_ptr<StyleEntry>> styles_;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800218 std::unordered_multimap<android::StringPiece, Entry*> indexed_strings_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219};
220
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223#endif // AAPT_STRING_POOL_H