-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.cpp
More file actions
executable file
·423 lines (366 loc) · 8.59 KB
/
Copy pathstring.cpp
File metadata and controls
executable file
·423 lines (366 loc) · 8.59 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "jj/string.h"
#include "jj/exception.h"
#include <cstring>
#include <cctype>
#include <cwctype>
#if defined(JJ_COMPILER_MSVC)
#include <string.h> // _strnicmp, _wcsnicmp
#else
#include <strings.h> // strncasecmp, wcsncasecmp
#endif // defined(JJ_COMPILER_MSVC)
#if defined(JJ_USE_CODECVT)
#include <locale>
#include <codecvt>
namespace jj
{
namespace strcvt
{
std::string to_string(const wchar_t* str)
{
if (str == nullptr)
return jj::str::EmptyString;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.to_bytes(str);
}
std::string to_string(const std::wstring& str)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.to_bytes(str);
}
std::wstring to_wstring(const char* str)
{
if (str == nullptr)
return jj::str::EmptyWString;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.from_bytes(str);
}
std::wstring to_wstring(const std::string& str)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.from_bytes(str);
}
} // namespace strcvt
} // namespace jj
#else // defined(JJ_USE_CODECVT)...
namespace jj
{
namespace strcvt
{
std::string to_string(const wchar_t* str)
{
throw exception::not_implemented();
}
std::string to_string(const std::wstring& str)
{
throw exception::not_implemented();
}
std::wstring to_wstring(const char* str)
{
throw exception::not_implemented();
}
std::wstring to_wstring(const std::string& str)
{
throw exception::not_implemented();
}
} // namespace strcvt
} // namespace jj
#endif // defined(JJ_USE_CODECVT)...
namespace jj
{
namespace str
{
const std::string EmptyString;
const std::wstring EmptyWString;
#if defined(JJ_USE_WSTRING)
const string_t& Empty = EmptyWString;
#else
const string_t& Empty = EmptyString;
#endif // defined(JJ_USE_WSTRING)
namespace // <anonymous>
{
template<typename CH> struct CHtrait;
template<> struct CHtrait<char>
{
static inline const char* empty() { return ""; }
};
template<> struct CHtrait<wchar_t>
{
static inline const wchar_t* empty() { return L""; }
};
} // namespace <anonymous>
bool isspace(char ch)
{
return std::isspace(ch);
}
bool isspace(wchar_t ch)
{
return std::iswspace(ch);
}
bool isalpha(char ch)
{
return std::isalpha(ch);
}
bool isalpha(wchar_t ch)
{
return std::iswalpha(ch);
}
bool isalnum(char ch)
{
return std::isalnum(ch);
}
bool isalnum(wchar_t ch)
{
return std::iswalnum(ch);
}
bool isdigit(char ch)
{
return std::isdigit(ch);
}
bool isdigit(wchar_t ch)
{
return std::iswdigit(ch);
}
bool isxdigit(char ch)
{
return std::isxdigit(ch);
}
bool isxdigit(wchar_t ch)
{
return std::iswxdigit(ch);
}
bool iscntrl(char ch)
{
return std::iscntrl(ch);
}
bool iscntrl(wchar_t ch)
{
return std::iswcntrl(ch);
}
template<typename CH>
inline void strprecheck(const CH*& a, const CH*& b, size_t& pos)
{
if (a == nullptr)
a = CHtrait<CH>::empty();
if (b == nullptr)
b = CHtrait<CH>::empty();
while (pos > 0 && *a != 0 && *b != 0)
{
--pos;
++a;
++b;
}
while (pos > 0 && *a != 0)
{
--pos;
++a;
}
while (pos > 0 && *b != 0)
{
--pos;
++b;
}
}
static inline int sgn(int n)
{
if (n < 0)
return -1;
if (n > 0)
return 1;
return 0;
}
int cmp(const char* a, const char* b, size_t pos, size_t len)
{
strprecheck(a, b, pos);
return sgn(strncmp(a, b, len));
}
int cmp(const wchar_t* a, const wchar_t* b, size_t pos, size_t len)
{
strprecheck(a, b, pos);
return sgn(wcsncmp(a, b, len));
}
int cmpi(char a, char b)
{
a = std::tolower(a);
b = std::tolower(b);
if (a < b)
return -1;
if (b < a)
return 1;
return 0;
}
int cmpi(const char* a, const char* b, size_t pos, size_t len)
{
strprecheck(a, b, pos);
#if defined(JJ_COMPILER_MSVC)
if (len == std::string::npos)
return sgn(_strcmpi(a, b));
return sgn(_strnicmp(a, b, len));
#else
return sgn(strncasecmp(a, b, len));
#endif // defined(JJ_COMPILER_MSVC)
}
int cmpi(wchar_t a, wchar_t b)
{
a = std::towlower(a);
b = std::towlower(b);
if (a < b)
return -1;
if (b < a)
return 1;
return 0;
}
int cmpi(const wchar_t* a, const wchar_t* b, size_t pos, size_t len)
{
strprecheck(a, b, pos);
#if defined(JJ_COMPILER_MSVC)
if (len == std::wstring::npos)
return sgn(_wcsicmp(a, b));
return sgn(_wcsnicmp(a, b, len));
#else
return sgn(wcsncasecmp(a, b, len));
#endif // defined(JJ_COMPILER_MSVC)
}
bool equali(char a, char b)
{
return std::tolower(a) == std::tolower(b);
}
bool equali(const char* a, const char* b)
{
if (a == nullptr)
return b == nullptr || *b == 0;
if (b == nullptr)
return *a == 0;
for (; *a != 0 && *b != 0 && std::tolower(*a) == std::tolower(*b); ++a, ++b)
;
return *a == *b;
}
bool equali(wchar_t a, wchar_t b)
{
return std::tolower(a) == std::tolower(b);
}
bool equali(const wchar_t* a, const wchar_t* b)
{
if (a == nullptr)
return b == nullptr || *b == 0;
if (b == nullptr)
return *a == 0;
for (; *a != 0 && *b != 0 && std::tolower(*a) == std::tolower(*b); ++a, ++b)
;
return *a == *b;
}
bool lessi(char a, char b)
{
return std::tolower(a) < std::tolower(b);
}
bool lessi(const char* a, const char* b)
{
if (b == nullptr || *b == 0)
return false; // b is prefix of a or both empty
if (a == nullptr)
return true; // empty a is always prefix of b
for (; *a != 0 && *b != 0 && std::tolower(*a) == std::tolower(*b); ++a, ++b)
;
if (*b == 0)
return false; // b is prefix of a, ie. b is less than a
if (*a == 0)
return true; // a is prefix of b, ie. a is less than b
return std::tolower(*a) < std::tolower(*b); // found non-matching chars
}
bool lessi(const std::string& a, const std::string& b)
{
std::string::size_type ai = 0, bi = 0, al = a.length(), bl = b.length();
for (; ai < al && bi < bl && std::tolower(a[ai]) == std::tolower(b[bi]); ++ai, ++bi)
;
if (bi == bl)
return false; // b is prefix of a, ie. b is less than a
if (ai == al)
return true; // a is prefix of b, ie. a is less than b
return std::tolower(a[ai]) < std::tolower(b[bi]); // found non-matching chars
}
bool lessi(wchar_t a, wchar_t b)
{
return std::towlower(a) < std::towlower(b);
}
bool lessi(const wchar_t* a, const wchar_t* b)
{
if (b == nullptr || *b == 0)
return false; // b is prefix of a or both empty
if (a == nullptr)
return true; // empty a is always prefix of b
for (; *a != 0 && *b != 0 && std::towlower(*a) == std::towlower(*b); ++a, ++b)
;
if (*b == 0)
return false; // b is prefix of a, ie. b is less than a
if (*a == 0)
return true; // a is prefix of b, ie. a is less than b
return std::towlower(*a) < std::towlower(*b); // found non-matching chars
}
bool lessi(const std::wstring& a, const std::wstring& b)
{
std::wstring::size_type ai = 0, bi = 0, al = a.length(), bl = b.length();
for (; ai < al && bi < bl && std::towlower(a[ai]) == std::towlower(b[bi]); ++ai, ++bi)
;
if (bi == bl)
return false; // b is prefix of a, ie. b is less than a
if (ai == al)
return true; // a is prefix of b, ie. a is less than b
return std::towlower(a[ai]) < std::towlower(b[bi]); // found non-matching chars
}
const char* find(const char* str, char what, size_t pos)
{
if (str == nullptr)
return nullptr;
while (pos>0 && *str!=0)
{
--pos;
++str;
}
if (pos>0)
return nullptr;
return std::strchr(str, what);
}
const char* find(const char* str, const char* what, size_t pos)
{
if (what == nullptr)
return str;
if (str == nullptr)
return nullptr;
while (pos>0 && *str!=0)
{
--pos;
++str;
}
if (pos>0)
return nullptr;
return std::strstr(str, what);
}
const wchar_t* find(const wchar_t* str, wchar_t what, size_t pos)
{
if (str == nullptr)
return nullptr;
while (pos>0 && *str!=0)
{
--pos;
++str;
}
if (pos>0)
return nullptr;
return std::wcschr(str, what);
}
const wchar_t* find(const wchar_t* str, const wchar_t* what, size_t pos)
{
if (what == nullptr)
return str;
if (str == nullptr)
return nullptr;
while (pos>0 && *str!=0)
{
--pos;
++str;
}
if (pos>0)
return nullptr;
return std::wcsstr(str, what);
}
} // namespace str
} // namespace jj