-
-
Notifications
You must be signed in to change notification settings - Fork 941
/
Copy pathstring_utils.cpp
215 lines (198 loc) · 4.57 KB
/
string_utils.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
#include <random>
#include <cassert>
#include "api/string_utils.h"
namespace SonicPi
{
// String split with multiple delims
void string_split(const std::string& text, const char* delims, std::vector<std::string>& tokens)
{
tokens.clear();
std::size_t start = text.find_first_not_of(delims), end = 0;
while ((end = text.find_first_of(delims, start)) != std::string::npos)
{
tokens.push_back(text.substr(start, end - start));
start = text.find_first_not_of(delims, end);
}
if (start != std::string::npos)
tokens.push_back(text.substr(start));
}
std::vector<std::string> string_split(const std::string& text, const char* delims)
{
std::vector<std::string> tok;
string_split(text, delims, tok);
return tok;
}
std::string string_replace(std::string subject, const std::string& search, const std::string& replace)
{
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos)
{
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}
// trim from beginning of string (left)
std::string string_left_trim(std::string s, const char* t)
{
s.erase(0, s.find_first_not_of(t));
return s;
}
// trim from end of string (right)
std::string string_right_trim(std::string s, const char* t)
{
s.erase(s.find_last_not_of(t) + 1);
return s;
}
// trim from both ends of string (left & right)
std::string string_trim(std::string s, const char* t)
{
return string_left_trim(string_right_trim(s, t), t);
}
std::string random_string(std::string::size_type length)
{
static auto& chrs = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
thread_local static std::mt19937 rg{ std::random_device{}() };
thread_local static std::uniform_int_distribution<std::string::size_type> pick(0, sizeof(chrs) - 2);
std::string s;
s.reserve(length);
while (length--)
s += chrs[pick(rg)];
return s;
}
std::string string_number_name(int i)
{
switch (i)
{
case 0:
return "zero";
case 1:
return "one";
case 2:
return "two";
case 3:
return "three";
case 4:
return "four";
case 5:
return "five";
case 6:
return "six";
case 7:
return "seven";
case 8:
return "eight";
case 9:
return "nine";
case 10:
return "ten";
case 11:
return "eleven";
case 12:
return "twelve";
case 13:
return "thirteen";
case 14:
return "4teen";
case 15:
return "fifteen";
case 16:
return "6teen";
case 17:
return "7teen";
case 18:
return "8teen";
case 19:
return "9teen";
default:
assert(false);
return "";
}
}
uint32_t string_number_from_name(const std::string& name)
{
if (name.find("zero") != std::string::npos)
{
return 0;
}
else if (name.find("one") != std::string::npos)
{
return 1;
}
else if (name.find("two") != std::string::npos)
{
return 2;
}
else if (name.find("three") != std::string::npos)
{
return 3;
}
else if (name.find("four") != std::string::npos)
{
return 4;
}
else if (name.find("five") != std::string::npos)
{
return 5;
}
else if (name.find("six") != std::string::npos)
{
return 6;
}
else if (name.find("seven") != std::string::npos)
{
return 7;
}
else if (name.find("eight") != std::string::npos)
{
return 8;
}
else if (name.find("nine") != std::string::npos)
{
return 9;
}
if (name.find("ten") != std::string::npos)
{
return 10;
}
else if (name.find("eleven") != std::string::npos)
{
return 11;
}
else if (name.find("twelve") != std::string::npos)
{
return 12;
}
else if (name.find("thirteen") != std::string::npos)
{
return 13;
}
else if (name.find("4teen") != std::string::npos)
{
return 14;
}
else if (name.find("fifteen") != std::string::npos)
{
return 15;
}
else if (name.find("6teen") != std::string::npos)
{
return 16;
}
else if (name.find("7teen") != std::string::npos)
{
return 17;
}
else if (name.find("8teen") != std::string::npos)
{
return 18;
}
else if (name.find("9teen") != std::string::npos)
{
return 19;
}
return 0;
}
} // namespace SonicPi