-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb.c
4330 lines (3959 loc) · 106 KB
/
b.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
/* b.c (base) -- esl */
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
#include <wchar.h>
#include <time.h>
#include <math.h>
#include "b.h"
#include "r.h"
/* globals */
static const char *g_progname = NULL; /* program name for messages */
static const char *g_usage = NULL; /* usage string for messages */
int g_wlevel = 0; /* warnings below this level are ignored */
int g_verbosity = 0; /* how verbose are we? */
int g_quietness = 0; /* how quiet are we? */
/* AT&T-like option parser */
int eoptind = 1; /* start */
int eopterr = 1; /* throw errors by default */
int eoptopt = 0;
char* eoptarg = NULL;
int eoptich = 0;
/* common utility functions */
void eprintf(const char *fmt, ...)
{
va_list args;
fflush(stdout);
if (progname() != NULL)
fprintf(stderr, "%s: ", progname());
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':')
fprintf(stderr, " %s", strerror(errno));
fprintf(stderr, "\naborting execution...\n");
exit(2); /* conventional value for failed execution */
}
void *emalloc(size_t n)
{
void *p = malloc(n);
if (p == NULL) eprintf("malloc() failed:");
return p;
}
void *ecalloc(size_t n, size_t s)
{
void *p = calloc(n, s);
if (p == NULL) eprintf("calloc() failed:");
return p;
}
void *erealloc(void *m, size_t n)
{
void *p = realloc(m, n);
if (p == NULL) eprintf("realloc() failed:");
return p;
}
char *estrdup(const char *s)
{
char *t = (char *)emalloc(strlen(s)+1);
strcpy(t, s);
return t;
}
char *estrndup(const char* s, size_t n)
{
char *t = (char *)emalloc(n+1);
strncpy(t, s, n); t[n] = '\0';
return t;
}
char *ememdup(const char* s, size_t n)
{
char *t = (char *)emalloc(n);
memcpy(t, s, n);
return t;
}
char *strtrc(char* str, int c, int toc)
{
char *s;
for (s = str; *s; ++s) if (*s == c) *s = toc;
return str;
}
char *strprf(const char *str, const char *prefix)
{
assert(str); assert(prefix);
while (*str && *str == *prefix) ++str, ++prefix;
if (*prefix) return NULL;
return (char *)str;
}
char *strsuf(const char *str, const char *suffix)
{
size_t l, sl;
assert(str); assert(suffix);
l = strlen(str), sl = strlen(suffix);
if (l >= sl) {
str += (l - sl);
if (strcmp(str, suffix) == 0) return (char *)str;
}
return NULL;
}
bool streql(const char *s1, const char *s2)
{
if (s1 == NULL && s2 == NULL) return true;
if (s1 == NULL || s2 == NULL) return false;
return strcmp(s1, s2) == 0;
}
bool strieql(const char *s1, const char *s2)
{
if (s1 == NULL && s2 == NULL) return true;
if (s1 == NULL || s2 == NULL) return false;
while (*s1 != 0 && *s2 != 0) {
int c1 = *s1++ & 0xff, c2 = *s2++ & 0xff;
if (c1 == c2) continue;
if (c1 < 0xff && c2 < 0xff && tolower(c1) == tolower(c2)) continue;
break;
}
return *s1 == 0 && *s2 == 0;
}
size_t strcnt(const char *str, int c)
{
int c1; size_t cnt = 0; assert(str);
while ((c1 = *str++)) if (c1 == c) ++cnt;
return cnt;
}
void memswap(void *mem1, void *mem2, size_t sz)
{
char c, *p1, *p2;
assert(mem1); assert(mem2);
p1 = (char*)mem1; p2 = (char*)mem2;
while (sz--) c = *p1, *p1++ = *p2, *p2++ = c;
}
/* encodes legal unicode as well as other code points up to 21 bits long (0 to U+001FFFFF) */
unsigned char *utf8(unsigned long c, unsigned char *s)
{ /* if c has more than 21 bits (> U+1FFFFF), *s is set to 0 and s does not advance; accepts any other c w/o error */
int t = (int)!!(c & ~0x7fL) + (int)!!(c & ~0x7ffL) + (int)!!(c & ~0xffffL) + (int)!!(c & ~0x1fffffL);
*s = (unsigned char)(((c >> t["\0\0\0\x12\0"]) & t["\0\0\0\x07\0"]) | t["\0\0\0\xf0\0"]);
s += (unsigned char)(t["\0\0\0\1\0"]);
*s = (unsigned char)(((c >> t["\0\0\x0c\x0c\0"]) & t["\0\0\x0f\x3f\0"]) | t["\0\0\xe0\x80\0"]);
s += (unsigned char)(t["\0\0\1\1\0"]);
*s = (unsigned char)(((c >> t["\0\x06\x06\x06\0"]) & t["\0\x1f\x3f\x3f\0"]) | t["\0\xc0\x80\x80\0"]);
s += (unsigned char)(t["\0\1\1\1\0"]);
*s = (unsigned char)(((c & t["\x7f\x3f\x3f\x3f\0"])) | t["\0\x80\x80\x80\0"]);
s += (unsigned char)(t["\1\1\1\1\0"]);
return s;
}
/* decodes legal utf-8 as well as any other encoded sequences for code points up to 21 bits (0 to U+001FFFFF) */
/* CAVEAT: in broken encoding can step over \0 byte if it is a (bad) payload byte and thus run past end of line!! */
unsigned long unutf8(unsigned char **ps)
{ /* NB: unutf8 never advances less than 1 and more than 4 bytes; gobbles up arbitrary bytes w/o error */
unsigned char *s = *ps; unsigned long c; unsigned t = *s >> 4;
c = (unsigned long)(*s & t["\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\0\0\0\0\x1f\x1f\x0f\x07"]) << t["\0\0\0\0\0\0\0\0\0\0\0\0\x06\x06\x0c\x12"];
s += t["\1\1\1\1\1\1\1\1\0\0\0\0\1\1\1\1"];
c |= (unsigned long)(*s & t["\0\0\0\0\0\0\0\0\0\0\0\0\x3f\x3f\x3f\x3f"]) << t["\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x06\x0c"];
s += t["\0\0\0\0\0\0\0\0\0\0\0\0\1\1\1\1"];
c |= (unsigned long)(*s & t["\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x3f\x3f"]) << t["\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x06"];
s += t["\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1\1"];
c |= (unsigned long)(*s & t["\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x3f"]);
s += t["\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1"];
*ps = s; return c;
}
unsigned long strtou8c(const char *s, char **ep)
{
unsigned long c = unutf8((unsigned char **)&s);
if (ep) *ep = (char *)s; return c;
}
/* size of buffer large enough to hold char escapes */
#define SBSIZE 32
/* convert a single C99-like char/escape sequence (-1 on error) */
unsigned long strtocc32(const char *s, char** ep, bool *rp)
{
char buf[SBSIZE+1];
int c;
assert(s);
if (rp) *rp = true;
if (*s) {
c = *s++;
if (c == '\\') {
switch (c = *s++) {
case 'a': c = '\a'; break;
case 'b': c = '\b'; break;
case 'f': c = '\f'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'v': c = '\v'; break;
case '\\': break;
case '\'': break;
case '\"': break;
case '\?': break;
case 'x': {
int i = 0; long l;
while (i < SBSIZE && (c = *s++) && isxdigit(c))
buf[i++] = c;
if (i == SBSIZE) goto err;
--s;
buf[i] = 0;
l = strtol(buf, NULL, 16);
c = (int)l & 0xff;
if ((long)c != l) goto err;
} break;
case 'u': {
int i = 0; long l;
c = *s++; if (isxdigit(c)) buf[i++] = c; else goto err;
c = *s++; if (isxdigit(c)) buf[i++] = c; else goto err;
c = *s++; if (isxdigit(c)) buf[i++] = c; else goto err;
c = *s++; if (isxdigit(c)) buf[i++] = c; else goto err;
buf[i] = 0;
l = strtol(buf, NULL, 16);
c = (int)l & 0xffff;
if ((long)c != l) goto err;
if (rp) *rp = false;
} break;
case 'U': {
int i = 0; long l;
c = *s++; if (isxdigit(c)) buf[i++] = c; else goto err;
c = *s++; if (isxdigit(c)) buf[i++] = c; else goto err;
c = *s++; if (isxdigit(c)) buf[i++] = c; else goto err;
c = *s++; if (isxdigit(c)) buf[i++] = c; else goto err;
c = *s++; if (isxdigit(c)) buf[i++] = c; else goto err;
c = *s++; if (isxdigit(c)) buf[i++] = c; else goto err;
c = *s++; if (isxdigit(c)) buf[i++] = c; else goto err;
c = *s++; if (isxdigit(c)) buf[i++] = c; else goto err;
buf[i] = 0;
l = strtol(buf, NULL, 16);
c = (int)l & 0x7fffffff;
if ((long)c != l) goto err;
if (rp) *rp = false;
} break;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7': {
int i = 0;
buf[i++] = c; c = *s++;
if (c >= '0' && c <= '7') {
buf[i++] = c; c = *s++;
if (c >= '0' && c <= '7')
buf[i++] = c;
else --s;
} else --s;
buf[i] = 0;
c = (int)strtol(buf, NULL, 8);
} break;
case 0:
goto err;
default:
/* warn? return as is */
break;
}
}
if (ep) *ep = (char*)s;
return (unsigned long)c;
err:;
}
if (ep) *ep = (char*)s;
errno = EINVAL;
return (unsigned long)(-1);
}
unsigned long strtou8cc32(const char *s, char **ep, bool *rp)
{
if (is8chead(*s)) { if (rp) *rp = false; return strtou8c(s, ep); }
return strtocc32(s, ep, rp);
}
bool fget8bom(FILE *fp)
{
int c = fgetc(fp);
if (c == EOF) return false;
/* not comitted yet - just looking ahead... */
if ((c & 0xFF) != 0xEF) { ungetc(c, fp); return false; }
/* now we are comitted to read all 3 bytes - partial reads can happen */
if ((c = fgetc(fp)) == EOF || (c & 0xFF) != 0xBB) return false;
if ((c = fgetc(fp)) == EOF || (c & 0xFF) != 0xBF) return false;
/* all three bytes are there */
return true;
}
int int_cmp(const void *pi1, const void *pi2)
{
assert(pi1); assert(pi2);
return *(int*)pi1 - *(int*)pi2;
}
/* floating-point reinterpret casts and exact hex i/o */
unsigned long long as_uint64(double f) /* NB: asuint64 is WCPL intrinsic */
{
union { unsigned long long u; double f; } uf;
uf.f = f;
return uf.u;
}
double as_double(unsigned long long u) /* NB: asdouble is WCPL intrinsic */
{
union { unsigned long long u; double f; } uf;
uf.u = u;
return uf.f;
}
unsigned as_uint32(float f) /* NB: asuint32 is WCPL intrinsic */
{
union { unsigned u; float f; } uf;
uf.f = f;
return uf.u;
}
float as_float(unsigned u) /* NB: asfloat is WCPL intrinsic */
{
union { unsigned u; float f; } uf;
uf.u = u;
return uf.f;
}
/* print uint64 representation of double exactly in WASM-compatible hex format */
char *udtohex(unsigned long long uval, char *buf) /* needs 32 chars (25 chars, rounded up) */
{
bool dneg = (bool)(uval >> 63ULL);
int exp2 = (int)((uval >> 52ULL) & 0x07FFULL);
unsigned long long mant = (uval & 0xFFFFFFFFFFFFFULL);
if (exp2 == 0x7FF) {
/* print as canonical NaN; todo: WASM allows :0xHHHHHHHHHH suffix! */
if (mant != 0ULL) strcpy(buf, dneg ? "-nan" : "+nan");
else strcpy(buf, dneg ? "-inf" : "+inf");
} else {
size_t i = 0, j; int val, vneg = false, dig, mdigs;
if (exp2 == 0) val = mant == 0 ? 0 : -1022; else val = exp2 - 1023;
if (val < 0) val = -val, vneg = true;
do { dig = val % 10; buf[i++] = '0' + dig; val /= 10; } while (val);
buf[i++] = vneg ? '-' : '+'; buf[i++] = 'p';
for (mdigs = 13; mdigs > 0; --mdigs, mant >>= 4ULL) {
dig = (int)(mant % 16); buf[i++] = dig < 10 ? '0' + dig : 'a' + dig - 10;
}
buf[i++] = '.'; buf[i++] = '0' + ((exp2 == 0) ? 0 : 1);
buf[i++] = 'x'; buf[i++] = '0';
if (dneg) buf[i++] = '-'; buf[i] = 0;
for (j = 0, i -= 1; j < i; ++j, --i) {
dig = buf[j]; buf[j] = buf[i]; buf[i] = dig;
}
}
return buf;
}
/* read back uint64 representation of double as printed via udtohex; -1 on error */
unsigned long long hextoud(const char *buf)
{
bool dneg = false;
if (*buf == '-') ++buf, dneg = true;
else if (*buf == '+') ++buf;
if (strcmp(buf, "inf") == 0) {
return dneg ? 0xFFF0000000000000ULL : 0x7FF0000000000000ULL;
} else if (strcmp(buf, "nan") == 0) {
/* read canonical NaN; todo: WASM allows :0xHHHHHHHHHH suffix! */
return dneg ? 0xFFF8000000000000ULL : 0x7FF8000000000000ULL;
} else {
int exp2 = 0; unsigned long long mant = 0ULL;
int ldig, val, vneg, dig, mdigs;
if (*buf++ != '0') goto err;
if (*buf++ != 'x') goto err;
if (*buf != '0' && *buf != '1') goto err;
ldig = *buf++ - '0';
if (*buf++ != '.') goto err;
for (mdigs = 13; mdigs > 0; --mdigs) {
if (!*buf || !isxdigit(*buf)) goto err;
val = *buf++;
dig = val <= '9' ? val - '0'
: val <= 'F' ? val - 'A' + 10
: val - 'a' + 10;
mant = (mant << 4ULL) | dig;
}
if (*buf++ != 'p') goto err;
if (*buf != '+' && *buf != '-') goto err;
vneg = *buf++ == '-';
for (val = 0; *buf; ++buf) {
if (!isdigit(*buf)) goto err;
val = val * 10 + *buf - '0';
}
if (vneg) val = -val;
exp2 = ldig == 1 ? val + 1023 : 0;
if (0 <= exp2 && exp2 <= 2046)
return (unsigned long long)dneg << 63ULL | (unsigned long long)exp2 << 52ULL | mant;
err:;
}
return 0xFFFFFFFFFFFFFFFFULL;
}
/* print uint32 representation of double exactly in WASM-compatible hex format */
char *uftohex(unsigned uval, char *buf) /* needs 32 chars (17 chars, rounded up) */
{
bool dneg = (bool)(uval >> 31U);
int exp2 = (int)((uval >> 23U) & 0x0FFU);
unsigned mant = (uval & 0x7FFFFFU);
if (exp2 == 0xFF) {
/* print as canonical NaN; todo: WASM allows :0xHHHHHH suffix! */
if (mant != 0UL) strcpy(buf, dneg ? "-nan" : "+nan");
else strcpy(buf, dneg ? "-inf" : "+inf");
} else {
size_t i = 0, j; int val, vneg = false, dig, mdigs;
if (exp2 == 0) val = mant == 0 ? 0 : -126; else val = exp2 - 127;
if (val < 0) val = -val, vneg = true;
do { dig = val % 10; buf[i++] = '0' + dig; val /= 10; } while (val);
buf[i++] = vneg ? '-' : '+'; buf[i++] = 'p';
for (mdigs = 6, mant <<= 1; mdigs > 0; --mdigs, mant >>= 4) {
dig = (int)(mant % 16); buf[i++] = dig < 10 ? '0' + dig : 'a' + dig - 10;
}
buf[i++] = '.'; buf[i++] = '0' + ((exp2 == 0) ? 0 : 1);
buf[i++] = 'x'; buf[i++] = '0';
if (dneg) buf[i++] = '-'; buf[i] = 0;
for (j = 0, i -= 1; j < i; ++j, --i) {
dig = buf[j]; buf[j] = buf[i]; buf[i] = dig;
}
}
return buf;
}
/* read back uint32 representation of float as printed via uftohex; -1 on error */
unsigned hextouf(const char *buf)
{
bool dneg = false;
if (*buf == '-') ++buf, dneg = true;
else if (*buf == '+') ++buf;
if (strcmp(buf, "inf") == 0) {
/* read canonical NaN; todo: WASM allows :0xHHHHHH suffix! */
return dneg ? 0xFF800000U : 0x7F800000U;
} else if (strcmp(buf, "nan") == 0) {
return dneg ? 0xFFC00000U : 0x7FC00000U;
} else {
int exp2 = 0; unsigned mant = 0UL;
int ldig, val, vneg, dig, mdigs;
if (*buf++ != '0') goto err;
if (*buf++ != 'x') goto err;
if (*buf != '0' && *buf != '1') goto err;
ldig = *buf++ - '0';
if (*buf++ != '.') goto err;
for (mdigs = 6; mdigs > 0; --mdigs) {
if (!*buf || !isxdigit(*buf)) goto err;
val = *buf++;
dig = val <= '9' ? val - '0'
: val <= 'F' ? val - 'A' + 10
: val - 'a' + 10;
mant = (mant << 4) | dig;
}
if (*buf++ != 'p') goto err;
if (*buf != '+' && *buf != '-') goto err;
vneg = *buf++ == '-';
for (val = 0; *buf; ++buf) {
if (!isdigit(*buf)) goto err;
val = val * 10 + *buf - '0';
}
if (vneg) val = -val;
exp2 = ldig == 1 ? val + 127 : 0;
if (0 <= exp2 && exp2 <= 254)
return (unsigned)dneg << 31U | (unsigned)exp2 << 23U | mant >> 1;
err:;
}
return 0xFFFFFFFFU;
}
/* dynamic (heap-allocated) 0-terminated strings */
void dsicpy(dstr_t* mem, const dstr_t* pds)
{
dstr_t ds = NULL;
assert(mem); assert(pds);
if (*pds) strcpy((ds = emalloc(strlen(*pds)+1)), *pds);
*mem = ds;
}
void dsfini(dstr_t* pds)
{
if (pds) free(*pds);
}
void dscpy(dstr_t* pds, const dstr_t* pdss)
{
dstr_t ds = NULL;
assert(pds); assert(pdss);
if (*pdss) strcpy((ds = emalloc(strlen(*pdss)+1)), *pdss);
free(*pds); *pds = ds;
}
void dssets(dstr_t* pds, const char *s)
{
dstr_t ds = NULL;
assert(pds);
if (s) strcpy((ds = emalloc(strlen(s)+1)), s);
free(*pds); *pds = ds;
}
int dstr_cmp(const void *pds1, const void *pds2)
{
assert(pds1); assert(pds2);
return strcmp(*(dstr_t*)pds1, *(dstr_t*)pds2);
}
/* simple dynamic memory buffers */
buf_t* bufinit(buf_t* pb, size_t esz)
{
assert(pb); assert(esz);
pb->esz = esz;
pb->end = 0;
pb->buf = NULL;
pb->fill = 0;
return pb;
}
buf_t* buficpy(buf_t* mem, const buf_t* pb) /* over non-inited */
{
assert(mem); assert(pb);
assert(mem != pb);
mem->esz = pb->esz;
mem->end = pb->end;
mem->fill = pb->fill;
mem->buf = NULL;
if (pb->end > 0) {
mem->buf = ecalloc(pb->end, pb->esz);
memcpy(mem->buf, pb->buf, pb->esz*pb->fill);
}
return mem;
}
void *buffini(buf_t* pb)
{
assert(pb);
if (pb->buf) free(pb->buf);
pb->buf = NULL;
return pb;
}
buf_t mkbuf(size_t esz)
{
buf_t b;
assert(esz);
bufinit(&b, esz);
return b;
}
buf_t* newbuf(size_t esz)
{
buf_t* pb = (buf_t*)emalloc(sizeof(buf_t));
bufinit(pb, esz);
return pb;
}
void freebuf(buf_t* pb)
{
if (pb) {
buffini(pb);
free(pb);
}
}
size_t buflen(const buf_t* pb)
{
assert(pb);
return pb->fill;
}
void* bufdata(buf_t* pb)
{
assert(pb);
return pb->buf;
}
bool bufempty(const buf_t* pb)
{
assert(pb);
return pb->fill == 0;
}
void bufclear(buf_t* pb)
{
assert(pb);
pb->fill = 0;
}
void bufgrow(buf_t* pb, size_t n)
{
assert(pb);
if (n > 0) {
size_t oldsz = pb->end;
size_t newsz = oldsz*2;
if (oldsz + n > newsz) newsz += n;
pb->buf = erealloc(pb->buf, newsz*pb->esz);
pb->end = newsz;
}
}
void bufresize(buf_t* pb, size_t n)
{
assert(pb);
if (n > pb->end) bufgrow(pb, n - pb->end);
if (n > pb->fill) {
assert(pb->buf != NULL);
memset((char*)pb->buf + pb->fill*pb->esz, 0, (n - pb->fill)*pb->esz);
}
pb->fill = n;
}
void* bufref(buf_t* pb, size_t i)
{
assert(pb);
assert(i < pb->fill);
assert(pb->buf != NULL);
return (char*)pb->buf + i*pb->esz;
}
void bufrem(buf_t* pb, size_t i)
{
size_t esz;
assert(pb); assert(i < pb->fill);
assert(pb->buf != NULL);
esz = pb->esz;
if (i < pb->fill - 1)
memmove((char*)pb->buf + i*esz,
(char*)pb->buf + (i+1)*esz,
(pb->fill - 1 - i)*esz);
--pb->fill;
}
void bufnrem(buf_t* pb, size_t i, size_t cnt)
{
if (cnt > 0) {
size_t esz;
assert(pb); assert(i + cnt <= pb->fill);
assert(pb->buf != NULL);
esz = pb->esz;
if (i < pb->fill - cnt)
memmove((char*)pb->buf + i*esz,
(char*)pb->buf + (i+cnt)*esz,
(pb->fill - cnt - i)*esz);
pb->fill -= cnt;
}
}
void* bufins(buf_t* pb, size_t i)
{
size_t esz; char *pnewe;
assert(pb); assert(i <= pb->fill);
esz = pb->esz;
if (pb->fill == pb->end) bufgrow(pb, 1);
assert(pb->buf != NULL);
pnewe = (char*)pb->buf + i*esz;
if (i < pb->fill)
memmove(pnewe + esz, pnewe,
(pb->fill - i)*esz);
memset(pnewe, 0, esz);
++pb->fill;
return pnewe;
}
void *bufbk(buf_t* pb)
{
char* pbk;
assert(pb); assert(pb->fill > 0);
assert(pb->buf != NULL);
pbk = (char*)pb->buf + (pb->fill-1)*pb->esz;
return pbk;
}
void *bufnewbk(buf_t* pb)
{
char* pbk; size_t esz;
assert(pb);
if (pb->fill == pb->end) bufgrow(pb, 1);
assert(pb->buf != NULL);
esz = pb->esz;
pbk = (char*)pb->buf + pb->fill*esz;
memset(pbk, 0, esz);
++pb->fill;
return pbk;
}
void *bufpopbk(buf_t* pb)
{
char* pbk;
assert(pb); assert(pb->fill > 0);
assert(pb->buf != NULL);
pbk = (char*)pb->buf + (pb->fill-1)*pb->esz;
pb->fill -= 1;
return pbk; /* outside but untouched */
}
void *bufnewfr(buf_t* pb)
{
return bufins(pb, 0);
}
void bufpopfr(buf_t* pb)
{
bufrem(pb, 0);
}
void *bufalloc(buf_t* pb, size_t n)
{
size_t esz; char* pbk;
assert(pb);
if (pb->fill + n > pb->end) bufgrow(pb, pb->fill + n - pb->end);
else if (!pb->buf && n == 0) bufgrow(pb, 1); /* ensure buf != NULL */
esz = pb->esz;
assert(pb->buf != NULL);
pbk = (char*)pb->buf + pb->fill*esz;
memset(pbk, 0, esz*n);
pb->fill += n;
return pbk;
}
void bufrev(buf_t* pb)
{
char *pdata;
size_t i, j, len, esz;
assert(pb);
len = pb->fill;
if (len < 2) return;
esz = pb->esz;
i = 0; j = len-1;
pdata = (char*)pb->buf;
assert(pb->buf != NULL);
while (i < j) {
memswap(pdata+i*esz, pdata+j*esz, esz);
++i, --j;
}
}
void bufcpy(buf_t* pb, const buf_t* pab)
{
size_t an = buflen(pab);
assert(pb->esz == pab->esz);
bufclear(pb);
if (pab->buf) memcpy(bufalloc(pb, an), pab->buf, an*pab->esz);
}
void bufcat(buf_t* pb, const buf_t* pab)
{
size_t an = buflen(pab);
assert(pb->esz == pab->esz);
if (pab->buf) memcpy(bufalloc(pb, an), pab->buf, an*pab->esz);
}
/* unstable sort */
void bufqsort(buf_t* pb, int (*cmp)(const void *, const void *))
{
assert(pb); assert(cmp);
if (pb->buf) qsort(pb->buf, pb->fill, pb->esz, cmp);
}
/* removes adjacent */
void bufremdups(buf_t* pb, int (*cmp)(const void *, const void *), void (*fini)(void *))
{
char *pdata; size_t i, j, len, esz;
assert(pb); assert(cmp);
len = pb->fill;
esz = pb->esz;
if (len < 2) return;
i = 0; j = 1;
pdata = (char*)pb->buf;
assert(pb->buf != NULL);
while (j < len) {
/* invariant: elements [0..i] are unique, [i+1,j[ is a gap */
assert(i < j);
if (0 == (*cmp)(pdata+i*esz, pdata+j*esz)) {
/* duplicate: drop it */
if (fini) (*fini)(pdata+j*esz);
} else {
/* unique: move it */
++i; if (i < j) memcpy(pdata+i*esz, pdata+j*esz, esz);
}
++j;
}
pb->fill = i+1;
}
/* linear search */
void* bufsearch(const buf_t* pb, const void *pe, int (*cmp)(const void *, const void *))
{
size_t esz; char *pdata, *pend;
assert(pb); assert(pe); assert(cmp);
if (pb->fill == 0) return NULL;
esz = pb->esz;
pdata = (char*)pb->buf;
assert(pb->buf != NULL);
pend = pdata + esz*pb->fill;
while (pdata < pend) {
if (0 == (*cmp)(pdata, pe))
return pdata;
pdata += esz;
}
return NULL;
}
/* binary search */
void* bufbsearch(const buf_t* pb, const void *pe, int (*cmp)(const void *, const void *))
{
assert(pb); assert(pe); assert(cmp);
return pb->buf ? bsearch(pe, pb->buf, pb->fill, pb->esz, cmp) : NULL;
}
size_t bufoff(const buf_t* pb, const void *pe)
{
char *p0, *p; size_t off;
assert(pb); assert(pe);
assert(pb->buf != NULL);
p0 = (char*)pb->buf, p = (char*)pe;
assert(p >= p0);
off = (p - p0) / pb->esz;
assert(off <= pb->fill); /* internal pointers and pointer past last elt are legal */
return off;
}
void bufswap(buf_t* pb1, buf_t* pb2)
{
assert(pb1); assert(pb2);
assert(pb1->esz == pb2->esz);
memswap(pb1, pb2, sizeof(buf_t));
}
/* unicode charsets */
bool ucsin(unsigned c, const ucset_t *ps)
{
unsigned e = c;
const unsigned *pbase = ps->buf; /* int partition base */
size_t psize = ps->fill; /* partition size in int pairs */
while (psize) { /* still have int pairs in partition */
/* pick central pair, rounding offset to a pair */
const unsigned *ci = pbase + (psize & ~1);
if (e < ci[0]) { /* FIRST */
/* search from pbase to pair before ci */
psize >>= 1;
} else if (e > ci[1]) { /* LAST */
/* search from pair after ci */
pbase = ci + 2;
psize = (psize-1) >> 1;
} else {
return true;
}
}
return false;
}
void ucspushi(ucset_t *ps, unsigned fc, unsigned lc)
{
unsigned *nl = bufnewbk(ps);
/* ps spacing/ordering preconds must hold! */
assert(fc <= lc);
assert(nl == bufdata(ps) || fc > nl[-1]);
nl[0] = fc, nl[1] = lc;
}
/* dstr_t buffers */
void dsbicpy(dsbuf_t* mem, const dsbuf_t* pb)
{
dstr_t *pdsm, *pds; size_t i;
assert(mem); assert(pb); assert(pb->esz == sizeof(dstr_t));
buficpy(mem, pb);
pdsm = (dstr_t*)(mem->buf), pds = (dstr_t*)(pb->buf);
for (i = 0; i < pb->fill; ++i) dsicpy(pdsm+i, pds+i);
}
void dsbclear(dsbuf_t* pb)
{
dstr_t *pds; size_t i;
assert(pb); assert(pb->esz == sizeof(dstr_t));
pds = (dstr_t*)(pb->buf);
for (i = 0; i < pb->fill; ++i) dsfini(pds+i);
bufclear(pb);
}
void dsbfini(dsbuf_t* pb)
{
dsbclear(pb);
buffini(pb);
}
/* char buffers */
void (cbputc)(int c, cbuf_t* pcb)
{
*(char*)bufnewbk(pcb) = (char)c;
}
void cbput(const char *s, size_t n, cbuf_t* pb)
{
size_t l = buflen(pb);
bufresize(pb, l+n);
memcpy(l+(char*)pb->buf, s, n);
}
void cbputs(const char *s, cbuf_t* pb)
{
size_t n = strlen(s);
cbput(s, n, pb);
}
size_t cbwrite(const char *s, size_t sz, size_t c, cbuf_t* pb)
{
size_t n = sz * c;
if (pb->fill + n > pb->end) bufgrow(pb, n);
memcpy((char*)pb->buf+pb->fill, s, n);
pb->fill += n;
return c;
}
void cbputlc(unsigned long uc, cbuf_t* pb)
{
if (uc < 128) cbputc((unsigned char)uc, pb);
else {
char buf[5], *pc = (char *)utf8(uc, (unsigned char *)&buf[0]);
cbput(&buf[0], pc-&buf[0], pb);
}
}
void cbputwc(wchar_t wc, cbuf_t* pb)
{
assert(WCHAR_MIN <= wc && wc <= WCHAR_MAX);
if (wc < 128) cbputc((unsigned char)wc, pb);
else {
char buf[5], *pc = (char *)utf8(wc, (unsigned char *)&buf[0]);
cbput(&buf[0], pc-&buf[0], pb);
}
}
void cbputd(int val, cbuf_t* pb)
{
char buf[39+1]; /* enough up to 128 bits (w/sign) */
char *e = &buf[40], *p = e;
if (val) {
unsigned m;
if (val == INT_MIN) m = INT_MAX + (unsigned)1;
else if (val < 0) m = -val;
else m = val;
do *--p = (int)(m%10) + '0';
while ((m /= 10) > 0);
if (val < 0) *--p = '-';
} else *--p = '0';
cbput(p, e-p, pb);
}
void cbputld(long val, cbuf_t* pb)
{
char buf[39+1]; /* enough up to 128 bits (w/sign) */
char *e = &buf[40], *p = e;
if (val) {
unsigned long m;
if (val == LONG_MIN) m = LONG_MAX + (unsigned long)1;
else if (val < 0) m = -val;
else m = val;
do *--p = (int)(m%10) + '0';
while ((m /= 10) > 0);
if (val < 0) *--p = '-';
} else *--p = '0';
cbput(p, e-p, pb);
}
void cbputt(ptrdiff_t val, cbuf_t* pb)
{
char buf[39+1]; /* enough up to 128 bits (w/sign) */
char *e = &buf[40], *p = e;
if (val) {
size_t m; assert(sizeof(ptrdiff_t) == sizeof(size_t));
if (val < 0 && val-1 > 0) m = (val-1) + (size_t)1;
else if (val < 0) m = -val;
else m = val;