Skip to content

Commit

Permalink
Use C++20 string{,_view}::{starts,ends}_with(), part 2.
Browse files Browse the repository at this point in the history
Replace uses of `android::base::{Starts,Ends}With()`.

Test: m test-art-host-gtest
Test: testrunner.py --host --optimizing
Change-Id: I80a0ca93f433464270989d248dd999e9366a1c17
  • Loading branch information
vmarko committed Jun 14, 2024
1 parent 9132d90 commit 2b4eb67
Show file tree
Hide file tree
Showing 26 changed files with 101 additions and 108 deletions.
3 changes: 1 addition & 2 deletions artd/artd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ using ::android::base::ParseInt;
using ::android::base::ReadFileToString;
using ::android::base::Result;
using ::android::base::Split;
using ::android::base::StartsWith;
using ::android::base::Tokenize;
using ::android::base::Trim;
using ::android::base::WriteStringToFd;
Expand Down Expand Up @@ -1939,7 +1938,7 @@ Result<BuildSystemProperties> BuildSystemProperties::Create(const std::string& f
std::unordered_map<std::string, std::string> system_properties;
for (const std::string& raw_line : Split(content, "\n")) {
std::string line = Trim(raw_line);
if (line.empty() || StartsWith(line, '#')) {
if (line.empty() || line.starts_with('#')) {
continue;
}
size_t pos = line.find('=');
Expand Down
4 changes: 1 addition & 3 deletions artd/path_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "aidl/com/android/server/art/BnArtd.h"
#include "android-base/errors.h"
#include "android-base/result.h"
#include "android-base/strings.h"
#include "arch/instruction_set.h"
#include "base/file_utils.h"
#include "base/macros.h"
Expand All @@ -45,7 +44,6 @@ using ::aidl::com::android::server::art::OutputProfile;
using ::aidl::com::android::server::art::ProfilePath;
using ::aidl::com::android::server::art::RuntimeArtifactsPath;
using ::aidl::com::android::server::art::VdexPath;
using ::android::base::EndsWith;
using ::android::base::Error;
using ::android::base::Result;
using ::art::service::ValidateDexPath;
Expand Down Expand Up @@ -335,7 +333,7 @@ bool PreRebootFlag(const VdexPath& vdex_path) {
}

bool IsPreRebootStagedFile(std::string_view filename) {
return EndsWith(filename, kPreRebootSuffix);
return filename.ends_with(kPreRebootSuffix);
}

void TestOnlySetListRootDir(std::string_view root_dir) { gListRootDir = root_dir; }
Expand Down
20 changes: 10 additions & 10 deletions cmdline/cmdline_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -813,58 +813,58 @@ struct CmdlineType<ProfileSaverOptions> : CmdlineTypeParser<ProfileSaverOptions>
// The rest of these options are always the wildcard from '-Xps-*'
std::string suffix = RemovePrefix(option);

if (android::base::StartsWith(option, "min-save-period-ms:")) {
if (option.starts_with("min-save-period-ms:")) {
CmdlineType<unsigned int> type_parser;
return ParseInto(existing,
&ProfileSaverOptions::min_save_period_ms_,
type_parser.Parse(suffix));
}
if (android::base::StartsWith(option, "min-first-save-ms:")) {
if (option.starts_with("min-first-save-ms:")) {
CmdlineType<unsigned int> type_parser;
return ParseInto(existing,
&ProfileSaverOptions::min_first_save_ms_,
type_parser.Parse(suffix));
}
if (android::base::StartsWith(option, "save-resolved-classes-delay-ms:")) {
if (option.starts_with("save-resolved-classes-delay-ms:")) {
CmdlineType<unsigned int> type_parser;
return ParseInto(existing,
&ProfileSaverOptions::save_resolved_classes_delay_ms_,
type_parser.Parse(suffix));
}
if (android::base::StartsWith(option, "hot-startup-method-samples:")) {
if (option.starts_with("hot-startup-method-samples:")) {
LOG(WARNING) << "-Xps-hot-startup-method-samples option is deprecated";
return Result::SuccessNoValue();
}
if (android::base::StartsWith(option, "min-methods-to-save:")) {
if (option.starts_with("min-methods-to-save:")) {
CmdlineType<unsigned int> type_parser;
return ParseInto(existing,
&ProfileSaverOptions::min_methods_to_save_,
type_parser.Parse(suffix));
}
if (android::base::StartsWith(option, "min-classes-to-save:")) {
if (option.starts_with("min-classes-to-save:")) {
CmdlineType<unsigned int> type_parser;
return ParseInto(existing,
&ProfileSaverOptions::min_classes_to_save_,
type_parser.Parse(suffix));
}
if (android::base::StartsWith(option, "min-notification-before-wake:")) {
if (option.starts_with("min-notification-before-wake:")) {
CmdlineType<unsigned int> type_parser;
return ParseInto(existing,
&ProfileSaverOptions::min_notification_before_wake_,
type_parser.Parse(suffix));
}
if (android::base::StartsWith(option, "max-notification-before-wake:")) {
if (option.starts_with("max-notification-before-wake:")) {
CmdlineType<unsigned int> type_parser;
return ParseInto(existing,
&ProfileSaverOptions::max_notification_before_wake_,
type_parser.Parse(suffix));
}
if (android::base::StartsWith(option, "inline-cache-threshold:")) {
if (option.starts_with("inline-cache-threshold:")) {
CmdlineType<uint16_t> type_parser;
return ParseInto(
existing, &ProfileSaverOptions::inline_cache_threshold_, type_parser.Parse(suffix));
}
if (android::base::StartsWith(option, "profile-path:")) {
if (option.starts_with("profile-path:")) {
existing.profile_path_ = suffix;
return Result::SuccessNoValue();
}
Expand Down
36 changes: 19 additions & 17 deletions dex2oat/dex2oat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,34 +153,36 @@ static std::string StrippedCommandLine() {
bool saw_zip_fd = false;
bool saw_compiler_filter = false;
for (int i = 0; i < original_argc; ++i) {
if (android::base::StartsWith(original_argv[i], "--zip-fd=")) {
std::string_view arg(original_argv[i]);
if (arg.starts_with("--zip-fd=")) {
saw_zip_fd = true;
}
if (android::base::StartsWith(original_argv[i], "--compiler-filter=")) {
if (arg.starts_with("--compiler-filter=")) {
saw_compiler_filter = true;
}
}

// Now filter out things.
for (int i = 0; i < original_argc; ++i) {
std::string_view arg(original_argv[i]);
// All runtime-arg parameters are dropped.
if (strcmp(original_argv[i], "--runtime-arg") == 0) {
if (arg == "--runtime-arg") {
i++; // Drop the next part, too.
continue;
}

// Any instruction-setXXX is dropped.
if (android::base::StartsWith(original_argv[i], "--instruction-set")) {
if (arg.starts_with("--instruction-set")) {
continue;
}

// The boot image is dropped.
if (android::base::StartsWith(original_argv[i], "--boot-image=")) {
if (arg.starts_with("--boot-image=")) {
continue;
}

// The image format is dropped.
if (android::base::StartsWith(original_argv[i], "--image-format=")) {
if (arg.starts_with("--image-format=")) {
continue;
}

Expand All @@ -189,16 +191,16 @@ static std::string StrippedCommandLine() {
// However, we prefer to drop this when we saw --zip-fd.
if (saw_zip_fd) {
// Drop anything --zip-X, --dex-X, --oat-X, --swap-X, or --app-image-X
if (android::base::StartsWith(original_argv[i], "--zip-") ||
android::base::StartsWith(original_argv[i], "--dex-") ||
android::base::StartsWith(original_argv[i], "--oat-") ||
android::base::StartsWith(original_argv[i], "--swap-") ||
android::base::StartsWith(original_argv[i], "--app-image-")) {
if (arg.starts_with("--zip-") ||
arg.starts_with("--dex-") ||
arg.starts_with("--oat-") ||
arg.starts_with("--swap-") ||
arg.starts_with("--app-image-")) {
continue;
}
}

command.push_back(original_argv[i]);
command.push_back(std::string(arg));
}

if (!saw_compiler_filter) {
Expand Down Expand Up @@ -725,8 +727,8 @@ class Dex2Oat final {

if (!IsBootImage() && boot_image_filename_.empty()) {
DCHECK(!IsBootImageExtension());
if (std::any_of(runtime_args_.begin(), runtime_args_.end(), [](const char* arg) {
return android::base::StartsWith(arg, "-Xbootclasspath:");
if (std::any_of(runtime_args_.begin(), runtime_args_.end(), [](std::string_view arg) {
return arg.starts_with("-Xbootclasspath:");
})) {
LOG(WARNING) << "--boot-image is not specified while -Xbootclasspath is specified. Running "
"dex2oat in imageless mode";
Expand Down Expand Up @@ -1615,7 +1617,7 @@ class Dex2Oat final {
TimingLogger::ScopedTiming t3("Loading image checksum", timings_);
std::string full_bcp = android::base::Join(runtime->GetBootClassPathLocations(), ':');
std::string extension_part = ":" + android::base::Join(dex_locations_, ':');
if (!android::base::EndsWith(full_bcp, extension_part)) {
if (!full_bcp.ends_with(extension_part)) {
LOG(ERROR) << "Full boot class path does not end with extension parts, full: " << full_bcp
<< ", extension: " << extension_part.substr(1u);
return dex2oat::ReturnCode::kOther;
Expand Down Expand Up @@ -1873,7 +1875,7 @@ class Dex2Oat final {
for (const std::string& filter : no_inline_filters) {
// Use dex_file->GetLocation() rather than dex_file->GetBaseLocation(). This
// allows tests to specify <test-dexfile>!classes2.dex if needed but if the
// base location passes the StartsWith() test, so do all extra locations.
// base location passes the `starts_with()` test, so do all extra locations.
std::string dex_location = dex_file->GetLocation();
if (filter.find('/') == std::string::npos) {
// The filter does not contain the path. Remove the path from dex_location as well.
Expand All @@ -1883,7 +1885,7 @@ class Dex2Oat final {
}
}

if (android::base::StartsWith(dex_location, filter)) {
if (dex_location.starts_with(filter)) {
VLOG(compiler) << "Disabling inlining from " << dex_file->GetLocation();
no_inline_from_dex_files.push_back(dex_file);
break;
Expand Down
5 changes: 2 additions & 3 deletions dexopt_chroot_setup/dexopt_chroot_setup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ namespace dexopt_chroot_setup {
namespace {

using ::android::base::ConsumePrefix;
using ::android::base::EndsWith;
using ::android::base::Error;
using ::android::base::Join;
using ::android::base::NoDestructor;
Expand Down Expand Up @@ -210,7 +209,7 @@ Result<void> BindMount(const std::string& source, const std::string& target) {
}

Result<void> BindMountRecursive(const std::string& source, const std::string& target) {
CHECK(!EndsWith(source, '/'));
CHECK(!source.ends_with('/'));
OR_RETURN(BindMount(source, target));

// Mount and make slave one by one. Do not use MS_REC because we don't want to mount a child if
Expand All @@ -222,7 +221,7 @@ Result<void> BindMountRecursive(const std::string& source, const std::string& ta
// The list is in mount order.
std::vector<FstabEntry> entries = OR_RETURN(GetProcMountsDescendantsOfPath(source));
for (const FstabEntry& entry : entries) {
CHECK(!EndsWith(entry.mount_point, '/'));
CHECK(!entry.mount_point.ends_with('/'));
std::string_view sub_dir = entry.mount_point;
CHECK(ConsumePrefix(&sub_dir, source));
if (sub_dir.empty()) {
Expand Down
45 changes: 22 additions & 23 deletions libartbase/base/file_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -622,27 +622,27 @@ void GetDalvikCache(const char* subdir,

// Returns a path formed by encoding the dex location into the filename. The path returned will be
// rooted at `cache_location`.
static bool GetLocationEncodedFilename(const char* location,
const char* cache_location,
static bool GetLocationEncodedFilename(std::string_view location,
std::string_view cache_location,
std::string* filename,
std::string* error_msg) {
if (location[0] != '/') {
*error_msg = StringPrintf("Expected path in location to be absolute: %s", location);
if (!location.starts_with('/')) {
*error_msg = "Expected path in location to be absolute: " + std::string(location);
return false;
}
std::string cache_file(&location[1]); // skip leading slash
if (!android::base::EndsWith(location, ".dex") && !android::base::EndsWith(location, ".art") &&
!android::base::EndsWith(location, ".oat")) {
cache_file += "/";
cache_file += kClassesDex;
*filename = cache_location;
*filename += location; // Including the leading slash.
size_t replace_start = cache_location.length() + /* skip the leading slash from `location` */ 1u;
std::replace(filename->begin() + replace_start, filename->end(), '/', '@');
if (!location.ends_with(".dex") && !location.ends_with(".art") && !location.ends_with(".oat")) {
*filename += "@";
*filename += kClassesDex;
}
std::replace(cache_file.begin(), cache_file.end(), '/', '@');
*filename = StringPrintf("%s/%s", cache_location, cache_file.c_str());
return true;
}

bool GetDalvikCacheFilename(const char* location,
const char* cache_location,
bool GetDalvikCacheFilename(std::string_view location,
std::string_view cache_location,
std::string* filename,
std::string* error_msg) {
return GetLocationEncodedFilename(location, cache_location, filename, error_msg);
Expand All @@ -669,8 +669,8 @@ static std::string GetApexDataDalvikCacheFilename(std::string_view dex_location,
// Result:
// "/data/misc/apexdata/com.android.art/dalvik-cache/arm/system@[email protected]@classes.odex"
std::string result, unused_error_msg;
GetDalvikCacheFilename(std::string{dex_location}.c_str(),
apex_data_dalvik_cache.c_str(),
GetDalvikCacheFilename(dex_location,
apex_data_dalvik_cache,
&result,
&unused_error_msg);
return ReplaceFileExtension(result, file_extension);
Expand Down Expand Up @@ -724,8 +724,7 @@ std::string GetSystemOdexFilenameForApex(std::string_view location, InstructionS
DCHECK(LocationIsOnApex(location));
std::string dir = GetAndroidRoot() + "/framework/oat/" + GetInstructionSetString(isa);
std::string result, error_msg;
bool ret =
GetLocationEncodedFilename(std::string{location}.c_str(), dir.c_str(), &result, &error_msg);
bool ret = GetLocationEncodedFilename(location, dir, &result, &error_msg);
// This should never fail. The function fails only if the location is not absolute, and a location
// on /apex is always absolute.
DCHECK(ret) << error_msg;
Expand Down Expand Up @@ -764,7 +763,7 @@ std::string ReplaceFileExtension(std::string_view filename, std::string_view new

bool LocationIsOnArtApexData(std::string_view location) {
const std::string art_apex_data = GetArtApexData();
return android::base::StartsWith(location, art_apex_data);
return location.starts_with(art_apex_data);
}

bool LocationIsOnArtModule(std::string_view full_path) {
Expand All @@ -773,7 +772,7 @@ bool LocationIsOnArtModule(std::string_view full_path) {
if (module_path.empty()) {
return false;
}
return android::base::StartsWith(full_path, module_path);
return full_path.starts_with(module_path);
}

static bool StartsWithSlash(const char* str) {
Expand Down Expand Up @@ -821,7 +820,7 @@ static bool IsLocationOn(std::string_view full_path,
path_prefix.append(subdir);
}

return android::base::StartsWith(full_path, path_prefix);
return full_path.starts_with(path_prefix);
}

bool LocationIsOnSystemFramework(std::string_view full_path) {
Expand Down Expand Up @@ -853,11 +852,11 @@ bool LocationIsOnI18nModule(std::string_view full_path) {
}

bool LocationIsOnApex(std::string_view full_path) {
return android::base::StartsWith(full_path, kApexDefaultPath);
return full_path.starts_with(kApexDefaultPath);
}

std::string_view ApexNameFromLocation(std::string_view full_path) {
if (!android::base::StartsWith(full_path, kApexDefaultPath)) {
if (!full_path.starts_with(kApexDefaultPath)) {
return {};
}
size_t start = strlen(kApexDefaultPath);
Expand All @@ -874,7 +873,7 @@ bool LocationIsOnSystem(const std::string& location) {
LOG(FATAL) << "LocationIsOnSystem is unsupported on Windows.";
return false;
#else
return android::base::StartsWith(location, GetAndroidRoot());
return location.starts_with(GetAndroidRoot());
#endif
}

Expand Down
6 changes: 4 additions & 2 deletions libartbase/base/file_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalv

// Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be
// rooted at `cache_location`.
bool GetDalvikCacheFilename(const char* location, const char* cache_location,
std::string* filename, std::string* error_msg);
bool GetDalvikCacheFilename(std::string_view location,
std::string_view cache_location,
std::string* filename,
std::string* error_msg);

// Returns the absolute dalvik-cache path. The path may include the instruction set sub-directory
// if specified.
Expand Down
5 changes: 2 additions & 3 deletions libarttools/tools.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ using ::android::base::function_ref;
using ::android::base::ReadFileToString;
using ::android::base::Readlink;
using ::android::base::Result;
using ::android::base::StartsWith;
using ::android::base::unique_fd;
using ::android::fs_mgr::Fstab;
using ::android::fs_mgr::FstabEntry;
Expand Down Expand Up @@ -181,7 +180,7 @@ bool PathStartsWith(std::string_view path, std::string_view prefix) {
CHECK(!prefix.empty() && !path.empty() && prefix[0] == '/' && path[0] == '/')
<< ART_FORMAT("path={}, prefix={}", path, prefix);
ConsumeSuffix(&prefix, "/");
return StartsWith(path, prefix) &&
return path.starts_with(prefix) &&
(path.length() == prefix.length() || path[prefix.length()] == '/');
}

Expand All @@ -197,7 +196,7 @@ static Result<std::vector<FstabEntry>> GetProcMountsMatches(
// field, according to fstab(5). In addition, ignore any other entries whose mount points are
// not absolute paths, just in case there are other fs types that also have an meaningless mount
// point.
if (entry.fs_type == "swap" || !StartsWith(entry.mount_point, '/')) {
if (entry.fs_type == "swap" || !entry.mount_point.starts_with('/')) {
continue;
}
if (predicate(entry.mount_point)) {
Expand Down
Loading

0 comments on commit 2b4eb67

Please sign in to comment.