-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringhelpers.h
301 lines (236 loc) · 5.48 KB
/
stringhelpers.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
#pragma once
#include <ctype.h> // isspace, tolower
#include <string>
#include <string.h>
namespace chibi
{
static bool is_whitespace(const char c)
{
return isspace(c) != 0;
}
static bool is_comment_or_whitespace(const char * line)
{
for (int i = 0; line[i] != 0; ++i)
{
if (line[i] == '#')
return true;
if (is_whitespace(line[i]) == false)
return false;
}
return true;
}
// eats an arbitrary word and stores the result in 'word'. has built-in support for quoted strings
static bool eat_word_v2(char *& line, const char *& word)
{
while (*line != 0 && is_whitespace(*line) == true)
line++;
if (*line == 0)
return false;
const bool isQuoted = *line == '"';
if (isQuoted)
{
line++;
word = line;
while (*line != 0 && *line != '"')
line++;
}
else
{
word = line;
while (*line != 0 && is_whitespace(*line) == false)
line++;
}
if (line > word)
{
// null-terminate the extracted text
if (*line != 0)
{
*line = 0;
line++;
}
// make sure to increment the line pointer until we reach non-whitespace territory again
while (*line != 0 && is_whitespace(*line))
line++;
return true;
}
else
{
return false;
}
}
// checks if 'line' begin with 'word' and eats 'word' from 'line' when it does
static bool eat_word(char * & line, const char * word)
{
while (is_whitespace(*line))
line++;
int index = 0;
while (word[index] != 0)
{
if (line[index] != word[index])
return false;
index++;
}
if (line[index] != 0 && is_whitespace(line[index]) == false)
return false;
while (line[index] != 0 && is_whitespace(line[index]))
index++;
line += index;
return true;
}
static bool do_concat(char *& dst, int & dstSize, const char * s)
{
for (int i = 0; s[i] != 0; ++i)
{
if (dstSize == 0)
return false;
*dst = s[i];
dst += 1;
dstSize -= 1;
}
if (dstSize == 0)
return false;
*dst = 0;
return true;
}
static bool concat(char * dst, int dstSize, const char * s1, const char * s2 = nullptr, const char * s3 = nullptr, const char * s4 = nullptr)
{
return
do_concat(dst, dstSize, s1) &&
(s2 == nullptr || do_concat(dst, dstSize, s2)) &&
(s3 == nullptr || do_concat(dst, dstSize, s3)) &&
(s4 == nullptr || do_concat(dst, dstSize, s4));
}
static bool copy_string(char * dst, int dstSize, const char * s)
{
return concat(dst, dstSize, s);
}
static bool get_path_from_filename(const char * filename, char * path, int pathSize)
{
if (!concat(path, pathSize, filename))
return false;
char * term = nullptr;
for (int i = 0; path[i] != 0; ++i)
if (path[i] == '/')
term = &path[i];
if (term == nullptr)
return false;
*term = 0;
return true;
}
static std::string get_path_extension(const std::string & path, const bool to_lower)
{
size_t pos = path.find_last_of('.');
if (pos == std::string::npos)
return std::string();
else
pos++;
std::string extension = path.substr(pos);
if (to_lower)
{
for (auto & c : extension)
c = tolower(c);
}
return extension;
}
static bool string_starts_with(const std::string & text, const std::string & substring)
{
const size_t length1 = text.length();
const size_t length2 = substring.length();
if (length1 < length2)
return false;
for (size_t i = 0; i < length2; ++i)
if (text[i] != substring[i])
return false;
return true;
}
static bool string_ends_with(const std::string & text, const std::string & substring)
{
const size_t length1 = text.length();
const size_t length2 = substring.length();
if (length1 < length2)
return false;
for (size_t i = length1 - length2, j = 0; i < length1; ++i, ++j)
if (text[i] != substring[j])
return false;
return true;
}
static bool match_wildcard(const char * in_text, const char * wildcard, const char separator)
{
const char * text = in_text;
while (wildcard[0] != 0)
{
if (wildcard[0] == separator)
{
// did we match the entire element? match
if (text[0] == 0)
return true;
// restart matching against the next element
text = in_text;
wildcard++;
}
else if (wildcard[0] == '*')
{
if (wildcard[1] == 0 || wildcard[1] == separator)
return true;
else
{
while (text[0] != 0 && text[0] != wildcard[1])
text++;
if (text[0] == 0)
return false;
wildcard++;
}
}
else if (text[0] != wildcard[0])
{
// seek to the next element
while (wildcard[0] != 0 && wildcard[0] != separator)
wildcard++;
// no more elements? no match
if (wildcard[0] == 0)
return false;
// we found the next element. restart matching
in_text = text;
wildcard++;
}
else
{
text++;
wildcard++;
}
}
return text[0] == 0;
}
static bool match_element(const char * in_text, const char * element, const char separator)
{
const char * text = in_text;
while (element[0] != 0)
{
if (element[0] == separator)
{
if (text[0] == 0)
return true;
text = in_text;
element++;
}
else if (text[0] != element[0])
{
// seek to the next element
while (element[0] != 0 && element[0] != separator)
element++;
// no more elements? no match
if (element[0] == 0)
return false;
// we found the next element. restart matching
in_text = text;
element++;
}
else
{
text++;
element++;
}
}
return text[0] == 0;
}
}