From: huangqinjin Date: Tue, 10 Sep 2024 17:46:42 +0000 (+0800) Subject: fix: Fix util::replace_all for string_view to non-C string (#1502) X-Git-Tag: v4.11~83 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=590c6e3b476b5fe4d6f87cdc7ec4dfc36216b243;p=thirdparty%2Fccache.git fix: Fix util::replace_all for string_view to non-C string (#1502) --- diff --git a/src/ccache/util/string.cpp b/src/ccache/util/string.cpp index 111dac84..cf4fd81e 100644 --- a/src/ccache/util/string.cpp +++ b/src/ccache/util/string.cpp @@ -399,7 +399,7 @@ replace_all(const std::string_view string, while (left < string.size()) { size_t right = string.find(from, left); if (right == std::string_view::npos) { - result.append(string.data() + left); + result.append(string.data() + left, string.size() - left); break; } result.append(string.data() + left, right - left); diff --git a/unittest/test_util_string.cpp b/unittest/test_util_string.cpp index 203fad6d..6714d93e 100644 --- a/unittest/test_util_string.cpp +++ b/unittest/test_util_string.cpp @@ -494,6 +494,7 @@ TEST_CASE("util::replace_all") CHECK(util::replace_all("xabc", "abc", "defdef") == "xdefdef"); CHECK(util::replace_all("abcx", "abc", "defdef") == "defdefx"); CHECK(util::replace_all("xabcyabcz", "abc", "defdef") == "xdefdefydefdefz"); + CHECK(util::replace_all(std::string_view("xaxbc", 4), "x", "y") == "yayb"); } TEST_CASE("util::replace_first")