Move StringPiece to libandroidfw
libandroidfw needs to make use of StringPiece, so
move it to libandroidfw and update all code referencing
StringPiece in aapt2.
Test: make libandroidfw_tests libaapt2_tests
Change-Id: I68d7f0fc7c651b048d9d1f5e7971f10ef5349fa1
diff --git a/tools/aapt2/xml/XmlDom.cpp b/tools/aapt2/xml/XmlDom.cpp
index 960d361..fab2f19 100644
--- a/tools/aapt2/xml/XmlDom.cpp
+++ b/tools/aapt2/xml/XmlDom.cpp
@@ -29,6 +29,9 @@
#include "XmlPullParser.h"
#include "util/Util.h"
+using android::StringPiece;
+using android::StringPiece16;
+
namespace aapt {
namespace xml {
@@ -52,10 +55,10 @@
if (*p == 0) {
out_ns->clear();
- *out_name = StringPiece(name).ToString();
+ out_name->assign(name);
} else {
- *out_ns = StringPiece(name, (p - name)).ToString();
- *out_name = StringPiece(p + 1).ToString();
+ out_ns->assign(name, (p - name));
+ out_name->assign(p + 1);
}
}
@@ -83,11 +86,11 @@
std::unique_ptr<Namespace> ns = util::make_unique<Namespace>();
if (prefix) {
- ns->namespace_prefix = StringPiece(prefix).ToString();
+ ns->namespace_prefix = prefix;
}
if (uri) {
- ns->namespace_uri = StringPiece(uri).ToString();
+ ns->namespace_uri = uri;
}
AddToStack(stack, parser, std::move(ns));
@@ -117,7 +120,7 @@
while (*attrs) {
Attribute attribute;
SplitName(*attrs++, &attribute.namespace_uri, &attribute.name);
- attribute.value = StringPiece(*attrs++).ToString();
+ attribute.value = *attrs++;
// Insert in sorted order.
auto iter = std::lower_bound(el->attributes.begin(), el->attributes.end(),
@@ -153,14 +156,14 @@
if (!currentParent->children.empty()) {
Node* last_child = currentParent->children.back().get();
if (Text* text = NodeCast<Text>(last_child)) {
- text->text += StringPiece(s, len).ToString();
+ text->text.append(s, len);
return;
}
}
}
std::unique_ptr<Text> text = util::make_unique<Text>();
- text->text = StringPiece(s, len).ToString();
+ text->text.assign(s, len);
AddToStack(stack, parser, std::move(text));
}
@@ -495,15 +498,14 @@
Maybe<ExtractedPackage> PackageAwareVisitor::TransformPackageAlias(
const StringPiece& alias, const StringPiece& local_package) const {
if (alias.empty()) {
- return ExtractedPackage{local_package.ToString(), false /* private */};
+ return ExtractedPackage{local_package.to_string(), false /* private */};
}
const auto rend = package_decls_.rend();
for (auto iter = package_decls_.rbegin(); iter != rend; ++iter) {
if (alias == iter->prefix) {
if (iter->package.package.empty()) {
- return ExtractedPackage{local_package.ToString(),
- iter->package.private_namespace};
+ return ExtractedPackage{local_package.to_string(), iter->package.private_namespace};
}
return iter->package;
}
diff --git a/tools/aapt2/xml/XmlDom.h b/tools/aapt2/xml/XmlDom.h
index 720fe35..90cdfb6 100644
--- a/tools/aapt2/xml/XmlDom.h
+++ b/tools/aapt2/xml/XmlDom.h
@@ -22,10 +22,11 @@
#include <string>
#include <vector>
+#include "androidfw/StringPiece.h"
+
#include "Diagnostics.h"
#include "Resource.h"
#include "ResourceValues.h"
-#include "util/StringPiece.h"
#include "util/Util.h"
#include "xml/XmlUtil.h"
@@ -100,13 +101,13 @@
std::string name;
std::vector<Attribute> attributes;
- Attribute* FindAttribute(const StringPiece& ns, const StringPiece& name);
- xml::Element* FindChild(const StringPiece& ns, const StringPiece& name);
- xml::Element* FindChildWithAttribute(const StringPiece& ns,
- const StringPiece& name,
- const StringPiece& attr_ns,
- const StringPiece& attr_name,
- const StringPiece& attr_value);
+ Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name);
+ xml::Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
+ xml::Element* FindChildWithAttribute(const android::StringPiece& ns,
+ const android::StringPiece& name,
+ const android::StringPiece& attr_ns,
+ const android::StringPiece& attr_name,
+ const android::StringPiece& attr_value);
std::vector<xml::Element*> GetChildElements();
std::unique_ptr<Node> Clone() override;
};
@@ -190,8 +191,7 @@
void Visit(Namespace* ns) override;
Maybe<ExtractedPackage> TransformPackageAlias(
- const StringPiece& alias,
- const StringPiece& local_package) const override;
+ const android::StringPiece& alias, const android::StringPiece& local_package) const override;
private:
struct PackageDecl {
diff --git a/tools/aapt2/xml/XmlPullParser.cpp b/tools/aapt2/xml/XmlPullParser.cpp
index e59fa86..c2a9c82 100644
--- a/tools/aapt2/xml/XmlPullParser.cpp
+++ b/tools/aapt2/xml/XmlPullParser.cpp
@@ -22,6 +22,8 @@
#include "xml/XmlPullParser.h"
#include "xml/XmlUtil.h"
+using android::StringPiece;
+
namespace aapt {
namespace xml {
@@ -136,15 +138,14 @@
Maybe<ExtractedPackage> XmlPullParser::TransformPackageAlias(
const StringPiece& alias, const StringPiece& local_package) const {
if (alias.empty()) {
- return ExtractedPackage{local_package.ToString(), false /* private */};
+ return ExtractedPackage{local_package.to_string(), false /* private */};
}
const auto end_iter = package_aliases_.rend();
for (auto iter = package_aliases_.rbegin(); iter != end_iter; ++iter) {
if (alias == iter->prefix) {
if (iter->package.package.empty()) {
- return ExtractedPackage{local_package.ToString(),
- iter->package.private_namespace};
+ return ExtractedPackage{local_package.to_string(), iter->package.private_namespace};
}
return iter->package;
}
@@ -188,19 +189,18 @@
/**
* Extracts the namespace and name of an expanded element or attribute name.
*/
-static void SplitName(const char* name, std::string& out_ns,
- std::string& out_name) {
+static void SplitName(const char* name, std::string* out_ns, std::string* out_name) {
const char* p = name;
while (*p != 0 && *p != kXmlNamespaceSep) {
p++;
}
if (*p == 0) {
- out_ns = std::string();
- out_name = name;
+ out_ns->clear();
+ out_name->assign(name);
} else {
- out_ns = StringPiece(name, (p - name)).ToString();
- out_name = p + 1;
+ out_ns->assign(name, (p - name));
+ out_name->assign(p + 1);
}
}
@@ -224,11 +224,11 @@
EventData data = {Event::kStartElement,
XML_GetCurrentLineNumber(parser->parser_),
parser->depth_++};
- SplitName(name, data.data1, data.data2);
+ SplitName(name, &data.data1, &data.data2);
while (*attrs) {
Attribute attribute;
- SplitName(*attrs++, attribute.namespace_uri, attribute.name);
+ SplitName(*attrs++, &attribute.namespace_uri, &attribute.name);
attribute.value = *attrs++;
// Insert in sorted order.
@@ -245,9 +245,8 @@
int len) {
XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
- parser->event_queue_.push(
- EventData{Event::kText, XML_GetCurrentLineNumber(parser->parser_),
- parser->depth_, StringPiece(s, len).ToString()});
+ parser->event_queue_.push(EventData{Event::kText, XML_GetCurrentLineNumber(parser->parser_),
+ parser->depth_, std::string(s, len)});
}
void XMLCALL XmlPullParser::EndElementHandler(void* user_data,
@@ -257,7 +256,7 @@
EventData data = {Event::kEndElement,
XML_GetCurrentLineNumber(parser->parser_),
--(parser->depth_)};
- SplitName(name, data.data1, data.data2);
+ SplitName(name, &data.data1, &data.data2);
// Move the data into the queue (no copy).
parser->event_queue_.push(std::move(data));
diff --git a/tools/aapt2/xml/XmlPullParser.h b/tools/aapt2/xml/XmlPullParser.h
index ff58d60..cdeeefd 100644
--- a/tools/aapt2/xml/XmlPullParser.h
+++ b/tools/aapt2/xml/XmlPullParser.h
@@ -28,11 +28,11 @@
#include <vector>
#include "android-base/macros.h"
+#include "androidfw/StringPiece.h"
#include "Resource.h"
#include "process/IResourceTableConsumer.h"
#include "util/Maybe.h"
-#include "util/StringPiece.h"
#include "xml/XmlUtil.h"
namespace aapt {
@@ -119,8 +119,7 @@
* 'package' will be set to 'defaultPackage'.
*/
Maybe<ExtractedPackage> TransformPackageAlias(
- const StringPiece& alias,
- const StringPiece& local_package) const override;
+ const android::StringPiece& alias, const android::StringPiece& local_package) const override;
//
// Remaining methods are for retrieving information about attributes
@@ -146,8 +145,7 @@
const_iterator begin_attributes() const;
const_iterator end_attributes() const;
size_t attribute_count() const;
- const_iterator FindAttribute(StringPiece namespace_uri,
- StringPiece name) const;
+ const_iterator FindAttribute(android::StringPiece namespace_uri, android::StringPiece name) const;
private:
DISALLOW_COPY_AND_ASSIGN(XmlPullParser);
@@ -190,16 +188,16 @@
/**
* Finds the attribute in the current element within the global namespace.
*/
-Maybe<StringPiece> FindAttribute(const XmlPullParser* parser,
- const StringPiece& name);
+Maybe<android::StringPiece> FindAttribute(const XmlPullParser* parser,
+ const android::StringPiece& name);
/**
* Finds the attribute in the current element within the global namespace. The
* attribute's value
* must not be the empty string.
*/
-Maybe<StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser,
- const StringPiece& name);
+Maybe<android::StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser,
+ const android::StringPiece& name);
//
// Implementation
@@ -299,13 +297,13 @@
}
inline XmlPullParser::const_iterator XmlPullParser::FindAttribute(
- StringPiece namespace_uri, StringPiece name) const {
+ android::StringPiece namespace_uri, android::StringPiece name) const {
const auto end_iter = end_attributes();
const auto iter = std::lower_bound(
begin_attributes(), end_iter,
- std::pair<StringPiece, StringPiece>(namespace_uri, name),
+ std::pair<android::StringPiece, android::StringPiece>(namespace_uri, name),
[](const Attribute& attr,
- const std::pair<StringPiece, StringPiece>& rhs) -> bool {
+ const std::pair<android::StringPiece, android::StringPiece>& rhs) -> bool {
int cmp = attr.namespace_uri.compare(
0, attr.namespace_uri.size(), rhs.first.data(), rhs.first.size());
if (cmp < 0) return true;
diff --git a/tools/aapt2/xml/XmlPullParser_test.cpp b/tools/aapt2/xml/XmlPullParser_test.cpp
index 4f18cd2..1cce485 100644
--- a/tools/aapt2/xml/XmlPullParser_test.cpp
+++ b/tools/aapt2/xml/XmlPullParser_test.cpp
@@ -18,8 +18,11 @@
#include <sstream>
+#include "androidfw/StringPiece.h"
+
#include "test/Test.h"
-#include "util/StringPiece.h"
+
+using android::StringPiece;
namespace aapt {
diff --git a/tools/aapt2/xml/XmlUtil.cpp b/tools/aapt2/xml/XmlUtil.cpp
index d00f7f2..fb8cee8 100644
--- a/tools/aapt2/xml/XmlUtil.cpp
+++ b/tools/aapt2/xml/XmlUtil.cpp
@@ -21,6 +21,8 @@
#include "util/Maybe.h"
#include "util/Util.h"
+using android::StringPiece;
+
namespace aapt {
namespace xml {
@@ -42,7 +44,7 @@
if (package.empty()) {
return {};
}
- return ExtractedPackage{package.ToString(), false /* is_private */};
+ return ExtractedPackage{package.to_string(), false /* is_private */};
} else if (util::StartsWith(namespace_uri, kSchemaPrivatePrefix)) {
StringPiece schema_prefix = kSchemaPrivatePrefix;
@@ -52,7 +54,7 @@
if (package.empty()) {
return {};
}
- return ExtractedPackage{package.ToString(), true /* is_private */};
+ return ExtractedPackage{package.to_string(), true /* is_private */};
} else if (namespace_uri == kSchemaAuto) {
return ExtractedPackage{std::string(), true /* is_private */};
diff --git a/tools/aapt2/xml/XmlUtil.h b/tools/aapt2/xml/XmlUtil.h
index 5365401..1650ac2 100644
--- a/tools/aapt2/xml/XmlUtil.h
+++ b/tools/aapt2/xml/XmlUtil.h
@@ -74,7 +74,7 @@
*
* http://schemas.android.com/apk/prv/res/<package>
*/
-std::string BuildPackageNamespace(const StringPiece& package,
+std::string BuildPackageNamespace(const android::StringPiece& package,
bool private_reference = false);
/**
@@ -90,7 +90,7 @@
* package declaration.
*/
virtual Maybe<ExtractedPackage> TransformPackageAlias(
- const StringPiece& alias, const StringPiece& local_package) const = 0;
+ const android::StringPiece& alias, const android::StringPiece& local_package) const = 0;
};
/**
@@ -100,8 +100,7 @@
* the namespace of the package declaration was private.
*/
void TransformReferenceFromNamespace(IPackageDeclStack* decl_stack,
- const StringPiece& local_package,
- Reference* in_ref);
+ const android::StringPiece& local_package, Reference* in_ref);
} // namespace xml
} // namespace aapt