AAPT2: Rename to match new style

Use Google3 naming style to match new
projects' and open source google projects' style.

Preferred to do this in a massive CL so as to avoid
style inconsistencies that plague legacy code bases.
This is a relatively NEW code base, may as well keep
it up to date.

Test: name/style refactor - existing tests pass
Change-Id: Ie80ecb78d46ec53efdfca2336bb57d96cbb7fb87
This commit is contained in:
Adam Lesinski
2016-10-21 17:56:45 -07:00
parent 0f7cc4dc2c
commit ce5e56e243
181 changed files with 13853 additions and 13329 deletions

View File

@@ -15,263 +15,266 @@
*/
#include "StringPool.h"
#include "util/BigBuffer.h"
#include "util/StringPiece.h"
#include "util/Util.h"
#include <androidfw/ResourceTypes.h>
#include <algorithm>
#include <memory>
#include <string>
#include "android-base/logging.h"
#include "androidfw/ResourceTypes.h"
#include "util/BigBuffer.h"
#include "util/StringPiece.h"
#include "util/Util.h"
namespace aapt {
StringPool::Ref::Ref() : mEntry(nullptr) {}
StringPool::Ref::Ref() : entry_(nullptr) {}
StringPool::Ref::Ref(const StringPool::Ref& rhs) : mEntry(rhs.mEntry) {
if (mEntry != nullptr) {
mEntry->ref++;
StringPool::Ref::Ref(const StringPool::Ref& rhs) : entry_(rhs.entry_) {
if (entry_ != nullptr) {
entry_->ref_++;
}
}
StringPool::Ref::Ref(StringPool::Entry* entry) : mEntry(entry) {
if (mEntry != nullptr) {
mEntry->ref++;
StringPool::Ref::Ref(StringPool::Entry* entry) : entry_(entry) {
if (entry_ != nullptr) {
entry_->ref_++;
}
}
StringPool::Ref::~Ref() {
if (mEntry != nullptr) {
mEntry->ref--;
if (entry_ != nullptr) {
entry_->ref_--;
}
}
StringPool::Ref& StringPool::Ref::operator=(const StringPool::Ref& rhs) {
if (rhs.mEntry != nullptr) {
rhs.mEntry->ref++;
if (rhs.entry_ != nullptr) {
rhs.entry_->ref_++;
}
if (mEntry != nullptr) {
mEntry->ref--;
if (entry_ != nullptr) {
entry_->ref_--;
}
mEntry = rhs.mEntry;
entry_ = rhs.entry_;
return *this;
}
const std::string* StringPool::Ref::operator->() const {
return &mEntry->value;
return &entry_->value;
}
const std::string& StringPool::Ref::operator*() const { return mEntry->value; }
const std::string& StringPool::Ref::operator*() const { return entry_->value; }
size_t StringPool::Ref::getIndex() const { return mEntry->index; }
size_t StringPool::Ref::index() const { return entry_->index; }
const StringPool::Context& StringPool::Ref::getContext() const {
return mEntry->context;
const StringPool::Context& StringPool::Ref::GetContext() const {
return entry_->context;
}
StringPool::StyleRef::StyleRef() : mEntry(nullptr) {}
StringPool::StyleRef::StyleRef() : entry_(nullptr) {}
StringPool::StyleRef::StyleRef(const StringPool::StyleRef& rhs)
: mEntry(rhs.mEntry) {
if (mEntry != nullptr) {
mEntry->ref++;
: entry_(rhs.entry_) {
if (entry_ != nullptr) {
entry_->ref_++;
}
}
StringPool::StyleRef::StyleRef(StringPool::StyleEntry* entry) : mEntry(entry) {
if (mEntry != nullptr) {
mEntry->ref++;
StringPool::StyleRef::StyleRef(StringPool::StyleEntry* entry) : entry_(entry) {
if (entry_ != nullptr) {
entry_->ref_++;
}
}
StringPool::StyleRef::~StyleRef() {
if (mEntry != nullptr) {
mEntry->ref--;
if (entry_ != nullptr) {
entry_->ref_--;
}
}
StringPool::StyleRef& StringPool::StyleRef::operator=(
const StringPool::StyleRef& rhs) {
if (rhs.mEntry != nullptr) {
rhs.mEntry->ref++;
if (rhs.entry_ != nullptr) {
rhs.entry_->ref_++;
}
if (mEntry != nullptr) {
mEntry->ref--;
if (entry_ != nullptr) {
entry_->ref_--;
}
mEntry = rhs.mEntry;
entry_ = rhs.entry_;
return *this;
}
const StringPool::StyleEntry* StringPool::StyleRef::operator->() const {
return mEntry;
return entry_;
}
const StringPool::StyleEntry& StringPool::StyleRef::operator*() const {
return *mEntry;
return *entry_;
}
size_t StringPool::StyleRef::getIndex() const { return mEntry->str.getIndex(); }
size_t StringPool::StyleRef::index() const { return entry_->str.index(); }
const StringPool::Context& StringPool::StyleRef::getContext() const {
return mEntry->str.getContext();
const StringPool::Context& StringPool::StyleRef::GetContext() const {
return entry_->str.GetContext();
}
StringPool::Ref StringPool::makeRef(const StringPiece& str) {
return makeRefImpl(str, Context{}, true);
StringPool::Ref StringPool::MakeRef(const StringPiece& str) {
return MakeRefImpl(str, Context{}, true);
}
StringPool::Ref StringPool::makeRef(const StringPiece& str,
StringPool::Ref StringPool::MakeRef(const StringPiece& str,
const Context& context) {
return makeRefImpl(str, context, true);
return MakeRefImpl(str, context, true);
}
StringPool::Ref StringPool::makeRefImpl(const StringPiece& str,
StringPool::Ref StringPool::MakeRefImpl(const StringPiece& str,
const Context& context, bool unique) {
if (unique) {
auto iter = mIndexedStrings.find(str);
if (iter != std::end(mIndexedStrings)) {
auto iter = indexed_strings_.find(str);
if (iter != std::end(indexed_strings_)) {
return Ref(iter->second);
}
}
Entry* entry = new Entry();
entry->value = str.toString();
entry->value = str.ToString();
entry->context = context;
entry->index = mStrings.size();
entry->ref = 0;
mStrings.emplace_back(entry);
mIndexedStrings.insert(std::make_pair(StringPiece(entry->value), entry));
entry->index = strings_.size();
entry->ref_ = 0;
strings_.emplace_back(entry);
indexed_strings_.insert(std::make_pair(StringPiece(entry->value), entry));
return Ref(entry);
}
StringPool::StyleRef StringPool::makeRef(const StyleString& str) {
return makeRef(str, Context{});
StringPool::StyleRef StringPool::MakeRef(const StyleString& str) {
return MakeRef(str, Context{});
}
StringPool::StyleRef StringPool::makeRef(const StyleString& str,
StringPool::StyleRef StringPool::MakeRef(const StyleString& str,
const Context& context) {
Entry* entry = new Entry();
entry->value = str.str;
entry->context = context;
entry->index = mStrings.size();
entry->ref = 0;
mStrings.emplace_back(entry);
mIndexedStrings.insert(std::make_pair(StringPiece(entry->value), entry));
entry->index = strings_.size();
entry->ref_ = 0;
strings_.emplace_back(entry);
indexed_strings_.insert(std::make_pair(StringPiece(entry->value), entry));
StyleEntry* styleEntry = new StyleEntry();
styleEntry->str = Ref(entry);
StyleEntry* style_entry = new StyleEntry();
style_entry->str = Ref(entry);
for (const aapt::Span& span : str.spans) {
styleEntry->spans.emplace_back(
Span{makeRef(span.name), span.firstChar, span.lastChar});
style_entry->spans.emplace_back(
Span{MakeRef(span.name), span.first_char, span.last_char});
}
styleEntry->ref = 0;
mStyles.emplace_back(styleEntry);
return StyleRef(styleEntry);
style_entry->ref_ = 0;
styles_.emplace_back(style_entry);
return StyleRef(style_entry);
}
StringPool::StyleRef StringPool::makeRef(const StyleRef& ref) {
StringPool::StyleRef StringPool::MakeRef(const StyleRef& ref) {
Entry* entry = new Entry();
entry->value = *ref.mEntry->str;
entry->context = ref.mEntry->str.mEntry->context;
entry->index = mStrings.size();
entry->ref = 0;
mStrings.emplace_back(entry);
mIndexedStrings.insert(std::make_pair(StringPiece(entry->value), entry));
entry->value = *ref.entry_->str;
entry->context = ref.entry_->str.entry_->context;
entry->index = strings_.size();
entry->ref_ = 0;
strings_.emplace_back(entry);
indexed_strings_.insert(std::make_pair(StringPiece(entry->value), entry));
StyleEntry* styleEntry = new StyleEntry();
styleEntry->str = Ref(entry);
for (const Span& span : ref.mEntry->spans) {
styleEntry->spans.emplace_back(
Span{makeRef(*span.name), span.firstChar, span.lastChar});
StyleEntry* style_entry = new StyleEntry();
style_entry->str = Ref(entry);
for (const Span& span : ref.entry_->spans) {
style_entry->spans.emplace_back(
Span{MakeRef(*span.name), span.first_char, span.last_char});
}
styleEntry->ref = 0;
mStyles.emplace_back(styleEntry);
return StyleRef(styleEntry);
style_entry->ref_ = 0;
styles_.emplace_back(style_entry);
return StyleRef(style_entry);
}
void StringPool::merge(StringPool&& pool) {
mIndexedStrings.insert(pool.mIndexedStrings.begin(),
pool.mIndexedStrings.end());
pool.mIndexedStrings.clear();
std::move(pool.mStrings.begin(), pool.mStrings.end(),
std::back_inserter(mStrings));
pool.mStrings.clear();
std::move(pool.mStyles.begin(), pool.mStyles.end(),
std::back_inserter(mStyles));
pool.mStyles.clear();
void StringPool::Merge(StringPool&& pool) {
indexed_strings_.insert(pool.indexed_strings_.begin(),
pool.indexed_strings_.end());
pool.indexed_strings_.clear();
std::move(pool.strings_.begin(), pool.strings_.end(),
std::back_inserter(strings_));
pool.strings_.clear();
std::move(pool.styles_.begin(), pool.styles_.end(),
std::back_inserter(styles_));
pool.styles_.clear();
// Assign the indices.
const size_t len = mStrings.size();
const size_t len = strings_.size();
for (size_t index = 0; index < len; index++) {
mStrings[index]->index = index;
strings_[index]->index = index;
}
}
void StringPool::hintWillAdd(size_t stringCount, size_t styleCount) {
mStrings.reserve(mStrings.size() + stringCount);
mStyles.reserve(mStyles.size() + styleCount);
void StringPool::HintWillAdd(size_t stringCount, size_t styleCount) {
strings_.reserve(strings_.size() + stringCount);
styles_.reserve(styles_.size() + styleCount);
}
void StringPool::prune() {
const auto iterEnd = std::end(mIndexedStrings);
auto indexIter = std::begin(mIndexedStrings);
while (indexIter != iterEnd) {
if (indexIter->second->ref <= 0) {
indexIter = mIndexedStrings.erase(indexIter);
void StringPool::Prune() {
const auto iter_end = indexed_strings_.end();
auto index_iter = indexed_strings_.begin();
while (index_iter != iter_end) {
if (index_iter->second->ref_ <= 0) {
index_iter = indexed_strings_.erase(index_iter);
} else {
++indexIter;
++index_iter;
}
}
auto endIter2 =
std::remove_if(std::begin(mStrings), std::end(mStrings),
auto end_iter2 =
std::remove_if(strings_.begin(), strings_.end(),
[](const std::unique_ptr<Entry>& entry) -> bool {
return entry->ref <= 0;
return entry->ref_ <= 0;
});
auto endIter3 =
std::remove_if(std::begin(mStyles), std::end(mStyles),
auto end_iter3 =
std::remove_if(styles_.begin(), styles_.end(),
[](const std::unique_ptr<StyleEntry>& entry) -> bool {
return entry->ref <= 0;
return entry->ref_ <= 0;
});
// Remove the entries at the end or else we'll be accessing
// a deleted string from the StyleEntry.
mStrings.erase(endIter2, std::end(mStrings));
mStyles.erase(endIter3, std::end(mStyles));
strings_.erase(end_iter2, strings_.end());
styles_.erase(end_iter3, styles_.end());
// Reassign the indices.
const size_t len = mStrings.size();
const size_t len = strings_.size();
for (size_t index = 0; index < len; index++) {
mStrings[index]->index = index;
strings_[index]->index = index;
}
}
void StringPool::sort(
void StringPool::Sort(
const std::function<bool(const Entry&, const Entry&)>& cmp) {
std::sort(
std::begin(mStrings), std::end(mStrings),
strings_.begin(), strings_.end(),
[&cmp](const std::unique_ptr<Entry>& a,
const std::unique_ptr<Entry>& b) -> bool { return cmp(*a, *b); });
// Assign the indices.
const size_t len = mStrings.size();
const size_t len = strings_.size();
for (size_t index = 0; index < len; index++) {
mStrings[index]->index = index;
strings_[index]->index = index;
}
// Reorder the styles.
std::sort(std::begin(mStyles), std::end(mStyles),
std::sort(styles_.begin(), styles_.end(),
[](const std::unique_ptr<StyleEntry>& lhs,
const std::unique_ptr<StyleEntry>& rhs) -> bool {
return lhs->str.getIndex() < rhs->str.getIndex();
return lhs->str.index() < rhs->str.index();
});
}
template <typename T>
static T* encodeLength(T* data, size_t length) {
static T* EncodeLength(T* data, size_t length) {
static_assert(std::is_integral<T>::value, "wat.");
constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1);
@@ -284,7 +287,7 @@ static T* encodeLength(T* data, size_t length) {
}
template <typename T>
static size_t encodedLengthUnits(size_t length) {
static size_t EncodedLengthUnits(size_t length) {
static_assert(std::is_integral<T>::value, "wat.");
constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1);
@@ -292,10 +295,10 @@ static size_t encodedLengthUnits(size_t length) {
return length > kMaxSize ? 2 : 1;
}
bool StringPool::flatten(BigBuffer* out, const StringPool& pool, bool utf8) {
const size_t startIndex = out->size();
bool StringPool::Flatten(BigBuffer* out, const StringPool& pool, bool utf8) {
const size_t start_index = out->size();
android::ResStringPool_header* header =
out->nextBlock<android::ResStringPool_header>();
out->NextBlock<android::ResStringPool_header>();
header->header.type = android::RES_STRING_POOL_TYPE;
header->header.headerSize = sizeof(*header);
header->stringCount = pool.size();
@@ -304,92 +307,90 @@ bool StringPool::flatten(BigBuffer* out, const StringPool& pool, bool utf8) {
}
uint32_t* indices =
pool.size() != 0 ? out->nextBlock<uint32_t>(pool.size()) : nullptr;
pool.size() != 0 ? out->NextBlock<uint32_t>(pool.size()) : nullptr;
uint32_t* styleIndices = nullptr;
if (!pool.mStyles.empty()) {
header->styleCount = pool.mStyles.back()->str.getIndex() + 1;
styleIndices = out->nextBlock<uint32_t>(header->styleCount);
uint32_t* style_indices = nullptr;
if (!pool.styles_.empty()) {
header->styleCount = pool.styles_.back()->str.index() + 1;
style_indices = out->NextBlock<uint32_t>(header->styleCount);
}
const size_t beforeStringsIndex = out->size();
header->stringsStart = beforeStringsIndex - startIndex;
const size_t before_strings_index = out->size();
header->stringsStart = before_strings_index - start_index;
for (const auto& entry : pool) {
*indices = out->size() - beforeStringsIndex;
*indices = out->size() - before_strings_index;
indices++;
if (utf8) {
const std::string& encoded = entry->value;
const ssize_t utf16Length = utf8_to_utf16_length(
const ssize_t utf16_length = utf8_to_utf16_length(
reinterpret_cast<const uint8_t*>(entry->value.data()),
entry->value.size());
assert(utf16Length >= 0);
CHECK(utf16_length >= 0);
const size_t totalSize = encodedLengthUnits<char>(utf16Length) +
encodedLengthUnits<char>(encoded.length()) +
encoded.size() + 1;
const size_t total_size = EncodedLengthUnits<char>(utf16_length) +
EncodedLengthUnits<char>(encoded.length()) +
encoded.size() + 1;
char* data = out->nextBlock<char>(totalSize);
char* data = out->NextBlock<char>(total_size);
// First encode the UTF16 string length.
data = encodeLength(data, utf16Length);
data = EncodeLength(data, utf16_length);
// Now encode the size of the real UTF8 string.
data = encodeLength(data, encoded.length());
data = EncodeLength(data, encoded.length());
strncpy(data, encoded.data(), encoded.size());
} else {
const std::u16string encoded = util::utf8ToUtf16(entry->value);
const ssize_t utf16Length = encoded.size();
const std::u16string encoded = util::Utf8ToUtf16(entry->value);
const ssize_t utf16_length = encoded.size();
// Total number of 16-bit words to write.
const size_t totalSize =
encodedLengthUnits<char16_t>(utf16Length) + encoded.size() + 1;
const size_t total_size =
EncodedLengthUnits<char16_t>(utf16_length) + encoded.size() + 1;
char16_t* data = out->nextBlock<char16_t>(totalSize);
char16_t* data = out->NextBlock<char16_t>(total_size);
// Encode the actual UTF16 string length.
data = encodeLength(data, utf16Length);
const size_t byteLength = encoded.size() * sizeof(char16_t);
data = EncodeLength(data, utf16_length);
const size_t byte_length = encoded.size() * sizeof(char16_t);
// NOTE: For some reason, strncpy16(data, entry->value.data(),
// entry->value.size())
// truncates the string.
memcpy(data, encoded.data(), byteLength);
// entry->value.size()) truncates the string.
memcpy(data, encoded.data(), byte_length);
// The null-terminating character is already here due to the block of data
// being set
// to 0s on allocation.
// being set to 0s on allocation.
}
}
out->align4();
out->Align4();
if (!pool.mStyles.empty()) {
const size_t beforeStylesIndex = out->size();
header->stylesStart = beforeStylesIndex - startIndex;
if (!pool.styles_.empty()) {
const size_t before_styles_index = out->size();
header->stylesStart = before_styles_index - start_index;
size_t currentIndex = 0;
for (const auto& entry : pool.mStyles) {
while (entry->str.getIndex() > currentIndex) {
styleIndices[currentIndex++] = out->size() - beforeStylesIndex;
size_t current_index = 0;
for (const auto& entry : pool.styles_) {
while (entry->str.index() > current_index) {
style_indices[current_index++] = out->size() - before_styles_index;
uint32_t* spanOffset = out->nextBlock<uint32_t>();
*spanOffset = android::ResStringPool_span::END;
uint32_t* span_offset = out->NextBlock<uint32_t>();
*span_offset = android::ResStringPool_span::END;
}
styleIndices[currentIndex++] = out->size() - beforeStylesIndex;
style_indices[current_index++] = out->size() - before_styles_index;
android::ResStringPool_span* span =
out->nextBlock<android::ResStringPool_span>(entry->spans.size());
out->NextBlock<android::ResStringPool_span>(entry->spans.size());
for (const auto& s : entry->spans) {
span->name.index = s.name.getIndex();
span->firstChar = s.firstChar;
span->lastChar = s.lastChar;
span->name.index = s.name.index();
span->firstChar = s.first_char;
span->lastChar = s.last_char;
span++;
}
uint32_t* spanEnd = out->nextBlock<uint32_t>();
uint32_t* spanEnd = out->NextBlock<uint32_t>();
*spanEnd = android::ResStringPool_span::END;
}
@@ -397,22 +398,22 @@ bool StringPool::flatten(BigBuffer* out, const StringPool& pool, bool utf8) {
// ResStringPool_span structure worth of 0xFFFFFFFF at the end
// of the style block, so fill in the remaining 2 32bit words
// with 0xFFFFFFFF.
const size_t paddingLength = sizeof(android::ResStringPool_span) -
sizeof(android::ResStringPool_span::name);
uint8_t* padding = out->nextBlock<uint8_t>(paddingLength);
memset(padding, 0xff, paddingLength);
out->align4();
const size_t padding_length = sizeof(android::ResStringPool_span) -
sizeof(android::ResStringPool_span::name);
uint8_t* padding = out->NextBlock<uint8_t>(padding_length);
memset(padding, 0xff, padding_length);
out->Align4();
}
header->header.size = out->size() - startIndex;
header->header.size = out->size() - start_index;
return true;
}
bool StringPool::flattenUtf8(BigBuffer* out, const StringPool& pool) {
return flatten(out, pool, true);
bool StringPool::FlattenUtf8(BigBuffer* out, const StringPool& pool) {
return Flatten(out, pool, true);
}
bool StringPool::flattenUtf16(BigBuffer* out, const StringPool& pool) {
return flatten(out, pool, false);
bool StringPool::FlattenUtf16(BigBuffer* out, const StringPool& pool) {
return Flatten(out, pool, false);
}
} // namespace aapt