Make StringPiece to be std::string_view alias
Bug: 237583012
Test: build + boot + UTs
Change-Id: I849831f4466d3b9c7ec842b75256e7fcba77a0c0
diff --git a/tools/aapt2/util/Files.cpp b/tools/aapt2/util/Files.cpp
index 5d5b7cd..93c1b61 100644
--- a/tools/aapt2/util/Files.cpp
+++ b/tools/aapt2/util/Files.cpp
@@ -139,7 +139,7 @@
return ::android::base::utf8::mkdir(path.c_str(), mode) == 0 || errno == EEXIST;
}
-StringPiece GetStem(const StringPiece& path) {
+StringPiece GetStem(StringPiece path) {
const char* start = path.begin();
const char* end = path.end();
for (const char* current = end - 1; current != start - 1; --current) {
@@ -150,7 +150,7 @@
return {};
}
-StringPiece GetFilename(const StringPiece& path) {
+StringPiece GetFilename(StringPiece path) {
const char* end = path.end();
const char* last_dir_sep = path.begin();
for (const char* c = path.begin(); c != end; ++c) {
@@ -161,7 +161,7 @@
return StringPiece(last_dir_sep, end - last_dir_sep);
}
-StringPiece GetExtension(const StringPiece& path) {
+StringPiece GetExtension(StringPiece path) {
StringPiece filename = GetFilename(path);
const char* const end = filename.end();
const char* c = std::find(filename.begin(), end, '.');
@@ -171,7 +171,7 @@
return {};
}
-bool IsHidden(const android::StringPiece& path) {
+bool IsHidden(android::StringPiece path) {
return util::StartsWith(GetFilename(path), ".");
}
@@ -193,16 +193,16 @@
if (args.empty()) {
return "";
}
- std::string out = args[0].to_string();
+ std::string out{args[0]};
for (int i = 1; i < args.size(); i++) {
file::AppendPath(&out, args[i]);
}
return out;
}
-std::string PackageToPath(const StringPiece& package) {
+std::string PackageToPath(StringPiece package) {
std::string out_path;
- for (const StringPiece& part : util::Tokenize(package, '.')) {
+ for (StringPiece part : util::Tokenize(package, '.')) {
AppendPath(&out_path, part);
}
return out_path;
@@ -241,10 +241,10 @@
return std::move(filemap);
}
-bool AppendArgsFromFile(const StringPiece& path, std::vector<std::string>* out_arglist,
+bool AppendArgsFromFile(StringPiece path, std::vector<std::string>* out_arglist,
std::string* out_error) {
std::string contents;
- if (!ReadFileToString(path.to_string(), &contents, true /*follow_symlinks*/)) {
+ if (!ReadFileToString(std::string(path), &contents, true /*follow_symlinks*/)) {
if (out_error) {
*out_error = "failed to read argument-list file";
}
@@ -254,16 +254,16 @@
for (StringPiece line : util::Tokenize(contents, ' ')) {
line = util::TrimWhitespace(line);
if (!line.empty()) {
- out_arglist->push_back(line.to_string());
+ out_arglist->emplace_back(line);
}
}
return true;
}
-bool AppendSetArgsFromFile(const StringPiece& path, std::unordered_set<std::string>* out_argset,
+bool AppendSetArgsFromFile(StringPiece path, std::unordered_set<std::string>* out_argset,
std::string* out_error) {
std::string contents;
- if(!ReadFileToString(path.to_string(), &contents, true /*follow_symlinks*/)) {
+ if (!ReadFileToString(std::string(path), &contents, true /*follow_symlinks*/)) {
if (out_error) {
*out_error = "failed to read argument-list file";
}
@@ -273,13 +273,13 @@
for (StringPiece line : util::Tokenize(contents, ' ')) {
line = util::TrimWhitespace(line);
if (!line.empty()) {
- out_argset->insert(line.to_string());
+ out_argset->emplace(line);
}
}
return true;
}
-bool FileFilter::SetPattern(const StringPiece& pattern) {
+bool FileFilter::SetPattern(StringPiece pattern) {
pattern_tokens_ = util::SplitAndLowercase(pattern, ':');
return true;
}
@@ -343,10 +343,10 @@
return true;
}
-std::optional<std::vector<std::string>> FindFiles(const android::StringPiece& path,
+std::optional<std::vector<std::string>> FindFiles(android::StringPiece path,
android::IDiagnostics* diag,
const FileFilter* filter) {
- const std::string root_dir = path.to_string();
+ const auto& root_dir = path;
std::unique_ptr<DIR, decltype(closedir)*> d(opendir(root_dir.data()), closedir);
if (!d) {
diag->Error(android::DiagMessage() << SystemErrorCodeToString(errno) << ": " << root_dir);
@@ -361,7 +361,7 @@
}
std::string file_name = entry->d_name;
- std::string full_path = root_dir;
+ std::string full_path{root_dir};
AppendPath(&full_path, file_name);
const FileType file_type = GetFileType(full_path);
@@ -380,7 +380,7 @@
// Now process subdirs.
for (const std::string& subdir : subdirs) {
- std::string full_subdir = root_dir;
+ std::string full_subdir{root_dir};
AppendPath(&full_subdir, subdir);
std::optional<std::vector<std::string>> subfiles = FindFiles(full_subdir, diag, filter);
if (!subfiles) {
diff --git a/tools/aapt2/util/Files.h b/tools/aapt2/util/Files.h
index ee95712..42eeaf2 100644
--- a/tools/aapt2/util/Files.h
+++ b/tools/aapt2/util/Files.h
@@ -66,31 +66,31 @@
bool mkdirs(const std::string& path);
// Returns all but the last part of the path.
-android::StringPiece GetStem(const android::StringPiece& path);
+android::StringPiece GetStem(android::StringPiece path);
// Returns the last part of the path with extension.
-android::StringPiece GetFilename(const android::StringPiece& path);
+android::StringPiece GetFilename(android::StringPiece path);
// Returns the extension of the path. This is the entire string after the first '.' of the last part
// of the path.
-android::StringPiece GetExtension(const android::StringPiece& path);
+android::StringPiece GetExtension(android::StringPiece path);
// Returns whether or not the name of the file or directory is a hidden file name
-bool IsHidden(const android::StringPiece& path);
+bool IsHidden(android::StringPiece path);
// Converts a package name (com.android.app) to a path: com/android/app
-std::string PackageToPath(const android::StringPiece& package);
+std::string PackageToPath(android::StringPiece package);
// Creates a FileMap for the file at path.
std::optional<android::FileMap> MmapPath(const std::string& path, std::string* out_error);
// Reads the file at path and appends each line to the outArgList vector.
-bool AppendArgsFromFile(const android::StringPiece& path, std::vector<std::string>* out_arglist,
+bool AppendArgsFromFile(android::StringPiece path, std::vector<std::string>* out_arglist,
std::string* out_error);
// Reads the file at path and appends each line to the outargset set.
-bool AppendSetArgsFromFile(const android::StringPiece& path,
- std::unordered_set<std::string>* out_argset, std::string* out_error);
+bool AppendSetArgsFromFile(android::StringPiece path, std::unordered_set<std::string>* out_argset,
+ std::string* out_error);
// Filter that determines which resource files/directories are
// processed by AAPT. Takes a pattern string supplied by the user.
@@ -112,7 +112,7 @@
// - The special filenames "." and ".." are always ignored.
// - Otherwise the full string is matched.
// - match is not case-sensitive.
- bool SetPattern(const android::StringPiece& pattern);
+ bool SetPattern(android::StringPiece pattern);
// Applies the filter, returning true for pass, false for fail.
bool operator()(const std::string& filename, FileType type) const;
@@ -126,7 +126,7 @@
// Returns a list of files relative to the directory identified by `path`.
// An optional FileFilter filters out any files that don't pass.
-std::optional<std::vector<std::string>> FindFiles(const android::StringPiece& path,
+std::optional<std::vector<std::string>> FindFiles(android::StringPiece path,
android::IDiagnostics* diag,
const FileFilter* filter = nullptr);
diff --git a/tools/aapt2/util/Util.cpp b/tools/aapt2/util/Util.cpp
index 9b7ebdd..be87766 100644
--- a/tools/aapt2/util/Util.cpp
+++ b/tools/aapt2/util/Util.cpp
@@ -43,15 +43,14 @@
// See frameworks/base/core/java/android/content/pm/parsing/ParsingPackageUtils.java
constexpr static const size_t kMaxPackageNameSize = 223;
-static std::vector<std::string> SplitAndTransform(
- const StringPiece& str, char sep, const std::function<char(char)>& f) {
+static std::vector<std::string> SplitAndTransform(StringPiece str, char sep, char (*f)(char)) {
std::vector<std::string> parts;
const StringPiece::const_iterator end = std::end(str);
StringPiece::const_iterator start = std::begin(str);
StringPiece::const_iterator current;
do {
current = std::find(start, end, sep);
- parts.emplace_back(str.substr(start, current).to_string());
+ parts.emplace_back(start, current);
if (f) {
std::string& part = parts.back();
std::transform(part.begin(), part.end(), part.begin(), f);
@@ -61,29 +60,29 @@
return parts;
}
-std::vector<std::string> Split(const StringPiece& str, char sep) {
+std::vector<std::string> Split(StringPiece str, char sep) {
return SplitAndTransform(str, sep, nullptr);
}
-std::vector<std::string> SplitAndLowercase(const StringPiece& str, char sep) {
- return SplitAndTransform(str, sep, ::tolower);
+std::vector<std::string> SplitAndLowercase(StringPiece str, char sep) {
+ return SplitAndTransform(str, sep, [](char c) -> char { return ::tolower(c); });
}
-bool StartsWith(const StringPiece& str, const StringPiece& prefix) {
+bool StartsWith(StringPiece str, StringPiece prefix) {
if (str.size() < prefix.size()) {
return false;
}
return str.substr(0, prefix.size()) == prefix;
}
-bool EndsWith(const StringPiece& str, const StringPiece& suffix) {
+bool EndsWith(StringPiece str, StringPiece suffix) {
if (str.size() < suffix.size()) {
return false;
}
return str.substr(str.size() - suffix.size(), suffix.size()) == suffix;
}
-StringPiece TrimLeadingWhitespace(const StringPiece& str) {
+StringPiece TrimLeadingWhitespace(StringPiece str) {
if (str.size() == 0 || str.data() == nullptr) {
return str;
}
@@ -97,7 +96,7 @@
return StringPiece(start, end - start);
}
-StringPiece TrimTrailingWhitespace(const StringPiece& str) {
+StringPiece TrimTrailingWhitespace(StringPiece str) {
if (str.size() == 0 || str.data() == nullptr) {
return str;
}
@@ -111,7 +110,7 @@
return StringPiece(start, end - start);
}
-StringPiece TrimWhitespace(const StringPiece& str) {
+StringPiece TrimWhitespace(StringPiece str) {
if (str.size() == 0 || str.data() == nullptr) {
return str;
}
@@ -130,9 +129,9 @@
return StringPiece(start, end - start);
}
-static int IsJavaNameImpl(const StringPiece& str) {
+static int IsJavaNameImpl(StringPiece str) {
int pieces = 0;
- for (const StringPiece& piece : Tokenize(str, '.')) {
+ for (StringPiece piece : Tokenize(str, '.')) {
pieces++;
if (!text::IsJavaIdentifier(piece)) {
return -1;
@@ -141,17 +140,17 @@
return pieces;
}
-bool IsJavaClassName(const StringPiece& str) {
+bool IsJavaClassName(StringPiece str) {
return IsJavaNameImpl(str) >= 2;
}
-bool IsJavaPackageName(const StringPiece& str) {
+bool IsJavaPackageName(StringPiece str) {
return IsJavaNameImpl(str) >= 1;
}
-static int IsAndroidNameImpl(const StringPiece& str) {
+static int IsAndroidNameImpl(StringPiece str) {
int pieces = 0;
- for (const StringPiece& piece : Tokenize(str, '.')) {
+ for (StringPiece piece : Tokenize(str, '.')) {
if (piece.empty()) {
return -1;
}
@@ -173,15 +172,14 @@
return pieces;
}
-bool IsAndroidPackageName(const StringPiece& str) {
+bool IsAndroidPackageName(StringPiece str) {
if (str.size() > kMaxPackageNameSize) {
return false;
}
return IsAndroidNameImpl(str) > 1 || str == "android";
}
-bool IsAndroidSharedUserId(const android::StringPiece& package_name,
- const android::StringPiece& shared_user_id) {
+bool IsAndroidSharedUserId(android::StringPiece package_name, android::StringPiece shared_user_id) {
if (shared_user_id.size() > kMaxPackageNameSize) {
return false;
}
@@ -189,25 +187,24 @@
package_name == "android";
}
-bool IsAndroidSplitName(const StringPiece& str) {
+bool IsAndroidSplitName(StringPiece str) {
return IsAndroidNameImpl(str) > 0;
}
-std::optional<std::string> GetFullyQualifiedClassName(const StringPiece& package,
- const StringPiece& classname) {
+std::optional<std::string> GetFullyQualifiedClassName(StringPiece package, StringPiece classname) {
if (classname.empty()) {
return {};
}
if (util::IsJavaClassName(classname)) {
- return classname.to_string();
+ return std::string(classname);
}
if (package.empty()) {
return {};
}
- std::string result = package.to_string();
+ std::string result{package};
if (classname.data()[0] != '.') {
result += '.';
}
@@ -251,7 +248,7 @@
return static_cast<size_t>(c - start);
}
-bool VerifyJavaStringFormat(const StringPiece& str) {
+bool VerifyJavaStringFormat(StringPiece str) {
const char* c = str.begin();
const char* const end = str.end();
@@ -341,7 +338,7 @@
return true;
}
-std::u16string Utf8ToUtf16(const StringPiece& utf8) {
+std::u16string Utf8ToUtf16(StringPiece utf8) {
ssize_t utf16_length = utf8_to_utf16_length(
reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
if (utf16_length <= 0) {
@@ -381,7 +378,7 @@
const char* end = str_.end();
if (start == end) {
end_ = true;
- token_.assign(token_.end(), 0);
+ token_ = StringPiece(token_.end(), 0);
return *this;
}
@@ -389,12 +386,12 @@
const char* current = start;
while (current != end) {
if (*current == separator_) {
- token_.assign(start, current - start);
+ token_ = StringPiece(start, current - start);
return *this;
}
++current;
}
- token_.assign(start, end - start);
+ token_ = StringPiece(start, end - start);
return *this;
}
@@ -409,15 +406,17 @@
return !(*this == rhs);
}
-Tokenizer::iterator::iterator(const StringPiece& s, char sep, const StringPiece& tok, bool end)
- : str_(s), separator_(sep), token_(tok), end_(end) {}
+Tokenizer::iterator::iterator(StringPiece s, char sep, StringPiece tok, bool end)
+ : str_(s), separator_(sep), token_(tok), end_(end) {
+}
-Tokenizer::Tokenizer(const StringPiece& str, char sep)
+Tokenizer::Tokenizer(StringPiece str, char sep)
: begin_(++iterator(str, sep, StringPiece(str.begin() - 1, 0), false)),
- end_(str, sep, StringPiece(str.end(), 0), true) {}
+ end_(str, sep, StringPiece(str.end(), 0), true) {
+}
-bool ExtractResFilePathParts(const StringPiece& path, StringPiece* out_prefix,
- StringPiece* out_entry, StringPiece* out_suffix) {
+bool ExtractResFilePathParts(StringPiece path, StringPiece* out_prefix, StringPiece* out_entry,
+ StringPiece* out_suffix) {
const StringPiece res_prefix("res/");
if (!StartsWith(path, res_prefix)) {
return false;
diff --git a/tools/aapt2/util/Util.h b/tools/aapt2/util/Util.h
index 8d3b413..40ff5b6 100644
--- a/tools/aapt2/util/Util.h
+++ b/tools/aapt2/util/Util.h
@@ -48,44 +48,44 @@
T end;
};
-std::vector<std::string> Split(const android::StringPiece& str, char sep);
-std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep);
+std::vector<std::string> Split(android::StringPiece str, char sep);
+std::vector<std::string> SplitAndLowercase(android::StringPiece str, char sep);
// Returns true if the string starts with prefix.
-bool StartsWith(const android::StringPiece& str, const android::StringPiece& prefix);
+bool StartsWith(android::StringPiece str, android::StringPiece prefix);
// Returns true if the string ends with suffix.
-bool EndsWith(const android::StringPiece& str, const android::StringPiece& suffix);
+bool EndsWith(android::StringPiece str, android::StringPiece suffix);
// Creates a new StringPiece that points to a substring of the original string without leading
// whitespace.
-android::StringPiece TrimLeadingWhitespace(const android::StringPiece& str);
+android::StringPiece TrimLeadingWhitespace(android::StringPiece str);
// Creates a new StringPiece that points to a substring of the original string without trailing
// whitespace.
-android::StringPiece TrimTrailingWhitespace(const android::StringPiece& str);
+android::StringPiece TrimTrailingWhitespace(android::StringPiece str);
// Creates a new StringPiece that points to a substring of the original string without leading or
// trailing whitespace.
-android::StringPiece TrimWhitespace(const android::StringPiece& str);
+android::StringPiece TrimWhitespace(android::StringPiece str);
// Tests that the string is a valid Java class name.
-bool IsJavaClassName(const android::StringPiece& str);
+bool IsJavaClassName(android::StringPiece str);
// Tests that the string is a valid Java package name.
-bool IsJavaPackageName(const android::StringPiece& str);
+bool IsJavaPackageName(android::StringPiece str);
// Tests that the string is a valid Android package name. More strict than a Java package name.
// - First character of each component (separated by '.') must be an ASCII letter.
// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
// - Package must contain at least two components, unless it is 'android'.
// - The maximum package name length is 223.
-bool IsAndroidPackageName(const android::StringPiece& str);
+bool IsAndroidPackageName(android::StringPiece str);
// Tests that the string is a valid Android split name.
// - First character of each component (separated by '.') must be an ASCII letter.
// - Subsequent characters of a component can be ASCII alphanumeric or an underscore.
-bool IsAndroidSplitName(const android::StringPiece& str);
+bool IsAndroidSplitName(android::StringPiece str);
// Tests that the string is a valid Android shared user id.
// - First character of each component (separated by '.') must be an ASCII letter.
@@ -93,8 +93,7 @@
// - Must contain at least two components, unless package name is 'android'.
// - The maximum shared user id length is 223.
// - Treat empty string as valid, it's the case of no shared user id.
-bool IsAndroidSharedUserId(const android::StringPiece& package_name,
- const android::StringPiece& shared_user_id);
+bool IsAndroidSharedUserId(android::StringPiece package_name, android::StringPiece shared_user_id);
// Converts the class name to a fully qualified class name from the given
// `package`. Ex:
@@ -103,8 +102,8 @@
// .asdf --> package.asdf
// .a.b --> package.a.b
// asdf.adsf --> asdf.adsf
-std::optional<std::string> GetFullyQualifiedClassName(const android::StringPiece& package,
- const android::StringPiece& class_name);
+std::optional<std::string> GetFullyQualifiedClassName(android::StringPiece package,
+ android::StringPiece class_name);
// Retrieves the formatted name of aapt2.
const char* GetToolName();
@@ -152,16 +151,16 @@
// explicitly specifying an index) when there are more than one argument. This is an error
// because translations may rearrange the order of the arguments in the string, which will
// break the string interpolation.
-bool VerifyJavaStringFormat(const android::StringPiece& str);
+bool VerifyJavaStringFormat(android::StringPiece str);
-bool AppendStyledString(const android::StringPiece& input, bool preserve_spaces,
- std::string* out_str, std::string* out_error);
+bool AppendStyledString(android::StringPiece input, bool preserve_spaces, std::string* out_str,
+ std::string* out_error);
class StringBuilder {
public:
StringBuilder() = default;
- StringBuilder& Append(const android::StringPiece& str);
+ StringBuilder& Append(android::StringPiece str);
const std::string& ToString() const;
const std::string& Error() const;
bool IsEmpty() const;
@@ -229,7 +228,7 @@
private:
friend class Tokenizer;
- iterator(const android::StringPiece& s, char sep, const android::StringPiece& tok, bool end);
+ iterator(android::StringPiece s, char sep, android::StringPiece tok, bool end);
android::StringPiece str_;
char separator_;
@@ -237,7 +236,7 @@
bool end_;
};
- Tokenizer(const android::StringPiece& str, char sep);
+ Tokenizer(android::StringPiece str, char sep);
iterator begin() const {
return begin_;
@@ -252,7 +251,7 @@
const iterator end_;
};
-inline Tokenizer Tokenize(const android::StringPiece& str, char sep) {
+inline Tokenizer Tokenize(android::StringPiece str, char sep) {
return Tokenizer(str, sep);
}
@@ -263,7 +262,7 @@
// Extracts ".xml" into outSuffix.
//
// Returns true if successful.
-bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
+bool ExtractResFilePathParts(android::StringPiece path, android::StringPiece* out_prefix,
android::StringPiece* out_entry, android::StringPiece* out_suffix);
} // namespace util
diff --git a/tools/aapt2/util/Util_test.cpp b/tools/aapt2/util/Util_test.cpp
index 4ebcb11..15135690 100644
--- a/tools/aapt2/util/Util_test.cpp
+++ b/tools/aapt2/util/Util_test.cpp
@@ -84,6 +84,14 @@
ASSERT_THAT(*iter, Eq(StringPiece()));
}
+TEST(UtilTest, TokenizeNone) {
+ auto tokenizer = util::Tokenize(StringPiece("none"), '.');
+ auto iter = tokenizer.begin();
+ ASSERT_THAT(*iter, Eq("none"));
+ ++iter;
+ ASSERT_THAT(iter, Eq(tokenizer.end()));
+}
+
TEST(UtilTest, IsJavaClassName) {
EXPECT_TRUE(util::IsJavaClassName("android.test.Class"));
EXPECT_TRUE(util::IsJavaClassName("android.test.Class$Inner"));