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
|
#include "strings.h"
#include <Logging/logdata.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using std::string;
using std::wstring;
using std::vector;
using std::pair;
using std::endl;
bool endswith(const char* str, const char* ending) {
size_t str_len = strlen(str);
size_t end_len = strlen(ending);
if( str_len >= end_len ) {
for( unsigned i = 0; i < end_len; i++ ) {
int str_len_index = i + str_len - end_len;
if( str[str_len_index] != ending[i] ) {
return false;
}
}
return true;
} else {
return false;
}
}
bool beginswith(const char* str, const char* begin) {
size_t str_len = strlen(str);
size_t begin_len = strlen(begin);
if( str_len >= begin_len ) {
for( unsigned i = 0; i < begin_len; i++ ) {
if( str[i] != begin[i] ) {
return false;
}
}
return true;
} else {
return false;
}
}
bool pstrmtch( const char* first, const char* second, size_t count) {
if( strlen(first) >= count && strlen(second) >= count ) {
for( size_t i = 0; i < count; i++ ) {
if( first[i] != second[i] ) {
return false;
}
}
return true;
} else {
return false;
}
}
bool strcont( const char* string, const char* substring ) {
size_t sub_len = strlen(substring);
size_t str_len = strlen(string);
for( size_t i = 0; i < str_len; i++ ) {
bool match = true;
if( (str_len - i) >= sub_len ) {
for( size_t k = 0; k < sub_len; k++ ) {
if( string[i] != substring[k] ) {
match = false;
break;
}
}
if( match ) {
return true;
}
}
}
return false;
}
#ifdef WIN32
wstring fromUTF8( const string& path_utf8 ) {
int size = MultiByteToWideChar(CP_UTF8, 0, path_utf8.c_str(), -1, NULL, 0);
wstring path_utf16;
path_utf16.resize(size);
MultiByteToWideChar(CP_UTF8, 0, path_utf8.c_str(), -1, &path_utf16[0], size);
return path_utf16;
}
string fromUTF16( const wstring& path_utf16 ){
int size = WideCharToMultiByte(CP_UTF8, 0, path_utf16.c_str(), -1, NULL, 0, NULL, NULL );
string path_utf8;
path_utf8.resize(size);
WideCharToMultiByte(CP_UTF8, 0, path_utf16.c_str(), -1, &path_utf8[0], size, NULL, NULL);
return path_utf8;
}
#endif
int FindStringInArray( const char** values, size_t values_size, const char* q )
{
for( unsigned i = 0; i < values_size; i++ )
{
if( strmtch( values[i], q ) )
{
return i;
}
}
return -1;
}
int FindStringInArray( const vector<pair< const char*, const char* > >& values, const char* q )
{
for( unsigned i = 0; i < values.size(); i++ )
{
if( strmtch( values[i].first, q ) )
{
return i;
}
}
return -1;
}
int FindStringInArray( const vector< const char* >& values, const char* q )
{
for( unsigned i = 0; i < values.size(); i++ )
{
if( strmtch( values[i], q ) )
{
return i;
}
}
return -1;
}
void UTF8InPlaceLower( char* arr ) {
for( unsigned i = 0; i < strlen( arr ); i++ ) {
//A to Z in unicode, this is all the letters in the world, right?
if( arr[i] >= 0x41 && arr[i] <= 0x5A ) {
arr[i] = arr[i]+0x20;
} else {
arr[i] = arr[i];
}
}
}
int UTF8ToUpper( char* out, size_t outsize, const char* in )
{
//We should be using the ICU lib for this, but good god, it's massive and difficult,
//so we just start of with using the first 128 characters which have simple rules. (ASCII).
//The version in ICU is much more context sensitive, handles some really interesting cases.
//http://site.icu-project.org/download
memset(out,'\0',outsize);
for( unsigned i = 0; i < strlen( in ) && i < outsize-1; i++ )
{
//A to Z in unicode, this is all the letters in the world, right?
if( in[i] >= 0x61 && in[i] <= 0x7a )
{
out[i] = in[i]-0x20;
}
else
{
out[i] = in[i];
}
}
return strlen(in)+1;
//Return how many bytes we wrote to the output string, inluding the terminating.
//this is a placeholder for a smart solution in the future, where expansion may occur;
}
string UTF8ToLower(const string& in) {
string out;
out.resize(in.size());
for( unsigned i = 0; i < out.size(); i++ )
{
//A to Z in unicode, this is all the letters in the world, right?
if( in[i] >= 0x41 && in[i] <= 0x5A )
{
out[i] = in[i]+0x20;
}
else
{
out[i] = in[i];
}
}
return out;
}
int UTF8ToLower( char* out, size_t outsize, const char* in ) {
//We should be using the ICU lib for this, but good god, it's massive and difficult,
//so we just start of with using the first 128 characters which have simple rules. (ASCII).
//The version in ICU is much more context sensitive, handles some really interesting cases.
//http://site.icu-project.org/download
memset(out,'\0',outsize);
for( unsigned i = 0; i < strlen( in ) && i < outsize-1; i++ )
{
//A to Z in unicode, this is all the letters in the world, right?
if( in[i] >= 0x41 && in[i] <= 0x5A )
{
out[i] = in[i]+0x20;
}
else
{
out[i] = in[i];
}
}
return strlen(in)+1;
//Return how many bytes we wrote to the output string, inluding the terminating.
//this is a placeholder for a smart solution in the future, where expansion may occur;
}
bool hasBeginning( const string &fullString, const string &beginning ) {
if( fullString.length() >= beginning.length() ) {
if( fullString.substr(0,beginning.length()) == beginning ) {
return true;
}
}
return false;
}
bool hasEnding (string const &fullString, string const &ending) {
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
} else {
return false;
}
}
bool hasAnyOfEndings( const string &fullString, const vector<string> &endings)
{
for( vector<string>::const_iterator sit = endings.begin();
sit != endings.end();
sit++ )
{
if( hasEnding( fullString, *sit ) )
{
return true;
}
}
return false;
}
int CountCharsInString(const char* str, char the_char) {
int count = 0;
int str_length = (int)strlen(str);
for(int i=0; i<str_length; ++i){
if(str[i] == the_char){
++count;
}
}
return count;
}
int FindNthCharFromBack(const char* str, char the_char, int n){
int count = 0;
int str_length = (int)strlen(str);
for(int i= str_length-1; i>=0; --i){
if(str[i] == the_char){
++count;
if(count == n){
return i;
}
}
}
return -1;
}
int saysTrue(const char* str) {
if(str) {
if(strmtch(str, "true")) {
return SAYS_TRUE;
} else if(strmtch(str, "false")) {
return SAYS_FALSE;
} else {
return SAYS_TRUE_NO_MATCH;
}
} else {
return SAYS_TRUE_NULL_INPUT;
}
}
const char* nullAsEmpty( const char* str ) {
if( str ) {
return str;
} else {
return "";
}
}
uint32_t GetLengthInBytesForNCodepoints( const string& utf8in, uint32_t codepoint_index ) {
uint32_t codepoints = 0;
if( codepoint_index == 0 )
return 0;
for( unsigned i = 0; i < utf8in.size(); i++ ) {
uint8_t c = utf8in[i];
if( ( c & 0x80) == 0 ) {
codepoints++;
} else if( (c & 0xC0) == 0xC0 ) {
codepoints++;
if( (c & 0x20) == 0) {
i += 1;
} else if( (c & 0x10) == 0 ) {
i += 2;
} else if( (c & 0x08) == 0 ) {
i += 3;
}
} else {
LOGE << "Malformed utf-8 string" << endl;
}
if( i >= utf8in.size() ) {
LOGE << "Malformed utf-8 string, codepoint indication doesn't match offset" << endl;
}
if( codepoints == codepoint_index ) {
return i+1;
}
}
return utf8in.size();
}
uint32_t GetCodepointCount( const string &utf8in ) {
uint32_t codepoints = 0;
for( unsigned i = 0; i < utf8in.size(); i++ ) {
uint8_t c = utf8in[i];
if( ( c & 0x80) == 0 ) {
codepoints++;
} else if( (c & 0xC0) == 0xC0 ) {
codepoints++;
if( (c & 0x20) == 0) {
i += 1;
} else if( (c & 0x10) == 0 ) {
i += 2;
} else if( (c & 0x08) == 0 ) {
i += 3;
}
} else {
LOGE << "Malformed utf-8 string" << endl;
}
}
return codepoints;
}
string RemoveFileEnding(string in) {
size_t last_point = in.size();
for( int i = in.size()-1; i >= 0; i-- ) {
if( in[i] == '.' ) {
last_point = i;
}
if( in[i] == '/' ) {
return in.substr(0,last_point);
}
}
if( last_point > 0 ) {
return in.substr(0,last_point);
} else {
return in;
}
}
|