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 <steveanton@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26032}
diff --git a/pc/peerconnection.cc b/pc/peerconnection.cc
index 6c92816..9434b0b 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 @@
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 @@
// 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 0bf7660..aa45975 100644
--- a/pc/rtcstats_integrationtest.cc
+++ b/pc/rtcstats_integrationtest.cc
@@ -16,6 +16,7 @@
#include <string>
#include <vector>
+#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 @@
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<const RTCStatsMember<std::string>>();
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<const RTCStatsMember<std::vector<std::string>>>();
for (const std::string& id : *ids)
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index 829a5fe..1ee1ed5 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -1110,6 +1110,7 @@
":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 0dd8f12..83ee807 100644
--- a/rtc_base/gunit.cc
+++ b/rtc_base/gunit.cc
@@ -12,17 +12,18 @@
#include <string>
-#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 910fbf3..7f23fbb 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 c808eb2..57ca408 100644
--- a/rtc_base/stringutils.cc
+++ b/rtc_base/stringutils.cc
@@ -42,22 +42,6 @@
}
}
-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 702bc67..2a53e69 100644
--- a/rtc_base/stringutils.h
+++ b/rtc_base/stringutils.h
@@ -100,12 +100,6 @@
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 cf9debe..a07d8bb 100644
--- a/rtc_base/stringutils_unittest.cc
+++ b/rtc_base/stringutils_unittest.cc
@@ -22,26 +22,6 @@
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 4cdfd09..aef8238 100644
--- a/rtc_tools/BUILD.gn
+++ b/rtc_tools/BUILD.gn
@@ -69,6 +69,7 @@
"../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 @@
"../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_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 1a0d2e6..2bfa034 100644
--- a/rtc_tools/frame_analyzer/frame_analyzer.cc
+++ b/rtc_tools/frame_analyzer/frame_analyzer.cc
@@ -14,9 +14,9 @@
#include <string>
#include <vector>
+#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 @@
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 26c7554..b98e386 100644
--- a/rtc_tools/video_file_reader.cc
+++ b/rtc_tools/video_file_reader.cc
@@ -14,6 +14,7 @@
#include <string>
#include <vector>
+#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<Video> OpenYuvOrY4mFile(const std::string& file_name,
int width,
int height) {
- if (rtc::ends_with(file_name.c_str(), ".yuv"))
+ if (absl::EndsWith(file_name, ".yuv"))
return OpenYuvFile(file_name, width, height);
- if (rtc::ends_with(file_name.c_str(), ".y4m"))
+ if (absl::EndsWith(file_name, ".y4m"))
return OpenY4mFile(file_name);
RTC_LOG(LS_ERROR) << "Video file does not end in either .yuv or .y4m: "
diff --git a/rtc_tools/video_file_writer.cc b/rtc_tools/video_file_writer.cc
index dfacc25..e7a649f 100644
--- a/rtc_tools/video_file_writer.cc
+++ b/rtc_tools/video_file_writer.cc
@@ -14,9 +14,9 @@
#include <cstdio>
#include <string>
+#include "absl/strings/match.h"
#include "api/video/video_frame_buffer.h"
#include "rtc_base/logging.h"
-#include "rtc_base/stringutils.h"
namespace webrtc {
namespace test {
@@ -30,7 +30,7 @@
return;
}
- bool isY4m = rtc::ends_with(file_name.c_str(), ".y4m");
+ bool isY4m = absl::EndsWith(file_name, ".y4m");
if (isY4m) {
fprintf(output_file, "YUV4MPEG2 W%d H%d F%d:1 C420\n", video->width(),
video->height(), fps);