AAPT2: Remove usage of u16string
For legacy reasons, we kept around the use of UTF-16 internally
in AAPT2. We don't need this and this CL removes all instances of
std::u16string and StringPiece16. The only places still needed
are when interacting with the ResTable APIs that only operate in
UTF16.
Change-Id: I492475b84bb9014fa13bf992cff447ee7a5fe588
diff --git a/tools/aapt2/process/IResourceTableConsumer.h b/tools/aapt2/process/IResourceTableConsumer.h
index 762725d..69b7d92 100644
--- a/tools/aapt2/process/IResourceTableConsumer.h
+++ b/tools/aapt2/process/IResourceTableConsumer.h
@@ -37,7 +37,7 @@
virtual SymbolTable* getExternalSymbols() = 0;
virtual IDiagnostics* getDiagnostics() = 0;
- virtual const std::u16string& getCompilationPackage() = 0;
+ virtual const std::string& getCompilationPackage() = 0;
virtual uint8_t getPackageId() = 0;
virtual NameMangler* getNameMangler() = 0;
virtual bool verbose() = 0;
diff --git a/tools/aapt2/process/SymbolTable.cpp b/tools/aapt2/process/SymbolTable.cpp
index eaaf06f..6c506df 100644
--- a/tools/aapt2/process/SymbolTable.cpp
+++ b/tools/aapt2/process/SymbolTable.cpp
@@ -16,6 +16,7 @@
#include "ConfigDescription.h"
#include "Resource.h"
+#include "ResourceUtils.h"
#include "ValueVisitor.h"
#include "process/SymbolTable.h"
#include "util/Util.h"
@@ -84,7 +85,7 @@
const SymbolTable::Symbol* SymbolTable::findByReference(const Reference& ref) {
// First try the ID. This is because when we lookup by ID, we only fill in the ID cache.
// Looking up by name fills in the name and ID cache. So a cache miss will cause a failed
- // ID lookup, then a successfull name lookup. Subsequent look ups will hit immediately
+ // ID lookup, then a successful name lookup. Subsequent look ups will hit immediately
// because the ID is cached too.
//
// If we looked up by name first, a cache miss would mean we failed to lookup by name, then
@@ -184,18 +185,13 @@
return nullptr;
}
- const ResourceType* parsedType = parseResourceType(
- StringPiece16(entryName.type, entryName.typeLen));
- if (!parsedType) {
- table.unlockBag(entry);
+ Maybe<ResourceName> parsedName = ResourceUtils::toResourceName(entryName);
+ if (!parsedName) {
return nullptr;
}
Attribute::Symbol symbol;
- symbol.symbol.name = ResourceName(
- StringPiece16(entryName.package, entryName.packageLen),
- *parsedType,
- StringPiece16(entryName.name, entryName.nameLen));
+ symbol.symbol.name = parsedName.value();
symbol.symbol.id = ResourceId(mapEntry.name.ident);
symbol.value = mapEntry.value.data;
s->attribute->symbols.push_back(std::move(symbol));
@@ -208,11 +204,15 @@
std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findByName(
const ResourceName& name) {
const android::ResTable& table = mAssets.getResources(false);
- StringPiece16 typeStr = toString(name.type);
+
+ const std::u16string package16 = util::utf8ToUtf16(name.package);
+ const std::u16string type16 = util::utf8ToUtf16(toString(name.type));
+ const std::u16string entry16 = util::utf8ToUtf16(name.entry);
+
uint32_t typeSpecFlags = 0;
- ResourceId resId = table.identifierForName(name.entry.data(), name.entry.size(),
- typeStr.data(), typeStr.size(),
- name.package.data(), name.package.size(),
+ ResourceId resId = table.identifierForName(entry16.data(), entry16.size(),
+ type16.data(), type16.size(),
+ package16.data(), package16.size(),
&typeSpecFlags);
if (!resId.isValid()) {
return {};
@@ -238,37 +238,7 @@
if (!table.getResourceName(id.id, true, &resName)) {
return {};
}
-
- ResourceName name;
- if (resName.package) {
- name.package = StringPiece16(resName.package, resName.packageLen).toString();
- }
-
- const ResourceType* type;
- if (resName.type) {
- type = parseResourceType(StringPiece16(resName.type, resName.typeLen));
-
- } else if (resName.type8) {
- type = parseResourceType(util::utf8ToUtf16(StringPiece(resName.type8, resName.typeLen)));
- } else {
- return {};
- }
-
- if (!type) {
- return {};
- }
-
- name.type = *type;
-
- if (resName.name) {
- name.entry = StringPiece16(resName.name, resName.nameLen).toString();
- } else if (resName.name8) {
- name.entry = util::utf8ToUtf16(StringPiece(resName.name8, resName.nameLen));
- } else {
- return {};
- }
-
- return name;
+ return ResourceUtils::toResourceName(resName);
}
std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findById(ResourceId id) {
diff --git a/tools/aapt2/process/SymbolTable.h b/tools/aapt2/process/SymbolTable.h
index e684bb0..43f4dd7 100644
--- a/tools/aapt2/process/SymbolTable.h
+++ b/tools/aapt2/process/SymbolTable.h
@@ -34,7 +34,7 @@
namespace aapt {
inline android::hash_t hash_type(const ResourceName& name) {
- std::hash<std::u16string> strHash;
+ std::hash<std::string> strHash;
android::hash_t hash = 0;
hash = android::JenkinsHashMix(hash, (uint32_t) strHash(name.package));
hash = android::JenkinsHashMix(hash, (uint32_t) name.type);
diff --git a/tools/aapt2/process/SymbolTable_test.cpp b/tools/aapt2/process/SymbolTable_test.cpp
index 34f31be..6866974 100644
--- a/tools/aapt2/process/SymbolTable_test.cpp
+++ b/tools/aapt2/process/SymbolTable_test.cpp
@@ -21,31 +21,31 @@
TEST(ResourceTableSymbolSourceTest, FindSymbols) {
std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
- .addSimple(u"@android:id/foo", ResourceId(0x01020000))
- .addSimple(u"@android:id/bar")
- .addValue(u"@android:attr/foo", ResourceId(0x01010000),
+ .addSimple("@android:id/foo", ResourceId(0x01020000))
+ .addSimple("@android:id/bar")
+ .addValue("@android:attr/foo", ResourceId(0x01010000),
test::AttributeBuilder().build())
.build();
ResourceTableSymbolSource symbolSource(table.get());
- EXPECT_NE(nullptr, symbolSource.findByName(test::parseNameOrDie(u"@android:id/foo")));
- EXPECT_NE(nullptr, symbolSource.findByName(test::parseNameOrDie(u"@android:id/bar")));
+ EXPECT_NE(nullptr, symbolSource.findByName(test::parseNameOrDie("@android:id/foo")));
+ EXPECT_NE(nullptr, symbolSource.findByName(test::parseNameOrDie("@android:id/bar")));
std::unique_ptr<SymbolTable::Symbol> s = symbolSource.findByName(
- test::parseNameOrDie(u"@android:attr/foo"));
+ test::parseNameOrDie("@android:attr/foo"));
ASSERT_NE(nullptr, s);
EXPECT_NE(nullptr, s->attribute);
}
TEST(ResourceTableSymbolSourceTest, FindPrivateAttrSymbol) {
std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
- .addValue(u"@android:^attr-private/foo", ResourceId(0x01010000),
+ .addValue("@android:^attr-private/foo", ResourceId(0x01010000),
test::AttributeBuilder().build())
.build();
ResourceTableSymbolSource symbolSource(table.get());
std::unique_ptr<SymbolTable::Symbol> s = symbolSource.findByName(
- test::parseNameOrDie(u"@android:attr/foo"));
+ test::parseNameOrDie("@android:attr/foo"));
ASSERT_NE(nullptr, s);
EXPECT_NE(nullptr, s->attribute);
}