From 3dbf13e00b9dcb5106e54745966a2a8a62c9e620 Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sat, 3 Apr 2021 23:43:28 +0200 Subject: [PATCH] utils: cleanup ReplaceAll --- src/zm_rtsp_auth.cpp | 2 +- src/zm_utils.cpp | 10 +++++----- src/zm_utils.h | 3 +-- tests/zm_utils.cpp | 18 +++++++++--------- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/zm_rtsp_auth.cpp b/src/zm_rtsp_auth.cpp index 9351300ca..5e6dcc655 100644 --- a/src/zm_rtsp_auth.cpp +++ b/src/zm_rtsp_auth.cpp @@ -92,7 +92,7 @@ void Authenticator::authHandleHeader(std::string headerData) { } // end void Authenticator::authHandleHeader(std::string headerData) std::string Authenticator::quote( const std::string &src ) { - return replaceAll(replaceAll(src, "\\", "\\\\"), "\"", "\\\""); + return ReplaceAll(ReplaceAll(src, "\\", "\\\\"), "\"", "\\\""); } std::string Authenticator::getAuthHeader(std::string method, std::string uri) { diff --git a/src/zm_utils.cpp b/src/zm_utils.cpp index 21feb171d..0f2cc307e 100644 --- a/src/zm_utils.cpp +++ b/src/zm_utils.cpp @@ -50,13 +50,13 @@ std::string Trim(const std::string &str, const std::string &char_set) { return str.substr(start_pos, end_pos - start_pos + 1); } -std::string replaceAll(std::string str, std::string from, std::string to) { - if ( from.empty() ) +std::string ReplaceAll(std::string str, const std::string &old_value, const std::string &new_value) { + if (old_value.empty()) return str; size_t start_pos = 0; - while ( (start_pos = str.find(from, start_pos)) != std::string::npos ) { - str.replace(start_pos, from.length(), to); - start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' + while ((start_pos = str.find(old_value, start_pos)) != std::string::npos) { + str.replace(start_pos, old_value.length(), new_value); + start_pos += new_value.length(); // In case 'new_value' contains 'old_value', like replacing 'x' with 'yx' } return str; } diff --git a/src/zm_utils.h b/src/zm_utils.h index ce66cd4bf..692d87acf 100644 --- a/src/zm_utils.h +++ b/src/zm_utils.h @@ -33,8 +33,7 @@ typedef std::vector StringVector; std::string Trim(const std::string &str, const std::string &char_set); inline std::string TrimSpaces(const std::string &str) { return Trim(str, " \t"); } - -std::string replaceAll(std::string str, std::string from, std::string to); +std::string ReplaceAll(std::string str, const std::string& old_value, const std::string& new_value); template std::string stringtf(const std::string &format, Args... args) { diff --git a/tests/zm_utils.cpp b/tests/zm_utils.cpp index 9590d0d29..312d60920 100644 --- a/tests/zm_utils.cpp +++ b/tests/zm_utils.cpp @@ -51,18 +51,18 @@ TEST_CASE("TrimSpaces") { REQUIRE(TrimSpaces("\t te st \t") == "te st"); } -TEST_CASE("replaceAll") { - REQUIRE(replaceAll("", "", "") == ""); +TEST_CASE("ReplaceAll") { + REQUIRE(ReplaceAll("", "", "") == ""); - REQUIRE(replaceAll("a", "", "b") == "a"); - REQUIRE(replaceAll("a", "a", "b") == "b"); - REQUIRE(replaceAll("a", "b", "c") == "a"); + REQUIRE(ReplaceAll("a", "", "b") == "a"); + REQUIRE(ReplaceAll("a", "a", "b") == "b"); + REQUIRE(ReplaceAll("a", "b", "c") == "a"); - REQUIRE(replaceAll("aa", "a", "b") == "bb"); - REQUIRE(replaceAll("aba", "a", "c") == "cbc"); + REQUIRE(ReplaceAll("aa", "a", "b") == "bb"); + REQUIRE(ReplaceAll("aba", "a", "c") == "cbc"); - REQUIRE(replaceAll("aTOKENa", "TOKEN", "VAL") == "aVALa"); - REQUIRE(replaceAll("aTOKENaTOKEN", "TOKEN", "VAL") == "aVALaVAL"); + REQUIRE(ReplaceAll("aTOKENa", "TOKEN", "VAL") == "aVALa"); + REQUIRE(ReplaceAll("aTOKENaTOKEN", "TOKEN", "VAL") == "aVALaVAL"); } TEST_CASE("startsWith") {