Make StringPiece to be std::string_view alias
Bug: 237583012
Test: build + boot + UTs
Change-Id: I849831f4466d3b9c7ec842b75256e7fcba77a0c0
diff --git a/tools/aapt2/xml/XmlActionExecutor.cpp b/tools/aapt2/xml/XmlActionExecutor.cpp
index 9bdbd22..3ccbaa2 100644
--- a/tools/aapt2/xml/XmlActionExecutor.cpp
+++ b/tools/aapt2/xml/XmlActionExecutor.cpp
@@ -84,7 +84,7 @@
error_msg << "unexpected element ";
PrintElementToDiagMessage(child_el, &error_msg);
error_msg << " found in ";
- for (const StringPiece& element : *bread_crumb) {
+ for (StringPiece element : *bread_crumb) {
error_msg << "<" << element << ">";
}
if (policy == XmlActionExecutorPolicy::kAllowListWarning) {
diff --git a/tools/aapt2/xml/XmlDom.cpp b/tools/aapt2/xml/XmlDom.cpp
index f51e8a4..8dea8ea 100644
--- a/tools/aapt2/xml/XmlDom.cpp
+++ b/tools/aapt2/xml/XmlDom.cpp
@@ -169,7 +169,7 @@
stack->last_text_node = util::make_unique<Text>();
stack->last_text_node->line_number = XML_GetCurrentLineNumber(parser);
stack->last_text_node->column_number = XML_GetCurrentColumnNumber(parser);
- stack->last_text_node->text = str.to_string();
+ stack->last_text_node->text.assign(str);
}
static void XMLCALL CommentDataHandler(void* user_data, const char* comment) {
@@ -417,11 +417,11 @@
children.insert(children.begin() + index, std::move(child));
}
-Attribute* Element::FindAttribute(const StringPiece& ns, const StringPiece& name) {
+Attribute* Element::FindAttribute(StringPiece ns, StringPiece name) {
return const_cast<Attribute*>(static_cast<const Element*>(this)->FindAttribute(ns, name));
}
-const Attribute* Element::FindAttribute(const StringPiece& ns, const StringPiece& name) const {
+const Attribute* Element::FindAttribute(StringPiece ns, StringPiece name) const {
for (const auto& attr : attributes) {
if (ns == attr.namespace_uri && name == attr.name) {
return &attr;
@@ -430,7 +430,7 @@
return nullptr;
}
-void Element::RemoveAttribute(const StringPiece& ns, const StringPiece& name) {
+void Element::RemoveAttribute(StringPiece ns, StringPiece name) {
auto new_attr_end = std::remove_if(attributes.begin(), attributes.end(),
[&](const Attribute& attr) -> bool {
return ns == attr.namespace_uri && name == attr.name;
@@ -439,34 +439,32 @@
attributes.erase(new_attr_end, attributes.end());
}
-Attribute* Element::FindOrCreateAttribute(const StringPiece& ns, const StringPiece& name) {
+Attribute* Element::FindOrCreateAttribute(StringPiece ns, StringPiece name) {
Attribute* attr = FindAttribute(ns, name);
if (attr == nullptr) {
- attributes.push_back(Attribute{ns.to_string(), name.to_string()});
+ attributes.push_back(Attribute{std::string(ns), std::string(name)});
attr = &attributes.back();
}
return attr;
}
-Element* Element::FindChild(const StringPiece& ns, const StringPiece& name) {
+Element* Element::FindChild(StringPiece ns, StringPiece name) {
return FindChildWithAttribute(ns, name, {}, {}, {});
}
-const Element* Element::FindChild(const StringPiece& ns, const StringPiece& name) const {
+const Element* Element::FindChild(StringPiece ns, StringPiece name) const {
return FindChildWithAttribute(ns, name, {}, {}, {});
}
-Element* Element::FindChildWithAttribute(const StringPiece& ns, const StringPiece& name,
- const StringPiece& attr_ns, const StringPiece& attr_name,
- const StringPiece& attr_value) {
+Element* Element::FindChildWithAttribute(StringPiece ns, StringPiece name, StringPiece attr_ns,
+ StringPiece attr_name, StringPiece attr_value) {
return const_cast<Element*>(static_cast<const Element*>(this)->FindChildWithAttribute(
ns, name, attr_ns, attr_name, attr_value));
}
-const Element* Element::FindChildWithAttribute(const StringPiece& ns, const StringPiece& name,
- const StringPiece& attr_ns,
- const StringPiece& attr_name,
- const StringPiece& attr_value) const {
+const Element* Element::FindChildWithAttribute(StringPiece ns, StringPiece name,
+ StringPiece attr_ns, StringPiece attr_name,
+ StringPiece attr_value) const {
for (const auto& child : children) {
if (const Element* el = NodeCast<Element>(child.get())) {
if (ns == el->namespace_uri && name == el->name) {
@@ -559,7 +557,7 @@
}
std::optional<ExtractedPackage> PackageAwareVisitor::TransformPackageAlias(
- const StringPiece& alias) const {
+ StringPiece alias) const {
if (alias.empty()) {
return ExtractedPackage{{}, false /*private*/};
}
diff --git a/tools/aapt2/xml/XmlDom.h b/tools/aapt2/xml/XmlDom.h
index 5bc55b6..c253b0a 100644
--- a/tools/aapt2/xml/XmlDom.h
+++ b/tools/aapt2/xml/XmlDom.h
@@ -96,27 +96,22 @@
void AppendChild(std::unique_ptr<Node> child);
void InsertChild(size_t index, std::unique_ptr<Node> child);
- Attribute* FindAttribute(const android::StringPiece& ns, const android::StringPiece& name);
- const Attribute* FindAttribute(const android::StringPiece& ns,
- const android::StringPiece& name) const;
- Attribute* FindOrCreateAttribute(const android::StringPiece& ns,
- const android::StringPiece& name);
- void RemoveAttribute(const android::StringPiece& ns,
- const android::StringPiece& name);
+ Attribute* FindAttribute(android::StringPiece ns, android::StringPiece name);
+ const Attribute* FindAttribute(android::StringPiece ns, android::StringPiece name) const;
+ Attribute* FindOrCreateAttribute(android::StringPiece ns, android::StringPiece name);
+ void RemoveAttribute(android::StringPiece ns, android::StringPiece name);
- Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name);
- const Element* FindChild(const android::StringPiece& ns, const android::StringPiece& name) const;
+ Element* FindChild(android::StringPiece ns, android::StringPiece name);
+ const Element* FindChild(android::StringPiece ns, android::StringPiece name) const;
- 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);
+ Element* FindChildWithAttribute(android::StringPiece ns, android::StringPiece name,
+ android::StringPiece attr_ns, android::StringPiece attr_name,
+ android::StringPiece attr_value);
- const 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) const;
+ const Element* FindChildWithAttribute(android::StringPiece ns, android::StringPiece name,
+ android::StringPiece attr_ns,
+ android::StringPiece attr_name,
+ android::StringPiece attr_value) const;
std::vector<Element*> GetChildElements();
@@ -235,8 +230,7 @@
public:
using Visitor::Visit;
- std::optional<ExtractedPackage> TransformPackageAlias(
- const android::StringPiece& alias) const override;
+ std::optional<ExtractedPackage> TransformPackageAlias(android::StringPiece alias) const override;
protected:
PackageAwareVisitor() = default;
diff --git a/tools/aapt2/xml/XmlPullParser.cpp b/tools/aapt2/xml/XmlPullParser.cpp
index bfa0749..d79446b 100644
--- a/tools/aapt2/xml/XmlPullParser.cpp
+++ b/tools/aapt2/xml/XmlPullParser.cpp
@@ -140,8 +140,7 @@
return event_queue_.front().data2;
}
-std::optional<ExtractedPackage> XmlPullParser::TransformPackageAlias(
- const StringPiece& alias) const {
+std::optional<ExtractedPackage> XmlPullParser::TransformPackageAlias(StringPiece alias) const {
if (alias.empty()) {
return ExtractedPackage{{}, false /*private*/};
}
@@ -307,7 +306,7 @@
parser->depth_ });
}
-std::optional<StringPiece> FindAttribute(const XmlPullParser* parser, const StringPiece& name) {
+std::optional<StringPiece> FindAttribute(const XmlPullParser* parser, StringPiece name) {
auto iter = parser->FindAttribute("", name);
if (iter != parser->end_attributes()) {
return StringPiece(util::TrimWhitespace(iter->value));
@@ -315,8 +314,7 @@
return {};
}
-std::optional<StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser,
- const StringPiece& name) {
+std::optional<StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser, StringPiece name) {
auto iter = parser->FindAttribute("", name);
if (iter != parser->end_attributes()) {
StringPiece trimmed = util::TrimWhitespace(iter->value);
diff --git a/tools/aapt2/xml/XmlPullParser.h b/tools/aapt2/xml/XmlPullParser.h
index ab34772..fe4cd01 100644
--- a/tools/aapt2/xml/XmlPullParser.h
+++ b/tools/aapt2/xml/XmlPullParser.h
@@ -120,8 +120,7 @@
* If xmlns:app="http://schemas.android.com/apk/res-auto", then
* 'package' will be set to 'defaultPackage'.
*/
- std::optional<ExtractedPackage> TransformPackageAlias(
- const android::StringPiece& alias) const override;
+ std::optional<ExtractedPackage> TransformPackageAlias(android::StringPiece alias) const override;
struct PackageDecl {
std::string prefix;
@@ -194,7 +193,7 @@
* Finds the attribute in the current element within the global namespace.
*/
std::optional<android::StringPiece> FindAttribute(const XmlPullParser* parser,
- const android::StringPiece& name);
+ android::StringPiece name);
/**
* Finds the attribute in the current element within the global namespace. The
@@ -202,7 +201,7 @@
* must not be the empty string.
*/
std::optional<android::StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser,
- const android::StringPiece& name);
+ android::StringPiece name);
//
// Implementation
diff --git a/tools/aapt2/xml/XmlUtil.cpp b/tools/aapt2/xml/XmlUtil.cpp
index 114b5ba..709755e 100644
--- a/tools/aapt2/xml/XmlUtil.cpp
+++ b/tools/aapt2/xml/XmlUtil.cpp
@@ -27,7 +27,7 @@
namespace aapt {
namespace xml {
-std::string BuildPackageNamespace(const StringPiece& package, bool private_reference) {
+std::string BuildPackageNamespace(StringPiece package, bool private_reference) {
std::string result = private_reference ? kSchemaPrivatePrefix : kSchemaPublicPrefix;
result.append(package.data(), package.size());
return result;
@@ -41,7 +41,7 @@
if (package.empty()) {
return {};
}
- return ExtractedPackage{package.to_string(), false /* is_private */};
+ return ExtractedPackage{std::string(package), false /* is_private */};
} else if (util::StartsWith(namespace_uri, kSchemaPrivatePrefix)) {
StringPiece schema_prefix = kSchemaPrivatePrefix;
@@ -50,7 +50,7 @@
if (package.empty()) {
return {};
}
- return ExtractedPackage{package.to_string(), true /* is_private */};
+ return ExtractedPackage{std::string(package), 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 1ab05a9..ad676ca 100644
--- a/tools/aapt2/xml/XmlUtil.h
+++ b/tools/aapt2/xml/XmlUtil.h
@@ -59,8 +59,7 @@
//
// If privateReference == true, the package will be of the form:
// http://schemas.android.com/apk/prv/res/<package>
-std::string BuildPackageNamespace(const android::StringPiece& package,
- bool private_reference = false);
+std::string BuildPackageNamespace(android::StringPiece package, bool private_reference = false);
// Interface representing a stack of XML namespace declarations. When looking up the package for a
// namespace prefix, the stack is checked from top to bottom.
@@ -69,7 +68,7 @@
// Returns an ExtractedPackage struct if the alias given corresponds with a package declaration.
virtual std::optional<ExtractedPackage> TransformPackageAlias(
- const android::StringPiece& alias) const = 0;
+ android::StringPiece alias) const = 0;
};
// Helper function for transforming the original Reference inRef to a fully qualified reference