From 68586e80fcb7d34860452995578e2be4afc485c0 Mon Sep 17 00:00:00 2001 From: Steve Anton Date: Thu, 13 Dec 2018 17:41:25 -0800 Subject: [PATCH] Replace starts_with and ends_with with Abseil Bug: None Change-Id: I7eae3db1aeb81f0f1d37ff50d5c85c16ecb1f366 Reviewed-on: https://webrtc-review.googlesource.com/c/114221 Commit-Queue: Steve Anton Reviewed-by: Mirko Bonadei Cr-Commit-Position: refs/heads/master@{#26032} --- pc/peerconnection.cc | 5 ++--- pc/rtcstats_integrationtest.cc | 6 +++--- rtc_base/BUILD.gn | 1 + rtc_base/gunit.cc | 13 +++++++------ rtc_base/gunit.h | 8 ++++---- rtc_base/stringutils.cc | 16 ---------------- rtc_base/stringutils.h | 6 ------ rtc_base/stringutils_unittest.cc | 20 -------------------- rtc_tools/BUILD.gn | 3 +++ rtc_tools/frame_analyzer/frame_analyzer.cc | 6 +++--- rtc_tools/video_file_reader.cc | 6 +++--- rtc_tools/video_file_writer.cc | 4 ++-- 12 files changed, 28 insertions(+), 66 deletions(-) diff --git a/pc/peerconnection.cc b/pc/peerconnection.cc index 6c92816f83..9434b0b6e2 100644 --- a/pc/peerconnection.cc +++ b/pc/peerconnection.cc @@ -49,7 +49,6 @@ #include "rtc_base/numerics/safe_conversions.h" #include "rtc_base/stringencode.h" #include "rtc_base/strings/string_builder.h" -#include "rtc_base/stringutils.h" #include "rtc_base/trace_event.h" #include "system_wrappers/include/clock.h" #include "system_wrappers/include/field_trial.h" @@ -2225,7 +2224,7 @@ RTCError PeerConnection::ApplyLocalDescription( if (data_content) { const cricket::DataContentDescription* data_desc = data_content->media_description()->as_data(); - if (rtc::starts_with(data_desc->protocol().data(), + if (absl::StartsWith(data_desc->protocol(), cricket::kMediaProtocolRtpPrefix)) { UpdateLocalRtpDataChannels(data_desc->streams()); } @@ -2625,7 +2624,7 @@ RTCError PeerConnection::ApplyRemoteDescription( // Update the DataChannels with the information from the remote peer. if (data_desc) { - if (rtc::starts_with(data_desc->protocol().data(), + if (absl::StartsWith(data_desc->protocol(), cricket::kMediaProtocolRtpPrefix)) { UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); } diff --git a/pc/rtcstats_integrationtest.cc b/pc/rtcstats_integrationtest.cc index 0bf76601ef..aa45975878 100644 --- a/pc/rtcstats_integrationtest.cc +++ b/pc/rtcstats_integrationtest.cc @@ -16,6 +16,7 @@ #include #include +#include "absl/strings/match.h" #include "api/audio_codecs/audio_decoder_factory.h" #include "api/audio_codecs/audio_encoder_factory.h" #include "api/audio_codecs/builtin_audio_decoder_factory.h" @@ -37,7 +38,6 @@ #include "rtc_base/gunit.h" #include "rtc_base/refcountedobject.h" #include "rtc_base/scoped_ref_ptr.h" -#include "rtc_base/stringutils.h" #include "rtc_base/thread.h" #include "rtc_base/trace_event.h" #include "rtc_base/virtualsocketserver.h" @@ -876,12 +876,12 @@ TEST_F(RTCStatsIntegrationTest, GetStatsReferencedIds) { if (!member->is_defined()) continue; if (member->type() == RTCStatsMemberInterface::kString) { - if (rtc::ends_with(member->name(), "Id")) { + if (absl::EndsWith(member->name(), "Id")) { const auto& id = member->cast_to>(); expected_ids.insert(&(*id)); } } else if (member->type() == RTCStatsMemberInterface::kSequenceString) { - if (rtc::ends_with(member->name(), "Ids")) { + if (absl::EndsWith(member->name(), "Ids")) { const auto& ids = member->cast_to>>(); for (const std::string& id : *ids) diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index 829a5feaaa..1ee1ed5240 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -1110,6 +1110,7 @@ rtc_source_set("gunit_helpers") { ":rtc_base_tests_utils", ":stringutils", "../test:test_support", + "//third_party/abseil-cpp/absl/strings", ] } diff --git a/rtc_base/gunit.cc b/rtc_base/gunit.cc index 0dd8f1230d..83ee8075fb 100644 --- a/rtc_base/gunit.cc +++ b/rtc_base/gunit.cc @@ -12,17 +12,18 @@ #include -#include "rtc_base/stringutils.h" +#include "absl/strings/match.h" -::testing::AssertionResult AssertStartsWith(const char* str_expr, +::testing::AssertionResult AssertStartsWith(const char* text_expr, const char* prefix_expr, - const std::string& str, - const std::string& prefix) { - if (rtc::starts_with(str.c_str(), prefix.c_str())) { + absl::string_view text, + absl::string_view prefix) { + if (absl::StartsWith(text, prefix)) { return ::testing::AssertionSuccess(); } else { return ::testing::AssertionFailure() - << str_expr << "\nwhich is\n\"" << str << "\"\ndoes not start with\n" + << text_expr << "\nwhich is\n\"" << text + << "\"\ndoes not start with\n" << prefix_expr << "\nwhich is\n\"" << prefix << "\""; } } diff --git a/rtc_base/gunit.h b/rtc_base/gunit.h index 910fbf39ba..7f23fbb3f7 100644 --- a/rtc_base/gunit.h +++ b/rtc_base/gunit.h @@ -153,11 +153,11 @@ } else \ GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__) : ASSERT_EQ(v1, v2) -// Usage: EXPECT_PRED_FORMAT2(AssertStartsWith, str, "prefix"); -testing::AssertionResult AssertStartsWith(const char* str_expr, +// Usage: EXPECT_PRED_FORMAT2(AssertStartsWith, text, "prefix"); +testing::AssertionResult AssertStartsWith(const char* text_expr, const char* prefix_expr, - const std::string& str, - const std::string& prefix); + absl::string_view text, + absl::string_view prefix); // Usage: EXPECT_PRED_FORMAT2(AssertStringContains, str, "substring"); testing::AssertionResult AssertStringContains(const char* str_expr, diff --git a/rtc_base/stringutils.cc b/rtc_base/stringutils.cc index c808eb2cc4..57ca408409 100644 --- a/rtc_base/stringutils.cc +++ b/rtc_base/stringutils.cc @@ -42,22 +42,6 @@ void replace_substrs(const char* search, } } -bool starts_with(const char* s1, const char* s2) { - return strncmp(s1, s2, strlen(s2)) == 0; -} - -bool ends_with(const char* s1, const char* s2) { - size_t s1_length = strlen(s1); - size_t s2_length = strlen(s2); - - if (s2_length > s1_length) { - return false; - } - - const char* start = s1 + (s1_length - s2_length); - return strncmp(start, s2, s2_length) == 0; -} - static const char kWhitespace[] = " \n\r\t"; std::string string_trim(const std::string& s) { diff --git a/rtc_base/stringutils.h b/rtc_base/stringutils.h index 702bc67b79..2a53e69a87 100644 --- a/rtc_base/stringutils.h +++ b/rtc_base/stringutils.h @@ -100,12 +100,6 @@ void replace_substrs(const char* search, size_t replace_len, std::string* s); -// True iff s1 starts with s2. -bool starts_with(const char* s1, const char* s2); - -// True iff s1 ends with s2. -bool ends_with(const char* s1, const char* s2); - // Remove leading and trailing whitespaces. std::string string_trim(const std::string& s); diff --git a/rtc_base/stringutils_unittest.cc b/rtc_base/stringutils_unittest.cc index cf9debef38..a07d8bb973 100644 --- a/rtc_base/stringutils_unittest.cc +++ b/rtc_base/stringutils_unittest.cc @@ -22,26 +22,6 @@ TEST(string_trim_Test, Trimming) { EXPECT_EQ("", string_trim("")); } -TEST(string_startsTest, StartsWith) { - EXPECT_TRUE(starts_with("foobar", "foo")); - EXPECT_TRUE(starts_with("foobar", "foobar")); - EXPECT_TRUE(starts_with("foobar", "")); - EXPECT_TRUE(starts_with("", "")); - EXPECT_FALSE(starts_with("foobar", "bar")); - EXPECT_FALSE(starts_with("foobar", "foobarbaz")); - EXPECT_FALSE(starts_with("", "f")); -} - -TEST(string_endsTest, EndsWith) { - EXPECT_TRUE(ends_with("foobar", "bar")); - EXPECT_TRUE(ends_with("foobar", "foobar")); - EXPECT_TRUE(ends_with("foobar", "")); - EXPECT_TRUE(ends_with("", "")); - EXPECT_FALSE(ends_with("foobar", "foo")); - EXPECT_FALSE(ends_with("foobar", "foobarbaz")); - EXPECT_FALSE(ends_with("", "f")); -} - TEST(string_toHexTest, ToHex) { EXPECT_EQ(ToHex(0), "0"); EXPECT_EQ(ToHex(0X1243E), "1243e"); diff --git a/rtc_tools/BUILD.gn b/rtc_tools/BUILD.gn index 4cdfd09c54..aef8238656 100644 --- a/rtc_tools/BUILD.gn +++ b/rtc_tools/BUILD.gn @@ -69,6 +69,7 @@ rtc_static_library("video_file_reader") { "../api/video:video_frame_i420", "../rtc_base:checks", "../rtc_base:rtc_base_approved", + "//third_party/abseil-cpp/absl/strings", "//third_party/abseil-cpp/absl/types:optional", ] } @@ -83,6 +84,7 @@ rtc_static_library("video_file_writer") { "../api/video:video_frame", "../api/video:video_frame_i420", "../rtc_base:rtc_base_approved", + "//third_party/abseil-cpp/absl/strings", "//third_party/abseil-cpp/absl/types:optional", ] } @@ -128,6 +130,7 @@ rtc_executable("frame_analyzer") { "../rtc_base:ptr_util", "../rtc_base:stringutils", "../test:perf_test", + "//third_party/abseil-cpp/absl/strings", ] } diff --git a/rtc_tools/frame_analyzer/frame_analyzer.cc b/rtc_tools/frame_analyzer/frame_analyzer.cc index 1a0d2e6833..2bfa0342fd 100644 --- a/rtc_tools/frame_analyzer/frame_analyzer.cc +++ b/rtc_tools/frame_analyzer/frame_analyzer.cc @@ -14,9 +14,9 @@ #include #include +#include "absl/strings/match.h" #include "rtc_base/scoped_ref_ptr.h" #include "rtc_base/strings/string_builder.h" -#include "rtc_base/stringutils.h" #include "rtc_tools/frame_analyzer/video_color_aligner.h" #include "rtc_tools/frame_analyzer/video_geometry_aligner.h" #include "rtc_tools/frame_analyzer/video_quality_analysis.h" @@ -112,8 +112,8 @@ int main(int argc, char* argv[]) { const std::string test_file_name = parser.GetFlag("test_file"); // .yuv files require explicit resolution. - if ((rtc::ends_with(reference_file_name.c_str(), ".yuv") || - rtc::ends_with(test_file_name.c_str(), ".yuv")) && + if ((absl::EndsWith(reference_file_name, ".yuv") || + absl::EndsWith(test_file_name, ".yuv")) && (width <= 0 || height <= 0)) { fprintf(stderr, "Error: You need to specify width and height when using .yuv " diff --git a/rtc_tools/video_file_reader.cc b/rtc_tools/video_file_reader.cc index 26c755465c..b98e386050 100644 --- a/rtc_tools/video_file_reader.cc +++ b/rtc_tools/video_file_reader.cc @@ -14,6 +14,7 @@ #include #include +#include "absl/strings/match.h" #include "absl/types/optional.h" #include "api/video/i420_buffer.h" #include "rtc_base/checks.h" @@ -21,7 +22,6 @@ #include "rtc_base/refcountedobject.h" #include "rtc_base/string_to_number.h" #include "rtc_base/stringencode.h" -#include "rtc_base/stringutils.h" namespace webrtc { namespace test { @@ -270,9 +270,9 @@ rtc::scoped_refptr