-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringHelper.h
374 lines (334 loc) · 10.5 KB
/
StringHelper.h
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//
// StringHelper.h
// Core
//
// Created by Rasmus Anthin.
//
#pragma once
#include "Math.h"
#include "StlUtils.h"
#include <string>
#include <algorithm>
namespace str
{
std::string rep_char(char c, int num)
{
std::string ret;
for (int i=0; i<num; ++i)
ret += c;
return ret;
}
// #FIXME: Align, not adjust.
enum class Adjustment { Left, Center, Right, LeftInteger };
std::string adjust_str(const std::string& str, Adjustment adj, int width, int start_idx = 0, char empty_char = ' ')
{
if (static_cast<int>(str.size()) > width)
return str.substr(0, width);
auto offset_str = rep_char(empty_char, start_idx);
int remaining = width - static_cast<int>(str.size());
switch (adj)
{
case Adjustment::Left: return offset_str + str + rep_char(empty_char, remaining);
case Adjustment::Center:
{
std::string result;
if (width % 2 == 0)
{
int remain_half = remaining / 2;
auto spaces = rep_char(empty_char, remain_half);
result = offset_str + spaces + str + spaces;
}
else
{
// Prefer slightly left adjustment over right adjustment.
int remain_left = (remaining - 1) / 2;
int remain_right = (remaining + 1) / 2;
auto spaces_left = rep_char(empty_char, remain_left);
auto spaces_right = rep_char(empty_char, remain_right);
result = offset_str + spaces_left + str + spaces_right;
}
result = result.substr(0, width);
return result;
}
case Adjustment::Right:
{
auto result = offset_str + rep_char(empty_char, remaining) + str;
result = result.substr(0, width);
return result;
}
case Adjustment::LeftInteger:
{
int number_idx = static_cast<int>(str.size()) - 1;
for (int i = 0; i <= 9; ++i)
{
auto needle = std::to_string(i);
auto idx = static_cast<int>(str.find(needle));
math::minimize<int>(number_idx, idx, { 0, static_cast<int>(str.size()), Range::Closed });
}
// 0 s w
// "bla: 1234 "
auto result = rep_char(empty_char, width);
for (int i = 0; i < std::min<int>(start_idx, number_idx); ++i)
result[i] = str[i];
for (int i = 0; i < static_cast<int>(str.length()); ++i)
if (start_idx + i < width && number_idx + i < static_cast<int>(str.length()))
result[start_idx + i] = str[number_idx + i];
result = result.substr(0, width);
return result;
}
default:
return str;
}
}
// char or wchar_t
template<typename char_t>
char_t to_lower(char_t ch)
{
return std::tolower(ch);
}
// char or wchar_t
template<typename char_t>
char_t to_upper(char_t ch)
{
return std::toupper(ch);
}
template<typename char_t>
std::basic_string<char_t> to_lower(const std::basic_string<char_t>& str)
{
std::basic_string<char_t> ret = str;
size_t len = str.length();
for (size_t c_idx = 0; c_idx < len; ++c_idx)
ret[c_idx] = to_lower(str[c_idx]);
return ret;
}
template<typename char_t>
std::basic_string<char_t> to_upper(const std::basic_string<char_t>& str)
{
std::basic_string<char_t> ret = str;
size_t len = str.length();
for (size_t c_idx = 0; c_idx < len; ++c_idx)
ret[c_idx] = to_upper(str[c_idx]);
return ret;
}
bool is_digit(char ch)
{
return '0' <= ch && ch <= '9';
}
bool is_vowel(char ch)
{
auto chl = to_lower(ch);
const char ch_aring = '\xE5';
const char ch_auml = '\xE4';
const char ch_ouml = '\xF6';
std::vector<char> vowels { 'a', 'o', 'u', ch_aring, 'e', 'i', 'y', ch_auml, ch_ouml };
return std::find(vowels.begin(), vowels.end(), chl) != vowels.end();
}
bool is_letter(char ch)
{
auto chl = to_lower(ch);
const char ch_aring = '\xE5';
const char ch_auml = '\xE4';
const char ch_ouml = '\xF6';
return ('a' <= chl && chl <= 'z')
|| (ch == ch_aring) || (ch == ch_auml) || (ch == ch_ouml);
}
std::string cat(const std::vector<std::string>& strings)
{
std::string ret;
for (const auto& str : strings)
ret += str;
return ret;
}
enum class BracketType { None, Parentheses, SquareBrackets, Braces, MatrixStyle };
template<typename Cont>
std::string row_vector(const Cont& c, BracketType bracket = BracketType::SquareBrackets, const std::string& separator = ", ")
{
std::string ret;
switch (bracket)
{
case BracketType::None: break;
case BracketType::Parentheses: ret = "("; break;
case BracketType::SquareBrackets: ret = "["; break;
case BracketType::Braces: ret = "{"; break;
case BracketType::MatrixStyle: ret = "("; break;
}
ret += " ";
ret += std::to_string(c.front());
auto N = c.size();
for (size_t e_idx = 1; e_idx < N; ++e_idx)
ret += separator + std::to_string(c[e_idx]);
ret += " ";
switch (bracket)
{
case BracketType::None: break;
case BracketType::Parentheses: ret += ")"; break;
case BracketType::SquareBrackets: ret += "]"; break;
case BracketType::Braces: ret += "}"; break;
case BracketType::MatrixStyle: ret += ")"; break;
}
ret += "\n";
return ret;
}
template<typename Cont>
std::string column_vector(const Cont& c, BracketType bracket = BracketType::SquareBrackets)
{
std::vector<std::string> lines;
size_t max_width = 0;
int N = static_cast<int>(c.size());
int l_idx = 0;
for (const auto& v : c)
{
std::string str;
switch (bracket)
{
case BracketType::None: break;
case BracketType::Parentheses: str = "("; break;
case BracketType::SquareBrackets: str = "["; break;
case BracketType::Braces: str = "{"; break;
case BracketType::MatrixStyle: str = N == 1 ? "(" : (l_idx == 0 ? "/" : (l_idx == N - 1 ? "\\" : "|")); break;
}
str += " ";
str += std::to_string(v);
lines.emplace_back(str);
math::maximize(max_width, lines.back().size());
l_idx++;
}
l_idx = 0;
for (auto& str : lines)
{
str = adjust_str(str, Adjustment::Left, static_cast<int>(max_width));
str += " ";
switch (bracket)
{
case BracketType::None: break;
case BracketType::Parentheses: str += ")"; break;
case BracketType::SquareBrackets: str += "]"; break;
case BracketType::Braces: str += "}"; break;
case BracketType::MatrixStyle: str += N == 1 ? ")" : (l_idx == 0 ? "\\" : (l_idx == N - 1 ? "/" : "|")); break;
}
str += "\n";
l_idx++;
}
return cat(lines);
}
// trim from start (in place)
inline void ltrim(std::string& s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
[](unsigned char ch) { return !std::isspace(ch); }
));
}
// trim from end (in place)
inline void rtrim(std::string& s)
{
s.erase(std::find_if(s.rbegin(), s.rend(),
[](unsigned char ch) { return !std::isspace(ch); }).base(),
s.end()
);
}
// trim from both ends (in place)
inline void trim(std::string &s)
{
rtrim(s);
ltrim(s);
}
// trim from start (copying)
inline std::string ltrim_ret(std::string s)
{
ltrim(s);
return s;
}
// trim from end (copying)
inline std::string rtrim_ret(std::string s)
{
rtrim(s);
return s;
}
// trim from both ends (copying)
inline std::string trim_ret(std::string s)
{
trim(s);
return s;
}
inline void remove_spaces(std::string& str)
{
str.erase(remove_if(str.begin(), str.end(), isspace), str.end());
}
bool is_an(const std::string& str)
{
auto strl = trim_ret(to_lower(str));
if (strl.empty())
return false;
char ch0 = strl[0];
auto N = strl.size();
char ch1 = N > 1 ? strl[1] : '?';
char ch2 = N > 2 ? strl[2] : '?';
// Handling cases like "ymca".
if (ch0 == 'y' && (N > 1 && is_vowel(ch1)))
return false;
// Handling cases like "npc". Consonant after 'n'. Starting consonants like 'f' [ef], 'n' [en], 'r' [ar] etc.
if (ch0 == 'f' || ch0 == 'l' || ch0 == 'm' || ch0 == 'n' || ch0 == 'r' || ch0 == 's' || ch0 == 'x')
if (N > 1 && !is_vowel(ch1))
return true;
// Handling cases like "honest", "hour" where 'h' is silent.
if (ch0 == 'h' && (N > 2 && ch1 == 'o' && ch2 == 'u'))
return true;
// Handling cases like "unicorn", "unique", "use", "usage case" where 'u' sounds like "you".
if (strl.starts_with("uni") || strl.starts_with("use") || strl.starts_with("usa"))
return false;
// Return true if the first character is a vowel.
return is_vowel(ch0);
}
// Returns input string (noun + rest of string) with prepended indefinite article.
std::string indef_art(const std::string& str)
{
return std::string("") + (is_an(str) ? "an" : "a") + " " + str;
}
// Makes sure the first letter of the string is in uppercase.
std::string anfangify(std::string str)
{
str[0] = to_upper(str[0]);
return str;
}
// min_scope_size : Allows you to ignore characters inside a scope regarded as scope_delim characters,
// but then your scopes have to contain strings no shorter than this limit.
template<typename CharT = char>
std::vector<std::basic_string<CharT>> tokenize(const std::basic_string<CharT>& str,
const std::vector<CharT>& delim,
const std::vector<CharT>& scope_delim = {},
size_t min_scope_size = 1)
{
std::vector<std::basic_string<CharT>> tokens;
size_t start = 0;
bool in_scope = false;
for (size_t pos = 0; pos < str.size(); ++pos)
{
if (in_scope)
{
if (pos >= start + min_scope_size && stlutils::contains(scope_delim, str[pos]))
{
tokens.emplace_back(str.substr(start, pos - start));
start = pos + 1;
in_scope = false;
}
}
else
{
if (stlutils::contains(scope_delim, str[pos]))
{
in_scope = true;
start = pos + 1;
}
else if (stlutils::contains(delim, str[pos]))
{
if (pos != start)
tokens.emplace_back(str.substr(start, pos - start));
start = pos + 1;
}
}
}
if (start != str.size())
tokens.emplace_back(str.substr(start));
return tokens;
}
}