mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-09-11 21:20:39 +02:00
refactor: use string views in ReplaceAll
PR #25803 changed these parameters to `const std::string&` for `std::regex_replace()`. The literal implementation no longer needs owned strings, so restore the original `std::string_view` interface.
This commit is contained in:
@@ -300,7 +300,7 @@ BOOST_AUTO_TEST_CASE(util_Join)
|
||||
BOOST_AUTO_TEST_CASE(util_ReplaceAll)
|
||||
{
|
||||
const std::string original("A test \"%s\" string '%s'.");
|
||||
auto test_replaceall{[](std::string test, const std::string& search, const std::string& substitute, const std::string& expected) {
|
||||
auto test_replaceall{[](std::string test, std::string_view search, std::string_view substitute, std::string_view expected) {
|
||||
ReplaceAll(test, search, substitute);
|
||||
BOOST_CHECK_EQUAL(test, expected);
|
||||
}};
|
||||
|
||||
@@ -8,9 +8,10 @@
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace util {
|
||||
void ReplaceAll(std::string& in_out, const std::string& search, const std::string& substitute)
|
||||
void ReplaceAll(std::string& in_out, std::string_view search, std::string_view substitute)
|
||||
{
|
||||
if (search.empty()) return;
|
||||
auto pos{in_out.find(search)};
|
||||
|
||||
@@ -99,7 +99,7 @@ struct ConstevalFormatString {
|
||||
};
|
||||
|
||||
/// Replace every non-overlapping occurrence of `search` with `substitute`, treating both literally; the replacement text is not searched again.
|
||||
void ReplaceAll(std::string& in_out, const std::string& search, const std::string& substitute);
|
||||
void ReplaceAll(std::string& in_out, std::string_view search, std::string_view substitute);
|
||||
|
||||
/** Split a string on any char found in separators, returning a vector.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user