From: Joel Rosdahl Date: Wed, 4 Aug 2021 17:45:22 +0000 (+0200) Subject: enhance(util): Add replace_first function X-Git-Tag: v4.4~60 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=582dc52b5c5f733db1da72d85a7597697df81264;p=thirdparty%2Fccache.git enhance(util): Add replace_first function --- diff --git a/src/util/string.cpp b/src/util/string.cpp index 88f1c9b54..87055605e 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -21,6 +21,7 @@ #include #include +#include namespace util { @@ -129,6 +130,27 @@ percent_decode(nonstd::string_view string) return result; } +std::string +replace_first(const nonstd::string_view string, + const nonstd::string_view from, + const nonstd::string_view to) +{ + if (from.empty()) { + return std::string(string); + } + + std::string result; + const auto pos = string.find(from); + if (pos != nonstd::string_view::npos) { + result.append(string.data(), pos); + result.append(to.data(), to.length()); + result.append(string.data() + pos + from.size()); + } else { + result = std::string(string); + } + return result; +} + std::pair> split_once(const nonstd::string_view string, const char split_char) { diff --git a/src/util/string.hpp b/src/util/string.hpp index aa9fccf51..4f782c37a 100644 --- a/src/util/string.hpp +++ b/src/util/string.hpp @@ -81,6 +81,11 @@ parse_unsigned(const std::string& value, nonstd::expected percent_decode(nonstd::string_view string); +// Replace the first occurrence of `from` to `to` in `string`. +std::string replace_first(nonstd::string_view string, + nonstd::string_view from, + nonstd::string_view to); + // Split `string` into two parts using `split_char` as the delimiter. The second // part will be `nullopt` if there is no `split_char` in `string.` std::pair> diff --git a/unittest/test_util_string.cpp b/unittest/test_util_string.cpp index e53238ef6..294cdb1ea 100644 --- a/unittest/test_util_string.cpp +++ b/unittest/test_util_string.cpp @@ -187,6 +187,17 @@ TEST_CASE("util::percent_decode") == "invalid percent-encoded string at position 1: a%0g"); } +TEST_CASE("util::replace_first") +{ + CHECK(util::replace_first("", "", "") == ""); + CHECK(util::replace_first("x", "", "") == "x"); + CHECK(util::replace_first("", "x", "") == ""); + CHECK(util::replace_first("", "", "x") == ""); + CHECK(util::replace_first("x", "y", "z") == "x"); + CHECK(util::replace_first("x", "x", "y") == "y"); + CHECK(util::replace_first("xabcyabcz", "abc", "defdef") == "xdefdefyabcz"); +} + TEST_CASE("util::split_once") { using nonstd::nullopt;