From 2e49a8de958645c9b568572de5ebd90363f1410e Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Tue, 4 Oct 2022 15:10:51 -0400 Subject: [PATCH] [ObjC] Use absl::StripAsciiWhitespace(). Dropping the custom impl that was used previously. --- .../compiler/objectivec/import_writer.cc | 10 +++++----- .../compiler/objectivec/line_consumer.cc | 12 ++---------- src/google/protobuf/compiler/objectivec/names.cc | 16 +++------------- src/google/protobuf/compiler/objectivec/names.h | 3 --- 4 files changed, 10 insertions(+), 31 deletions(-) diff --git a/src/google/protobuf/compiler/objectivec/import_writer.cc b/src/google/protobuf/compiler/objectivec/import_writer.cc index e4c81978bf..3a058a916f 100644 --- a/src/google/protobuf/compiler/objectivec/import_writer.cc +++ b/src/google/protobuf/compiler/objectivec/import_writer.cc @@ -32,6 +32,7 @@ #include "google/protobuf/compiler/objectivec/line_consumer.h" #include "google/protobuf/compiler/objectivec/names.h" #include "google/protobuf/io/printer.h" +#include "absl/strings/ascii.h" // NOTE: src/google/protobuf/compiler/plugin.cc makes use of cerr for some // error cases, so it seems to be ok to use as a back door for errors. @@ -63,9 +64,8 @@ bool ProtoFrameworkCollector::ConsumeLine( std::string(line) + "'."; return false; } - absl::string_view framework_name = line.substr(0, offset); - absl::string_view proto_file_list = line.substr(offset + 1); - TrimWhitespace(&framework_name); + absl::string_view framework_name = absl::StripAsciiWhitespace(line.substr(0, offset)); + absl::string_view proto_file_list = absl::StripAsciiWhitespace(line.substr(offset + 1)); int start = 0; while (start < proto_file_list.length()) { @@ -74,8 +74,8 @@ bool ProtoFrameworkCollector::ConsumeLine( offset = proto_file_list.length(); } - absl::string_view proto_file = proto_file_list.substr(start, offset - start); - TrimWhitespace(&proto_file); + absl::string_view proto_file = + absl::StripAsciiWhitespace(proto_file_list.substr(start, offset - start)); if (!proto_file.empty()) { std::map::iterator existing_entry = map_->find(std::string(proto_file)); diff --git a/src/google/protobuf/compiler/objectivec/line_consumer.cc b/src/google/protobuf/compiler/objectivec/line_consumer.cc index 700a8fdc73..2026c2826d 100644 --- a/src/google/protobuf/compiler/objectivec/line_consumer.cc +++ b/src/google/protobuf/compiler/objectivec/line_consumer.cc @@ -41,6 +41,7 @@ #include #include +#include "absl/strings/ascii.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_split.h" #include "google/protobuf/compiler/objectivec/line_consumer.h" @@ -67,15 +68,6 @@ using ::open; namespace { -void TrimWhitespace(absl::string_view* input) { - while (!input->empty() && absl::ascii_isspace(*input->data())) { - input->remove_prefix(1); - } - while (!input->empty() && absl::ascii_isspace((*input)[input->length() - 1])) { - input->remove_suffix(1); - } -} - bool ascii_isnewline(char c) { return c == '\n' || c == '\r'; } @@ -134,7 +126,7 @@ bool Parser::ParseChunk(absl::string_view chunk, std::string* out_error) { while (ReadLine(&full_chunk, &line)) { ++line_; RemoveComment(&line); - TrimWhitespace(&line); + line = absl::StripAsciiWhitespace(line); if (!line.empty() && !line_consumer_->ConsumeLine(line, out_error)) { if (out_error->empty()) { *out_error = "ConsumeLine failed without setting an error."; diff --git a/src/google/protobuf/compiler/objectivec/names.cc b/src/google/protobuf/compiler/objectivec/names.cc index 009a7ebf77..84fb4dd17d 100644 --- a/src/google/protobuf/compiler/objectivec/names.cc +++ b/src/google/protobuf/compiler/objectivec/names.cc @@ -36,6 +36,7 @@ #include #include "google/protobuf/compiler/code_generator.h" +#include "absl/strings/ascii.h" #include "absl/strings/str_split.h" #include "google/protobuf/compiler/objectivec/line_consumer.h" #include "google/protobuf/compiler/objectivec/names.h" @@ -497,15 +498,6 @@ void MaybeUnQuote(absl::string_view* input) { } // namespace -void TrimWhitespace(absl::string_view* input) { - while (!input->empty() && absl::ascii_isspace(*input->data())) { - input->remove_prefix(1); - } - while (!input->empty() && absl::ascii_isspace((*input)[input->length() - 1])) { - input->remove_suffix(1); - } -} - bool IsRetainedName(const std::string& name) { // List of prefixes from // http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html @@ -865,10 +857,8 @@ bool PackageToPrefixesCollector::ConsumeLine( *out_error = usage_ + " file line without equal sign: '" + absl::StrCat(line) + "'."; return false; } - absl::string_view package = line.substr(0, offset); - absl::string_view prefix = line.substr(offset + 1); - TrimWhitespace(&package); - TrimWhitespace(&prefix); + absl::string_view package = absl::StripAsciiWhitespace(line.substr(0, offset)); + absl::string_view prefix = absl::StripAsciiWhitespace(line.substr(offset + 1)); MaybeUnQuote(&prefix); // Don't really worry about error checking the package/prefix for // being valid. Assume the file is validated when it is created/edited. diff --git a/src/google/protobuf/compiler/objectivec/names.h b/src/google/protobuf/compiler/objectivec/names.h index 8b17b4dd65..e10370ff27 100644 --- a/src/google/protobuf/compiler/objectivec/names.h +++ b/src/google/protobuf/compiler/objectivec/names.h @@ -69,9 +69,6 @@ void PROTOC_EXPORT SetProtoPackagePrefixExceptionList( std::string PROTOC_EXPORT GetForcedPackagePrefix(); void PROTOC_EXPORT SetForcedPackagePrefix(const std::string& prefix); -// Remove white space from either end of a absl::string_view. -void PROTOC_EXPORT TrimWhitespace(absl::string_view* input); - // Returns true if the name requires a ns_returns_not_retained attribute applied // to it. bool PROTOC_EXPORT IsRetainedName(const std::string& name);