From d29fc3798422bee159fe348faaee50a9d9b43029 Mon Sep 17 00:00:00 2001 From: "Jorge E. Moreira" Date: Thu, 9 Apr 2026 18:21:23 -0700 Subject: [PATCH] Use absl::StripAsciiWhitespace instead of android::base::Trim Bug: b/501230062 --- .../cuttlefish/common/libs/utils/proc_file_utils.cpp | 4 ++-- .../host/commands/assemble_cvd/vendor_dlkm_utils.cc | 6 +++--- .../host/commands/cvd/cli/selector/selector.cpp | 4 ++-- .../commands/kernel_log_monitor/kernel_log_server.cc | 6 +++--- base/cvd/cuttlefish/host/commands/log_tee/log_tee.cpp | 5 +++-- .../host/libs/control_env/grpc_service_handler.cc | 11 +++++------ .../libs/screen_connector/composition_manager.cpp | 5 +++-- .../cvd/cuttlefish/host/libs/web/credential_source.cc | 8 ++++---- .../cuttlefish/host/libs/web/http_client/BUILD.bazel | 1 + .../host/libs/web/http_client/curl_http_client.cc | 7 ++++--- 10 files changed, 30 insertions(+), 27 deletions(-) diff --git a/base/cvd/cuttlefish/common/libs/utils/proc_file_utils.cpp b/base/cvd/cuttlefish/common/libs/utils/proc_file_utils.cpp index 68dc05add51..cf087412236 100644 --- a/base/cvd/cuttlefish/common/libs/utils/proc_file_utils.cpp +++ b/base/cvd/cuttlefish/common/libs/utils/proc_file_utils.cpp @@ -26,7 +26,7 @@ #include #include -#include +#include "absl/strings/ascii.h" #include "absl/strings/strip.h" #include "absl/strings/str_split.h" #include @@ -172,7 +172,7 @@ static Result CheckExecNameFromStatus(const std::string& exec_name, if (!absl::ConsumePrefix(&line, "Name:")) { continue; } - if (android::base::Trim(line) == exec_name) { + if (absl::StripAsciiWhitespace(line) == exec_name) { found = true; break; } diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/vendor_dlkm_utils.cc b/base/cvd/cuttlefish/host/commands/assemble_cvd/vendor_dlkm_utils.cc index b35f3aa9c1f..4653706eeed 100644 --- a/base/cvd/cuttlefish/host/commands/assemble_cvd/vendor_dlkm_utils.cc +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/vendor_dlkm_utils.cc @@ -26,10 +26,10 @@ #include #include "absl/log/log.h" +#include "absl/strings/ascii.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_split.h" #include "android-base/file.h" -#include "android-base/strings.h" #include "fmt/format.h" #include "cuttlefish/common/libs/fs/shared_buf.h" @@ -235,7 +235,7 @@ bool WriteDepsToFile( std::map> LoadModuleDeps( const std::string& filename) { std::map> dependency_map; - const auto dep_str = android::base::Trim(ReadFile(filename)); + const std::string dep_str(absl::StripAsciiWhitespace(ReadFile(filename))); const std::vector dep_lines = absl::StrSplit(dep_str, "\n"); for (const auto& line : dep_lines) { const auto mod_name = line.substr(0, line.find(":")); @@ -435,7 +435,7 @@ Result SplitRamdiskModules(const std::string& ramdisk_path, std::string module_load_file = CF_EXPECT(FindFile(ramdisk_stage_dir, "modules.load"), "Failed to find modules.dep file in input ramdisk"); - module_load_file = android::base::Trim(module_load_file); + module_load_file = std::string(absl::StripAsciiWhitespace(module_load_file)); CF_EXPECTF(!module_load_file.empty(), "Failed to find modules.dep file in input ramdisk '{}'", diff --git a/base/cvd/cuttlefish/host/commands/cvd/cli/selector/selector.cpp b/base/cvd/cuttlefish/host/commands/cvd/cli/selector/selector.cpp index b14fa9ffb7b..2b9539eafcc 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/cli/selector/selector.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/cli/selector/selector.cpp @@ -24,7 +24,7 @@ #include #include -#include +#include "absl/strings/ascii.h" #include "absl/strings/numbers.h" #include "cuttlefish/host/commands/cvd/cli/interruptible_terminal.h" @@ -114,7 +114,7 @@ Result PromptUserForGroup( } chosen_group_name = groups[selection].GroupName(); } else { - chosen_group_name = android::base::Trim(input_line); + chosen_group_name = std::string(absl::StripAsciiWhitespace(input_line)); } filter.group_name = chosen_group_name; diff --git a/base/cvd/cuttlefish/host/commands/kernel_log_monitor/kernel_log_server.cc b/base/cvd/cuttlefish/host/commands/kernel_log_monitor/kernel_log_server.cc index 77706bcec08..7f8144c5f94 100644 --- a/base/cvd/cuttlefish/host/commands/kernel_log_monitor/kernel_log_server.cc +++ b/base/cvd/cuttlefish/host/commands/kernel_log_monitor/kernel_log_server.cc @@ -25,7 +25,7 @@ #include #include -#include +#include "absl/strings/ascii.h" #include "absl/strings/str_split.h" #include "absl/log/log.h" @@ -161,8 +161,8 @@ bool KernelLogServer::HandleIncomingMessage() { // Expect space-separated key=value pairs in the log message. const std::vector fields = absl::StrSplit(line_.substr(pos + stage.size()), ' '); - for (std::string field : fields) { - field = android::base::Trim(field); + for (std::string_view field : fields) { + field = absl::StripAsciiWhitespace(field); if (field.empty()) { // Expected; absl::StrSplit() always returns at least // one (possibly empty) string. diff --git a/base/cvd/cuttlefish/host/commands/log_tee/log_tee.cpp b/base/cvd/cuttlefish/host/commands/log_tee/log_tee.cpp index 6e3a44e2bff..b144ae54c5e 100644 --- a/base/cvd/cuttlefish/host/commands/log_tee/log_tee.cpp +++ b/base/cvd/cuttlefish/host/commands/log_tee/log_tee.cpp @@ -20,7 +20,7 @@ #include -#include +#include "absl/strings/ascii.h" #include #include "absl/log/check.h" #include "absl/log/log.h" @@ -136,7 +136,8 @@ int main(int argc, char** argv) { if (chars_read == 0) { break; } - auto trimmed = android::base::Trim(std::string_view(buf, chars_read)); + std::string trimmed( + absl::StripAsciiWhitespace(std::string_view(buf, chars_read))); // Newlines inside `trimmed` are handled by the android logging code. // These checks attempt to determine the log severity coming from crosvm. // There is no guarantee of success all the time since log line boundaries diff --git a/base/cvd/cuttlefish/host/libs/control_env/grpc_service_handler.cc b/base/cvd/cuttlefish/host/libs/control_env/grpc_service_handler.cc index 77d4ae0a3d3..c840c26da6d 100644 --- a/base/cvd/cuttlefish/host/libs/control_env/grpc_service_handler.cc +++ b/base/cvd/cuttlefish/host/libs/control_env/grpc_service_handler.cc @@ -24,9 +24,9 @@ #include #include "absl/log/log.h" +#include "absl/strings/ascii.h" #include "absl/strings/match.h" #include "absl/strings/str_split.h" -#include "android-base/strings.h" #include "grpcpp/security/credentials.h" #include "json/json.h" #include "test/cpp/util/cli_credentials.h" @@ -38,7 +38,6 @@ using absl::EndsWith; using absl::StrSplit; -using android::base::Trim; using grpc::InsecureChannelCredentials; namespace cuttlefish { @@ -198,7 +197,7 @@ Result HandleLsCmd( Json::Value json; json["services"] = Json::Value(Json::arrayValue); for (std::string_view full_service_name : - StrSplit(Trim(command_output), "\n")) { + StrSplit(absl::StripAsciiWhitespace(command_output), "\n")) { if (full_service_name == kServiceServerReflection || full_service_name == kServiceHealth || full_service_name == kServiceControlEnvProxyFull) { @@ -225,8 +224,8 @@ Result HandleLsCmd( Json::Value json; json["methods"] = Json::Value(Json::arrayValue); - for (const auto& method_name : - std::vector(StrSplit(Trim(command_output), "\n"))) { + for (const auto& method_name : std::vector( + StrSplit(absl::StripAsciiWhitespace(command_output), "\n"))) { json["methods"].append(method_name); } Json::StreamWriterBuilder builder; @@ -252,7 +251,7 @@ Result HandleLsCmd( // rpc SetTxpower(wmediumdserver.SetTxpowerRequest) returns // (google.protobuf.Empty) {} std::vector parsed_output = - StrSplit(Trim(command_output), "()"); + StrSplit(absl::StripAsciiWhitespace(command_output), "()"); CF_EXPECT(parsed_output.size() == 5, "Unexpected parsing result"); Json::Value json; json["request_type"] = parsed_output[1]; diff --git a/base/cvd/cuttlefish/host/libs/screen_connector/composition_manager.cpp b/base/cvd/cuttlefish/host/libs/screen_connector/composition_manager.cpp index 183f5b9149c..d63eb8db9c7 100644 --- a/base/cvd/cuttlefish/host/libs/screen_connector/composition_manager.cpp +++ b/base/cvd/cuttlefish/host/libs/screen_connector/composition_manager.cpp @@ -34,7 +34,7 @@ #include #include -#include +#include "absl/strings/ascii.h" #include "absl/strings/str_split.h" #include "absl/log/log.h" #include "absl/strings/numbers.h" @@ -77,7 +77,8 @@ CompositionManager::ParseOverlays(std::vector overlay_items) { // index within that vm. Structured types are created as result. for (int display_index = 0; display_index < overlay_items.size(); display_index++) { - auto overlay_item = android::base::Trim(overlay_items[display_index]); + std::string_view overlay_item = + absl::StripAsciiWhitespace(overlay_items[display_index]); if (overlay_item.empty() || overlay_item == "_") { continue; diff --git a/base/cvd/cuttlefish/host/libs/web/credential_source.cc b/base/cvd/cuttlefish/host/libs/web/credential_source.cc index 1ae4de4bf06..ba224f71c7f 100644 --- a/base/cvd/cuttlefish/host/libs/web/credential_source.cc +++ b/base/cvd/cuttlefish/host/libs/web/credential_source.cc @@ -28,7 +28,7 @@ #include #include -#include +#include "absl/strings/ascii.h" #include "absl/strings/str_replace.h" #include "absl/strings/strip.h" #include "absl/strings/str_split.h" @@ -210,18 +210,18 @@ RefreshTokenCredentialSource::FromOauth2ClientFile( absl::StrSplit(oauth_contents, '\n', absl::SkipEmpty())) { static constexpr std::string_view kClientIdPrefix = "client_id ="; if (absl::ConsumePrefix(&line, kClientIdPrefix)) { - client_id = android::base::Trim(line); + client_id = std::string(absl::StripAsciiWhitespace(line)); continue; } static constexpr std::string_view kClientSecretPrefix = "client_secret ="; if (absl::ConsumePrefix(&line, kClientSecretPrefix)) { - client_secret = android::base::Trim(line); + client_secret = std::string(absl::StripAsciiWhitespace(line)); continue; } static constexpr std::string_view kRefreshTokenPrefix = "gs_oauth2_refresh_token ="; if (absl::ConsumePrefix(&line, kRefreshTokenPrefix)) { - refresh_token = android::base::Trim(line); + refresh_token = std::string(absl::StripAsciiWhitespace(line)); continue; } } diff --git a/base/cvd/cuttlefish/host/libs/web/http_client/BUILD.bazel b/base/cvd/cuttlefish/host/libs/web/http_client/BUILD.bazel index 4ace5b4a91e..944b6790d1a 100644 --- a/base/cvd/cuttlefish/host/libs/web/http_client/BUILD.bazel +++ b/base/cvd/cuttlefish/host/libs/web/http_client/BUILD.bazel @@ -21,6 +21,7 @@ cf_cc_library( "//cuttlefish/result", "//libbase", "@abseil-cpp//absl/log", + "@abseil-cpp//absl/strings", "@curl", ], ) diff --git a/base/cvd/cuttlefish/host/libs/web/http_client/curl_http_client.cc b/base/cvd/cuttlefish/host/libs/web/http_client/curl_http_client.cc index d6bf898e5b8..e0b70ec339a 100644 --- a/base/cvd/cuttlefish/host/libs/web/http_client/curl_http_client.cc +++ b/base/cvd/cuttlefish/host/libs/web/http_client/curl_http_client.cc @@ -25,10 +25,11 @@ #include #include #include +#include #include #include -#include +#include "absl/strings/ascii.h" #include "absl/log/log.h" #include "cuttlefish/host/libs/web/http_client/http_client.h" @@ -39,8 +40,8 @@ namespace cuttlefish { namespace { std::string TrimWhitespace(const char* data, const size_t size) { - std::string converted(data, size); - return android::base::Trim(converted); + std::string_view converted(data, size); + return std::string(absl::StripAsciiWhitespace(converted)); } int LoggingCurlDebugFunction(CURL*, curl_infotype type, char* data, size_t size,