-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfutil.cpp
282 lines (221 loc) · 6.85 KB
/
futil.cpp
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
#include "futil.h"
namespace formicine::util {
std::string filter(const std::string &str, const std::string &allowed_chars) {
return filter(str, [&](char ch) { return allowed_chars.find(ch) != std::string::npos; });
}
std::string antifilter(const std::string &str, const std::string &forbidden_chars) {
return filter(str, [&](char ch) { return forbidden_chars.find(ch) == std::string::npos; });
}
std::string lower(std::string str) {
// TODO: Unicode. Of course.
for (size_t i = 0, length = str.length(); i < length; ++i) {
if ('A' <= str[i] && str[i] <= 'Z')
str[i] += 'a' - 'A';
}
return str;
}
std::string upper(std::string str) {
// TODO: Unicode. Of course.
for (size_t i = 0, length = str.length(); i < length; ++i) {
if ('a' <= str[i] && str[i] <= 'z')
str[i] -= 'a' - 'A';
}
return str;
}
std::string nth_word(const std::string &str, size_t n, bool condense) {
if (condense) {
const std::vector<std::string> words = split(str, " ", true);
return n < words.size()? words[n] : "";
}
if (n == 0)
return str.substr(0, str.find(' '));
const size_t index = nth_index(str, ' ', n);
if (index == std::string::npos)
return "";
return str.substr(index + 1, nth_index(str, ' ', n + 1) - index - 1);
}
size_t word_count(const std::string &str, bool condense) {
size_t count = 0, i = 0, length = str.length();
if (length == 0)
return 0;
// If condense is true, count all spaces except the ones at the beginning, then count and subtract the ones at
// the end.
if (condense) {
for (; i < length && str[i] == ' '; ++i);
if (i == length)
return 0;
}
for (; i < length; ++i) {
if (str[i] == ' ' && (!condense || i == 0 || str[i - 1] != ' '))
++count;
}
if (condense) {
for (i = length - 1; 0 <= static_cast<long>(i) && str[i] == ' '; --i)
--count;
}
return count + 1;
}
std::string & trim(std::string &str) {
return ltrim(rtrim(str));
}
std::string trim(const std::string &str) {
std::string copy {str};
return trim(copy);
}
std::string & ltrim(std::string &str) {
if (str.empty())
return str;
const size_t first = str.find_first_not_of(" \t\r\n");
if (first == std::string::npos)
str.clear();
else
str.erase(0, first);
return str;
}
std::string ltrim(const std::string &str) {
std::string copy = str;
return ltrim(copy);
}
std::string & rtrim(std::string &str) {
if (str.empty())
return str;
char back = str.back();
while (back == ' ' || back == '\t' || back == '\r' || back == '\n') {
str.pop_back();
back = str.back();
}
return str;
}
std::string rtrim(const std::string &str) {
std::string copy = str;
return rtrim(copy);
}
std::pair<ssize_t, ssize_t> word_indices(const std::string &str, size_t cursor) {
const size_t length = str.length();
ssize_t word_index = -1;
ssize_t sub_index = -1;
char prev_char = '\0';
if (str.empty())
return {-1, -1};
if (cursor == 0)
return str[0] == ' '? std::pair(-1, -1) : std::pair(0, 0);
for (size_t i = 0; i < length; ++i) {
char ch = str[i];
if (ch != ' ' && (prev_char == '\0' || prev_char == ' ')) {
++word_index;
sub_index = 0;
}
if (ch == ' ') {
if (prev_char != ' ') {
// We've reached the end of a word. If this is where the cursor is, we're done.
// Otherwise, it's time to increment the word index.
if (i == cursor)
return {word_index, sub_index};
} else if (i == cursor) {
// If we're within a group of spaces and this is the where the cursor is, return a negative number
// because that means the cursor isn't in a word.
return {-word_index - 2, -1};
}
} else {
if (i == cursor)
return {word_index, sub_index};
++sub_index;
}
prev_char = ch;
}
if (cursor == length && prev_char != ' ')
return {word_index, sub_index};
return {-word_index - 2, -1};
}
size_t index_of_word(const std::string &str, size_t n) {
const size_t length = str.length();
size_t word_index = 0;
char prev_char = '\0';
for (size_t i = 0; i < length; ++i) {
char ch = str[i];
if (ch == ' ' && (prev_char != ' ' && prev_char != '\0')) {
// We're at a space and the previous character wasn't a space (or the beginning of the string).
// This means we've just left a word, so increment the word index.
++word_index;
}
if (ch != ' ' && (prev_char == ' ' || prev_char == '\0') && word_index == n)
return i;
prev_char = ch;
}
return length;
}
std::string skip_words(const std::string &str, size_t n) {
return str.substr(index_of_word(str, n));
}
size_t last_index_of_word(const std::string &str, size_t n) {
const size_t length = str.length();
size_t word_index = 0;
char prev_char = '\0';
for (size_t i = 0; i < length; ++i) {
char ch = str[i];
if (ch == ' ' && (prev_char != ' ' && prev_char != '\0')) {
// We're at a space and the previous character wasn't a space (or the beginning of the string).
// This means we've just left a word, so check whether we're at the requested word index.
if (word_index++ == n)
return i;
}
prev_char = ch;
}
return length;
}
ssize_t replace_word(std::string &str, size_t n, const std::string &word) {
const size_t index = index_of_word(str, n);
const size_t original_length = str.length();
if (index == original_length)
return -1;
size_t original_word_width = 0;
for (size_t i = index; i < original_length; ++i) {
if (std::isspace(str[i]))
break;
++original_word_width;
}
str.replace(index, original_word_width, word);
return index + word.length();
}
std::string & remove_suffix(std::string &word, const std::string &suffix) {
if (word.empty() || suffix.empty())
return word;
const size_t pos = word.rfind(suffix);
if (pos != std::string::npos && pos + suffix.length() == word.length())
word.erase(pos);
return word;
}
std::string remove_suffix(const std::string &word, const std::string &suffix) {
std::string word_copy {word};
remove_suffix(word_copy, suffix);
return word_copy;
}
bool parse_long(const std::string &str, long &out) {
const char *c_str = str.c_str();
char *end;
long parsed = strtol(c_str, &end, 10);
if (end == c_str + str.length()) {
out = parsed;
return true;
}
return false;
}
std::string replace_all(std::string str, const std::string &to_replace, const std::string &replace_with) {
const size_t replaced_length = to_replace.length();
size_t pos;
while ((pos = str.find(to_replace)) != std::string::npos)
str.replace(pos, replaced_length, replace_with);
return str;
}
std::string remove_html(std::string str) {
for (size_t open = str.find('<'); open != std::string::npos; open = str.find('<')) {
const size_t close = str.find('>', open);
if (close == std::string::npos) {
// No closing angle bracket? Give up.
return str;
}
str.erase(open, close - open + 1);
}
return str;
}
}