-
Notifications
You must be signed in to change notification settings - Fork 2
/
stringfunc.cpp
369 lines (329 loc) · 8.6 KB
/
stringfunc.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
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
#include "stringfunc.h"
#include "window.h"
#include <sstream>
#include <math.h> // digits_in() uses log10(); move_decimal() uses pow()
std::vector<std::string> break_into_lines(std::string text, int linesize)
{
std::vector<std::string> ret;
size_t chars = 0; // Number of actually-printed characters at...
size_t pos = 0; // ... this point in the string
size_t linebreak = std::string::npos; // The last acceptable breakpoint
std::string active_color_tag;
while ((text.length() > linesize || text.find('\n') != std::string::npos) &&
pos < text.size()) {
bool force = false;
if (text.substr(pos, 3) == "<c=") {
size_t tmppos = text.find('>', pos);
if (tmppos == std::string::npos) {
//debugmsg("Bad colortag!");
return ret;
} else {
active_color_tag = text.substr(pos, tmppos - pos + 1);
pos = tmppos;
}
linebreak = pos;
chars--;
} else if (text[pos] == '\n') {
linebreak = pos;
force = true;
} else if (text[pos] == ' ')
linebreak = pos;
pos++;
chars++;
if (force || chars > linesize) {
std::string tmp;
if (linebreak == std::string::npos) {
linebreak = linesize - 1;
tmp = text.substr(0, linebreak) + "-";
text = text.substr(linebreak);
} else if (text[linebreak] == '\n' || text[linebreak] == ' ') {
tmp = text.substr(0, linebreak);
text = text.substr(linebreak + 1);
// Ostensibly for color tags, but could cause a problem?
} else if (text[linebreak] == '>') {
linebreak++;
tmp = text.substr(0, linebreak);
text = text.substr(linebreak);
}
ret.push_back(tmp);
if (!active_color_tag.empty()) {
text = active_color_tag + text;
}
pos = 0;
chars = 0;
linebreak = std::string::npos;
}
}
if (!text.empty()) {
ret.push_back(text);
}
return ret;
/*
size_t linebreak = text.find_last_of(" ", linesize);
std::string tmp;
if (linebreak == std::string::npos) {
linebreak = linesize - 1;
tmp = text.substr(0, linebreak) + "-";
} else
tmp = text.substr(0, linebreak);
ret.push_back(tmp);
text = text.substr(linebreak + 1);
}
ret.push_back(text);
*/
return ret;
}
std::string load_to_delim(std::istream &datastream, std::string delim)
{
std::string ret, tmp;
do {
datastream >> tmp;
if (tmp != delim)
ret += tmp + " ";
} while (tmp != delim && !(datastream.eof()));
if (!ret.empty() && ret[ret.size() - 1] == ' ')
ret = ret.substr(0, ret.size() - 1);
return ret;
}
std::string load_to_character(std::istream &datastream, char ch, bool _trim)
{
std::string ret;
char *tmp = new char [1];
do {
datastream.read(tmp, 1);
if (tmp[0] != ch) {
ret.push_back(tmp[0]);
}
debugmsg(ret.c_str());
} while (tmp[0] != ch && !(datastream.eof()));
if (_trim)
ret = trim(ret);
delete [] tmp;
return ret;
}
std::string load_to_character(std::istream &datastream, std::string chars,
bool _trim)
{
std::string ret;
char *tmp = new char [1];
do {
datastream.read(tmp, 1);
if (chars.find(tmp[0]) == std::string::npos) {
ret.push_back(tmp[0]);
}
debugmsg(ret.c_str());
} while (chars.find(tmp[0]) == std::string::npos && chars[0] >= 0 &&
!(datastream.eof()));
if (_trim)
ret = trim(ret);
delete [] tmp;
return ret;
}
std::string trim(const std::string &orig)
{
std::string ret = orig;
int front = 0, back = ret.length() - 1;
while (front < ret.length() &&
(ret[front] == ' ' || ret[front] == '\n' || ret[front] == '\t')) {
front++;
}
ret = ret.substr(front);
back = ret.length() - 1;
while (back >= 0 &&
(ret[back] == ' ' || ret[back] == '\n' || ret[back] == '\t')) {
back--;
}
ret = ret.substr(0, back + 1);
return ret;
}
std::string all_caps(const std::string &orig)
{
std::string ret = orig;
for (int i = 0; i < ret.length(); i++) {
if (ret[i] >= 'a' && ret[i] <= 'z')
ret[i] += 'A' - 'a';
}
return ret;
}
std::string no_caps(const std::string &orig)
{
std::string ret = orig;
for (int i = 0; i < ret.length(); i++) {
if (ret[i] >= 'A' && ret[i] <= 'Z')
ret[i] += 'a' - 'A';
}
return ret;
}
std::string capitalize(const std::string &orig)
{
std::string ret = orig;
size_t tagpos = orig.find("<c="); // Find the first tag
size_t start; // Used below
if (tagpos != std::string::npos) {
for (int i = 0; i < tagpos; i++) { // Can we capitalize before the tag?
if (ret[i] >= 'a' && ret[i] <= 'z') {
ret[i] += 'A' - 'a'; // Capitalize!
return ret;
} else if (ret[i] != ' ') {
return ret; // We're already capitalized!
}
}
// If we reach this point, we found a tag but there's nothing before it.
start = orig.find(">", tagpos);
start++;
} else { // No tags - start from the beginning of the string
start = 0;
}
for (int i = start; i < ret.size(); i++) {
if (ret[i] >= 'a' && ret[i] <= 'z') {
ret[i] += 'A' - 'a';
return ret;
} else if (ret[i] != ' ') {
return ret; // We're already capitalized!
}
}
return ret; // All blank spaces??
}
std::string capitalize_all_words(const std::string &orig)
{
std::string ret = orig;
for (int i = 0; i < ret.size(); i++) {
std::string tag_check = no_caps( ret.substr(i, 3) );
if (tag_check == "<c=") { // It's a tag!
// Advance until we find the end of the tag.
while (ret[i] != '>' && i < ret.size()) {
i++;
}
if (i >= ret.size()) { // Unterminated tag, oh well
return ret;
}
}
if (ret[i] >= 'a' && ret[i] <= 'z' && (i == 0 || !is_letter(ret[i - 1]))) {
ret[i] = ret[i] - 'a' + 'A';
}
}
return ret;
}
std::string remove_color_tags(const std::string &orig)
{
std::string ret;
bool in_tag = false;
for (int i = 0; i < orig.size(); i++) {
if (in_tag) {
if (orig[i] == '>') {
in_tag = false;
}
} else {
if (orig[i] == '<') {
in_tag = true;
} else {
ret += orig[i];
}
}
}
return ret;
}
int tagless_length(const std::string &orig)
{
std::string tagless = remove_color_tags(orig);
return tagless.length();
}
std::string itos(int num)
{
std::stringstream ret;
ret << num;
return ret.str();
}
int digits_in(int num)
{
if (num == 0) {
return 1; // log10() chokes on 0
}
if (num < 0) {
return 1 + digits_in(0 - num); // 1 extra for the - character
}
return 1 + log10(num); // 1 extra since log10 is logarythmic
}
std::string move_decimal(int num, int moves)
{
std::stringstream ret;
int divisor = int(pow(10, moves));
int decimal = num % divisor;
ret << num / divisor << ".";
// We may need to add 0s so that "108" doesn't become "1.8"
for (int power = moves - 1; power >= 1; power--) {
int val = int(pow(10, power));
if (decimal < val) {
ret << "0";
} else {
power = 0;
}
}
ret << decimal;
return ret.str();
}
std::string color_gradient(int value, std::vector<int> breakpoints,
std::vector<nc_color> colors)
{
/* We need one more color than breakpoints, since breakpoints are the spots
* BETWEEN colors:
* gray <split: 5> white <split: 12> green
*/
if (breakpoints.size() + 1 != colors.size()) {
debugmsg("color_gradient() called with mismatched breakpoints/colors!");
return "";
}
nc_color col = c_null;
for (int i = 0; i < breakpoints.size(); i++) {
if (value <= breakpoints[i]) {
col = colors[i];
}
}
if (col == c_null) {
col = colors.back();
}
std::stringstream ret;
ret << "<c=" << color_tag(col) << ">";
return ret.str();
}
std::string letters_after(long letter, bool vowel_before)
{
// Force lowercase.
if (letter >= 'A' && letter <= 'Z') {
letter = letter + 'a' - 'A';
}
//abcdefghijklmnopqrstuvwxyz
if (vowel_before) {
if (is_vowel(letter)) {
return "bcdfghklmnpqrstvwxyz";
} else if (letter == 'y') {
return "abcdefgiklmnoprstuz";
} else {
return "abcdefghijklmnopqrstuvwyz";
}
} else {
switch (letter) {
case 'a': return "bcdefghiklmnpqrstuvwxyz";
case 'b': return "aeiloruy";
case 'c': return "aehiloru";
case 'd': return "aeioruy";
case 'e': return "abcdefghilmnpqrstuvwyz";
case 'f': return "aeiloruy";
case 'g': return "aeiloru";
case 'h': return "aeiou";
case 'i': return "abcdefglmnopqrstuvz";
case 'j': return "aeiou";
}
}
return "a";
}
bool is_letter(char ch)
{
return ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') );
}
bool is_vowel(char ch)
{
// TODO: And sometimes y?
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');
}