alpaqa 1.0.0a14
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 return std::make_tuple(full, std::string_view{});
18 auto tok_len = tok.size();
19 std::string_view key = full.substr(0, tok_pos);
20 std::string_view rem = full.substr(tok_pos + tok_len);
21 return std::make_tuple(key, rem);
22}
23
24/// Split the string @p s on the first occurrence of @p tok.
25/// Returns `("", s)` if tok was not found.
26inline auto split_second(std::string_view full, std::string_view tok) {
27 auto tok_pos = full.find(tok);
28 if (tok_pos == full.npos)
29 return std::make_tuple(std::string_view{}, full);
30 auto tok_len = tok.size();
31 std::string_view key = full.substr(0, tok_pos);
32 std::string_view rem = full.substr(tok_pos + tok_len);
33 return std::make_tuple(key, rem);
34}
35
36/// @see @ref join
37struct join_opt {
38 std::string_view sep = ", ";
39 std::string_view empty = "∅";
40};
41
42/// Join the list of strings into a single string, using the separator given by
43/// @p opt.
44/// @see @ref join_opt
45std::string join(std::ranges::input_range auto strings, join_opt opt = {}) {
46 if (std::ranges::empty(strings))
47 return std::string(opt.empty);
48 auto combine = [&opt](std::string &&acc, const auto &e) {
49 acc += opt.sep;
50 acc += e;
51 return std::move(acc);
52 };
53 auto begin = std::ranges::begin(strings);
54 auto end = std::ranges::end(strings);
55 using std::ranges::next;
56 std::string first{*begin};
57 return std::accumulate(next(begin), end, std::move(first), combine);
58}
59
60/// @see @ref join_quote
62 std::string_view sep = ", ";
63 std::string_view empty = "∅";
64 std::string_view quote_left = "\"";
65 std::string_view quote_right = "\"";
66};
67
68/// Join the list of strings into a single string, using the separator given by
69/// @p opt. Each original string is quoted using the quote strings specified
70/// by @p opt
71/// @see @ref join_quote_opt
72std::string join_quote(std::ranges::input_range auto strings,
73 join_quote_opt opt = {}) {
74 if (std::ranges::empty(strings))
75 return std::string(opt.empty);
76 auto combine = [&opt](std::string &&acc, const auto &e) {
77 acc += opt.quote_right;
78 acc += opt.sep;
79 acc += opt.quote_left;
80 acc += e;
81 return std::move(acc);
82 };
83 auto begin = std::ranges::begin(strings);
84 auto end = std::ranges::end(strings);
85 std::string first{*begin};
86 first.insert(0, opt.quote_left);
87 using std::ranges::next;
88 auto result = std::accumulate(next(begin), end, std::move(first), combine);
89 result += opt.quote_right;
90 return result;
91}
92
93/// Sort the given range of strings in-place in a case-insensitive manner.
95 auto cmp = [](const auto &a, const auto &b) {
96 auto toupper = [](unsigned char c) { return std::toupper(c); };
97 return std::ranges::lexicographical_compare(
98 std::views::transform(a, toupper),
99 std::views::transform(b, toupper));
100 };
101 std::ranges::sort(range, cmp);
102}
103
104} // 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:85