This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_string.c
1618 lines (1498 loc) · 41.7 KB
/
SDL_string.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Simple DirectMedia Layer
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
/* This file contains portable string manipulation functions for SDL */
#include "SDL_stdinc.h"
/* these are always #defined, make them real symbol in the library, too... */
#undef SDL_itoa
#undef SDL_uitoa
#undef SDL_atoi
#undef SDL_atof
char *SDL_itoa(int value, char *str, int radix) { return SDL_itoa_inline(value, str, radix); }
char *SDL_uitoa(unsigned int value, char *str, int radix) { return SDL_uitoa_inline(value, str, radix); }
int SDL_atoi(const char *str) { return SDL_atoi_inline(str); }
double SDL_atof(const char *str) { return SDL_atof_inline(str); }
#define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F'))
#define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f'))
#define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4)
#define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF)
static int UTF8_TrailingBytes(unsigned char c)
{
if (c >= 0xC0 && c <= 0xDF)
return 1;
else if (c >= 0xE0 && c <= 0xEF)
return 2;
else if (c >= 0xF0 && c <= 0xF4)
return 3;
else
return 0;
}
#if !defined(SDL_sscanf) || !defined(SDL_strtol)
static size_t
SDL_ScanLong(const char *text, int radix, long *valuep)
{
const char *textstart = text;
long value = 0;
SDL_bool negative = SDL_FALSE;
if (*text == '-') {
negative = SDL_TRUE;
++text;
}
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
text += 2;
}
for (;;) {
int v;
if (SDL_isdigit((unsigned char) *text)) {
v = *text - '0';
} else if (radix == 16 && SDL_isupperhex(*text)) {
v = 10 + (*text - 'A');
} else if (radix == 16 && SDL_islowerhex(*text)) {
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if (valuep) {
if (negative && value) {
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
#if !defined(SDL_sscanf) || !defined(SDL_strtoul) || !defined(SDL_strtod)
static size_t
SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
{
const char *textstart = text;
unsigned long value = 0;
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
text += 2;
}
for (;;) {
int v;
if (SDL_isdigit((unsigned char) *text)) {
v = *text - '0';
} else if (radix == 16 && SDL_isupperhex(*text)) {
v = 10 + (*text - 'A');
} else if (radix == 16 && SDL_islowerhex(*text)) {
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if (valuep) {
*valuep = value;
}
return (text - textstart);
}
#endif
#ifndef SDL_sscanf
static size_t
SDL_ScanUintPtrT(const char *text, int radix, uintptr_t * valuep)
{
const char *textstart = text;
uintptr_t value = 0;
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
text += 2;
}
for (;;) {
int v;
if (SDL_isdigit((unsigned char) *text)) {
v = *text - '0';
} else if (radix == 16 && SDL_isupperhex(*text)) {
v = 10 + (*text - 'A');
} else if (radix == 16 && SDL_islowerhex(*text)) {
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if (valuep) {
*valuep = value;
}
return (text - textstart);
}
#endif
#if !defined(SDL_sscanf) || !defined(SDL_strtoll)
static size_t
SDL_ScanLongLong(const char *text, int radix, Sint64 * valuep)
{
const char *textstart = text;
Sint64 value = 0;
SDL_bool negative = SDL_FALSE;
if (*text == '-') {
negative = SDL_TRUE;
++text;
}
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
text += 2;
}
for (;;) {
int v;
if (SDL_isdigit((unsigned char) *text)) {
v = *text - '0';
} else if (radix == 16 && SDL_isupperhex(*text)) {
v = 10 + (*text - 'A');
} else if (radix == 16 && SDL_islowerhex(*text)) {
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if (valuep) {
if (negative && value) {
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
#if !defined(SDL_sscanf) || !defined(SDL_strtoull)
static size_t
SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 * valuep)
{
const char *textstart = text;
Uint64 value = 0;
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
text += 2;
}
for (;;) {
int v;
if (SDL_isdigit((unsigned char) *text)) {
v = *text - '0';
} else if (radix == 16 && SDL_isupperhex(*text)) {
v = 10 + (*text - 'A');
} else if (radix == 16 && SDL_islowerhex(*text)) {
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if (valuep) {
*valuep = value;
}
return (text - textstart);
}
#endif
#if !defined(SDL_sscanf) || !defined(SDL_strtod)
static size_t
SDL_ScanFloat(const char *text, double *valuep)
{
const char *textstart = text;
unsigned long lvalue = 0;
double value = 0.0;
SDL_bool negative = SDL_FALSE;
if (*text == '-') {
negative = SDL_TRUE;
++text;
}
text += SDL_ScanUnsignedLong(text, 10, &lvalue);
value += lvalue;
if (*text == '.') {
int mult = 10;
++text;
while (SDL_isdigit((unsigned char) *text)) {
lvalue = *text - '0';
value += (double) lvalue / mult;
mult *= 10;
++text;
}
}
if (valuep) {
if (negative && value) {
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
#ifdef SDL_memset
#undef SDL_memset
void *SDL_memset(void *dst, int c, size_t len) { return SDL_memset_inline(dst, c, len); }
#else
void *
SDL_memset(void *dst, int c, size_t len)
{
size_t left = (len % 4);
Uint32 *dstp4;
Uint8 *dstp1;
Uint32 value4 = (c | (c << 8) | (c << 16) | (c << 24));
Uint8 value1 = (Uint8) c;
dstp4 = (Uint32 *) dst;
len /= 4;
while (len--) {
*dstp4++ = value4;
}
dstp1 = (Uint8 *) dstp4;
switch (left) {
case 3:
*dstp1++ = value1;
case 2:
*dstp1++ = value1;
case 1:
*dstp1++ = value1;
}
return dst;
}
#endif
#ifdef SDL_memcpy
#undef SDL_memcpy
void *SDL_memcpy(void *dst, const void *src, size_t len) { return SDL_memcpy_inline(dst, src, len); }
#else
void *
SDL_memcpy(void *dst, const void *src, size_t len)
{
size_t left = (len % 4);
Uint32 *srcp4, *dstp4;
Uint8 *srcp1, *dstp1;
srcp4 = (Uint32 *) src;
dstp4 = (Uint32 *) dst;
len /= 4;
while (len--) {
*dstp4++ = *srcp4++;
}
srcp1 = (Uint8 *) srcp4;
dstp1 = (Uint8 *) dstp4;
switch (left) {
case 3:
*dstp1++ = *srcp1++;
case 2:
*dstp1++ = *srcp1++;
case 1:
*dstp1++ = *srcp1++;
}
return dst;
}
#endif
#ifdef SDL_memmove
#undef SDL_memmove
void *SDL_memmove(void *dst, const void *src, size_t len) { return SDL_memmove_inline(dst, src, len); }
#else
void *
SDL_memmove(void *dst, const void *src, size_t len)
{
char *srcp = (char *) src;
char *dstp = (char *) dst;
if (src < dst) {
srcp += len - 1;
dstp += len - 1;
while (len--) {
*dstp-- = *srcp--;
}
} else {
while (len--) {
*dstp++ = *srcp++;
}
}
return dst;
}
#endif
#ifdef SDL_memcmp
#undef SDL_memcmp
int SDL_memcmp(const void *s1, const void *s2, size_t len) { return SDL_memcmp_inline(s1, s2, len); }
#else
int
SDL_memcmp(const void *s1, const void *s2, size_t len)
{
char *s1p = (char *) s1;
char *s2p = (char *) s2;
while (len--) {
if (*s1p != *s2p) {
return (*s1p - *s2p);
}
++s1p;
++s2p;
}
return 0;
}
#endif
#ifdef SDL_strlen
#undef SDL_strlen
size_t SDL_strlen(const char *string) { return SDL_strlen_inline(string); }
#else
size_t
SDL_strlen(const char *string)
{
size_t len = 0;
while (*string++) {
++len;
}
return len;
}
#endif
#ifdef SDL_wcslen
#undef SDL_wcslen
size_t SDL_wcslen(const wchar_t * string) { return SDL_wcslen_inline(string); }
#else
size_t
SDL_wcslen(const wchar_t * string)
{
size_t len = 0;
while (*string++) {
++len;
}
return len;
}
#endif
#ifdef SDL_wcslcpy
#undef SDL_wcslcpy
size_t SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen) { return SDL_wcslcpy_inline(dst, src, maxlen); }
#else
size_t
SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen)
{
size_t srclen = SDL_wcslen(src);
if (maxlen > 0) {
size_t len = SDL_min(srclen, maxlen - 1);
SDL_memcpy(dst, src, len * sizeof(wchar_t));
dst[len] = '\0';
}
return srclen;
}
#endif
#ifdef SDL_wcslcat
#undef SDL_wcslcat
size_t SDL_wcslcat(wchar_t *dst, const wchar_t *src, size_t maxlen) { return SDL_wcslcat_inline(dst, src, maxlen); }
#else
size_t
SDL_wcslcat(wchar_t *dst, const wchar_t *src, size_t maxlen)
{
size_t dstlen = SDL_wcslen(dst);
size_t srclen = SDL_wcslen(src);
if (dstlen < maxlen) {
SDL_wcslcpy(dst + dstlen, src, maxlen - dstlen);
}
return dstlen + srclen;
}
#endif
#ifdef SDL_strlcpy
#undef SDL_strlcpy
size_t SDL_strlcpy(char *dst, const char *src, size_t maxlen) { return SDL_strlcpy_inline(dst, src, maxlen); }
#else
size_t
SDL_strlcpy(char *dst, const char *src, size_t maxlen)
{
size_t srclen = SDL_strlen(src);
if (maxlen > 0) {
size_t len = SDL_min(srclen, maxlen - 1);
SDL_memcpy(dst, src, len);
dst[len] = '\0';
}
return srclen;
}
#endif
size_t SDL_utf8strlcpy(char *dst, const char *src, size_t dst_bytes)
{
size_t src_bytes = SDL_strlen(src);
size_t bytes = SDL_min(src_bytes, dst_bytes - 1);
size_t i = 0;
char trailing_bytes = 0;
if (bytes)
{
unsigned char c = (unsigned char)src[bytes - 1];
if (UTF8_IsLeadByte(c))
--bytes;
else if (UTF8_IsTrailingByte(c))
{
for (i = bytes - 1; i != 0; --i)
{
c = (unsigned char)src[i];
trailing_bytes = UTF8_TrailingBytes(c);
if (trailing_bytes)
{
if (bytes - i != trailing_bytes + 1)
bytes = i;
break;
}
}
}
SDL_memcpy(dst, src, bytes);
}
dst[bytes] = '\0';
return bytes;
}
#ifdef SDL_strlcat
#undef SDL_strlcat
size_t SDL_strlcat(char *dst, const char *src, size_t maxlen) { return SDL_strlcat_inline(dst, src, maxlen); }
#else
size_t
SDL_strlcat(char *dst, const char *src, size_t maxlen)
{
size_t dstlen = SDL_strlen(dst);
size_t srclen = SDL_strlen(src);
if (dstlen < maxlen) {
SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
}
return dstlen + srclen;
}
#endif
#ifdef SDL_strdup
#undef SDL_strdup
char *SDL_strdup(const char *string) { return SDL_strdup_inline(string); }
#else
char *
SDL_strdup(const char *string)
{
size_t len = SDL_strlen(string) + 1;
char *newstr = SDL_malloc(len);
if (newstr) {
SDL_strlcpy(newstr, string, len);
}
return newstr;
}
#endif
#ifdef SDL_strrev
#undef SDL_strrev
char *SDL_strrev(char *string) { return SDL_strrev_inline(string); }
#else
char *
SDL_strrev(char *string)
{
size_t len = SDL_strlen(string);
char *a = &string[0];
char *b = &string[len - 1];
len /= 2;
while (len--) {
char c = *a;
*a++ = *b;
*b-- = c;
}
return string;
}
#endif
#ifdef SDL_strupr
#undef SDL_strupr
char *SDL_strupr(char *string) { return SDL_strupr_inline(string); }
#else
char *
SDL_strupr(char *string)
{
char *bufp = string;
while (*bufp) {
*bufp = SDL_toupper((unsigned char) *bufp);
++bufp;
}
return string;
}
#endif
#ifdef SDL_strlwr
#undef SDL_strlwr
char *SDL_strlwr(char *string) { return SDL_strlwr_inline(string); }
#else
char *
SDL_strlwr(char *string)
{
char *bufp = string;
while (*bufp) {
*bufp = SDL_tolower((unsigned char) *bufp);
++bufp;
}
return string;
}
#endif
#ifdef SDL_strchr
#undef SDL_strchr
char *SDL_strchr(const char *string, int c) { return SDL_strchr_inline(string, c); }
#else
char *
SDL_strchr(const char *string, int c)
{
while (*string) {
if (*string == c) {
return (char *) string;
}
++string;
}
return NULL;
}
#endif
#ifdef SDL_strrchr
#undef SDL_strrchr
char *SDL_strrchr(const char *string, int c) { return SDL_strrchr_inline(string, c); }
#else
char *
SDL_strrchr(const char *string, int c)
{
const char *bufp = string + SDL_strlen(string) - 1;
while (bufp >= string) {
if (*bufp == c) {
return (char *) bufp;
}
--bufp;
}
return NULL;
}
#endif
#ifdef SDL_strstr
#undef SDL_strstr
char *SDL_strstr(const char *haystack, const char *needle) { return SDL_strstr_inline(haystack, needle); }
#else
char *
SDL_strstr(const char *haystack, const char *needle)
{
size_t length = SDL_strlen(needle);
while (*haystack) {
if (SDL_strncmp(haystack, needle, length) == 0) {
return (char *) haystack;
}
++haystack;
}
return NULL;
}
#endif
#if !defined(SDL_ltoa) || !defined(SDL_lltoa) || \
!defined(SDL_ultoa) || !defined(SDL_ulltoa)
static const char ntoa_table[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'
};
#endif /* ntoa() conversion table */
#ifdef SDL_ltoa
#undef SDL_ltoa
char *SDL_ltoa(long value, char *string, int radix) { return SDL_ltoa_inline(value, string, radix); }
#else
char *
SDL_ltoa(long value, char *string, int radix)
{
char *bufp = string;
if (value < 0) {
*bufp++ = '-';
value = -value;
}
if (value) {
while (value > 0) {
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
if (*string == '-') {
SDL_strrev(string + 1);
} else {
SDL_strrev(string);
}
return string;
}
#endif
#ifdef SDL_ultoa
#undef SDL_ultoa
char *SDL_ultoa(unsigned long value, char *string, int radix) { return SDL_ultoa_inline(value, string, radix); }
#else
char *
SDL_ultoa(unsigned long value, char *string, int radix)
{
char *bufp = string;
if (value) {
while (value > 0) {
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
SDL_strrev(string);
return string;
}
#endif
#ifdef SDL_strtol
#undef SDL_strtol
long SDL_strtol(const char *string, char **endp, int base) { return SDL_strtol_inline(string, endp, base); }
#else
long
SDL_strtol(const char *string, char **endp, int base)
{
size_t len;
long value;
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLong(string, base, &value);
if (endp) {
*endp = (char *) string + len;
}
return value;
}
#endif
#ifdef SDL_strtoul
#undef SDL_strtoul
unsigned long SDL_strtoul(const char *string, char **endp, int base) { return SDL_strtoul_inline(string, endp, base); }
#else
unsigned long
SDL_strtoul(const char *string, char **endp, int base)
{
size_t len;
unsigned long value;
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLong(string, base, &value);
if (endp) {
*endp = (char *) string + len;
}
return value;
}
#endif
#ifdef SDL_lltoa
#undef SDL_lltoa
char *SDL_lltoa(Sint64 value, char *string, int radix) { return SDL_lltoa_inline(value, string, radix); }
#else
char *
SDL_lltoa(Sint64 value, char *string, int radix)
{
char *bufp = string;
if (value < 0) {
*bufp++ = '-';
value = -value;
}
if (value) {
while (value > 0) {
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
if (*string == '-') {
SDL_strrev(string + 1);
} else {
SDL_strrev(string);
}
return string;
}
#endif
#ifdef SDL_ulltoa
#undef SDL_ulltoa
char *SDL_ulltoa(Uint64 value, char *string, int radix) { return SDL_ulltoa_inline(value, string, radix); }
#else
char *
SDL_ulltoa(Uint64 value, char *string, int radix)
{
char *bufp = string;
if (value) {
while (value > 0) {
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
SDL_strrev(string);
return string;
}
#endif
#ifdef SDL_strtoll
#undef SDL_strtoll
Sint64 SDL_strtoll(const char *string, char **endp, int base) { return SDL_strtoll_inline(string, endp, base); }
#else
Sint64
SDL_strtoll(const char *string, char **endp, int base)
{
size_t len;
Sint64 value;
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLongLong(string, base, &value);
if (endp) {
*endp = (char *) string + len;
}
return value;
}
#endif
#ifdef SDL_strtoull
#undef SDL_strtoull
Uint64 SDL_strtoull(const char *string, char **endp, int base) { return SDL_strtoull_inline(string, endp, base); }
#else
Uint64
SDL_strtoull(const char *string, char **endp, int base)
{
size_t len;
Uint64 value;
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLongLong(string, base, &value);
if (endp) {
*endp = (char *) string + len;
}
return value;
}
#endif
#ifdef SDL_strtod
#undef SDL_strtod
double SDL_strtod(const char *string, char **endp) { return SDL_strtod_inline(string, endp); }
#else
double
SDL_strtod(const char *string, char **endp)
{
size_t len;
double value;
len = SDL_ScanFloat(string, &value);
if (endp) {
*endp = (char *) string + len;
}
return value;
}
#endif
#ifdef SDL_strcmp
#undef SDL_strcmp
int SDL_strcmp(const char *str1, const char *str2) { return SDL_strcmp_inline(str1, str2); }
#else
int
SDL_strcmp(const char *str1, const char *str2)
{
while (*str1 && *str2) {
if (*str1 != *str2)
break;
++str1;
++str2;
}
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
}
#endif
#ifdef SDL_strncmp
#undef SDL_strncmp
int SDL_strncmp(const char *str1, const char *str2, size_t maxlen) { return SDL_strncmp_inline(str1, str2, maxlen); }
#else
int
SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
{
while (*str1 && *str2 && maxlen) {
if (*str1 != *str2)
break;
++str1;
++str2;
--maxlen;
}
if (!maxlen) {
return 0;
}
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
}
#endif
#ifdef SDL_strcasecmp
#undef SDL_strcasecmp
int SDL_strcasecmp(const char *str1, const char *str2) { return SDL_strcasecmp_inline(str1, str2); }
#else
int
SDL_strcasecmp(const char *str1, const char *str2)
{
char a = 0;
char b = 0;
while (*str1 && *str2) {
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
if (a != b)
break;
++str1;
++str2;
}
a = SDL_tolower(*str1);
b = SDL_tolower(*str2);
return (int) ((unsigned char) a - (unsigned char) b);
}
#endif
#ifdef SDL_strncasecmp
#undef SDL_strncasecmp
int SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen) { return SDL_strncasecmp_inline(str1, str2, maxlen); }
#else
int
SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
{
char a = 0;
char b = 0;
while (*str1 && *str2 && maxlen) {
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
if (a != b)
break;
++str1;
++str2;
--maxlen;
}
if (maxlen == 0) {
return 0;
} else {
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
return (int) ((unsigned char) a - (unsigned char) b);
}
}
#endif
#ifdef SDL_sscanf
#undef SDL_sscanf
int
SDL_sscanf(const char *text, const char *fmt, ...)
{
int rc;
va_list ap;
va_start(ap, fmt);
rc = vsscanf(text, fmt, ap);
va_end(ap);
return rc;
}
#else
int
SDL_sscanf(const char *text, const char *fmt, ...)
{
va_list ap;
int retval = 0;
va_start(ap, fmt);
while (*fmt) {
if (*fmt == ' ') {
while (SDL_isspace((unsigned char) *text)) {
++text;
}
++fmt;