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}
This commit is contained in:
Steve Anton
2019-02-14 15:13:09 -08:00
committed by Commit Bot
parent bf9e01ab4e
commit 1c9c9fc9b6
8 changed files with 30 additions and 63 deletions

1
DEPS
View File

@ -1443,6 +1443,7 @@ include_rules = [
"+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",

View File

@ -15,7 +15,8 @@ adds the first use.
* `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`.

View File

@ -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 @@ TEST_P(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
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 @@ TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
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());

View File

@ -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 @@ std::unique_ptr<CertificateInfo> CreateFakeCertificateAndInfoFromDers(
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.

View File

@ -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<AudioCodec> AudioCodecs;
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 bool SdpDeserializeCandidate(const std::string& message,
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 @@ class WebRtcSdpTest : public testing::Test {
// 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 @@ TEST_F(WebRtcSdpTest, SerializeWithSctpDataChannelAndNewPort) {
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 @@ TEST_F(WebRtcSdpTest, DeserializeSdpWithSctpDataChannelAndUnusualPort) {
// 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 @@ TEST_F(WebRtcSdpTest,
// 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));

View File

@ -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 @@ IdentityAndInfo CreateFakeIdentityAndInfoFromDers(
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.

View File

@ -30,18 +30,6 @@ size_t strcpyn(char* buffer,
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) {

View File

@ -93,13 +93,6 @@ inline std::string ToUtf8(const std::wstring& wstr) {
#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);