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
210
211
212
213
214
215
216
217
218
219
220
221
|
#include "common.hh"
#include "strutil.hh"
#include <errno.h>
#include <limits>
#include <stdlib.h>
namespace str {
namespace {
// Sadly strtoul(l) doesn't treat negative values an error but instead returns
// ULONG_MAX - value which is indistinguable from ULONG_MAX - value the positive
// way.
bool is_negative(std::string const& str) {
for (auto i = str.begin(); i != str.end(); ++i) {
if (!isspace(*i)) {
return *i == '-';
}
}
return false;
}
} // namespace
std::optional<uint16_t> parse_uint16(std::string const& str) {
static_assert(sizeof(unsigned long) >= sizeof(uint16_t),
"Need unsigned long to be >= uint16_t");
if (str.empty() || is_negative(str))
return std::nullopt;
char* end = nullptr;
errno = 0;
auto tmp = strtoul(str.c_str(), &end, 10);
if (errno || end != str.c_str() + str.size() ||
tmp > std::numeric_limits<uint16_t>::max())
return std::nullopt;
return static_cast<uint16_t>(tmp);
}
std::optional<uint32_t> parse_uint32(std::string const& str) {
static_assert(sizeof(unsigned long long) >= sizeof(uint32_t),
"Need unsigned long long to be >= uint32_t");
if (str.empty() || is_negative(str))
return std::nullopt;
char* end = nullptr;
errno = 0;
auto tmp = strtoull(str.c_str(), &end, 10);
if (errno || end != str.c_str() + str.size() ||
tmp > std::numeric_limits<uint32_t>::max())
return std::nullopt;
return static_cast<uint32_t>(tmp);
}
std::optional<uint64_t> parse_uint64(std::string const& str) {
static_assert(sizeof(unsigned long long) >= sizeof(uint64_t),
"Need unsigned long long to be >= uint64_t");
if (str.empty() || is_negative(str))
return std::nullopt;
char* end = nullptr;
errno = 0;
auto tmp = strtoull(str.c_str(), &end, 10);
if (errno || end != str.c_str() + str.size() ||
tmp > std::numeric_limits<uint64_t>::max())
return std::nullopt;
return static_cast<uint64_t>(tmp);
}
std::vector<std::string_view> split(std::string_view str, char delim) {
std::vector<std::string_view> ret;
size_t last = 0;
while (true) {
size_t next = str.find(delim, last);
if (next == std::string::npos)
break;
if (next > last)
ret.push_back(str.substr(last, next - last));
last = next + 1;
}
if (last < str.size() || ret.empty())
ret.push_back(str.substr(last));
return ret;
}
std::vector<std::string> split(std::string const& str, char delim) {
std::vector<std::string> ret;
size_t last = 0;
while (true) {
size_t next = str.find(delim, last);
if (next == std::string::npos)
break;
if (next > last)
ret.push_back(str.substr(last, next - last));
last = next + 1;
}
if (last < str.size() || ret.empty())
ret.push_back(str.substr(last));
return ret;
}
std::string join(std::vector<std::string> const& in, char delim) {
std::string ret;
join(in, delim, ret);
return ret;
}
std::string join(std::vector<std::string> const& in, std::string_view delim) {
std::string ret;
join(in, delim, ret);
return ret;
}
void join(std::vector<std::string> const& in, char delim, std::string& out) {
join(in, std::string_view(&delim, 1), out);
}
void join(std::vector<std::string> const& in, std::string_view delim,
std::string& out) {
auto it = in.begin();
if (it == in.end())
return;
out.append(*it);
for (++it; it != in.end(); ++it) {
out.append(delim);
out.append(*it);
}
}
std::string join(std::vector<std::string_view> const& in, char delim) {
std::string ret;
join(in, delim, ret);
return ret;
}
std::string join(std::vector<std::string_view> const& in,
std::string_view delim) {
std::string ret;
join(in, delim, ret);
return ret;
}
void join(std::vector<std::string_view> const& in, char delim,
std::string& out) {
join(in, std::string_view(&delim, 1), out);
}
void join(std::vector<std::string_view> const& in, std::string_view delim,
std::string& out) {
auto it = in.begin();
if (it == in.end())
return;
out.append(*it);
for (++it; it != in.end(); ++it) {
out.append(delim);
out.append(*it);
}
}
bool starts_with(std::string_view str, std::string_view prefix) {
return str.size() >= prefix.size() &&
str.compare(0, prefix.size(), prefix) == 0;
}
bool ends_with(std::string_view str, std::string_view suffix) {
return str.size() >= suffix.size() &&
str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}
std::string_view trim(std::string_view str) {
return ltrim(rtrim(str));
}
std::string_view ltrim(std::string_view str) {
size_t start = 0;
while (start < str.size() && str[start] == ' ')
++start;
return str.substr(start);
}
std::string_view rtrim(std::string_view str) {
size_t end = str.size();
while (end > 0 && str[end - 1] == ' ')
--end;
return str.substr(0, end);
}
std::string trim(std::string const& str) {
size_t start = 0;
while (start < str.size() && str[start] == ' ')
++start;
size_t end = str.size();
while (end > start && str[end - 1] == ' ')
--end;
return str.substr(start, end - start);
}
std::string ltrim(std::string const& str) {
size_t start = 0;
while (start < str.size() && str[start] == ' ')
++start;
return str.substr(start);
}
std::string rtrim(std::string const& str) {
size_t end = str.size();
while (end > 0 && str[end - 1] == ' ')
--end;
return str.substr(0, end);
}
std::string to_lower_ascii(std::string_view str) {
std::string ret(str);
for (auto& c : ret) {
if (c >= 'A' && c <= 'Z') {
c |= 0x20;
}
}
return ret;
}
} // namespace str
|