-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_utils.hpp
209 lines (177 loc) · 6.49 KB
/
string_utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) 2022 Francesco Cavaliere
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef CAV_INCLUDE_STRINGUTILS_HPP
#define CAV_INCLUDE_STRINGUTILS_HPP
#include <charconv>
#include <ranges>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
#include "../comptime/enum_name.hpp"
#include "../comptime/type_name.hpp"
#if __has_include(<fmt/core.h>)
#define CAV_FOUND_FMT
#include <fmt/core.h>
#endif
#define SPACES " \t\n\r\f\v"
namespace cav {
constexpr std::string_view filename(std::string_view string) {
int i = static_cast<int>(std::size(string)) - 1;
while (i >= 0 && string[i] != '/')
--i;
return {std::begin(string) + i + 1, std::end(string)};
}
constexpr std::string_view remove_extension(std::string_view string) {
int i = static_cast<int>(std::size(string)) - 1;
while (i > 0 && string[i] != '.')
--i;
if (i > 0)
return {std::begin(string), std::begin(string) + i};
return string;
}
// Inplace with string
static inline void ltrim(std::string& s, char const* delim = SPACES) {
auto pos = s.find_first_not_of(delim);
if (pos != std::string::npos)
s.erase(pos);
}
static inline void rtrim(std::string& s, char const* delim = SPACES) {
auto pos = s.find_last_not_of(delim);
if (pos != std::string::npos)
s.erase(0, pos + 1);
}
inline void trim(std::string& s, char const* delim = SPACES) {
ltrim(s, delim);
rtrim(s, delim);
}
// Remove multiple adjacent chars listed in delim, leaving only delim[0] as delimiter char
static inline void remove_multiple_adj(std::string& s, std::string_view delim = SPACES) {
auto s_wit = s.begin();
bool prev_is_delim = false;
for (char& c : s) {
if (std::ranges::find(delim, c) != delim.end()) {
if (!prev_is_delim) {
*s_wit = delim[0];
++s_wit;
prev_is_delim = true;
}
} else {
*s_wit = c;
++s_wit;
prev_is_delim = false;
}
}
s.erase(s_wit, s.end());
}
// Return "new" object with string_view (which are only a proxy on the same memory)
static inline std::string_view ltrim(std::string_view s, char const* delim = SPACES) {
auto pos = s.find_first_not_of(delim);
if (pos != std::string_view::npos)
return s.substr(pos);
return {};
}
static inline std::string_view rtrim(std::string_view s, char const* delim = SPACES) {
auto pos = s.find_last_not_of(delim);
if (pos != std::string_view::npos)
return s.substr(0, pos + 1);
return {};
}
static inline std::string_view trim(std::string_view s, char const* delim = SPACES) {
return rtrim(ltrim(s, delim), delim);
}
inline std::string remove_multiple_adj(std::string_view& s, char const* delim = SPACES) {
std::string s_new(s);
remove_multiple_adj(s_new, delim);
return s_new;
}
inline auto split_line(std::string_view line, std::string_view delim = SPACES) {
std::vector<std::string_view> tokens;
int lsize = static_cast<int>(line.size());
int last_i = 0;
for (int i = 0; i < lsize; ++i) {
if (std::ranges::find(delim, line[i]) != delim.end()) {
if (i > last_i) {
tokens.emplace_back(line.data() + last_i, i - last_i);
tokens.back() = trim(tokens.back());
}
last_i = i + 1;
}
}
if (last_i < lsize) {
tokens.emplace_back(line.data() + last_i, lsize - last_i);
tokens.back() = trim(tokens.back());
}
return tokens;
}
inline auto split_line(std::string_view line, char delim) {
return split_line(line, std::string(1, delim));
}
template <typename T>
auto from_string_view(std::string_view s, T& val) {
return std::from_chars(s.data(), s.data() + s.size(), val);
}
#ifdef CAV_FOUND_FMT
template <typename T, typename OnErrT = decltype([] { abort(); })>
void from_string_view_checked(std::string_view str, T& val, OnErrT&& on_error = {}) {
auto res = std::from_chars(str.data(), str.data() + str.size(), val);
if (res.ec == std::errc::invalid_argument) {
fmt::print(stderr,
"Error: Unable to parse {} into variable of type {}.\n",
str,
static_cast<std::string_view>(type_name<T>::name));
on_error();
}
if (res.ec == std::errc::result_out_of_range) {
fmt::print(stderr,
"Error: Found value larger than type max ({} > {}).\n",
str,
static_cast<std::string_view>(type_name<T>::name));
on_error();
}
}
template <typename T, typename OnErrT = decltype([] { abort(); })>
requires std::is_enum_v<T>
void from_string_view_checked(std::string_view str, T& val, OnErrT&& on_error = {}) {
using utype = std::underlying_type_t<T>;
for (utype i = 0; i < enum_size<T>::value; ++i)
if (enum_name<T>(static_cast<T>(i)) == str) {
val = static_cast<T>(i);
return;
}
fmt::print(stderr, "Error: enum value {} not found in enum {}\n", str, type_name<T>::name);
on_error();
}
template <typename OnErrT = decltype([] { abort(); })>
constexpr void from_string_view_checked(std::string_view str,
std::string_view& val,
OnErrT&& /*err*/ = {}) {
val = str;
}
template <typename OnErrT = decltype([] { abort(); })>
constexpr void from_string_view_checked(std::string_view str,
char const*& val,
OnErrT&& /*err*/ = {}) {
val = str.data();
}
template <typename OnErrT = decltype([] { abort(); })>
constexpr void from_string_view_checked(std::string_view str,
std::string& val,
OnErrT&& /*err*/ = {}) {
val = str;
}
#endif
} // namespace cav
#endif /* CAV_INCLUDE_STRINGUTILS_HPP */