Replace replace_substrs with Abseil

Bug: None
Change-Id: I155cc29db951ef1b812691c57aaafe037fbeb230
Reviewed-on: https://webrtc-review.googlesource.com/c/114241
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26699}
diff --git a/DEPS b/DEPS
index d35c489..ac8d14f 100644
--- a/DEPS
+++ b/DEPS
@@ -1443,6 +1443,7 @@
   "+absl/meta/type_traits.h",
   "+absl/strings/ascii.h",
   "+absl/strings/match.h",
+  "+absl/strings/str_replace.h",
   "+absl/strings/string_view.h",
   "+absl/types/optional.h",
   "+absl/types/variant.h",
diff --git a/abseil-in-webrtc.md b/abseil-in-webrtc.md
index 117d515..ba317c4 100644
--- a/abseil-in-webrtc.md
+++ b/abseil-in-webrtc.md
@@ -15,7 +15,8 @@
 * `absl::make_unique` and `absl::WrapUnique`
 * `absl::optional` and related stuff from `absl/types/optional.h`.
 * `absl::string_view`
-* The functions in `absl/strings/ascii.h` and `absl/strings/match.h`
+* The functions in `absl/strings/ascii.h`, `absl/strings/match.h`,
+  and `absl/strings/str_replace.h`.
 * `absl::is_trivially_copy_constructible`,
   `absl::is_trivially_copy_assignable`, and
   `absl::is_trivially_destructible` from `absl/meta/type_traits.h`.
diff --git a/pc/peer_connection_interface_unittest.cc b/pc/peer_connection_interface_unittest.cc
index 3803b7f..06d077a 100644
--- a/pc/peer_connection_interface_unittest.cc
+++ b/pc/peer_connection_interface_unittest.cc
@@ -17,6 +17,7 @@
 #include <vector>
 
 #include "absl/memory/memory.h"
+#include "absl/strings/str_replace.h"
 #include "absl/types/optional.h"
 #include "api/audio/audio_mixer.h"
 #include "api/audio_codecs/audio_decoder_factory.h"
@@ -81,7 +82,6 @@
 #include "rtc_base/ref_counted_object.h"
 #include "rtc_base/rtc_certificate_generator.h"
 #include "rtc_base/socket_address.h"
-#include "rtc_base/string_utils.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/time_utils.h"
 #include "rtc_base/virtual_socket_server.h"
@@ -2029,8 +2029,7 @@
   std::string receive_label = "answer_channel";
   std::string sdp;
   EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
-  rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
-                       receive_label.c_str(), receive_label.length(), &sdp);
+  absl::StrReplaceAll({{offer_label, receive_label}}, &sdp);
   CreateAnswerAsRemoteDescription(sdp);
 
   // Verify that a new incoming data channel has been created and that
@@ -2884,8 +2883,7 @@
   CreatePeerConnection(config);
 
   std::string recvonly_offer = GetSdpStringWithStream1();
-  rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
-                       strlen(kRecvonly), &recvonly_offer);
+  absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
   CreateAndSetRemoteOffer(recvonly_offer);
 
   EXPECT_EQ(0u, observer_.remote_streams()->count());
diff --git a/pc/rtc_stats_collector_unittest.cc b/pc/rtc_stats_collector_unittest.cc
index 74799d1..f628f82 100644
--- a/pc/rtc_stats_collector_unittest.cc
+++ b/pc/rtc_stats_collector_unittest.cc
@@ -16,6 +16,7 @@
 #include <vector>
 
 #include "absl/memory/memory.h"
+#include "absl/strings/str_replace.h"
 #include "api/rtp_parameters.h"
 #include "api/stats/rtc_stats_report.h"
 #include "api/stats/rtcstats_objects.h"
@@ -134,11 +135,10 @@
           new rtc::FakeSSLIdentity(info->pems)));
   // Strip header/footer and newline characters of PEM strings.
   for (size_t i = 0; i < info->pems.size(); ++i) {
-    rtc::replace_substrs("-----BEGIN CERTIFICATE-----", 27, "", 0,
-                         &info->pems[i]);
-    rtc::replace_substrs("-----END CERTIFICATE-----", 25, "", 0,
-                         &info->pems[i]);
-    rtc::replace_substrs("\n", 1, "", 0, &info->pems[i]);
+    absl::StrReplaceAll({{"-----BEGIN CERTIFICATE-----", ""},
+                         {"-----END CERTIFICATE-----", ""},
+                         {"\n", ""}},
+                        &info->pems[i]);
   }
   // Fingerprints for the whole certificate chain, starting with leaf
   // certificate.
diff --git a/pc/webrtc_sdp_unittest.cc b/pc/webrtc_sdp_unittest.cc
index b232caf..193a3ef 100644
--- a/pc/webrtc_sdp_unittest.cc
+++ b/pc/webrtc_sdp_unittest.cc
@@ -18,6 +18,7 @@
 #include <vector>
 
 #include "absl/algorithm/container.h"
+#include "absl/strings/str_replace.h"
 #include "api/array_view.h"
 #include "api/crypto_params.h"
 #include "api/jsep_session_description.h"
@@ -38,7 +39,6 @@
 #include "rtc_base/socket_address.h"
 #include "rtc_base/ssl_fingerprint.h"
 #include "rtc_base/string_encode.h"
-#include "rtc_base/string_utils.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 
@@ -88,9 +88,7 @@
 typedef std::vector<Candidate> Candidates;
 
 static const uint32_t kDefaultSctpPort = 5000;
-static const char kDefaultSctpPortStr[] = "5000";
 static const uint16_t kUnusualSctpPort = 9556;
-static const char kUnusualSctpPortStr[] = "9556";
 static const char kSessionTime[] = "t=0 0\r\n";
 static const uint32_t kCandidatePriority = 2130706432U;  // pref = 1.0
 static const char kAttributeIceUfragVoice[] = "a=ice-ufrag:ufrag_voice\r\n";
@@ -925,16 +923,13 @@
 static void InjectAfter(const std::string& line,
                         const std::string& newlines,
                         std::string* message) {
-  const std::string tmp = line + newlines;
-  rtc::replace_substrs(line.c_str(), line.length(), tmp.c_str(), tmp.length(),
-                       message);
+  absl::StrReplaceAll({{line, line + newlines}}, message);
 }
 
 static void Replace(const std::string& line,
                     const std::string& newlines,
                     std::string* message) {
-  rtc::replace_substrs(line.c_str(), line.length(), newlines.c_str(),
-                       newlines.length(), message);
+  absl::StrReplaceAll({{line, newlines}}, message);
 }
 
 // Expect fail to parase |bad_sdp| and expect |bad_part| be part of the error
@@ -1584,14 +1579,8 @@
   // Disable the ice-ufrag and ice-pwd in given |sdp| message by replacing
   // them with invalid keywords so that the parser will just ignore them.
   bool RemoveCandidateUfragPwd(std::string* sdp) {
-    const char ice_ufrag[] = "a=ice-ufrag";
-    const char ice_ufragx[] = "a=xice-ufrag";
-    const char ice_pwd[] = "a=ice-pwd";
-    const char ice_pwdx[] = "a=xice-pwd";
-    rtc::replace_substrs(ice_ufrag, strlen(ice_ufrag), ice_ufragx,
-                         strlen(ice_ufragx), sdp);
-    rtc::replace_substrs(ice_pwd, strlen(ice_pwd), ice_pwdx, strlen(ice_pwdx),
-                         sdp);
+    absl::StrReplaceAll(
+        {{"a=ice-ufrag", "a=xice-ufrag"}, {"a=ice-pwd", "a=xice-pwd"}}, sdp);
     return true;
   }
 
@@ -2244,12 +2233,9 @@
   std::string expected_sdp = kSdpString;
   expected_sdp.append(kSdpSctpDataChannelString);
 
-  char default_portstr[16];
-  char new_portstr[16];
-  snprintf(default_portstr, sizeof(default_portstr), "%d", kDefaultSctpPort);
-  snprintf(new_portstr, sizeof(new_portstr), "%d", kNewPort);
-  rtc::replace_substrs(default_portstr, strlen(default_portstr), new_portstr,
-                       strlen(new_portstr), &expected_sdp);
+  absl::StrReplaceAll(
+      {{rtc::ToString(kDefaultSctpPort), rtc::ToString(kNewPort)}},
+      &expected_sdp);
 
   EXPECT_EQ(expected_sdp, message);
 }
@@ -2934,9 +2920,9 @@
   // Then get the deserialized JsepSessionDescription.
   std::string sdp_with_data = kSdpString;
   sdp_with_data.append(kSdpSctpDataChannelString);
-  rtc::replace_substrs(kDefaultSctpPortStr, strlen(kDefaultSctpPortStr),
-                       kUnusualSctpPortStr, strlen(kUnusualSctpPortStr),
-                       &sdp_with_data);
+  absl::StrReplaceAll(
+      {{rtc::ToString(kDefaultSctpPort), rtc::ToString(kUnusualSctpPort)}},
+      &sdp_with_data);
   JsepSessionDescription jdesc_output(kDummyType);
 
   EXPECT_TRUE(SdpDeserialize(sdp_with_data, &jdesc_output));
@@ -2957,9 +2943,9 @@
   // a=sctp-port
   std::string sdp_with_data = kSdpString;
   sdp_with_data.append(kSdpSctpDataChannelStringWithSctpPort);
-  rtc::replace_substrs(kDefaultSctpPortStr, strlen(kDefaultSctpPortStr),
-                       kUnusualSctpPortStr, strlen(kUnusualSctpPortStr),
-                       &sdp_with_data);
+  absl::StrReplaceAll(
+      {{rtc::ToString(kDefaultSctpPort), rtc::ToString(kUnusualSctpPort)}},
+      &sdp_with_data);
   JsepSessionDescription jdesc_output(kDummyType);
 
   EXPECT_TRUE(SdpDeserialize(sdp_with_data, &jdesc_output));
diff --git a/rtc_base/ssl_identity_unittest.cc b/rtc_base/ssl_identity_unittest.cc
index f01f37b..748041e 100644
--- a/rtc_base/ssl_identity_unittest.cc
+++ b/rtc_base/ssl_identity_unittest.cc
@@ -13,6 +13,7 @@
 #include <string>
 #include <vector>
 
+#include "absl/strings/str_replace.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/fake_ssl_identity.h"
 #include "rtc_base/helpers.h"
@@ -20,7 +21,6 @@
 #include "rtc_base/message_digest.h"
 #include "rtc_base/ssl_fingerprint.h"
 #include "rtc_base/ssl_identity.h"
-#include "rtc_base/string_utils.h"
 #include "test/gtest.h"
 
 using rtc::SSLIdentity;
@@ -173,10 +173,10 @@
   info.identity.reset(new rtc::FakeSSLIdentity(info.pems));
   // Strip header/footer and newline characters of PEM strings.
   for (size_t i = 0; i < info.pems.size(); ++i) {
-    rtc::replace_substrs("-----BEGIN CERTIFICATE-----", 27, "", 0,
-                         &info.pems[i]);
-    rtc::replace_substrs("-----END CERTIFICATE-----", 25, "", 0, &info.pems[i]);
-    rtc::replace_substrs("\n", 1, "", 0, &info.pems[i]);
+    absl::StrReplaceAll({{"-----BEGIN CERTIFICATE-----", ""},
+                         {"-----END CERTIFICATE-----", ""},
+                         {"\n", ""}},
+                        &info.pems[i]);
   }
   // Fingerprints for the whole certificate chain, starting with leaf
   // certificate.
diff --git a/rtc_base/string_utils.cc b/rtc_base/string_utils.cc
index 97bf481..dfbb548 100644
--- a/rtc_base/string_utils.cc
+++ b/rtc_base/string_utils.cc
@@ -30,18 +30,6 @@
   return srclen;
 }
 
-void replace_substrs(const char* search,
-                     size_t search_len,
-                     const char* replace,
-                     size_t replace_len,
-                     std::string* s) {
-  size_t pos = 0;
-  while ((pos = s->find(search, pos, search_len)) != std::string::npos) {
-    s->replace(pos, search_len, replace, replace_len);
-    pos += replace_len;
-  }
-}
-
 static const char kWhitespace[] = " \n\r\t";
 
 std::string string_trim(const std::string& s) {
diff --git a/rtc_base/string_utils.h b/rtc_base/string_utils.h
index b4ce209..bc33284 100644
--- a/rtc_base/string_utils.h
+++ b/rtc_base/string_utils.h
@@ -93,13 +93,6 @@
 
 #endif  // WEBRTC_WIN
 
-// Replaces all occurrences of "search" with "replace".
-void replace_substrs(const char* search,
-                     size_t search_len,
-                     const char* replace,
-                     size_t replace_len,
-                     std::string* s);
-
 // Remove leading and trailing whitespaces.
 std::string string_trim(const std::string& s);