Skip to content

Commit bde5836

Browse files
committed
Merge bitcoin/bitcoin#25057: refactor: replace remaining boost::split with SplitString
f849e63 fuzz: SplitString with multiple separators (Martin Leitner-Ankerl) d1a9850 http: replace boost::split with SplitString (Martin Leitner-Ankerl) 0d7efcd core_read: Replace boost::split with SplitString (Martin Leitner-Ankerl) b7ab9db Extend Split to work with multiple separators (Martin Leitner-Ankerl) Pull request description: As a followup of #22953, this removes the remaining occurrences of `boost::split` and replaces them with our own `SplitString`. To be able to do so, this extends the function `spanparsing::Split` to work with multiple separators. Finally this removes 3 more files from `lint-includes.py`. ACKs for top commit: theStack: Code-review ACK f849e63 Tree-SHA512: f37d4dbe11cab2046e646045b0f018a75f978d521443a2c5001512737a1370e22b09247d5db0e5c9e4153229a4e2d66731903c1bba3713711c4cae8cedcc775d
2 parents 9183c66 + f849e63 commit bde5836

File tree

7 files changed

+49
-18
lines changed

7 files changed

+49
-18
lines changed

src/core_read.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
#include <util/strencodings.h>
1515
#include <version.h>
1616

17-
#include <boost/algorithm/string/classification.hpp>
18-
#include <boost/algorithm/string/split.hpp>
19-
2017
#include <algorithm>
2118
#include <string>
2219

@@ -66,12 +63,11 @@ CScript ParseScript(const std::string& s)
6663
{
6764
CScript result;
6865

69-
std::vector<std::string> words;
70-
boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
66+
std::vector<std::string> words = SplitString(s, " \t\n");
7167

7268
for (const std::string& w : words) {
7369
if (w.empty()) {
74-
// Empty string, ignore. (boost::split given '' will return one word)
70+
// Empty string, ignore. (SplitString doesn't combine multiple separators)
7571
} else if (std::all_of(w.begin(), w.end(), ::IsDigit) ||
7672
(w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
7773
{

src/httprpc.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
#include <string>
2222
#include <vector>
2323

24-
#include <boost/algorithm/string.hpp>
25-
2624
/** WWW-Authenticate to present with 401 Unauthorized response */
2725
static const char* WWW_AUTH_HEADER_DATA = "Basic realm=\"jsonrpc\"";
2826

@@ -276,8 +274,10 @@ static bool InitRPCAuthentication()
276274
std::set<std::string>& whitelist = g_rpc_whitelist[strUser];
277275
if (pos != std::string::npos) {
278276
std::string strWhitelist = strRPCWhitelist.substr(pos + 1);
279-
std::set<std::string> new_whitelist;
280-
boost::split(new_whitelist, strWhitelist, boost::is_any_of(", "));
277+
std::vector<std::string> whitelist_split = SplitString(strWhitelist, ", ");
278+
std::set<std::string> new_whitelist{
279+
std::make_move_iterator(whitelist_split.begin()),
280+
std::make_move_iterator(whitelist_split.end())};
281281
if (intersect) {
282282
std::set<std::string> tmp_whitelist;
283283
std::set_intersection(new_whitelist.begin(), new_whitelist.end(),

src/test/fuzz/string.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,12 @@ FUZZ_TARGET(string)
224224
int64_t amount_out;
225225
(void)ParseFixedPoint(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 1024), &amount_out);
226226
}
227-
(void)SplitString(random_string_1, fuzzed_data_provider.ConsumeIntegral<char>());
227+
{
228+
const auto single_split{SplitString(random_string_1, fuzzed_data_provider.ConsumeIntegral<char>())};
229+
assert(single_split.size() >= 1);
230+
const auto any_split{SplitString(random_string_1, random_string_2)};
231+
assert(any_split.size() >= 1);
232+
}
228233
{
229234
(void)Untranslated(random_string_1);
230235
const bilingual_str bs1{random_string_1, random_string_2};

src/test/util_tests.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,6 +2396,19 @@ BOOST_AUTO_TEST_CASE(test_SplitString)
23962396
BOOST_CHECK_EQUAL(result.size(), 1);
23972397
BOOST_CHECK_EQUAL(result[0], "AAA");
23982398
}
2399+
2400+
// multiple split characters
2401+
{
2402+
using V = std::vector<std::string>;
2403+
BOOST_TEST(SplitString("a,b.c:d;e", ",;") == V({"a", "b.c:d", "e"}));
2404+
BOOST_TEST(SplitString("a,b.c:d;e", ",;:.") == V({"a", "b", "c", "d", "e"}));
2405+
BOOST_TEST(SplitString("a,b.c:d;e", "") == V({"a,b.c:d;e"}));
2406+
BOOST_TEST(SplitString("aaa", "bcdefg") == V({"aaa"}));
2407+
BOOST_TEST(SplitString("x\0a,b"s, "\0"s) == V({"x", "a,b"}));
2408+
BOOST_TEST(SplitString("x\0a,b"s, '\0') == V({"x", "a,b"}));
2409+
BOOST_TEST(SplitString("x\0a,b"s, "\0,"s) == V({"x", "a", "b"}));
2410+
BOOST_TEST(SplitString("abcdefg", "bcd") == V({"a", "", "", "efg"}));
2411+
}
23992412
}
24002413

24012414
BOOST_AUTO_TEST_CASE(test_LogEscapeMessage)

src/util/spanparsing.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <span.h>
99

1010
#include <string>
11+
#include <string_view>
1112
#include <vector>
1213

1314
namespace spanparsing {
@@ -36,21 +37,21 @@ bool Func(const std::string& str, Span<const char>& sp);
3637
*/
3738
Span<const char> Expr(Span<const char>& sp);
3839

39-
/** Split a string on every instance of sep, returning a vector.
40+
/** Split a string on any char found in separators, returning a vector.
4041
*
4142
* If sep does not occur in sp, a singleton with the entirety of sp is returned.
4243
*
4344
* Note that this function does not care about braces, so splitting
4445
* "foo(bar(1),2),3) on ',' will return {"foo(bar(1)", "2)", "3)"}.
4546
*/
4647
template <typename T = Span<const char>>
47-
std::vector<T> Split(const Span<const char>& sp, char sep)
48+
std::vector<T> Split(const Span<const char>& sp, std::string_view separators)
4849
{
4950
std::vector<T> ret;
5051
auto it = sp.begin();
5152
auto start = it;
5253
while (it != sp.end()) {
53-
if (*it == sep) {
54+
if (separators.find(*it) != std::string::npos) {
5455
ret.emplace_back(start, it);
5556
start = it + 1;
5657
}
@@ -60,6 +61,19 @@ std::vector<T> Split(const Span<const char>& sp, char sep)
6061
return ret;
6162
}
6263

64+
/** Split a string on every instance of sep, returning a vector.
65+
*
66+
* If sep does not occur in sp, a singleton with the entirety of sp is returned.
67+
*
68+
* Note that this function does not care about braces, so splitting
69+
* "foo(bar(1),2),3) on ',' will return {"foo(bar(1)", "2)", "3)"}.
70+
*/
71+
template <typename T = Span<const char>>
72+
std::vector<T> Split(const Span<const char>& sp, char sep)
73+
{
74+
return Split<T>(sp, std::string_view{&sep, 1});
75+
}
76+
6377
} // namespace spanparsing
6478

6579
#endif // BITCOIN_UTIL_SPANPARSING_H

src/util/string.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@
1414
#include <locale>
1515
#include <sstream>
1616
#include <string>
17+
#include <string_view>
1718
#include <vector>
1819

1920
[[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, char sep)
2021
{
2122
return spanparsing::Split<std::string>(str, sep);
2223
}
2324

25+
[[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, std::string_view separators)
26+
{
27+
return spanparsing::Split<std::string>(str, separators);
28+
}
29+
2430
[[nodiscard]] inline std::string_view TrimStringView(std::string_view str, std::string_view pattern = " \f\n\r\t\v")
2531
{
2632
std::string::size_type front = str.find_first_not_of(pattern);

test/lint/lint-includes.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
"src/minisketch/",
2222
"src/univalue/"]
2323

24-
EXPECTED_BOOST_INCLUDES = ["boost/algorithm/string.hpp",
25-
"boost/algorithm/string/classification.hpp",
26-
"boost/algorithm/string/replace.hpp",
27-
"boost/algorithm/string/split.hpp",
24+
EXPECTED_BOOST_INCLUDES = ["boost/algorithm/string/replace.hpp",
2825
"boost/date_time/posix_time/posix_time.hpp",
2926
"boost/multi_index/hashed_index.hpp",
3027
"boost/multi_index/ordered_index.hpp",

0 commit comments

Comments
 (0)