| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef AAPT_STRING_POOL_H |
| 18 |
#define AAPT_STRING_POOL_H |
| 19 |
|
| 20 |
#include <functional> |
| 21 |
#include <memory> |
| 22 |
#include <string> |
| 23 |
#include <unordered_map> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
#include "android-base/macros.h" |
| 27 |
#include "androidfw/ConfigDescription.h" |
| 28 |
#include "androidfw/StringPiece.h" |
| 29 |
|
| 30 |
#include "Diagnostics.h" |
| 31 |
#include "util/BigBuffer.h" |
| 32 |
|
| 33 |
namespace aapt { |
| 34 |
|
| 35 |
struct Span { |
| 36 |
std::string name; |
| 37 |
uint32_t first_char; |
| 38 |
uint32_t last_char; |
| 39 |
|
| 40 |
bool operator==(const Span& right) const { |
| 41 |
return name == right.name && first_char == right.first_char && last_char == right.last_char; |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
struct StyleString { |
| 46 |
std::string str; |
| 47 |
std::vector<Span> spans; |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
class StringPool { |
| 56 |
public: |
| 57 |
using size_type = size_t; |
| 58 |
|
| 59 |
class Context { |
| 60 |
public: |
| 61 |
enum : uint32_t { |
| 62 |
kHighPriority = 1u, |
| 63 |
kNormalPriority = 0x7fffffffu, |
| 64 |
kLowPriority = 0xffffffffu, |
| 65 |
}; |
| 66 |
uint32_t priority = kNormalPriority; |
| 67 |
android::ConfigDescription config; |
| 68 |
|
| 69 |
Context() = default; |
| 70 |
Context(uint32_t p, const android::ConfigDescription& c) : priority(p), config(c) {} |
| 71 |
explicit Context(uint32_t p) : priority(p) {} |
| 72 |
explicit Context(const android::ConfigDescription& c) : priority(kNormalPriority), config(c) { |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
class Entry; |
| 77 |
|
| 78 |
class Ref { |
| 79 |
public: |
| 80 |
Ref(); |
| 81 |
Ref(const Ref&); |
| 82 |
~Ref(); |
| 83 |
|
| 84 |
Ref& operator=(const Ref& rhs); |
| 85 |
bool operator==(const Ref& rhs) const; |
| 86 |
bool operator!=(const Ref& rhs) const; |
| 87 |
const std::string* operator->() const; |
| 88 |
const std::string& operator*() const; |
| 89 |
|
| 90 |
size_t index() const; |
| 91 |
const Context& GetContext() const; |
| 92 |
|
| 93 |
private: |
| 94 |
friend class StringPool; |
| 95 |
|
| 96 |
explicit Ref(Entry* entry); |
| 97 |
|
| 98 |
Entry* entry_; |
| 99 |
}; |
| 100 |
|
| 101 |
class StyleEntry; |
| 102 |
|
| 103 |
class StyleRef { |
| 104 |
public: |
| 105 |
StyleRef(); |
| 106 |
StyleRef(const StyleRef&); |
| 107 |
~StyleRef(); |
| 108 |
|
| 109 |
StyleRef& operator=(const StyleRef& rhs); |
| 110 |
bool operator==(const StyleRef& rhs) const; |
| 111 |
bool operator!=(const StyleRef& rhs) const; |
| 112 |
const StyleEntry* operator->() const; |
| 113 |
const StyleEntry& operator*() const; |
| 114 |
|
| 115 |
size_t index() const; |
| 116 |
const Context& GetContext() const; |
| 117 |
|
| 118 |
private: |
| 119 |
friend class StringPool; |
| 120 |
|
| 121 |
explicit StyleRef(StyleEntry* entry); |
| 122 |
|
| 123 |
StyleEntry* entry_; |
| 124 |
}; |
| 125 |
|
| 126 |
class Entry { |
| 127 |
public: |
| 128 |
std::string value; |
| 129 |
Context context; |
| 130 |
|
| 131 |
private: |
| 132 |
friend class StringPool; |
| 133 |
friend class Ref; |
| 134 |
|
| 135 |
size_t index_; |
| 136 |
int ref_; |
| 137 |
const StringPool* pool_; |
| 138 |
}; |
| 139 |
|
| 140 |
struct Span { |
| 141 |
Ref name; |
| 142 |
uint32_t first_char; |
| 143 |
uint32_t last_char; |
| 144 |
}; |
| 145 |
|
| 146 |
class StyleEntry { |
| 147 |
public: |
| 148 |
std::string value; |
| 149 |
Context context; |
| 150 |
std::vector<Span> spans; |
| 151 |
|
| 152 |
private: |
| 153 |
friend class StringPool; |
| 154 |
friend class StyleRef; |
| 155 |
|
| 156 |
size_t index_; |
| 157 |
int ref_; |
| 158 |
}; |
| 159 |
|
| 160 |
static bool FlattenUtf8(BigBuffer* out, const StringPool& pool, IDiagnostics* diag); |
| 161 |
static bool FlattenUtf16(BigBuffer* out, const StringPool& pool, IDiagnostics* diag); |
| 162 |
|
| 163 |
StringPool() = default; |
| 164 |
StringPool(StringPool&&) = default; |
| 165 |
StringPool& operator=(StringPool&&) = default; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
Ref MakeRef(const android::StringPiece& str); |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
Ref MakeRef(const android::StringPiece& str, const Context& context); |
| 174 |
|
| 175 |
|
| 176 |
Ref MakeRef(const Ref& ref); |
| 177 |
|
| 178 |
|
| 179 |
StyleRef MakeRef(const StyleString& str); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
StyleRef MakeRef(const StyleString& str, const Context& context); |
| 184 |
|
| 185 |
|
| 186 |
StyleRef MakeRef(const StyleRef& ref); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
void Merge(StringPool&& pool); |
| 191 |
|
| 192 |
inline const std::vector<std::unique_ptr<Entry>>& strings() const { |
| 193 |
return strings_; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
inline size_t size() const { |
| 198 |
return styles_.size() + strings_.size(); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
void HintWillAdd(size_t string_count, size_t style_count); |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
void Sort(const std::function<int(const Context&, const Context&)>& cmp = nullptr); |
| 208 |
|
| 209 |
|
| 210 |
void Prune(); |
| 211 |
|
| 212 |
private: |
| 213 |
DISALLOW_COPY_AND_ASSIGN(StringPool); |
| 214 |
|
| 215 |
static bool Flatten(BigBuffer* out, const StringPool& pool, bool utf8, IDiagnostics* diag); |
| 216 |
|
| 217 |
Ref MakeRefImpl(const android::StringPiece& str, const Context& context, bool unique); |
| 218 |
void ReAssignIndices(); |
| 219 |
|
| 220 |
std::vector<std::unique_ptr<Entry>> strings_; |
| 221 |
std::vector<std::unique_ptr<StyleEntry>> styles_; |
| 222 |
std::unordered_multimap<android::StringPiece, Entry*> indexed_strings_; |
| 223 |
}; |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
#endif |