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