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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
|
#include "String.h"
#include "Funcs.h"
#include "Logger.h"
#include "Platform.h"
#include "Stream.h"
#include "Utils.h"
#ifdef __cplusplus
const cc_string String_Empty = { NULL, 0, 0 };
#else
const cc_string String_Empty;
#endif
int String_CalcLen(const char* raw, int capacity) {
int length = 0;
while (length < capacity && *raw) { raw++; length++; }
return length;
}
int String_Length(const char* raw) {
int length = 0;
while (length < UInt16_MaxValue && *raw) { raw++; length++; }
return length;
}
cc_string String_FromRaw(STRING_REF char* buffer, int capacity) {
return String_Init(buffer, String_CalcLen(buffer, capacity), capacity);
}
cc_string String_FromReadonly(STRING_REF const char* buffer) {
int len = String_Length(buffer);
return String_Init((char*)buffer, len, len);
}
void String_Copy(cc_string* dst, const cc_string* src) {
dst->length = 0;
String_AppendString(dst, src);
}
void String_CopyToRaw(char* dst, int capacity, const cc_string* src) {
int i, len = min(capacity, src->length);
for (i = 0; i < len; i++) { dst[i] = src->buffer[i]; }
/* add \0 to mark end of used portion of buffer */
if (len < capacity) dst[len] = '\0';
}
cc_string String_UNSAFE_Substring(STRING_REF const cc_string* str, int offset, int length) {
if (offset < 0 || offset > str->length) {
Logger_Abort("Offset for substring out of range");
}
if (length < 0 || length > str->length) {
Logger_Abort("Length for substring out of range");
}
if (offset + length > str->length) {
Logger_Abort("Result substring is out of range");
}
return String_Init(str->buffer + offset, length, length);
}
cc_string String_UNSAFE_SubstringAt(STRING_REF const cc_string* str, int offset) {
cc_string sub;
if (offset < 0 || offset > str->length) Logger_Abort("Sub offset out of range");
sub.buffer = str->buffer + offset;
sub.length = str->length - offset;
sub.capacity = str->length - offset; /* str->length to match String_UNSAFE_Substring */
return sub;
}
int String_UNSAFE_Split(STRING_REF const cc_string* str, char c, cc_string* subs, int maxSubs) {
int beg = 0, end, count, i;
for (i = 0; i < maxSubs && beg <= str->length; i++)
{
end = String_IndexOfAt(str, beg, c);
if (end == -1 || i == (maxSubs - 1)) end = str->length;
subs[i] = String_UNSAFE_Substring(str, beg, end - beg);
beg = end + 1;
}
count = i;
/* If not enough split substrings, make remaining NULL */
for (; i < maxSubs; i++) { subs[i] = String_Empty; }
return count;
}
void String_UNSAFE_SplitBy(STRING_REF cc_string* str, char c, cc_string* part) {
int idx = String_IndexOf(str, c);
if (idx == -1) {
*part = *str;
*str = String_Empty;
} else {
*part = String_UNSAFE_Substring(str, 0, idx); idx++;
*str = String_UNSAFE_SubstringAt(str, idx);
}
}
int String_UNSAFE_Separate(STRING_REF const cc_string* str, char c, cc_string* key, cc_string* value) {
int idx = String_IndexOf(str, c);
if (idx == -1) {
*key = *str;
*value = String_Empty;
return false;
}
*key = String_UNSAFE_Substring(str, 0, idx); idx++;
*value = String_UNSAFE_SubstringAt(str, idx);
/* Trim key [c] value to just key[c]value */
String_UNSAFE_TrimEnd(key);
String_UNSAFE_TrimStart(value);
return key->length > 0 && value->length > 0;
}
int String_Equals(const cc_string* a, const cc_string* b) {
return a->length == b->length && Mem_Equal(a->buffer, b->buffer, a->length);
}
int String_CaselessEquals(const cc_string* a, const cc_string* b) {
int i;
char aCur, bCur;
if (a->length != b->length) return false;
for (i = 0; i < a->length; i++) {
aCur = a->buffer[i]; Char_MakeLower(aCur);
bCur = b->buffer[i]; Char_MakeLower(bCur);
if (aCur != bCur) return false;
}
return true;
}
int String_CaselessEqualsConst(const cc_string* a, const char* b) {
int i;
char aCur, bCur;
for (i = 0; i < a->length; i++) {
aCur = a->buffer[i]; Char_MakeLower(aCur);
bCur = b[i]; Char_MakeLower(bCur);
if (aCur != bCur || bCur == '\0') return false;
}
/* ensure at end of string */
return b[a->length] == '\0';
}
void String_Append(cc_string* str, char c) {
/* MSVC in debug mode will initialise all variables on the stack with 0xCC by default */
/* So if a string is being passed with CC in all its fields, then it's probably invalid */
#if _MSC_VER && _DEBUG
if (str->length == 0xCCCC && str->capacity == 0xCCCC)
Logger_Abort("String must be initialised before calling String_Append");
#endif
if (str->length == str->capacity) return;
str->buffer[str->length++] = c;
}
void String_AppendBool(cc_string* str, cc_bool value) {
String_AppendConst(str, value ? "True" : "False");
}
int String_MakeUInt32(cc_uint32 num, char* digits) {
int len = 0;
do {
digits[len] = '0' + (num % 10); num /= 10; len++;
} while (num > 0);
return len;
}
void String_AppendInt(cc_string* str, int num) {
if (num < 0) {
num = -num;
String_Append(str, '-');
}
String_AppendUInt32(str, (cc_uint32)num);
}
void String_AppendUInt32(cc_string* str, cc_uint32 num) {
char digits[STRING_INT_CHARS];
int i, count = String_MakeUInt32(num, digits);
for (i = count - 1; i >= 0; i--) {
String_Append(str, digits[i]);
}
}
void String_AppendPaddedInt(cc_string* str, int num, int minDigits) {
char digits[STRING_INT_CHARS];
int i, count;
for (i = 0; i < minDigits; i++) { digits[i] = '0'; }
count = String_MakeUInt32(num, digits);
if (count < minDigits) count = minDigits;
for (i = count - 1; i >= 0; i--) {
String_Append(str, digits[i]);
}
}
void String_AppendFloat(cc_string* str, float num, int fracDigits) {
int i, whole, digit;
double frac;
if (num < 0.0f) {
String_Append(str, '-'); /* don't need to check success */
num = -num;
}
whole = (int)num;
String_AppendUInt32(str, whole);
frac = (double)num - (double)whole;
if (frac == 0.0) return;
String_Append(str, '.'); /* don't need to check success */
for (i = 0; i < fracDigits; i++) {
frac *= 10;
digit = (int)frac % 10;
String_Append(str, '0' + digit);
}
}
void String_AppendHex(cc_string* str, cc_uint8 value) {
/* 48 = index of 0, 55 = index of (A - 10) */
cc_uint8 hi = (value >> 4) & 0xF;
char c_hi = hi < 10 ? (hi + 48) : (hi + 55);
cc_uint8 lo = value & 0xF;
char c_lo = lo < 10 ? (lo + 48) : (lo + 55);
String_Append(str, c_hi);
String_Append(str, c_lo);
}
CC_NOINLINE static void String_Hex32(cc_string* str, cc_uint32 value) {
int shift;
for (shift = 24; shift >= 0; shift -= 8) {
cc_uint8 part = (cc_uint8)(value >> shift);
String_AppendHex(str, part);
}
}
CC_NOINLINE static void String_Hex64(cc_string* str, cc_uint64 value) {
int shift;
for (shift = 56; shift >= 0; shift -= 8) {
cc_uint8 part = (cc_uint8)(value >> shift);
String_AppendHex(str, part);
}
}
void String_AppendConst(cc_string* str, const char* src) {
for (; *src; src++) {
String_Append(str, *src);
}
}
void String_AppendAll(cc_string* str, const void* data, int len) {
const char* src = (const char*)data;
int i;
for (i = 0; i < len; i++) String_Append(str, src[i]);
}
void String_AppendString(cc_string* str, const cc_string* src) {
int i;
for (i = 0; i < src->length; i++) {
String_Append(str, src->buffer[i]);
}
}
void String_AppendColorless(cc_string* str, const cc_string* src) {
char c;
int i;
for (i = 0; i < src->length; i++) {
c = src->buffer[i];
if (c == '&') { i++; continue; } /* Skip over the following color code */
String_Append(str, c);
}
}
int String_IndexOfAt(const cc_string* str, int offset, char c) {
int i;
for (i = offset; i < str->length; i++) {
if (str->buffer[i] == c) return i;
}
return -1;
}
int String_LastIndexOfAt(const cc_string* str, int offset, char c) {
int i;
for (i = (str->length - 1) - offset; i >= 0; i--) {
if (str->buffer[i] == c) return i;
}
return -1;
}
void String_InsertAt(cc_string* str, int offset, char c) {
int i;
if (offset < 0 || offset > str->length) {
Logger_Abort("Offset for InsertAt out of range");
}
if (str->length == str->capacity) {
Logger_Abort("Cannot insert character into full string");
}
for (i = str->length; i > offset; i--) {
str->buffer[i] = str->buffer[i - 1];
}
str->buffer[offset] = c;
str->length++;
}
void String_DeleteAt(cc_string* str, int offset) {
int i;
if (offset < 0 || offset >= str->length) {
Logger_Abort("Offset for DeleteAt out of range");
}
for (i = offset; i < str->length - 1; i++) {
str->buffer[i] = str->buffer[i + 1];
}
str->buffer[str->length - 1] = '\0';
str->length--;
}
void String_UNSAFE_TrimStart(cc_string* str) {
int i;
for (i = 0; i < str->length; i++) {
if (str->buffer[i] != ' ') break;
str->buffer++;
str->length--; i--;
}
}
void String_UNSAFE_TrimEnd(cc_string* str) {
int i;
for (i = str->length - 1; i >= 0; i--) {
if (str->buffer[i] != ' ') break;
str->length--;
}
}
int String_IndexOfConst(const cc_string* str, const char* sub) {
int i, j;
for (i = 0; i < str->length; i++) {
for (j = 0; sub[j] && (i + j) < str->length; j++) {
if (str->buffer[i + j] != sub[j]) break;
}
if (sub[j] == '\0') return i;
}
return -1;
}
int String_CaselessContains(const cc_string* str, const cc_string* sub) {
char strCur, subCur;
int i, j;
for (i = 0; i < str->length; i++) {
for (j = 0; j < sub->length && (i + j) < str->length; j++) {
strCur = str->buffer[i + j]; Char_MakeLower(strCur);
subCur = sub->buffer[j]; Char_MakeLower(subCur);
if (strCur != subCur) break;
}
if (j == sub->length) return true;
}
return false;
}
int String_CaselessStarts(const cc_string* str, const cc_string* sub) {
char strCur, subCur;
int i;
if (str->length < sub->length) return false;
for (i = 0; i < sub->length; i++) {
strCur = str->buffer[i]; Char_MakeLower(strCur);
subCur = sub->buffer[i]; Char_MakeLower(subCur);
if (strCur != subCur) return false;
}
return true;
}
int String_CaselessEnds(const cc_string* str, const cc_string* sub) {
char strCur, subCur;
int i, j = str->length - sub->length;
if (j < 0) return false; /* sub longer than str */
for (i = 0; i < sub->length; i++) {
strCur = str->buffer[j + i]; Char_MakeLower(strCur);
subCur = sub->buffer[i]; Char_MakeLower(subCur);
if (strCur != subCur) return false;
}
return true;
}
int String_Compare(const cc_string* a, const cc_string* b) {
char aCur, bCur;
int i, minLen = min(a->length, b->length);
for (i = 0; i < minLen; i++) {
aCur = a->buffer[i]; Char_MakeLower(aCur);
bCur = b->buffer[i]; Char_MakeLower(bCur);
if (aCur == bCur) continue;
return (cc_uint8)aCur - (cc_uint8)bCur;
}
/* all chars are equal here - same string, or a substring */
return a->length - b->length;
}
void String_Format1(cc_string* str, const char* format, const void* a1) {
String_Format4(str, format, a1, NULL, NULL, NULL);
}
void String_Format2(cc_string* str, const char* format, const void* a1, const void* a2) {
String_Format4(str, format, a1, a2, NULL, NULL);
}
void String_Format3(cc_string* str, const char* format, const void* a1, const void* a2, const void* a3) {
String_Format4(str, format, a1, a2, a3, NULL);
}
void String_Format4(cc_string* str, const char* format, const void* a1, const void* a2, const void* a3, const void* a4) {
const void* arg;
int i, j = 0, digits;
const void* args[4];
args[0] = a1; args[1] = a2; args[2] = a3; args[3] = a4;
for (i = 0; format[i]; i++) {
if (format[i] != '%') { String_Append(str, format[i]); continue; }
arg = args[j++];
switch (format[++i]) {
case 'b':
String_AppendInt(str, *((cc_uint8*)arg));
break;
case 'i':
String_AppendInt(str, *((int*)arg));
break;
case 'f':
digits = format[++i] - '0';
String_AppendFloat(str, *((float*)arg), digits);
break;
case 'p':
digits = format[++i] - '0';
String_AppendPaddedInt(str, *((int*)arg), digits);
break;
case 't':
String_AppendBool(str, *((cc_bool*)arg));
break;
case 'c':
String_AppendConst(str, (char*)arg);
break;
case 's':
String_AppendString(str, (cc_string*)arg);
break;
case 'r':
String_Append(str, *((char*)arg));
break;
case 'x':
if (sizeof(cc_uintptr) == 4) {
String_Hex32(str, *((cc_uint32*)arg));
} else {
String_Hex64(str, *((cc_uint64*)arg));
}
break;
case 'h':
String_Hex32(str, *((cc_uint32*)arg));
break;
case 'e':
digits = *((int*)arg);
if (digits >= -0xFFFF && digits <= 0xFFFF) {
String_AppendInt(str, digits);
} else {
String_Hex32(str, (cc_uint32)digits);
}
break;
case '%':
String_Append(str, '%');
break;
default:
Logger_Abort("Invalid type for string format");
}
}
}
/*########################################################################################################################*
*------------------------------------------------Character set conversions------------------------------------------------*
*#########################################################################################################################*/
static const cc_unichar controlChars[32] = {
0x0000, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C,
0x25BA, 0x25C4, 0x2195, 0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8,
0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC
};
static const cc_unichar extendedChars[129] = { 0x2302,
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,
0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9,
0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA,
0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,
0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4,
0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248,
0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
};
cc_unichar Convert_CP437ToUnicode(char c) {
cc_uint8 raw = (cc_uint8)c;
if (raw < 0x20) return controlChars[raw];
if (raw < 0x7F) return raw;
return extendedChars[raw - 0x7F];
}
char Convert_CodepointToCP437(cc_codepoint cp) {
char c; Convert_TryCodepointToCP437(cp, &c); return c;
}
static cc_codepoint ReduceEmoji(cc_codepoint cp) {
if (cp == 0x1F31E) return 0x263C;
if (cp == 0x1F3B5) return 0x266B;
if (cp == 0x1F642) return 0x263A;
if (cp == 0x1F600 || cp == 0x1F601 || cp == 0x1F603) return 0x263A;
if (cp == 0x1F604 || cp == 0x1F606 || cp == 0x1F60A) return 0x263A;
return cp;
}
cc_bool Convert_TryCodepointToCP437(cc_codepoint cp, char* c) {
int i;
if (cp >= 0x20 && cp < 0x7F) { *c = (char)cp; return true; }
if (cp >= 0x1F000) cp = ReduceEmoji(cp);
for (i = 0; i < Array_Elems(controlChars); i++) {
if (controlChars[i] == cp) { *c = i; return true; }
}
for (i = 0; i < Array_Elems(extendedChars); i++) {
if (extendedChars[i] == cp) { *c = i + 0x7F; return true; }
}
*c = '?'; return false;
}
int Convert_Utf8ToCodepoint(cc_codepoint* cp, const cc_uint8* data, cc_uint32 len) {
*cp = '\0';
if (!len) return 0;
if (data[0] <= 0x7F) {
*cp = data[0];
return 1;
} else if ((data[0] & 0xE0) == 0xC0) {
if (len < 2) return 0;
*cp = ((data[0] & 0x1F) << 6) | ((data[1] & 0x3F));
return 2;
} else if ((data[0] & 0xF0) == 0xE0) {
if (len < 3) return 0;
*cp = ((data[0] & 0x0F) << 12) | ((data[1] & 0x3F) << 6)
| ((data[2] & 0x3F));
return 3;
} else {
if (len < 4) return 0;
*cp = ((data[0] & 0x07) << 18) | ((data[1] & 0x3F) << 12)
| ((data[2] & 0x3F) << 6) | (data[3] & 0x3F);
return 4;
}
}
/* Encodes a unicode character in UTF8, returning number of bytes written */
static int Convert_UnicodeToUtf8(cc_unichar uc, cc_uint8* data) {
if (uc <= 0x7F) {
data[0] = (cc_uint8)uc;
return 1;
} else if (uc <= 0x7FF) {
data[0] = 0xC0 | ((uc >> 6) & 0x1F);
data[1] = 0x80 | ((uc) & 0x3F);
return 2;
} else {
data[0] = 0xE0 | ((uc >> 12) & 0x0F);
data[1] = 0x80 | ((uc >> 6) & 0x3F);
data[2] = 0x80 | ((uc) & 0x3F);
return 3;
}
}
int Convert_CP437ToUtf8(char c, cc_uint8* data) {
/* Common ASCII case */
if (c >= 0x20 && c < 0x7F) {
data[0] = (cc_uint8)c;
return 1;
}
return Convert_UnicodeToUtf8(Convert_CP437ToUnicode(c), data);
}
void String_AppendUtf16(cc_string* value, const void* data, int numBytes) {
const cc_unichar* chars = (const cc_unichar*)data;
int i; char c;
for (i = 0; i < (numBytes >> 1); i++) {
/* TODO: UTF16 to codepoint conversion */
if (Convert_TryCodepointToCP437(chars[i], &c)) String_Append(value, c);
}
}
void String_AppendUtf8(cc_string* value, const void* data, int numBytes) {
const cc_uint8* chars = (const cc_uint8*)data;
int len; cc_codepoint cp; char c;
for (; numBytes > 0; numBytes -= len) {
len = Convert_Utf8ToCodepoint(&cp, chars, numBytes);
if (!len) return;
if (Convert_TryCodepointToCP437(cp, &c)) String_Append(value, c);
chars += len;
}
}
void String_DecodeCP1252(cc_string* value, const void* data, int numBytes) {
const cc_uint8* chars = (const cc_uint8*)data;
int i; char c;
for (i = 0; i < numBytes; i++) {
if (Convert_TryCodepointToCP437(chars[i], &c)) String_Append(value, c);
}
}
int String_EncodeUtf8(void* data, const cc_string* src) {
cc_uint8* dst = (cc_uint8*)data;
cc_uint8* cur;
int i, len = 0;
if (src->length > FILENAME_SIZE) Logger_Abort("String too long to expand");
for (i = 0; i < src->length; i++) {
cur = dst + len;
len += Convert_CP437ToUtf8(src->buffer[i], cur);
}
dst[len] = '\0';
return len;
}
/*########################################################################################################################*
*--------------------------------------------------Numerical conversions--------------------------------------------------*
*#########################################################################################################################*/
cc_bool Convert_ParseUInt8(const cc_string* str, cc_uint8* value) {
int tmp;
*value = 0;
if (!Convert_ParseInt(str, &tmp) || tmp < 0 || tmp > UInt8_MaxValue) return false;
*value = (cc_uint8)tmp; return true;
}
cc_bool Convert_ParseUInt16(const cc_string* str, cc_uint16* value) {
int tmp;
*value = 0;
if (!Convert_ParseInt(str, &tmp) || tmp < 0 || tmp > UInt16_MaxValue) return false;
*value = (cc_uint16)tmp; return true;
}
static int Convert_CompareDigits(const char* digits, const char* magnitude) {
int i;
for (i = 0; ; i++) {
if (magnitude[i] == '\0') return 0;
if (digits[i] > magnitude[i]) return 1;
if (digits[i] < magnitude[i]) return -1;
}
return 0;
}
static cc_bool Convert_TryParseDigits(const cc_string* str, cc_bool* negative, char* digits, int maxDigits) {
char* start = digits;
int offset = 0, i;
*negative = false;
if (!str->length) return false;
digits += (maxDigits - 1);
/* Handle number signs */
if (str->buffer[0] == '-') { *negative = true; offset = 1; }
if (str->buffer[0] == '+') { offset = 1; }
/* add digits, starting at last digit */
for (i = str->length - 1; i >= offset; i--)
{
char c = str->buffer[i];
if (c < '0' || c > '9' || digits < start) return false;
*digits-- = c;
}
for (; digits >= start; ) { *digits-- = '0'; }
return true;
}
#define INT32_DIGITS 10
cc_bool Convert_ParseInt(const cc_string* str, int* value) {
cc_bool negative;
char digits[INT32_DIGITS];
int i, compare, sum = 0;
*value = 0;
if (!Convert_TryParseDigits(str, &negative, digits, INT32_DIGITS)) return false;
if (negative) {
compare = Convert_CompareDigits(digits, "2147483648");
/* Special case, since |largest min value| is > |largest max value| */
if (compare == 0) { *value = Int32_MinValue; return true; }
} else {
compare = Convert_CompareDigits(digits, "2147483647");
}
if (compare > 0) return false;
for (i = 0; i < INT32_DIGITS; i++) {
sum *= 10; sum += digits[i] - '0';
}
if (negative) sum = -sum;
*value = sum;
return true;
}
#define UINT64_DIGITS 20
cc_bool Convert_ParseUInt64(const cc_string* str, cc_uint64* value) {
cc_bool negative;
char digits[UINT64_DIGITS];
int i, compare;
cc_uint64 sum = 0;
*value = 0;
if (!Convert_TryParseDigits(str, &negative, digits, UINT64_DIGITS)) return false;
compare = Convert_CompareDigits(digits, "18446744073709551615");
if (negative || compare > 0) return false;
for (i = 0; i < UINT64_DIGITS; i++) {
sum *= 10; sum += digits[i] - '0';
}
*value = sum;
return true;
}
cc_bool Convert_ParseFloat(const cc_string* str, float* value) {
int i = 0;
cc_bool negate = false;
double sum, whole, fract, divide = 10.0;
*value = 0.0f;
/* Handle number signs */
if (!str->length) return false;
if (str->buffer[0] == '-') { negate = true; i++; }
if (str->buffer[0] == '+') { i++; }
for (whole = 0.0; i < str->length; i++) {
char c = str->buffer[i];
if (c == '.' || c == ',') { i++; break; }
if (c < '0' || c > '9') return false;
whole *= 10; whole += (c - '0');
}
for (fract = 0.0; i < str->length; i++) {
char c = str->buffer[i];
if (c < '0' || c > '9') return false;
fract += (c - '0') / divide; divide *= 10;
}
sum = whole + fract;
if (negate) sum = -sum;
*value = (float)sum;
return true;
}
cc_bool Convert_ParseBool(const cc_string* str, cc_bool* value) {
if (String_CaselessEqualsConst(str, "True")) {
*value = true; return true;
}
if (String_CaselessEqualsConst(str, "False")) {
*value = false; return true;
}
*value = false; return false;
}
/*########################################################################################################################*
*------------------------------------------------------StringsBuffer------------------------------------------------------*
*#########################################################################################################################*/
#define STRINGSBUFFER_BUFFER_EXPAND_SIZE 8192
#define StringsBuffer_GetOffset(raw) ((raw) >> buffer->_lenShift)
#define StringsBuffer_GetLength(raw) ((raw) & buffer->_lenMask)
#define StringsBuffer_PackOffset(off) ((off) << buffer->_lenShift)
void StringsBuffer_Init(struct StringsBuffer* buffer) {
buffer->count = 0;
buffer->totalLength = 0;
buffer->textBuffer = buffer->_defaultBuffer;
buffer->flagsBuffer = buffer->_defaultFlags;
buffer->_textCapacity = STRINGSBUFFER_BUFFER_DEF_SIZE;
buffer->_flagsCapacity = STRINGSBUFFER_FLAGS_DEF_ELEMS;
if (buffer->_lenShift) return;
StringsBuffer_SetLengthBits(buffer, STRINGSBUFFER_DEF_LEN_SHIFT);
}
void StringsBuffer_SetLengthBits(struct StringsBuffer* buffer, int bits) {
buffer->_lenShift = bits;
buffer->_lenMask = (1 << bits) - 1;
}
void StringsBuffer_Clear(struct StringsBuffer* buffer) {
/* Never initialised to begin with */
if (!buffer->_flagsCapacity) return;
if (buffer->textBuffer != buffer->_defaultBuffer) {
Mem_Free(buffer->textBuffer);
}
if (buffer->flagsBuffer != buffer->_defaultFlags) {
Mem_Free(buffer->flagsBuffer);
}
StringsBuffer_Init(buffer);
}
cc_string StringsBuffer_UNSAFE_Get(struct StringsBuffer* buffer, int i) {
cc_uint32 flags, offset, len;
if (i < 0 || i >= buffer->count) Logger_Abort("Tried to get String past StringsBuffer end");
flags = buffer->flagsBuffer[i];
offset = StringsBuffer_GetOffset(flags);
len = StringsBuffer_GetLength(flags);
return String_Init(&buffer->textBuffer[offset], len, len);
}
void StringsBuffer_UNSAFE_GetRaw(struct StringsBuffer* buffer, int i, cc_string* dst) {
cc_uint32 flags = buffer->flagsBuffer[i];
dst->buffer = buffer->textBuffer + StringsBuffer_GetOffset(flags);
dst->length = StringsBuffer_GetLength(flags);
dst->capacity = 0;
}
void StringsBuffer_Add(struct StringsBuffer* buffer, const cc_string* str) {
int textOffset;
/* StringsBuffer hasn't been initialised yet, do it here */
if (!buffer->_flagsCapacity) StringsBuffer_Init(buffer);
if (buffer->count == buffer->_flagsCapacity) {
Utils_Resize((void**)&buffer->flagsBuffer, &buffer->_flagsCapacity,
4, STRINGSBUFFER_FLAGS_DEF_ELEMS, 512);
}
if (str->length > buffer->_lenMask) {
Logger_Abort("String too big to insert into StringsBuffer");
}
textOffset = buffer->totalLength;
if (textOffset + str->length >= buffer->_textCapacity) {
Utils_Resize((void**)&buffer->textBuffer, &buffer->_textCapacity,
1, STRINGSBUFFER_BUFFER_DEF_SIZE, 8192);
}
Mem_Copy(&buffer->textBuffer[textOffset], str->buffer, str->length);
buffer->flagsBuffer[buffer->count] = str->length | StringsBuffer_PackOffset(textOffset);
buffer->count++;
buffer->totalLength += str->length;
}
void StringsBuffer_Remove(struct StringsBuffer* buffer, int index) {
cc_uint32 flags, offset, len;
cc_uint32 i, offsetAdj;
if (index < 0 || index >= buffer->count) Logger_Abort("Tried to remove String past StringsBuffer end");
flags = buffer->flagsBuffer[index];
offset = StringsBuffer_GetOffset(flags);
len = StringsBuffer_GetLength(flags);
/* Imagine buffer is this: AAXXYYZZ, and want to delete XX */
/* We iterate from first char of Y to last char of Z, */
/* shifting each character two to the left. */
for (i = offset + len; i < buffer->totalLength; i++) {
buffer->textBuffer[i - len] = buffer->textBuffer[i];
}
/* Adjust text offset of elements after this element */
/* Elements may not be in order so must account for that */
offsetAdj = StringsBuffer_PackOffset(len);
for (i = index; i < buffer->count - 1; i++) {
buffer->flagsBuffer[i] = buffer->flagsBuffer[i + 1];
if (buffer->flagsBuffer[i] >= flags) {
buffer->flagsBuffer[i] -= offsetAdj;
}
}
buffer->count--;
buffer->totalLength -= len;
}
static struct StringsBuffer* sort_buffer;
static void StringsBuffer_QuickSort(int left, int right) {
struct StringsBuffer* buffer = sort_buffer;
cc_uint32* keys = buffer->flagsBuffer; cc_uint32 key;
while (left < right) {
int i = left, j = right;
cc_string pivot = StringsBuffer_UNSAFE_Get(buffer, (i + j) >> 1);
cc_string strI, strJ;
/* partition the list */
while (i <= j) {
while ((strI = StringsBuffer_UNSAFE_Get(buffer, i), String_Compare(&pivot, &strI)) > 0) i++;
while ((strJ = StringsBuffer_UNSAFE_Get(buffer, j), String_Compare(&pivot, &strJ)) < 0) j--;
QuickSort_Swap_Maybe();
}
/* recurse into the smaller subset */
QuickSort_Recurse(StringsBuffer_QuickSort)
}
}
void StringsBuffer_Sort(struct StringsBuffer* buffer) {
sort_buffer = buffer;
StringsBuffer_QuickSort(0, buffer->count - 1);
}
/*########################################################################################################################*
*------------------------------------------------------Word wrapper-------------------------------------------------------*
*#########################################################################################################################*/
static cc_bool WordWrap_IsWrapper(char c) {
return c == '\0' || c == ' ' || c == '-' || c == '>' || c == '<' || c == '/' || c == '\\';
}
void WordWrap_Do(STRING_REF cc_string* text, cc_string* lines, int numLines, int lineLen) {
int i, lineStart, lineEnd;
for (i = 0; i < numLines; i++) { lines[i] = String_Empty; }
for (i = 0, lineStart = 0; i < numLines; i++) {
int nextLineStart = lineStart + lineLen;
/* No more text to wrap */
if (nextLineStart >= text->length) {
lines[i] = String_UNSAFE_SubstringAt(text, lineStart); return;
}
/* Find beginning of last word on current line */
for (lineEnd = nextLineStart; lineEnd >= lineStart; lineEnd--) {
if (WordWrap_IsWrapper(text->buffer[lineEnd])) break;
}
lineEnd++; /* move after wrapper char (i.e. beginning of last word) */
if (lineEnd <= lineStart || lineEnd >= nextLineStart) {
/* Three special cases handled by this: */
/* - Entire line is filled with a single word */
/* - Last character(s) on current line are wrapper characters */
/* - First character on next line is a wrapper character (last word ends at current line end) */
lines[i] = String_UNSAFE_Substring(text, lineStart, lineLen);
lineStart += lineLen;
} else {
/* Last word in current line does not end in current line (extends onto next line) */
/* Trim current line to end at beginning of last word */
/* Set next line to start at beginning of last word */
lines[i] = String_UNSAFE_Substring(text, lineStart, lineEnd - lineStart);
lineStart = lineEnd;
}
}
}
void WordWrap_GetCoords(int index, const cc_string* lines, int numLines, int* coordX, int* coordY) {
int y, offset = 0, lineLen;
if (index == -1) index = Int32_MaxValue;
*coordX = -1; *coordY = 0;
for (y = 0; y < numLines; y++) {
lineLen = lines[y].length;
if (!lineLen) break;
*coordY = y;
if (index < offset + lineLen) {
*coordX = index - offset; break;
}
offset += lineLen;
}
if (*coordX == -1) *coordX = lines[*coordY].length;
}
int WordWrap_GetBackLength(const cc_string* text, int index) {
int start = index;
if (index <= 0) return 0;
if (index >= text->length) Logger_Abort("WordWrap_GetBackLength - index past end of string");
/* Go backward to the end of the previous word */
while (index > 0 && text->buffer[index] == ' ') index--;
/* Go backward to the start of the current word */
while (index > 0 && text->buffer[index] != ' ') index--;
return start - index;
}
int WordWrap_GetForwardLength(const cc_string* text, int index) {
int start = index, length = text->length;
if (index == -1) return 0;
if (index >= text->length) Logger_Abort("WordWrap_GetForwardLength - index past end of string");
/* Go forward to the end of the word 'index' is currently in */
while (index < length && text->buffer[index] != ' ') index++;
/* Go forward to the start of the next word after 'index' */
while (index < length && text->buffer[index] == ' ') index++;
return index - start;
}
|