2014-11-14 14:48:12 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
#include "ResourceValues.h"
|
2015-10-22 12:48:43 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
#include <algorithm>
|
2014-11-14 14:48:12 -08:00
|
|
|
#include <limits>
|
2016-08-19 12:16:49 -07:00
|
|
|
#include <set>
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
#include "androidfw/ResourceTypes.h"
|
|
|
|
|
|
|
|
#include "Resource.h"
|
|
|
|
#include "ResourceUtils.h"
|
|
|
|
#include "ValueVisitor.h"
|
|
|
|
#include "util/Util.h"
|
|
|
|
|
2014-11-14 14:48:12 -08:00
|
|
|
namespace aapt {
|
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
template <typename Derived>
|
2016-10-21 17:56:45 -07:00
|
|
|
void BaseValue<Derived>::Accept(RawValueVisitor* visitor) {
|
|
|
|
visitor->Visit(static_cast<Derived*>(this));
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Derived>
|
2016-10-21 17:56:45 -07:00
|
|
|
void BaseItem<Derived>::Accept(RawValueVisitor* visitor) {
|
|
|
|
visitor->Visit(static_cast<Derived*>(this));
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
RawString::RawString(const StringPool::Ref& ref) : value(ref) {}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool RawString::Equals(const Value* value) const {
|
|
|
|
const RawString* other = ValueCast<RawString>(value);
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!other) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return *this->value == *other->value;
|
2016-04-25 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
RawString* RawString::Clone(StringPool* new_pool) const {
|
|
|
|
RawString* rs = new RawString(new_pool->MakeRef(*value));
|
|
|
|
rs->comment_ = comment_;
|
|
|
|
rs->source_ = source_;
|
2016-10-19 12:18:14 -07:00
|
|
|
return rs;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool RawString::Flatten(android::Res_value* out_value) const {
|
|
|
|
out_value->dataType = android::Res_value::TYPE_STRING;
|
|
|
|
out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
|
2016-10-19 12:18:14 -07:00
|
|
|
return true;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void RawString::Print(std::ostream* out) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "(raw string) " << *value;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
Reference::Reference() : reference_type(Type::kResource) {}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
Reference::Reference(const ResourceNameRef& n, Type t)
|
2016-10-21 17:56:45 -07:00
|
|
|
: name(n.ToResourceName()), reference_type(t) {}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
Reference::Reference(const ResourceId& i, Type type)
|
2016-10-21 17:56:45 -07:00
|
|
|
: id(i), reference_type(type) {}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
Reference::Reference(const ResourceNameRef& n, const ResourceId& i)
|
2016-10-21 17:56:45 -07:00
|
|
|
: name(n.ToResourceName()), id(i), reference_type(Type::kResource) {}
|
2016-08-24 16:03:48 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool Reference::Equals(const Value* value) const {
|
|
|
|
const Reference* other = ValueCast<Reference>(value);
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!other) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-21 17:56:45 -07:00
|
|
|
return reference_type == other->reference_type &&
|
|
|
|
private_reference == other->private_reference && id == other->id &&
|
2016-10-19 12:18:14 -07:00
|
|
|
name == other->name;
|
2016-04-25 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool Reference::Flatten(android::Res_value* out_value) const {
|
2017-02-22 19:29:29 -08:00
|
|
|
const ResourceId resid = id.value_or_default(ResourceId(0));
|
|
|
|
const bool dynamic =
|
|
|
|
(resid.package_id() != kFrameworkPackageId && resid.package_id() != kAppPackageId);
|
|
|
|
|
|
|
|
if (reference_type == Reference::Type::kResource) {
|
|
|
|
if (dynamic) {
|
|
|
|
out_value->dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE;
|
|
|
|
} else {
|
|
|
|
out_value->dataType = android::Res_value::TYPE_REFERENCE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (dynamic) {
|
|
|
|
out_value->dataType = android::Res_value::TYPE_DYNAMIC_ATTRIBUTE;
|
|
|
|
} else {
|
|
|
|
out_value->dataType = android::Res_value::TYPE_ATTRIBUTE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out_value->data = util::HostToDevice32(resid.id);
|
2016-10-19 12:18:14 -07:00
|
|
|
return true;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
Reference* Reference::Clone(StringPool* /*new_pool*/) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
return new Reference(*this);
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Reference::Print(std::ostream* out) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "(reference) ";
|
2016-10-21 17:56:45 -07:00
|
|
|
if (reference_type == Reference::Type::kResource) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "@";
|
2016-10-21 17:56:45 -07:00
|
|
|
if (private_reference) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "*";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
} else {
|
|
|
|
*out << "?";
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (name) {
|
|
|
|
*out << name.value();
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (id && !Res_INTERNALID(id.value().id)) {
|
|
|
|
*out << " " << id.value();
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool Id::Equals(const Value* value) const {
|
|
|
|
return ValueCast<Id>(value) != nullptr;
|
2016-04-25 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool Id::Flatten(android::Res_value* out) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
|
2016-10-21 17:56:45 -07:00
|
|
|
out->data = util::HostToDevice32(0);
|
2016-10-19 12:18:14 -07:00
|
|
|
return true;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
Id* Id::Clone(StringPool* /*new_pool*/) const { return new Id(*this); }
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Id::Print(std::ostream* out) const { *out << "(id)"; }
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
String::String(const StringPool::Ref& ref) : value(ref) {}
|
2015-12-17 13:03:11 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool String::Equals(const Value* value) const {
|
|
|
|
const String* other = ValueCast<String>(value);
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!other) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-06 15:20:04 -08:00
|
|
|
|
|
|
|
if (this->value != other->value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto other_iter = other->untranslatable_sections.begin();
|
|
|
|
for (const UntranslatableSection& this_section : untranslatable_sections) {
|
|
|
|
if (this_section != *other_iter) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++other_iter;
|
|
|
|
}
|
|
|
|
return true;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool String::Flatten(android::Res_value* out_value) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
// Verify that our StringPool index is within encode-able limits.
|
2016-10-21 17:56:45 -07:00
|
|
|
if (value.index() > std::numeric_limits<uint32_t>::max()) {
|
2016-10-19 12:18:14 -07:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
out_value->dataType = android::Res_value::TYPE_STRING;
|
|
|
|
out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
|
2016-10-19 12:18:14 -07:00
|
|
|
return true;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
String* String::Clone(StringPool* new_pool) const {
|
|
|
|
String* str = new String(new_pool->MakeRef(*value));
|
|
|
|
str->comment_ = comment_;
|
|
|
|
str->source_ = source_;
|
2017-01-06 15:20:04 -08:00
|
|
|
str->untranslatable_sections = untranslatable_sections;
|
2016-10-19 12:18:14 -07:00
|
|
|
return str;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void String::Print(std::ostream* out) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "(string) \"" << *value << "\"";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {}
|
2015-12-17 13:03:11 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool StyledString::Equals(const Value* value) const {
|
|
|
|
const StyledString* other = ValueCast<StyledString>(value);
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!other) {
|
2016-04-25 14:20:21 -07:00
|
|
|
return false;
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
|
|
|
|
2017-01-06 15:20:04 -08:00
|
|
|
if (this->value != other->value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (untranslatable_sections.size() != other->untranslatable_sections.size()) {
|
|
|
|
return false;
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
2017-01-06 15:20:04 -08:00
|
|
|
|
|
|
|
auto other_iter = other->untranslatable_sections.begin();
|
|
|
|
for (const UntranslatableSection& this_section : untranslatable_sections) {
|
|
|
|
if (this_section != *other_iter) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++other_iter;
|
|
|
|
}
|
|
|
|
return true;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool StyledString::Flatten(android::Res_value* out_value) const {
|
|
|
|
if (value.index() > std::numeric_limits<uint32_t>::max()) {
|
2016-10-19 12:18:14 -07:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
out_value->dataType = android::Res_value::TYPE_STRING;
|
|
|
|
out_value->data = util::HostToDevice32(static_cast<uint32_t>(value.index()));
|
2016-10-19 12:18:14 -07:00
|
|
|
return true;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
StyledString* StyledString::Clone(StringPool* new_pool) const {
|
|
|
|
StyledString* str = new StyledString(new_pool->MakeRef(value));
|
|
|
|
str->comment_ = comment_;
|
|
|
|
str->source_ = source_;
|
2017-01-06 15:20:04 -08:00
|
|
|
str->untranslatable_sections = untranslatable_sections;
|
2016-10-19 12:18:14 -07:00
|
|
|
return str;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void StyledString::Print(std::ostream* out) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "(styled string) \"" << *value->str << "\"";
|
|
|
|
for (const StringPool::Span& span : value->spans) {
|
2016-10-21 17:56:45 -07:00
|
|
|
*out << " " << *span.name << ":" << span.first_char << ","
|
|
|
|
<< span.last_char;
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool FileReference::Equals(const Value* value) const {
|
|
|
|
const FileReference* other = ValueCast<FileReference>(value);
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!other) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-06 15:20:04 -08:00
|
|
|
return path == other->path;
|
2016-04-25 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool FileReference::Flatten(android::Res_value* out_value) const {
|
|
|
|
if (path.index() > std::numeric_limits<uint32_t>::max()) {
|
2016-10-19 12:18:14 -07:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
out_value->dataType = android::Res_value::TYPE_STRING;
|
|
|
|
out_value->data = util::HostToDevice32(static_cast<uint32_t>(path.index()));
|
2016-10-19 12:18:14 -07:00
|
|
|
return true;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
FileReference* FileReference::Clone(StringPool* new_pool) const {
|
|
|
|
FileReference* fr = new FileReference(new_pool->MakeRef(*path));
|
2016-10-19 12:18:14 -07:00
|
|
|
fr->file = file;
|
2016-10-21 17:56:45 -07:00
|
|
|
fr->comment_ = comment_;
|
|
|
|
fr->source_ = source_;
|
2016-10-19 12:18:14 -07:00
|
|
|
return fr;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void FileReference::Print(std::ostream* out) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "(file) " << *path;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
BinaryPrimitive::BinaryPrimitive(const android::Res_value& val) : value(val) {}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
BinaryPrimitive::BinaryPrimitive(uint8_t dataType, uint32_t data) {
|
2016-10-19 12:18:14 -07:00
|
|
|
value.dataType = dataType;
|
|
|
|
value.data = data;
|
2015-08-14 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool BinaryPrimitive::Equals(const Value* value) const {
|
|
|
|
const BinaryPrimitive* other = ValueCast<BinaryPrimitive>(value);
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!other) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this->value.dataType == other->value.dataType &&
|
|
|
|
this->value.data == other->value.data;
|
2016-04-25 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool BinaryPrimitive::Flatten(android::Res_value* out_value) const {
|
|
|
|
out_value->dataType = value.dataType;
|
|
|
|
out_value->data = util::HostToDevice32(value.data);
|
2016-10-19 12:18:14 -07:00
|
|
|
return true;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
BinaryPrimitive* BinaryPrimitive::Clone(StringPool* /*new_pool*/) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
return new BinaryPrimitive(*this);
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void BinaryPrimitive::Print(std::ostream* out) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
switch (value.dataType) {
|
|
|
|
case android::Res_value::TYPE_NULL:
|
|
|
|
*out << "(null)";
|
|
|
|
break;
|
|
|
|
case android::Res_value::TYPE_INT_DEC:
|
|
|
|
*out << "(integer) " << static_cast<int32_t>(value.data);
|
|
|
|
break;
|
|
|
|
case android::Res_value::TYPE_INT_HEX:
|
|
|
|
*out << "(integer) 0x" << std::hex << value.data << std::dec;
|
|
|
|
break;
|
|
|
|
case android::Res_value::TYPE_INT_BOOLEAN:
|
|
|
|
*out << "(boolean) " << (value.data != 0 ? "true" : "false");
|
|
|
|
break;
|
|
|
|
case android::Res_value::TYPE_INT_COLOR_ARGB8:
|
|
|
|
case android::Res_value::TYPE_INT_COLOR_RGB8:
|
|
|
|
case android::Res_value::TYPE_INT_COLOR_ARGB4:
|
|
|
|
case android::Res_value::TYPE_INT_COLOR_RGB4:
|
|
|
|
*out << "(color) #" << std::hex << value.data << std::dec;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*out << "(unknown 0x" << std::hex << (int)value.dataType << ") 0x"
|
|
|
|
<< std::hex << value.data << std::dec;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Attribute::Attribute(bool w, uint32_t t)
|
2016-10-21 17:56:45 -07:00
|
|
|
: type_mask(t),
|
|
|
|
min_int(std::numeric_limits<int32_t>::min()),
|
|
|
|
max_int(std::numeric_limits<int32_t>::max()) {
|
|
|
|
weak_ = w;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-06-27 16:21:42 -07:00
|
|
|
template <typename T>
|
|
|
|
T* addPointer(T& val) {
|
2016-10-19 12:18:14 -07:00
|
|
|
return &val;
|
2016-06-27 16:21:42 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool Attribute::Equals(const Value* value) const {
|
|
|
|
const Attribute* other = ValueCast<Attribute>(value);
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!other) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-27 16:21:42 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (symbols.size() != other->symbols.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-27 16:21:42 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (type_mask != other->type_mask || min_int != other->min_int ||
|
|
|
|
max_int != other->max_int) {
|
2016-10-19 12:18:14 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
std::vector<const Symbol*> sorted_a;
|
|
|
|
std::transform(symbols.begin(), symbols.end(), std::back_inserter(sorted_a),
|
2016-10-19 12:18:14 -07:00
|
|
|
addPointer<const Symbol>);
|
2016-10-21 17:56:45 -07:00
|
|
|
std::sort(sorted_a.begin(), sorted_a.end(),
|
2016-10-19 12:18:14 -07:00
|
|
|
[](const Symbol* a, const Symbol* b) -> bool {
|
|
|
|
return a->symbol.name < b->symbol.name;
|
|
|
|
});
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
std::vector<const Symbol*> sorted_b;
|
2016-10-19 12:18:14 -07:00
|
|
|
std::transform(other->symbols.begin(), other->symbols.end(),
|
2016-10-21 17:56:45 -07:00
|
|
|
std::back_inserter(sorted_b), addPointer<const Symbol>);
|
|
|
|
std::sort(sorted_b.begin(), sorted_b.end(),
|
2016-10-19 12:18:14 -07:00
|
|
|
[](const Symbol* a, const Symbol* b) -> bool {
|
|
|
|
return a->symbol.name < b->symbol.name;
|
|
|
|
});
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
|
2016-10-19 12:18:14 -07:00
|
|
|
[](const Symbol* a, const Symbol* b) -> bool {
|
2016-10-21 17:56:45 -07:00
|
|
|
return a->symbol.Equals(&b->symbol) &&
|
2016-10-19 12:18:14 -07:00
|
|
|
a->value == b->value;
|
|
|
|
});
|
2016-04-25 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
Attribute* Attribute::Clone(StringPool* /*new_pool*/) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
return new Attribute(*this);
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Attribute::PrintMask(std::ostream* out) const {
|
|
|
|
if (type_mask == android::ResTable_map::TYPE_ANY) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "any";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool set = false;
|
2016-10-21 17:56:45 -07:00
|
|
|
if ((type_mask & android::ResTable_map::TYPE_REFERENCE) != 0) {
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!set) {
|
|
|
|
set = true;
|
|
|
|
} else {
|
|
|
|
*out << "|";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "reference";
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if ((type_mask & android::ResTable_map::TYPE_STRING) != 0) {
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!set) {
|
|
|
|
set = true;
|
|
|
|
} else {
|
|
|
|
*out << "|";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "string";
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if ((type_mask & android::ResTable_map::TYPE_INTEGER) != 0) {
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!set) {
|
|
|
|
set = true;
|
|
|
|
} else {
|
|
|
|
*out << "|";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "integer";
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if ((type_mask & android::ResTable_map::TYPE_BOOLEAN) != 0) {
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!set) {
|
|
|
|
set = true;
|
|
|
|
} else {
|
|
|
|
*out << "|";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "boolean";
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if ((type_mask & android::ResTable_map::TYPE_COLOR) != 0) {
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!set) {
|
|
|
|
set = true;
|
|
|
|
} else {
|
|
|
|
*out << "|";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "color";
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if ((type_mask & android::ResTable_map::TYPE_FLOAT) != 0) {
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!set) {
|
|
|
|
set = true;
|
|
|
|
} else {
|
|
|
|
*out << "|";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "float";
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if ((type_mask & android::ResTable_map::TYPE_DIMENSION) != 0) {
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!set) {
|
|
|
|
set = true;
|
|
|
|
} else {
|
|
|
|
*out << "|";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "dimension";
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if ((type_mask & android::ResTable_map::TYPE_FRACTION) != 0) {
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!set) {
|
|
|
|
set = true;
|
|
|
|
} else {
|
|
|
|
*out << "|";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "fraction";
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if ((type_mask & android::ResTable_map::TYPE_ENUM) != 0) {
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!set) {
|
|
|
|
set = true;
|
|
|
|
} else {
|
|
|
|
*out << "|";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "enum";
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if ((type_mask & android::ResTable_map::TYPE_FLAGS) != 0) {
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!set) {
|
|
|
|
set = true;
|
|
|
|
} else {
|
|
|
|
*out << "|";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "flags";
|
|
|
|
}
|
2015-05-04 17:40:56 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Attribute::Print(std::ostream* out) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "(attr) ";
|
2016-10-21 17:56:45 -07:00
|
|
|
PrintMask(out);
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!symbols.empty()) {
|
2016-10-21 17:56:45 -07:00
|
|
|
*out << " [" << util::Joiner(symbols, ", ") << "]";
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (min_int != std::numeric_limits<int32_t>::min()) {
|
|
|
|
*out << " min=" << min_int;
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
2016-04-25 14:20:21 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (max_int != std::numeric_limits<int32_t>::max()) {
|
|
|
|
*out << " max=" << max_int;
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
2016-04-25 14:20:21 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (IsWeak()) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << " [weak]";
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
static void BuildAttributeMismatchMessage(DiagMessage* msg,
|
2016-10-19 12:18:14 -07:00
|
|
|
const Attribute* attr,
|
2015-11-20 15:32:30 -08:00
|
|
|
const Item* value) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*msg << "expected";
|
2016-10-21 17:56:45 -07:00
|
|
|
if (attr->type_mask & android::ResTable_map::TYPE_BOOLEAN) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*msg << " boolean";
|
|
|
|
}
|
2015-11-20 15:32:30 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (attr->type_mask & android::ResTable_map::TYPE_COLOR) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*msg << " color";
|
|
|
|
}
|
2015-11-20 15:32:30 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (attr->type_mask & android::ResTable_map::TYPE_DIMENSION) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*msg << " dimension";
|
|
|
|
}
|
2015-11-20 15:32:30 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (attr->type_mask & android::ResTable_map::TYPE_ENUM) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*msg << " enum";
|
|
|
|
}
|
2015-11-20 15:32:30 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (attr->type_mask & android::ResTable_map::TYPE_FLAGS) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*msg << " flags";
|
|
|
|
}
|
2015-11-20 15:32:30 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (attr->type_mask & android::ResTable_map::TYPE_FLOAT) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*msg << " float";
|
|
|
|
}
|
2015-11-20 15:32:30 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (attr->type_mask & android::ResTable_map::TYPE_FRACTION) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*msg << " fraction";
|
|
|
|
}
|
2015-11-20 15:32:30 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (attr->type_mask & android::ResTable_map::TYPE_INTEGER) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*msg << " integer";
|
|
|
|
}
|
2015-11-20 15:32:30 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (attr->type_mask & android::ResTable_map::TYPE_REFERENCE) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*msg << " reference";
|
|
|
|
}
|
2015-11-20 15:32:30 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
if (attr->type_mask & android::ResTable_map::TYPE_STRING) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*msg << " string";
|
|
|
|
}
|
2015-11-20 15:32:30 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
*msg << " but got " << *value;
|
2015-11-20 15:32:30 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool Attribute::Matches(const Item* item, DiagMessage* out_msg) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
android::Res_value val = {};
|
2016-10-21 17:56:45 -07:00
|
|
|
item->Flatten(&val);
|
2015-11-20 15:32:30 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
// Always allow references.
|
2016-10-21 17:56:45 -07:00
|
|
|
const uint32_t mask = type_mask | android::ResTable_map::TYPE_REFERENCE;
|
|
|
|
if (!(mask & ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType))) {
|
|
|
|
if (out_msg) {
|
|
|
|
BuildAttributeMismatchMessage(out_msg, this, item);
|
2016-04-25 14:20:21 -07:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
return false;
|
2016-06-27 16:21:42 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
} else if (ResourceUtils::AndroidTypeToAttributeTypeMask(val.dataType) &
|
2016-10-19 12:18:14 -07:00
|
|
|
android::ResTable_map::TYPE_INTEGER) {
|
2016-10-21 17:56:45 -07:00
|
|
|
if (static_cast<int32_t>(util::DeviceToHost32(val.data)) < min_int) {
|
|
|
|
if (out_msg) {
|
|
|
|
*out_msg << *item << " is less than minimum integer " << min_int;
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
|
|
|
return false;
|
2016-10-21 17:56:45 -07:00
|
|
|
} else if (static_cast<int32_t>(util::DeviceToHost32(val.data)) > max_int) {
|
|
|
|
if (out_msg) {
|
|
|
|
*out_msg << *item << " is greater than maximum integer " << max_int;
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
|
|
|
return false;
|
2016-06-27 16:21:42 -07:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-27 16:21:42 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool Style::Equals(const Value* value) const {
|
|
|
|
const Style* other = ValueCast<Style>(value);
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!other) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (bool(parent) != bool(other->parent) ||
|
|
|
|
(parent && other->parent &&
|
2016-10-21 17:56:45 -07:00
|
|
|
!parent.value().Equals(&other->parent.value()))) {
|
2016-10-19 12:18:14 -07:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-27 16:21:42 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (entries.size() != other->entries.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
std::vector<const Entry*> sorted_a;
|
|
|
|
std::transform(entries.begin(), entries.end(), std::back_inserter(sorted_a),
|
2016-10-19 12:18:14 -07:00
|
|
|
addPointer<const Entry>);
|
2016-10-21 17:56:45 -07:00
|
|
|
std::sort(sorted_a.begin(), sorted_a.end(),
|
2016-10-19 12:18:14 -07:00
|
|
|
[](const Entry* a, const Entry* b) -> bool {
|
|
|
|
return a->key.name < b->key.name;
|
|
|
|
});
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
std::vector<const Entry*> sorted_b;
|
2016-10-19 12:18:14 -07:00
|
|
|
std::transform(other->entries.begin(), other->entries.end(),
|
2016-10-21 17:56:45 -07:00
|
|
|
std::back_inserter(sorted_b), addPointer<const Entry>);
|
|
|
|
std::sort(sorted_b.begin(), sorted_b.end(),
|
2016-10-19 12:18:14 -07:00
|
|
|
[](const Entry* a, const Entry* b) -> bool {
|
|
|
|
return a->key.name < b->key.name;
|
|
|
|
});
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin(),
|
2016-10-19 12:18:14 -07:00
|
|
|
[](const Entry* a, const Entry* b) -> bool {
|
2016-10-21 17:56:45 -07:00
|
|
|
return a->key.Equals(&b->key) &&
|
|
|
|
a->value->Equals(b->value.get());
|
2016-10-19 12:18:14 -07:00
|
|
|
});
|
2016-04-25 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
Style* Style::Clone(StringPool* new_pool) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
Style* style = new Style();
|
|
|
|
style->parent = parent;
|
2016-10-21 17:56:45 -07:00
|
|
|
style->parent_inferred = parent_inferred;
|
|
|
|
style->comment_ = comment_;
|
|
|
|
style->source_ = source_;
|
2016-10-19 12:18:14 -07:00
|
|
|
for (auto& entry : entries) {
|
|
|
|
style->entries.push_back(
|
2016-10-21 17:56:45 -07:00
|
|
|
Entry{entry.key, std::unique_ptr<Item>(entry.value->Clone(new_pool))});
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
|
|
|
return style;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Style::Print(std::ostream* out) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "(style) ";
|
|
|
|
if (parent && parent.value().name) {
|
2016-10-21 17:56:45 -07:00
|
|
|
if (parent.value().private_reference) {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "*";
|
|
|
|
}
|
|
|
|
*out << parent.value().name.value();
|
|
|
|
}
|
2016-10-21 17:56:45 -07:00
|
|
|
*out << " [" << util::Joiner(entries, ", ") << "]";
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static ::std::ostream& operator<<(::std::ostream& out,
|
|
|
|
const Style::Entry& value) {
|
|
|
|
if (value.key.name) {
|
|
|
|
out << value.key.name.value();
|
|
|
|
} else if (value.key.id) {
|
|
|
|
out << value.key.id.value();
|
|
|
|
} else {
|
|
|
|
out << "???";
|
|
|
|
}
|
|
|
|
out << " = ";
|
2016-10-21 17:56:45 -07:00
|
|
|
value.value->Print(&out);
|
2016-10-19 12:18:14 -07:00
|
|
|
return out;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool Array::Equals(const Value* value) const {
|
|
|
|
const Array* other = ValueCast<Array>(value);
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!other) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-25 14:20:21 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (items.size() != other->items.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-27 16:21:42 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
return std::equal(items.begin(), items.end(), other->items.begin(),
|
|
|
|
[](const std::unique_ptr<Item>& a,
|
|
|
|
const std::unique_ptr<Item>& b) -> bool {
|
2016-10-21 17:56:45 -07:00
|
|
|
return a->Equals(b.get());
|
2016-10-19 12:18:14 -07:00
|
|
|
});
|
2016-04-25 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
Array* Array::Clone(StringPool* new_pool) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
Array* array = new Array();
|
2016-10-21 17:56:45 -07:00
|
|
|
array->comment_ = comment_;
|
|
|
|
array->source_ = source_;
|
2016-10-19 12:18:14 -07:00
|
|
|
for (auto& item : items) {
|
2016-10-21 17:56:45 -07:00
|
|
|
array->items.emplace_back(std::unique_ptr<Item>(item->Clone(new_pool)));
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
|
|
|
return array;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Array::Print(std::ostream* out) const {
|
|
|
|
*out << "(array) [" << util::Joiner(items, ", ") << "]";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool Plural::Equals(const Value* value) const {
|
|
|
|
const Plural* other = ValueCast<Plural>(value);
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!other) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-25 14:20:21 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (values.size() != other->values.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-27 16:21:42 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
return std::equal(values.begin(), values.end(), other->values.begin(),
|
|
|
|
[](const std::unique_ptr<Item>& a,
|
|
|
|
const std::unique_ptr<Item>& b) -> bool {
|
|
|
|
if (bool(a) != bool(b)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-21 17:56:45 -07:00
|
|
|
return bool(a) == bool(b) || a->Equals(b.get());
|
2016-10-19 12:18:14 -07:00
|
|
|
});
|
2016-04-25 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
Plural* Plural::Clone(StringPool* new_pool) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
Plural* p = new Plural();
|
2016-10-21 17:56:45 -07:00
|
|
|
p->comment_ = comment_;
|
|
|
|
p->source_ = source_;
|
2016-10-19 12:18:14 -07:00
|
|
|
const size_t count = values.size();
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
if (values[i]) {
|
2016-10-21 17:56:45 -07:00
|
|
|
p->values[i] = std::unique_ptr<Item>(values[i]->Clone(new_pool));
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
2016-10-19 12:18:14 -07:00
|
|
|
}
|
|
|
|
return p;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Plural::Print(std::ostream* out) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "(plural)";
|
|
|
|
if (values[Zero]) {
|
|
|
|
*out << " zero=" << *values[Zero];
|
|
|
|
}
|
2016-04-25 14:20:21 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (values[One]) {
|
|
|
|
*out << " one=" << *values[One];
|
|
|
|
}
|
2016-04-25 14:20:21 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (values[Two]) {
|
|
|
|
*out << " two=" << *values[Two];
|
|
|
|
}
|
2016-04-25 14:20:21 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (values[Few]) {
|
|
|
|
*out << " few=" << *values[Few];
|
|
|
|
}
|
2016-04-25 14:20:21 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (values[Many]) {
|
|
|
|
*out << " many=" << *values[Many];
|
|
|
|
}
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
static ::std::ostream& operator<<(::std::ostream& out,
|
|
|
|
const std::unique_ptr<Item>& item) {
|
|
|
|
return out << *item;
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
bool Styleable::Equals(const Value* value) const {
|
|
|
|
const Styleable* other = ValueCast<Styleable>(value);
|
2016-10-19 12:18:14 -07:00
|
|
|
if (!other) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-27 16:21:42 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
if (entries.size() != other->entries.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-27 16:21:42 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
return std::equal(entries.begin(), entries.end(), other->entries.begin(),
|
|
|
|
[](const Reference& a, const Reference& b) -> bool {
|
2016-10-21 17:56:45 -07:00
|
|
|
return a.Equals(&b);
|
2016-10-19 12:18:14 -07:00
|
|
|
});
|
2016-04-25 14:20:21 -07:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
Styleable* Styleable::Clone(StringPool* /*new_pool*/) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
return new Styleable(*this);
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Styleable::Print(std::ostream* out) const {
|
2016-10-19 12:18:14 -07:00
|
|
|
*out << "(styleable) "
|
2016-10-21 17:56:45 -07:00
|
|
|
<< " [" << util::Joiner(entries, ", ") << "]";
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-08-19 12:16:49 -07:00
|
|
|
bool operator<(const Reference& a, const Reference& b) {
|
2016-10-21 17:56:45 -07:00
|
|
|
int cmp = a.name.value_or_default({}).compare(b.name.value_or_default({}));
|
2016-10-19 12:18:14 -07:00
|
|
|
if (cmp != 0) return cmp < 0;
|
|
|
|
return a.id < b.id;
|
2016-08-19 12:16:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Reference& a, const Reference& b) {
|
2016-10-19 12:18:14 -07:00
|
|
|
return a.name == b.name && a.id == b.id;
|
2016-08-19 12:16:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Reference& a, const Reference& b) {
|
2016-10-19 12:18:14 -07:00
|
|
|
return a.name != b.name || a.id != b.id;
|
2016-08-19 12:16:49 -07:00
|
|
|
}
|
|
|
|
|
2016-08-24 16:03:48 -07:00
|
|
|
struct NameOnlyComparator {
|
2016-10-19 12:18:14 -07:00
|
|
|
bool operator()(const Reference& a, const Reference& b) const {
|
|
|
|
return a.name < b.name;
|
|
|
|
}
|
2016-08-24 16:03:48 -07:00
|
|
|
};
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
void Styleable::MergeWith(Styleable* other) {
|
2016-10-19 12:18:14 -07:00
|
|
|
// Compare only names, because some References may already have their IDs
|
|
|
|
// assigned
|
|
|
|
// (framework IDs that don't change).
|
|
|
|
std::set<Reference, NameOnlyComparator> references;
|
|
|
|
references.insert(entries.begin(), entries.end());
|
|
|
|
references.insert(other->entries.begin(), other->entries.end());
|
|
|
|
entries.clear();
|
|
|
|
entries.reserve(references.size());
|
|
|
|
entries.insert(entries.end(), references.begin(), references.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aapt
|