forked from fantaisie-software/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
text.c
562 lines (510 loc) · 11.9 KB
/
text.c
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/* text - uniform multi-byte/wide text handling
* Copyright (c) 2003 Michael B. Allen <mba2000 ioplex.com>
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <wchar.h>
#include <wctype.h>
#include "mba/msgno.h"
#include "mba/text.h"
int
str_length(const unsigned char *src, const unsigned char *slim)
{
const unsigned char *start = src;
if (src == NULL || src >= slim) {
return 0;
}
while (*src) {
src++;
if (src == slim) {
return 0;
}
}
return src - start;
}
int
wcs_length(const wchar_t *src, const wchar_t *slim)
{
const wchar_t *start = src;
if (src == NULL || src >= slim) {
return 0;
}
while (*src) {
src++;
if (src == slim) {
return 0;
}
}
return src - start;
}
size_t
str_size(const unsigned char *src, const unsigned char *slim)
{
const unsigned char *start = src;
if (src == NULL || src >= slim) {
return 0;
}
while (*src) {
src++;
if (src == slim) {
return 0;
}
}
return (src - start + 1) * sizeof *src;
}
size_t
wcs_size(const wchar_t *src, const wchar_t *slim)
{
const wchar_t *start = src;
if (src == NULL || src >= slim) {
return 0;
}
while (*src) {
src++;
if (src == slim) {
return 0;
}
}
return (src - start + 1) * sizeof *src;
}
int
str_copy(const unsigned char *src, const unsigned char *slim,
unsigned char *dst, unsigned char *dlim, int n)
{
unsigned char *start = dst;
if (dst == NULL || dst >= dlim) {
return 0;
}
if (src == NULL || src >= slim) {
*dst = '\0';
return 0;
}
while (n-- && *src) {
*dst++ = *src++;
if (src == slim || dst == dlim) {
dst = start;
break;
}
}
*dst = '\0';
return dst - start;
}
int
wcs_copy(const wchar_t *src, const wchar_t *slim,
wchar_t *dst, wchar_t *dlim, int n)
{
wchar_t *start = dst;
if (dst == NULL || dst >= dlim) {
return 0;
}
if (src == NULL || src >= slim) {
*dst = L'\0';
return 0;
}
while (n-- && *src) {
*dst++ = *src++;
if (src == slim || dst == dlim) {
dst = start;
break;
}
}
*dst = L'\0';
return dst - start;
}
int
str_copy_new(const unsigned char *src,
const unsigned char *slim,
unsigned char **dst,
int n,
struct allocator *al)
{
const unsigned char *start = src;
size_t siz;
if (dst == NULL) {
return 0;
}
if (src == NULL || src >= slim) {
*dst = NULL;
return 0;
}
while (n-- && *src) {
src++;
if (src == slim) {
*dst = NULL;
return 0;
}
}
siz = src - start + 1;
if ((*dst = allocator_alloc(al, siz, 0)) == NULL) {
return -1;
}
memcpy(*dst, start, siz);
(*dst)[src - start] = '\0';
return src - start;
}
int
wcs_copy_new(const wchar_t *src,
const wchar_t *slim,
wchar_t **dst,
int n,
struct allocator *al)
{
const wchar_t *start = src;
size_t siz;
if (dst == NULL) {
return 0;
}
if (src == NULL || src >= slim) {
*dst = NULL;
return 0;
}
while (n-- && *src) {
src++;
if (src == slim) {
*dst = NULL;
return 0;
}
}
siz = (src - start + 1) * sizeof *src;
if ((*dst = allocator_alloc(al, siz, 0)) == NULL) {
return -1;
}
memcpy(*dst, start, siz);
(*dst)[src - start] = L'\0';
return src - start;
}
/* Standard UTF-8 decoder
*/
int
utf8towc(const unsigned char *src, const unsigned char *slim, wchar_t *wc)
{
const unsigned char *start = src;
ptrdiff_t n = slim - src;
if (n < 1) return 0;
if (*src < 0x80) {
*wc = *src;
} else if ((*src & 0xE0) == 0xC0) {
if (n < 2) return 0;
*wc = (*src++ & 0x1F) << 6;
if ((*src & 0xC0) != 0x80) {
errno = EILSEQ;
return -1;
} else {
*wc |= *src & 0x3F;
}
if (*wc < 0x80) {
errno = EILSEQ;
return -1;
}
} else if ((*src & 0xF0) == 0xE0) {
if (n < 3) return 0;
if (sizeof *wc < 3) {
errno = EILSEQ; /* serrogates not supported */
return -1;
}
*wc = (*src++ & 0x0F) << 12;
if ((*src & 0xC0) != 0x80) {
errno = EILSEQ;
return -1;
} else {
*wc |= (*src++ & 0x3F) << 6;
if ((*src & 0xC0) != 0x80) {
errno = EILSEQ;
return -1;
} else {
*wc |= *src & 0x3F;
}
}
if (*wc < 0x800) {
errno = EILSEQ;
return -1;
}
} else if ((*src & 0xF8) == 0xF0) {
if (n < 4) return 0;
*wc = (*src++ & 0x07) << 18;
if ((*src & 0xC0) != 0x80) {
errno = EILSEQ;
return -1;
} else {
*wc |= (*src++ & 0x3F) << 12;
if ((*src & 0xC0) != 0x80) {
errno = EILSEQ;
return -1;
} else {
*wc |= (*src++ & 0x3F) << 6;
if ((*src & 0xC0) != 0x80) {
errno = EILSEQ;
return -1;
} else {
*wc |= *src & 0x3F;
}
}
}
if (*wc < 0x10000) {
errno = EILSEQ;
return -1;
}
}
src++;
return src - start;
}
int
utf8casecmp(const unsigned char *str1, const unsigned char *str1lim,
const unsigned char *str2, const unsigned char *str2lim)
{
int n1, n2;
wchar_t ucs1, ucs2;
int ch1, ch2;
#if (__STDC_VERSION__ >= 199901L) || (_XOPEN_VERSION >= 500)
mbstate_t ps1, ps2;
memset(&ps1, 0, sizeof(ps1));
memset(&ps2, 0, sizeof(ps2));
while (str1 < str1lim && str2 < str2lim) {
if ((*str1 & 0x80) && (*str2 & 0x80)) { /* both multibyte */
if ((n1 = mbrtowc(&ucs1, (const char *)str1, str1lim - str1, &ps1)) < 0 ||
(n2 = mbrtowc(&ucs2, (const char *)str2, str2lim - str2, &ps2)) < 0) {
#else
while (str1 < str1lim && str2 < str2lim) {
if ((*str1 & 0x80) && (*str2 & 0x80)) { /* both multibyte */
if ((n1 = mbtowc(&ucs1, (const char *)str1, str1lim - str1)) < 0 ||
(n2 = mbtowc(&ucs2, (const char *)str2, str2lim - str2)) < 0) {
#endif
PMNO(errno);
return -1;
}
if (ucs1 != ucs2 && (ucs1 = towupper(ucs1)) != (ucs2 = towupper(ucs2))) {
return ucs1 < ucs2 ? -1 : 1;
}
str1 += n1;
str2 += n2;
} else { /* neither or one multibyte */
ch1 = *str1;
ch2 = *str2;
if (ch1 != ch2 && (ch1 = toupper(ch1)) != (ch2 = toupper(ch2))) {
return ch1 < ch2 ? -1 : 1;
} else if (ch1 == '\0') {
return 0;
}
str1++;
str2++;
}
}
return 0;
}
int
utf8tolower(unsigned char *str, unsigned char *slim)
{
unsigned char *start = str;
#if (__STDC_VERSION__ >= 199901L) || (_XOPEN_VERSION >= 500)
mbstate_t psw, psm;
memset(&psw, 0, sizeof(psw));
memset(&psm, 0, sizeof(psm));
#endif
while (str < slim && *str) {
if ((*str & 0x80) == 0) {
*str = tolower(*str);
++str;
} else {
wchar_t wc, wcl;
size_t n;
#if (__STDC_VERSION__ >= 199901L) || (_XOPEN_VERSION >= 500)
if ((n = mbrtowc(&wc, (const char *)str, slim - str, &psw)) == (size_t)-1) {
PMNO(errno);
return -1;
}
if ((wcl = towlower(wc)) != wc) {
/* These functions are flawed because there are a few characters that encode
* as a different number of bytes depending on whether or not it's the
* upper or lower case version of the UCS code. Right here we to see if
* it didn't convert back to same size as lowercase and if so, return -1.
*/
if (wcrtomb((char *)str, wcl, &psm) != n) {
#else
if ((n = mbtowc(&wc, (const char *)str, slim - str)) == (size_t)-1) {
PMNO(errno);
return -1;
}
if ((wcl = towlower(wc)) != wc) {
if ((size_t)wctomb((char *)str, wcl) != n) {
#endif
PMNO(errno);
return -1;
}
}
str += n;
}
}
return str - start;
}
int
utf8toupper(unsigned char *str, unsigned char *slim)
{
unsigned char *start = str;
#if (__STDC_VERSION__ >= 199901L) || (_XOPEN_VERSION >= 500)
mbstate_t psw, psm;
memset(&psw, 0, sizeof(psw));
memset(&psm, 0, sizeof(psm));
#endif
while (str < slim && *str) {
if ((*str & 0x80) == 0 && 0) {
*str = toupper(*str);
++str;
} else {
wchar_t wc, wcu;
size_t n;
#if (__STDC_VERSION__ >= 199901L) || (_XOPEN_VERSION >= 500)
if ((n = mbrtowc(&wc, (const char *)str, slim - str, &psw)) == (size_t)-1) {
PMNO(errno);
return -1;
}
if ((wcu = towupper(wc)) != wc) {
if (wcrtomb((char *)str, wcu, &psm) != n) {
#else
if ((n = mbtowc(&wc, (const char *)str, slim - str)) == (size_t)-1) {
PMNO(errno);
return -1;
}
if ((wcu = towupper(wc)) != wc) {
if ((size_t)wctomb((char *)str, wcu) != n) {
#endif
PMNO(errno);
return -1;
}
}
str += n;
}
}
return str - start;
}
/* Even though fputws is defined in C99 and UNIX98 we cannot safely mix
* wide character I/O with regular so we might as well just unconditionally
* use our own
*/
int
_fputws(const wchar_t *buf, FILE *stream)
{
char mb[16];
int n = 0;
#if (__STDC_VERSION__ >= 199901L) || (_XOPEN_VERSION >= 500)
mbstate_t ps;
memset(&ps, 0, sizeof(ps));
while (*buf) {
if ((n = wcrtomb(mb, *buf, &ps)) == -1) {
#else
while (*buf) {
if ((n = wctomb(mb, *buf)) == -1) {
#endif
PMNO(errno);
return -1;
}
if (fwrite(mb, n, 1, stream) != 1) {
PMNO(errno);
return -1;
}
buf++;
}
return 0;
}
#if !defined(_GNU_SOURCE)
#if !defined(_BSD_SOURCE) && \
!defined(_XOPEN_SOURCE_EXTENDED) && \
!defined(_WIN32) && \
!(defined(__APPLE__) && defined(__MACH__))
char *
strdup(const char *s)
{
return s ? strcpy(malloc(strlen(s) + 1), s) : NULL;
}
#endif
wchar_t *
wcsdup(const wchar_t *s)
{
return s ? wcscpy(malloc((wcslen(s) + 1) * sizeof *s), s) : NULL;
}
size_t
strnlen(const char *s, size_t maxlen)
{
size_t len;
for (len = 0; *s && len < maxlen; s++, len++);
return len;
}
#if (__STDC_VERSION__ < 199901L) && \
!defined(_BSD_SOURCE) && \
(_XOPEN_VERSION < 500) && \
!(defined(__APPLE__) && defined(__MACH__))
int
vsnprintf(char *str, size_t size, const char *format, va_list ap)
{
(void)size;
return vsprintf(str, format, ap);
}
#endif
size_t
wcsnlen(const wchar_t *s, size_t maxlen)
{
size_t len;
for (len = 0; *s && len < maxlen; s++, len++);
return len;
}
int
wcscasecmp(const wchar_t *s1, const wchar_t *s2)
{
wchar_t c1, c2;
do {
c1 = *s1++;
c2 = *s2++;
if (c1 == L'\0' || c2 == L'\0') {
break;
}
if (c1 != c2) {
c1 = towupper(c1);
c2 = towupper(c2);
}
} while (c1 == c2);
return c1 - c2;
}
#endif
/* "dumb" snprintf that just returns -1 if the buffer wasn't large enough
*/
int
dsnprintf(char *str, size_t size, const char *format, ...)
{
va_list ap;
int n;
va_start(ap, format);
n = vsnprintf(str, size, format, ap);
va_end(ap);
if (n < 0) {
PMNO(errno);
return -1;
} else if ((size_t)n >= size) {
PMNO(errno = ENOBUFS);
return -1;
}
return n;
}