alpaqa develop
Nonconvex constrained optimization
Loading...
Searching...
No Matches
string-util.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <numeric>
5#include <ranges>
6#include <string>
7#include <string_view>
8#include <tuple>
9
10namespace alpaqa::util {
11
12/// Split the string @p full on the first occurrence of @p tok.
13/// Returns `(s, "")` if tok was not found.
14inline auto split(std::string_view full, std::string_view tok) {
15 auto tok_pos = full.find(tok);
16 if (tok_pos == full.npos) {
17 std::string_view key = full;
18 std::string_view rem{key.data() + key.size(), 0};
19 return std::make_tuple(key, rem);
20 } else {
21 auto tok_len = tok.size();
22 std::string_view key = full.substr(0, tok_pos);
23 std::string_view rem = full.substr(tok_pos + tok_len);
24 return std::make_tuple(key, rem);
25 }
26}
27
28/// Split the string @p s on the first occurrence of @p tok.
29/// Returns `("", s)` if tok was not found.
30inline auto split_second(std::string_view full, std::string_view tok) {
31 auto tok_pos = full.find(tok);
32 if (tok_pos == full.npos) {
33 std::string_view key{full.data(), 0};
34 std::string_view rem = full;
35 return std::make_tuple(key, rem);
36 } else {
37 auto tok_len = tok.size();
38 std::string_view key = full.substr(0, tok_pos);
39 std::string_view rem = full.substr(tok_pos + tok_len);
40 return std::make_tuple(key, rem);
41 }
42}
43
44/// @see @ref join
45struct join_opt {
46 std::string_view sep = ", ";
47 std::string_view empty = "∅";
48};
49
50/// Join the list of strings into a single string, using the separator given by
51/// @p opt.
52/// @see @ref join_opt
53std::string join(std::ranges::input_range auto strings, join_opt opt = {}) {
54 if (std::ranges::empty(strings))
55 return std::string(opt.empty);
56 auto combine = [&opt](std::string &&acc, const auto &e) {
57 acc += opt.sep;
58 acc += e;
59 return std::move(acc);
60 };
61 auto begin = std::ranges::begin(strings);
62 auto end = std::ranges::end(strings);
63 using std::ranges::next;
64 std::string first{*begin};
65 return std::accumulate(next(begin), end, std::move(first), combine);
66}
67
68/// @see @ref join_quote
70 std::string_view sep = ", ";
71 std::string_view empty = "∅";
72 std::string_view quote_left = "\"";
73 std::string_view quote_right = "\"";
74};
75
76/// Join the list of strings into a single string, using the separator given by
77/// @p opt. Each original string is quoted using the quote strings specified
78/// by @p opt
79/// @see @ref join_quote_opt
80std::string join_quote(std::ranges::input_range auto strings,
81 join_quote_opt opt = {}) {
82 if (std::ranges::empty(strings))
83 return std::string(opt.empty);
84 auto combine = [&opt](std::string &&acc, const auto &e) {
85 acc += opt.quote_right;
86 acc += opt.sep;
87 acc += opt.quote_left;
88 acc += e;
89 return std::move(acc);
90 };
91 auto begin = std::ranges::begin(strings);
92 auto end = std::ranges::end(strings);
93 std::string first{*begin};
94 first.insert(0, opt.quote_left);
95 using std::ranges::next;
96 auto result = std::accumulate(next(begin), end, std::move(first), combine);
97 result += opt.quote_right;
98 return result;
99}
100
101/// Sort the given range of strings in-place in a case-insensitive manner.
103 auto cmp = [](const auto &a, const auto &b) {
104 auto toupper = [](unsigned char c) { return std::toupper(c); };
105 return std::ranges::lexicographical_compare(
106 std::views::transform(a, toupper),
107 std::views::transform(b, toupper));
108 };
109 std::ranges::sort(range, cmp);
110}
111
112} // namespace alpaqa::util
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_view sep
auto split_second(std::string_view full, std::string_view tok)
Split the string s on the first occurrence of tok.
std::string_view empty
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.
void sort_case_insensitive(auto &range)
Sort the given range of strings in-place in a case-insensitive manner.
auto split(std::string_view full, std::string_view tok)
Split the string full on the first occurrence of tok.
constexpr const auto inf
Definition config.hpp:112