Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 17 | #include "StringPool.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <string> |
| 22 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | #include "android-base/logging.h" |
| 24 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 25 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | |
| 27 | #include "util/BigBuffer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 | #include "util/Util.h" |
| 29 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 30 | using android::StringPiece; |
| 31 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | StringPool::Ref::Ref() : entry_(nullptr) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 35 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | StringPool::Ref::Ref(const StringPool::Ref& rhs) : entry_(rhs.entry_) { |
| 37 | if (entry_ != nullptr) { |
| 38 | entry_->ref_++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 39 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | StringPool::Ref::Ref(StringPool::Entry* entry) : entry_(entry) { |
| 43 | if (entry_ != nullptr) { |
| 44 | entry_->ref_++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | StringPool::Ref::~Ref() { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | if (entry_ != nullptr) { |
| 50 | entry_->ref_--; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | StringPool::Ref& StringPool::Ref::operator=(const StringPool::Ref& rhs) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | if (rhs.entry_ != nullptr) { |
| 56 | rhs.entry_->ref_++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 57 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | if (entry_ != nullptr) { |
| 60 | entry_->ref_--; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | entry_ = rhs.entry_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 66 | bool StringPool::Ref::operator==(const Ref& rhs) const { |
| 67 | return entry_->value == rhs.entry_->value; |
| 68 | } |
| 69 | |
| 70 | bool StringPool::Ref::operator!=(const Ref& rhs) const { |
| 71 | return entry_->value != rhs.entry_->value; |
| 72 | } |
| 73 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 74 | const std::string* StringPool::Ref::operator->() const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | return &entry_->value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | const std::string& StringPool::Ref::operator*() const { return entry_->value; } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | size_t StringPool::Ref::index() const { return entry_->index; } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 81 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | const StringPool::Context& StringPool::Ref::GetContext() const { |
| 83 | return entry_->context; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | StringPool::StyleRef::StyleRef() : entry_(nullptr) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 87 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | StringPool::StyleRef::StyleRef(const StringPool::StyleRef& rhs) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | : entry_(rhs.entry_) { |
| 90 | if (entry_ != nullptr) { |
| 91 | entry_->ref_++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | StringPool::StyleRef::StyleRef(StringPool::StyleEntry* entry) : entry_(entry) { |
| 96 | if (entry_ != nullptr) { |
| 97 | entry_->ref_++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 98 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | StringPool::StyleRef::~StyleRef() { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | if (entry_ != nullptr) { |
| 103 | entry_->ref_--; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | StringPool::StyleRef& StringPool::StyleRef::operator=( |
| 108 | const StringPool::StyleRef& rhs) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | if (rhs.entry_ != nullptr) { |
| 110 | rhs.entry_->ref_++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 112 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 113 | if (entry_ != nullptr) { |
| 114 | entry_->ref_--; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | entry_ = rhs.entry_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 120 | bool StringPool::StyleRef::operator==(const StyleRef& rhs) const { |
| 121 | if (entry_->str != rhs.entry_->str) { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | if (entry_->spans.size() != rhs.entry_->spans.size()) { |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | auto rhs_iter = rhs.entry_->spans.begin(); |
| 130 | for (const Span& span : entry_->spans) { |
| 131 | const Span& rhs_span = *rhs_iter; |
| 132 | if (span.first_char != rhs_span.first_char || span.last_char != rhs_span.last_char || |
| 133 | span.name != rhs_span.name) { |
| 134 | return false; |
| 135 | } |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | bool StringPool::StyleRef::operator!=(const StyleRef& rhs) const { return !operator==(rhs); } |
| 141 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 142 | const StringPool::StyleEntry* StringPool::StyleRef::operator->() const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 143 | return entry_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | const StringPool::StyleEntry& StringPool::StyleRef::operator*() const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 147 | return *entry_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 150 | size_t StringPool::StyleRef::index() const { return entry_->str.index(); } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 151 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 152 | const StringPool::Context& StringPool::StyleRef::GetContext() const { |
| 153 | return entry_->str.GetContext(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | StringPool::Ref StringPool::MakeRef(const StringPiece& str) { |
| 157 | return MakeRefImpl(str, Context{}, true); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 160 | StringPool::Ref StringPool::MakeRef(const StringPiece& str, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | const Context& context) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 162 | return MakeRefImpl(str, context, true); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | StringPool::Ref StringPool::MakeRefImpl(const StringPiece& str, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | const Context& context, bool unique) { |
| 167 | if (unique) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | auto iter = indexed_strings_.find(str); |
| 169 | if (iter != std::end(indexed_strings_)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 170 | return Ref(iter->second); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 171 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 172 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 173 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 174 | Entry* entry = new Entry(); |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 175 | entry->value = str.to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | entry->context = context; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | entry->index = strings_.size(); |
| 178 | entry->ref_ = 0; |
| 179 | strings_.emplace_back(entry); |
| 180 | indexed_strings_.insert(std::make_pair(StringPiece(entry->value), entry)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | return Ref(entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | StringPool::StyleRef StringPool::MakeRef(const StyleString& str) { |
| 185 | return MakeRef(str, Context{}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 188 | StringPool::StyleRef StringPool::MakeRef(const StyleString& str, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 189 | const Context& context) { |
| 190 | Entry* entry = new Entry(); |
| 191 | entry->value = str.str; |
| 192 | entry->context = context; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | entry->index = strings_.size(); |
| 194 | entry->ref_ = 0; |
| 195 | strings_.emplace_back(entry); |
| 196 | indexed_strings_.insert(std::make_pair(StringPiece(entry->value), entry)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 197 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 198 | StyleEntry* style_entry = new StyleEntry(); |
| 199 | style_entry->str = Ref(entry); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 200 | for (const aapt::Span& span : str.spans) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 201 | style_entry->spans.emplace_back( |
| 202 | Span{MakeRef(span.name), span.first_char, span.last_char}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | style_entry->ref_ = 0; |
| 205 | styles_.emplace_back(style_entry); |
| 206 | return StyleRef(style_entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 209 | StringPool::StyleRef StringPool::MakeRef(const StyleRef& ref) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | Entry* entry = new Entry(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | entry->value = *ref.entry_->str; |
| 212 | entry->context = ref.entry_->str.entry_->context; |
| 213 | entry->index = strings_.size(); |
| 214 | entry->ref_ = 0; |
| 215 | strings_.emplace_back(entry); |
| 216 | indexed_strings_.insert(std::make_pair(StringPiece(entry->value), entry)); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 217 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 218 | StyleEntry* style_entry = new StyleEntry(); |
| 219 | style_entry->str = Ref(entry); |
| 220 | for (const Span& span : ref.entry_->spans) { |
| 221 | style_entry->spans.emplace_back( |
| 222 | Span{MakeRef(*span.name), span.first_char, span.last_char}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 223 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 224 | style_entry->ref_ = 0; |
| 225 | styles_.emplace_back(style_entry); |
| 226 | return StyleRef(style_entry); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 229 | void StringPool::Merge(StringPool&& pool) { |
| 230 | indexed_strings_.insert(pool.indexed_strings_.begin(), |
| 231 | pool.indexed_strings_.end()); |
| 232 | pool.indexed_strings_.clear(); |
| 233 | std::move(pool.strings_.begin(), pool.strings_.end(), |
| 234 | std::back_inserter(strings_)); |
| 235 | pool.strings_.clear(); |
| 236 | std::move(pool.styles_.begin(), pool.styles_.end(), |
| 237 | std::back_inserter(styles_)); |
| 238 | pool.styles_.clear(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 239 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 240 | // Assign the indices. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 241 | const size_t len = strings_.size(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 242 | for (size_t index = 0; index < len; index++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 243 | strings_[index]->index = index; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 244 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | void StringPool::HintWillAdd(size_t stringCount, size_t styleCount) { |
| 248 | strings_.reserve(strings_.size() + stringCount); |
| 249 | styles_.reserve(styles_.size() + styleCount); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 252 | void StringPool::Prune() { |
| 253 | const auto iter_end = indexed_strings_.end(); |
| 254 | auto index_iter = indexed_strings_.begin(); |
| 255 | while (index_iter != iter_end) { |
| 256 | if (index_iter->second->ref_ <= 0) { |
| 257 | index_iter = indexed_strings_.erase(index_iter); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 258 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 259 | ++index_iter; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 260 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 261 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 262 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 263 | auto end_iter2 = |
| 264 | std::remove_if(strings_.begin(), strings_.end(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 265 | [](const std::unique_ptr<Entry>& entry) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | return entry->ref_ <= 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 267 | }); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 268 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 269 | auto end_iter3 = |
| 270 | std::remove_if(styles_.begin(), styles_.end(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 271 | [](const std::unique_ptr<StyleEntry>& entry) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | return entry->ref_ <= 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | }); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 274 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 275 | // Remove the entries at the end or else we'll be accessing |
| 276 | // a deleted string from the StyleEntry. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 277 | strings_.erase(end_iter2, strings_.end()); |
| 278 | styles_.erase(end_iter3, styles_.end()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 279 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 280 | // Reassign the indices. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 281 | const size_t len = strings_.size(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 282 | for (size_t index = 0; index < len; index++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 283 | strings_[index]->index = index; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 284 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 287 | void StringPool::Sort( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 288 | const std::function<bool(const Entry&, const Entry&)>& cmp) { |
| 289 | std::sort( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 | strings_.begin(), strings_.end(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 291 | [&cmp](const std::unique_ptr<Entry>& a, |
| 292 | const std::unique_ptr<Entry>& b) -> bool { return cmp(*a, *b); }); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 293 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 294 | // Assign the indices. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 295 | const size_t len = strings_.size(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 296 | for (size_t index = 0; index < len; index++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 297 | strings_[index]->index = index; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 298 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 299 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 300 | // Reorder the styles. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 301 | std::sort(styles_.begin(), styles_.end(), |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 302 | [](const std::unique_ptr<StyleEntry>& lhs, |
| 303 | const std::unique_ptr<StyleEntry>& rhs) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 304 | return lhs->str.index() < rhs->str.index(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 305 | }); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 308 | template <typename T> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 309 | static T* EncodeLength(T* data, size_t length) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 310 | static_assert(std::is_integral<T>::value, "wat."); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 311 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 312 | constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1); |
| 313 | constexpr size_t kMaxSize = kMask - 1; |
| 314 | if (length > kMaxSize) { |
| 315 | *data++ = kMask | (kMaxSize & (length >> (sizeof(T) * 8))); |
| 316 | } |
| 317 | *data++ = length; |
| 318 | return data; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 321 | template <typename T> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 322 | static size_t EncodedLengthUnits(size_t length) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 323 | static_assert(std::is_integral<T>::value, "wat."); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 324 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 325 | constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1); |
| 326 | constexpr size_t kMaxSize = kMask - 1; |
| 327 | return length > kMaxSize ? 2 : 1; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 330 | bool StringPool::Flatten(BigBuffer* out, const StringPool& pool, bool utf8) { |
| 331 | const size_t start_index = out->size(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 332 | android::ResStringPool_header* header = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | out->NextBlock<android::ResStringPool_header>(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 334 | header->header.type = android::RES_STRING_POOL_TYPE; |
| 335 | header->header.headerSize = sizeof(*header); |
| 336 | header->stringCount = pool.size(); |
| 337 | if (utf8) { |
| 338 | header->flags |= android::ResStringPool_header::UTF8_FLAG; |
| 339 | } |
| 340 | |
| 341 | uint32_t* indices = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 342 | pool.size() != 0 ? out->NextBlock<uint32_t>(pool.size()) : nullptr; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 343 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 344 | uint32_t* style_indices = nullptr; |
| 345 | if (!pool.styles_.empty()) { |
| 346 | header->styleCount = pool.styles_.back()->str.index() + 1; |
| 347 | style_indices = out->NextBlock<uint32_t>(header->styleCount); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 350 | const size_t before_strings_index = out->size(); |
| 351 | header->stringsStart = before_strings_index - start_index; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 352 | |
| 353 | for (const auto& entry : pool) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 354 | *indices = out->size() - before_strings_index; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 355 | indices++; |
| 356 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 357 | if (utf8) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 358 | const std::string& encoded = entry->value; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 359 | const ssize_t utf16_length = utf8_to_utf16_length( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 360 | reinterpret_cast<const uint8_t*>(entry->value.data()), |
| 361 | entry->value.size()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 362 | CHECK(utf16_length >= 0); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 363 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 364 | const size_t total_size = EncodedLengthUnits<char>(utf16_length) + |
| 365 | EncodedLengthUnits<char>(encoded.length()) + |
| 366 | encoded.size() + 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 367 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 368 | char* data = out->NextBlock<char>(total_size); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 369 | |
| 370 | // First encode the UTF16 string length. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 371 | data = EncodeLength(data, utf16_length); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 372 | |
| 373 | // Now encode the size of the real UTF8 string. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 374 | data = EncodeLength(data, encoded.length()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 375 | strncpy(data, encoded.data(), encoded.size()); |
| 376 | |
| 377 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 378 | const std::u16string encoded = util::Utf8ToUtf16(entry->value); |
| 379 | const ssize_t utf16_length = encoded.size(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 380 | |
| 381 | // Total number of 16-bit words to write. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 382 | const size_t total_size = |
| 383 | EncodedLengthUnits<char16_t>(utf16_length) + encoded.size() + 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 384 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 385 | char16_t* data = out->NextBlock<char16_t>(total_size); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 386 | |
| 387 | // Encode the actual UTF16 string length. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 388 | data = EncodeLength(data, utf16_length); |
| 389 | const size_t byte_length = encoded.size() * sizeof(char16_t); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 390 | |
| 391 | // NOTE: For some reason, strncpy16(data, entry->value.data(), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 392 | // entry->value.size()) truncates the string. |
| 393 | memcpy(data, encoded.data(), byte_length); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 394 | |
| 395 | // The null-terminating character is already here due to the block of data |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 396 | // being set to 0s on allocation. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 400 | out->Align4(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 401 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 402 | if (!pool.styles_.empty()) { |
| 403 | const size_t before_styles_index = out->size(); |
| 404 | header->stylesStart = before_styles_index - start_index; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 405 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 406 | size_t current_index = 0; |
| 407 | for (const auto& entry : pool.styles_) { |
| 408 | while (entry->str.index() > current_index) { |
| 409 | style_indices[current_index++] = out->size() - before_styles_index; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 410 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 411 | uint32_t* span_offset = out->NextBlock<uint32_t>(); |
| 412 | *span_offset = android::ResStringPool_span::END; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 413 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 414 | style_indices[current_index++] = out->size() - before_styles_index; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 415 | |
| 416 | android::ResStringPool_span* span = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 417 | out->NextBlock<android::ResStringPool_span>(entry->spans.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 418 | for (const auto& s : entry->spans) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 419 | span->name.index = s.name.index(); |
| 420 | span->firstChar = s.first_char; |
| 421 | span->lastChar = s.last_char; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 422 | span++; |
| 423 | } |
| 424 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 425 | uint32_t* spanEnd = out->NextBlock<uint32_t>(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 426 | *spanEnd = android::ResStringPool_span::END; |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 427 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 428 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 429 | // The error checking code in the platform looks for an entire |
| 430 | // ResStringPool_span structure worth of 0xFFFFFFFF at the end |
| 431 | // of the style block, so fill in the remaining 2 32bit words |
| 432 | // with 0xFFFFFFFF. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 433 | const size_t padding_length = sizeof(android::ResStringPool_span) - |
| 434 | sizeof(android::ResStringPool_span::name); |
| 435 | uint8_t* padding = out->NextBlock<uint8_t>(padding_length); |
| 436 | memset(padding, 0xff, padding_length); |
| 437 | out->Align4(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 438 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 439 | header->header.size = out->size() - start_index; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 440 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 443 | bool StringPool::FlattenUtf8(BigBuffer* out, const StringPool& pool) { |
| 444 | return Flatten(out, pool, true); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 447 | bool StringPool::FlattenUtf16(BigBuffer* out, const StringPool& pool) { |
| 448 | return Flatten(out, pool, false); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 451 | } // namespace aapt |