Skip to content

Commit 6dd2ad4

Browse files
committed
util: move spanparsing.h Split functions to string.h
This will help move the miniscript / descriptor parsing functions out of the util library in an upcoming commit, so they are not exposed to libbitcoinkernel applications. Moving the Split functions should also make them more discoverable since they now close to related functions like Join. The functions are moved verbatim without any changes.
1 parent 23cc8dd commit 6dd2ad4

File tree

3 files changed

+46
-40
lines changed

3 files changed

+46
-40
lines changed

src/test/descriptor_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <script/sign.h>
88
#include <test/util/setup_common.h>
99
#include <util/strencodings.h>
10+
#include <util/string.h>
1011

1112
#include <boost/test/unit_test.hpp>
1213

@@ -400,7 +401,6 @@ void CheckInferDescriptor(const std::string& script_hex, const std::string& expe
400401
provider.pubkeys.emplace(origin_pubkey.GetID(), origin_pubkey);
401402

402403
if (!origin_str.empty()) {
403-
using namespace spanparsing;
404404
KeyOriginInfo info;
405405
Span<const char> origin_sp{origin_str};
406406
std::vector<Span<const char>> origin_split = Split(origin_sp, "/");

src/util/spanparsing.h

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
#define BITCOIN_UTIL_SPANPARSING_H
77

88
#include <span.h>
9+
#include <util/string.h>
910

1011
#include <string>
11-
#include <string_view>
12-
#include <vector>
1312

1413
namespace spanparsing {
1514

@@ -37,41 +36,11 @@ bool Func(const std::string& str, Span<const char>& sp);
3736
*/
3837
Span<const char> Expr(Span<const char>& sp);
3938

40-
/** Split a string on any char found in separators, returning a vector.
41-
*
42-
* If sep does not occur in sp, a singleton with the entirety of sp is returned.
43-
*
44-
* Note that this function does not care about braces, so splitting
45-
* "foo(bar(1),2),3) on ',' will return {"foo(bar(1)", "2)", "3)"}.
46-
*/
47-
template <typename T = Span<const char>>
48-
std::vector<T> Split(const Span<const char>& sp, std::string_view separators)
49-
{
50-
std::vector<T> ret;
51-
auto it = sp.begin();
52-
auto start = it;
53-
while (it != sp.end()) {
54-
if (separators.find(*it) != std::string::npos) {
55-
ret.emplace_back(start, it);
56-
start = it + 1;
57-
}
58-
++it;
59-
}
60-
ret.emplace_back(start, it);
61-
return ret;
62-
}
63-
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)
39+
/** Split alias for backwards compatibility */
40+
template <typename... Args>
41+
auto Split(Args&&... args)
7342
{
74-
return Split<T>(sp, std::string_view{&sep, 1});
43+
return ::Split(std::forward<Args>(args)...);
7544
}
7645

7746
} // namespace spanparsing

src/util/string.h

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef BITCOIN_UTIL_STRING_H
66
#define BITCOIN_UTIL_STRING_H
77

8-
#include <util/spanparsing.h>
8+
#include <span.h>
99

1010
#include <array>
1111
#include <cstdint>
@@ -18,14 +18,51 @@
1818

1919
void ReplaceAll(std::string& in_out, const std::string& search, const std::string& substitute);
2020

21+
/** Split a string on any char found in separators, returning a vector.
22+
*
23+
* If sep does not occur in sp, a singleton with the entirety of sp is returned.
24+
*
25+
* Note that this function does not care about braces, so splitting
26+
* "foo(bar(1),2),3) on ',' will return {"foo(bar(1)", "2)", "3)"}.
27+
*/
28+
template <typename T = Span<const char>>
29+
std::vector<T> Split(const Span<const char>& sp, std::string_view separators)
30+
{
31+
std::vector<T> ret;
32+
auto it = sp.begin();
33+
auto start = it;
34+
while (it != sp.end()) {
35+
if (separators.find(*it) != std::string::npos) {
36+
ret.emplace_back(start, it);
37+
start = it + 1;
38+
}
39+
++it;
40+
}
41+
ret.emplace_back(start, it);
42+
return ret;
43+
}
44+
45+
/** Split a string on every instance of sep, returning a vector.
46+
*
47+
* If sep does not occur in sp, a singleton with the entirety of sp is returned.
48+
*
49+
* Note that this function does not care about braces, so splitting
50+
* "foo(bar(1),2),3) on ',' will return {"foo(bar(1)", "2)", "3)"}.
51+
*/
52+
template <typename T = Span<const char>>
53+
std::vector<T> Split(const Span<const char>& sp, char sep)
54+
{
55+
return Split<T>(sp, std::string_view{&sep, 1});
56+
}
57+
2158
[[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, char sep)
2259
{
23-
return spanparsing::Split<std::string>(str, sep);
60+
return Split<std::string>(str, sep);
2461
}
2562

2663
[[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, std::string_view separators)
2764
{
28-
return spanparsing::Split<std::string>(str, separators);
65+
return Split<std::string>(str, separators);
2966
}
3067

3168
[[nodiscard]] inline std::string_view TrimStringView(std::string_view str, std::string_view pattern = " \f\n\r\t\v")

0 commit comments

Comments
 (0)