From e5b22206501ba1edd38018981e0dae8358986a3d Mon Sep 17 00:00:00 2001 From: Ali Tofigh Date: Fri, 25 Mar 2022 00:08:32 +0100 Subject: [PATCH] Adopt absl::string_view in rtc_base/string_utils* Bug: webrtc:13579 Change-Id: I2def83ae546156ff0ec8ef000b7ed0c48d15777c Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/256721 Reviewed-by: Mirko Bonadei Commit-Queue: Ali Tofigh Cr-Commit-Position: refs/heads/main@{#36329} --- rtc_base/http_common.cc | 6 +++--- rtc_base/string_utils.cc | 11 +++-------- rtc_base/string_utils.h | 5 +---- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/rtc_base/http_common.cc b/rtc_base/http_common.cc index 88639a7c70..5ac222f468 100644 --- a/rtc_base/http_common.cc +++ b/rtc_base/http_common.cc @@ -282,7 +282,7 @@ HttpAuthResult HttpAuthenticate(const char* challenge, // std::string decoded = username + ":" + password; size_t len = username.size() + password.GetLength() + 2; char* sensitive = new char[len]; - size_t pos = strcpyn(sensitive, len, username.data(), username.size()); + size_t pos = strcpyn(sensitive, len, username); pos += strcpyn(sensitive + pos, len - pos, ":"); password.CopyTo(sensitive + pos, true); @@ -322,9 +322,9 @@ HttpAuthResult HttpAuthenticate(const char* challenge, // std::string A1 = username + ":" + realm + ":" + password; size_t len = username.size() + realm.size() + password.GetLength() + 3; char* sensitive = new char[len]; // A1 - size_t pos = strcpyn(sensitive, len, username.data(), username.size()); + size_t pos = strcpyn(sensitive, len, username); pos += strcpyn(sensitive + pos, len - pos, ":"); - pos += strcpyn(sensitive + pos, len - pos, realm.c_str()); + pos += strcpyn(sensitive + pos, len - pos, realm); pos += strcpyn(sensitive + pos, len - pos, ":"); password.CopyTo(sensitive + pos, true); diff --git a/rtc_base/string_utils.cc b/rtc_base/string_utils.cc index e8c13464bd..b93e615705 100644 --- a/rtc_base/string_utils.cc +++ b/rtc_base/string_utils.cc @@ -14,20 +14,15 @@ namespace rtc { -size_t strcpyn(char* buffer, - size_t buflen, - const char* source, - size_t srclen /* = SIZE_UNKNOWN */) { +size_t strcpyn(char* buffer, size_t buflen, absl::string_view source) { if (buflen <= 0) return 0; - if (srclen == SIZE_UNKNOWN) { - srclen = strlen(source); - } + size_t srclen = source.length(); if (srclen >= buflen) { srclen = buflen - 1; } - memcpy(buffer, source, srclen); + memcpy(buffer, source.data(), srclen); buffer[srclen] = 0; return srclen; } diff --git a/rtc_base/string_utils.h b/rtc_base/string_utils.h index a9cdd61a7b..d062675a94 100644 --- a/rtc_base/string_utils.h +++ b/rtc_base/string_utils.h @@ -51,10 +51,7 @@ struct AbslStringViewCmp { }; // Safe version of strncpy that always nul-terminate. -size_t strcpyn(char* buffer, - size_t buflen, - const char* source, - size_t srclen = SIZE_UNKNOWN); +size_t strcpyn(char* buffer, size_t buflen, absl::string_view source); /////////////////////////////////////////////////////////////////////////////// // UTF helpers (Windows only)