Bitcoin ABC 0.34.2
P2P Digital Currency
string.cpp
Go to the documentation of this file.
1// Copyright (c) 2019 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <util/string.h>
6
7#include <string>
8#include <string_view>
9
10namespace util {
11void ReplaceAll(std::string &in_out, std::string_view search,
12 std::string_view substitute) {
13 if (search.empty()) {
14 return;
15 }
16 auto pos{in_out.find(search)};
17 if (pos == std::string::npos) {
18 return;
19 }
20
21 // Build separately because repeated std::string::replace() calls move the
22 // remaining suffix when sizes differ
23 std::string result;
24 result.reserve(in_out.size());
25 std::string::size_type start{0};
26 for (; pos != std::string::npos; pos = in_out.find(search, start)) {
27 result.append(in_out, start, pos - start).append(substitute);
28 start = pos + search.size();
29 }
30 result.append(in_out, start);
31 in_out.swap(result);
32}
33} // namespace util
void ReplaceAll(std::string &in_out, std::string_view search, std::string_view substitute)
Replace every non-overlapping occurrence of search with substitute, treating both literally; the repl...
Definition: string.cpp:11