21inline auto split(std::string_view full, std::string_view tok) {
22 auto tok_pos = full.find(tok);
23 if (tok_pos == full.npos) {
24 std::string_view key = full;
25 std::string_view rem{key.data() + key.size(), 0};
26 return std::make_tuple(key, rem);
28 auto tok_len = tok.size();
29 std::string_view key = full.substr(0, tok_pos);
30 std::string_view rem = full.substr(tok_pos + tok_len);
31 return std::make_tuple(key, rem);
37inline auto split_second(std::string_view full, std::string_view tok) {
38 auto tok_pos = full.find(tok);
39 if (tok_pos == full.npos) {
40 std::string_view key{full.data(), 0};
41 std::string_view rem = full;
42 return std::make_tuple(key, rem);
44 auto tok_len = tok.size();
45 std::string_view key = full.substr(0, tok_pos);
46 std::string_view rem = full.substr(tok_pos + tok_len);
47 return std::make_tuple(key, rem);
53 std::string_view
sep =
", ";
60std::string
join(std::ranges::input_range
auto strings,
join_opt opt = {}) {
61 if (std::ranges::empty(strings))
62 return std::string(opt.empty);
63 auto combine = [&opt](std::string &&acc,
const auto &e) {
66 return std::move(acc);
68 auto begin = std::ranges::begin(strings);
69 auto end = std::ranges::end(strings);
70 using std::ranges::next;
71 std::string first{*begin};
72 return std::accumulate(next(begin), end, std::move(first), combine);
77 std::string_view
sep =
", ";
87std::string
join_quote(std::ranges::input_range
auto strings,
89 if (std::ranges::empty(strings))
90 return std::string(opt.empty);
91 auto combine = [&opt](std::string &&acc,
const auto &e) {
92 acc += opt.quote_right;
94 acc += opt.quote_left;
96 return std::move(acc);
98 auto begin = std::ranges::begin(strings);
99 auto end = std::ranges::end(strings);
100 std::string first{*begin};
101 first.insert(0, opt.quote_left);
102 using std::ranges::next;
103 auto result = std::accumulate(next(begin), end, std::move(first), combine);
104 result += opt.quote_right;
110 auto cmp = [](
const auto &a,
const auto &b) {
111 auto toupper = [](
unsigned char c) {
return std::toupper(c); };
112 return std::ranges::lexicographical_compare(
113 std::views::transform(a, toupper),
114 std::views::transform(b, toupper));
116 std::ranges::sort(range, cmp);
std::string_view quote_right
std::string_view quote_left
void sort_case_insensitive(auto &range)
Sort the given range of strings in-place in a case-insensitive manner.
std::string join_quote(std::ranges::input_range auto strings, join_quote_opt opt={})
Join the list of strings into a single string, using the separator given by opt.
std::string join(std::ranges::input_range auto strings, join_opt opt={})
Join the list of strings into a single string, using the separator given by opt.
auto split_second(std::string_view full, std::string_view tok)
Split the string s on the first occurrence of tok.
auto split(std::string_view full, std::string_view tok)
Split the string full on the first occurrence of tok.