1
0

refactor: rename one overload of "replace" in string op into "to_replace" to indicate it produce new instance.

This commit is contained in:
2026-01-22 10:23:26 +08:00
parent 718fe426bf
commit 8989e909ad
4 changed files with 9 additions and 9 deletions

View File

@@ -37,7 +37,7 @@ yycc::string::op provide 2 functions for programmer do string replacement:
\code \code
void replace(std::u8string& strl, const std::u8string_view& from_strl, const std::u8string_view& to_strl); void replace(std::u8string& strl, const std::u8string_view& from_strl, const std::u8string_view& to_strl);
std::u8string replace(const std::u8string_view& strl, const std::u8string_view& from_strl, const std::u8string_view& to_strl); std::u8string to_replace(const std::u8string_view& strl, const std::u8string_view& from_strl, const std::u8string_view& to_strl);
\endcode \endcode
The first overload will do replacement in given string container directly. The first overload will do replacement in given string container directly.

View File

@@ -97,7 +97,7 @@ namespace yycc::string::op {
} }
} }
std::u8string replace(const std::u8string_view& _strl, const std::u8string_view& _from_strl, const std::u8string_view& _to_strl) { std::u8string to_replace(const std::u8string_view& _strl, const std::u8string_view& _from_strl, const std::u8string_view& _to_strl) {
// prepare result // prepare result
std::u8string strl(_strl); std::u8string strl(_strl);
replace(strl, _from_strl, _to_strl); replace(strl, _from_strl, _to_strl);

View File

@@ -66,7 +66,7 @@ namespace yycc::string::op {
* @param[in] _to_strl The \e new string. * @param[in] _to_strl The \e new string.
* @return The result of replacement. * @return The result of replacement.
*/ */
std::u8string replace(const std::u8string_view& _strl, const std::u8string_view& _from_strl, const std::u8string_view& _to_strl); std::u8string to_replace(const std::u8string_view& _strl, const std::u8string_view& _from_strl, const std::u8string_view& _to_strl);
#pragma endregion #pragma endregion

View File

@@ -25,32 +25,32 @@ namespace yycctest::string::op {
TEST(StringOp, Replace) { TEST(StringOp, Replace) {
// Normal case // Normal case
{ {
auto rv = OP::replace(u8"aabbcc", u8"bb", u8"dd"); auto rv = OP::to_replace(u8"aabbcc", u8"bb", u8"dd");
EXPECT_EQ(rv, u8"aaddcc"); EXPECT_EQ(rv, u8"aaddcc");
} }
// No matched expected string // No matched expected string
{ {
auto rv = OP::replace(u8"aabbcc", u8"zz", u8"yy"); auto rv = OP::to_replace(u8"aabbcc", u8"zz", u8"yy");
EXPECT_EQ(rv, u8"aabbcc"); EXPECT_EQ(rv, u8"aabbcc");
} }
// Empty expected string // Empty expected string
{ {
auto rv = OP::replace(u8"aabbcc", std::u8string_view(), u8"zz"); auto rv = OP::to_replace(u8"aabbcc", std::u8string_view(), u8"zz");
EXPECT_EQ(rv, u8"aabbcc"); EXPECT_EQ(rv, u8"aabbcc");
} }
// Empty replace string // Empty replace string
{ {
auto rv = OP::replace(u8"aaaabbaa", u8"aa", u8""); auto rv = OP::to_replace(u8"aaaabbaa", u8"aa", u8"");
EXPECT_EQ(rv, u8"bb"); EXPECT_EQ(rv, u8"bb");
} }
// Nested replacing // Nested replacing
{ {
auto rv = OP::replace(u8"aaxcc", u8"x", u8"yx"); auto rv = OP::to_replace(u8"aaxcc", u8"x", u8"yx");
EXPECT_EQ(rv, u8"aayxcc"); EXPECT_EQ(rv, u8"aayxcc");
} }
// Empty source string // Empty source string
{ {
auto rv = OP::replace(std::u8string_view(), u8"", u8"xy"); auto rv = OP::to_replace(std::u8string_view(), u8"", u8"xy");
EXPECT_EQ(rv, u8""); EXPECT_EQ(rv, u8"");
} }
} }