forked from rexim/aids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aids.hpp
1316 lines (1123 loc) · 34.9 KB
/
aids.hpp
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
// Copyright 2020 Alexey Kutepov <reximkut@gmail.com>
//
// 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.
//
// ============================================================
//
// aids — 1.3.0 — std replacement for C++. Designed to aid developers
// to a better programming experience.
//
// https://github.com/rexim/aids
//
// ============================================================
//
// ChangeLog (https://semver.org/ is implied)
//
// 1.3.0 fix memory leak in read_file_as_string_view()
// add operator[] for Dynamic_Array
// add struct Newline for println system
// 1.2.0 add String_View::as_cstr(Ator ator)
// 1.1.0 add constexpr Maybe<T> some(T x)
// 1.0.0 remove Stretchy_Buffer{}
// remove Args::pop()
// 0.40.0 Fix MSVC warnings
// 0.39.0 Fix common GCC warnings
// 0.38.0 struct Mtor{}
// struct Fixed_Region{}
// Make allocator for read_file_as_string_view customizable
// 0.37.0 NEVER HAPPENED
// 0.36.0 void destroy(String_View sv)
// 0.35.1 Fix compilation when using todo() and unreachable()
// 0.35.0 [[noreturn]] void unreachable(Args... args)
// [[noreturn]] void todo(Args... args)
// 0.34.1 Fix -Wtype-limits warning in utf8_get_code()
// 0.34.0 Hash_Map::contains(Key key)
// 0.33.0 Maybe::value_or(T t)
// 0.32.0 Hash_Map::operator[](Key key)
// 0.31.0 String_View::has_suffix(String_View suffix)
// 0.30.0 String_View String_View::chop_while(Predicate_Char predicate)
// 0.29.0 void destroy(Dynamic_Array<T> dynamic_array)
// 0.28.0 struct Hash_Map
// 0.27.0 NEVER HAPPENED
// 0.26.0 panic() is marked with [[noreturn]] attribute
// code_to_utf8() implementation is refactored in a backward compatible way
// 0.25.0 void print1(FILE *stream, Hex<char> hex)
// void print1(FILE *stream, HEX<char> hex)
// struct Hex_Bytes
// void print1(FILE *stream, Hex_Bytes hex_bytes)
// 0.24.0 String_View Utf8_Char::view()
// struct Hex
// void print1(FILE *stream, Hex<uint32_t> hex)
// struct HEX
// void print1(FILE *stream, HEX<uint32_t> hex)
// 0.23.0 code_to_utf8()
// struct Utf8_Char
// 0.22.0 panic()
// 0.21.0 void sprint1(String_Buffer *buffer, unsigned int x)
// 0.20.0 Escape
// 0.19.0 unwrap_or_panic()
// 0.18.0 Rename Args::pop() -> Args::shift()
// Add more details to Stretchy_Buffer deprecation message
// 0.17.0 Dynamic_Array::concat()
// Dynamic_Array::expand_capacity()
// 0.16.0 Dynamic_Array
// deprecate Stretchy_Buffer
// 0.15.0 Make min() and max() variadic
// 0.14.0 size_t String_View::count_chars(char x) const
// 0.13.3 Fix control flow in utf8_get_code
// 0.13.2 Fix magic constant types in utf8_get_code
// 0.13.1 Remove macros from utf8_get_code implementation
// 0.13.0 void print1(FILE *stream, unsigned int x)
// Maybe<uint32_t> utf8_get_code(String_View view, size_t *size)
// 0.12.1 Fix print1 and sprint1 bug for unsigned long long
// 0.12.0 void print1(FILE *stream, String_Buffer buffer)
// void sprint1(String_Buffer *buffer, String_Buffer another_buffer)
// String_View String_Buffer::view() const
// 0.11.0 Caps
// 0.10.0 sprint1(String_Buffer *buffer, String_View view)
// 0.9.0 String_Buffer
// sprintln
// 0.8.0 Args
// 0.7.0 String_View::operator<()
// print1(FILE*, bool)
// 0.6.0 swap
// 0.5.0 Equality operations for Maybe<T>
// 0.4.0 mod
// 0.3.0 Stretchy_Buffer
// 0.2.0 unwrap_into
// print1 for long int
// 0.1.0 print1 for long unsigned int
// print1 for int
// Pad
// 0.0.3 bugfix for print1 of Maybe<T>
// 0.0.2 fix sign-unsigned integer comparison in aids::read_file_as_string_view
// 0.0.1 min, max, clamp,
// defer,
// Maybe<T>,
// String_View,
// print, println
//
// ============================================================
//
// Contributors:
// Alexey Kutepov (github:rexim)
// Aodhnait Étaín (github:aodhneine)
// Jarosław Wiosna (github:JaroslawWiosna)
// Danil Kolumbet (github:kolumb)
#ifndef AIDS_HPP_
#define AIDS_HPP_
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
namespace aids
{
////////////////////////////////////////////////////////////
// ALLOCATORS
////////////////////////////////////////////////////////////
struct Mtor
{
template <typename T>
T *alloc(size_t count, T def = {})
{
T *result = static_cast<T*>(malloc(sizeof(T) * count));
for (size_t i = 0; i < count; ++i) {
result[i] = def;
}
return result;
}
template <typename T>
void dealloc(T *ptr, size_t)
{
free(ptr);
}
template <typename T>
void dealloc(const T *ptr, size_t)
{
free(const_cast<T*>(ptr));
}
};
Mtor mtor;
template <size_t Capacity>
struct Fixed_Region
{
size_t size;
char buffer[Capacity];
template <typename T>
T *alloc(size_t count, T def = T())
{
if (count * sizeof(T) > Capacity) {
return nullptr;
}
T *result = static_cast<T*>(buffer + size);
for (size_t i = 0; i < count; ++i) {
result[i] = def;
}
size += count * sizeof(T);
return result;
}
template <typename T>
void dealloc(T*, size_t)
{
}
template <typename T>
void dealloc(const T*, size_t)
{
}
void clean()
{
size = 0;
}
};
////////////////////////////////////////////////////////////
// ALGORITHM
////////////////////////////////////////////////////////////
template <typename T>
T min(T x)
{
return x;
}
template <typename T, typename... Rest>
T min(T x, Rest... rest)
{
auto y = min(rest...);
return x < y ? x : y;
}
template <typename T>
T max(T x)
{
return x;
}
template <typename T, typename... Rest>
T max(T x, Rest... rest)
{
auto y = max(rest...);
return x < y ? y : x;
}
template <typename T>
T clamp(T x, T low, T high)
{
return min(max(low, x), high);
}
template <typename T>
T mod(T a, T b)
{
return (a % b + b) % b;
}
template <typename T>
void swap(T *a, T *b)
{
T t = *a;
*a = *b;
*b = t;
}
////////////////////////////////////////////////////////////
// DEFER
////////////////////////////////////////////////////////////
// https://www.reddit.com/r/ProgrammerTIL/comments/58c6dx/til_how_to_defer_in_c/
template <typename F>
struct saucy_defer {
F f;
saucy_defer(F f) : f(f) {}
~saucy_defer() { f(); }
};
template <typename F>
saucy_defer<F> defer_func(F f)
{
return saucy_defer<F>(f);
}
#define DEFER_1(x, y) x##y
#define DEFER_2(x, y) DEFER_1(x, y)
#define DEFER_3(x) DEFER_2(x, __COUNTER__)
#define defer(code) auto DEFER_3(_defer_) = ::aids::defer_func([&](){code;})
////////////////////////////////////////////////////////////
// MAYBE
////////////////////////////////////////////////////////////
template <typename T>
struct Maybe
{
bool has_value;
T unwrap;
bool operator!=(const Maybe<T> &that) const
{
return !(*this == that);
}
bool operator==(const Maybe<T> &that) const
{
if (this->has_value && that.has_value) {
return this->unwrap == that.unwrap;
}
return !this->has_value && !that.has_value;
}
T value_or(T t) const {
return (has_value ? unwrap : t);
}
};
template <typename T>
constexpr Maybe<T> some(T x)
{
return {true, x};
}
#define unwrap_into(lvalue, maybe) \
do { \
auto maybe_var = (maybe); \
if (!maybe_var.has_value) return {}; \
(lvalue) = maybe_var.unwrap; \
} while (0)
////////////////////////////////////////////////////////////
// STRING_VIEW
////////////////////////////////////////////////////////////
struct String_View
{
size_t count;
const char *data;
[[nodiscard]]
String_View trim_begin(void) const
{
String_View view = *this;
while (view.count != 0 && isspace(*view.data)) {
view.data += 1;
view.count -= 1;
}
return view;
}
[[nodiscard]]
String_View trim_end(void) const
{
String_View view = *this;
while (view.count != 0 && isspace(*(view.data + view.count - 1))) {
view.count -= 1;
}
return view;
}
[[nodiscard]]
String_View trim(void) const
{
return trim_begin().trim_end();
}
void chop_back(size_t n)
{
count -= n < count ? n : count;
}
void chop(size_t n)
{
if (n > count) {
data += count;
count = 0;
} else {
data += n;
count -= n;
}
}
void grow(size_t n)
{
count += n;
}
using Predicate_Char = bool (*)(char);
String_View chop_while(Predicate_Char predicate)
{
size_t size = 0;
while (size < count && predicate(data[size])) {
size += 1;
}
auto result = subview(0, size);
chop(size);
return result;
}
String_View chop_by_delim(char delim)
{
assert(data);
size_t i = 0;
while (i < count && data[i] != delim) i++;
String_View result = {i, data};
chop(i + 1);
return result;
}
String_View chop_word(void)
{
*this = trim_begin();
size_t i = 0;
while (i < count && !isspace(data[i])) i++;
String_View result = { i, data };
count -= i;
data += i;
return result;
}
template <typename Integer>
Maybe<Integer> from_hex() const
{
Integer result = {};
for (size_t i = 0; i < count; ++i) {
Integer x = data[i];
if ('0' <= x && x <= '9') {
x = (Integer) (x - '0');
} else if ('a' <= x && x <= 'f') {
x = (Integer) (x - 'a' + 10);
} else if ('A' <= x && x <= 'F') {
x = (Integer) (x - 'A' + 10);
} else {
return {};
}
result = result * (Integer) 0x10 + x;
}
return some(result);
}
template <typename Integer>
Maybe<Integer> as_integer() const
{
Integer sign = 1;
Integer number = {};
String_View view = *this;
if (view.count == 0) {
return {};
}
if (*view.data == '-') {
sign = -1;
view.chop(1);
}
while (view.count) {
if (!isdigit(*view.data)) {
return {};
}
number = number * 10 + (*view.data - '0');
view.chop(1);
}
return { true, number * sign };
}
Maybe<float> as_float() const
{
char buffer[300] = {};
memcpy(buffer, data, min(sizeof(buffer) - 1, count));
char *endptr = NULL;
float result = strtof(buffer, &endptr);
if (buffer > endptr || (size_t) (endptr - buffer) != count) {
return {};
}
return some(result);
}
String_View subview(size_t start, size_t count) const
{
if (start + count <= this->count) {
return {count, data + start};
}
return {};
}
bool operator<(String_View b) const
{
auto a = *this;
while (a.count > 0 && b.count > 0) {
if (*a.data != *b.data) {
return *a.data < *b.data;
}
a.chop(1);
b.chop(1);
}
return a.count < b.count;
}
bool operator==(String_View view) const
{
if (this->count != view.count) return false;
return memcmp(this->data, view.data, this->count) == 0;
}
bool operator!=(String_View view) const
{
return !(*this == view);
}
bool has_prefix(String_View prefix) const
{
return prefix.count <= this->count
&& this->subview(0, prefix.count) == prefix;
}
bool has_suffix(String_View suffix) const
{
return suffix.count <= this->count
&& this->subview(this->count - suffix.count, suffix.count) == suffix;
}
size_t count_chars(char x) const
{
size_t result = 0;
for (size_t i = 0; i < count; ++i) {
if (data[i] == x) {
result += 1;
}
}
return result;
}
template <typename Ator = Mtor>
char *as_cstr(Ator *ator = &mtor)
{
char *result = ator->template alloc<char>(count + 1);
if (result != nullptr) {
memcpy(result, data, count);
result[count] = '\0';
}
return result;
}
};
String_View operator ""_sv(const char *data, size_t count)
{
return {count, data};
}
String_View cstr_as_string_view(const char *cstr)
{
return {strlen(cstr), cstr};
}
void print1(FILE *stream, String_View view)
{
fwrite(view.data, 1, view.count, stream);
}
template <typename Ator = Mtor>
Maybe<String_View> read_file_as_string_view(const char *filename,
Ator *ator = &mtor)
{
FILE *f = fopen(filename, "rb");
if (!f) return {};
defer(fclose(f));
int err = fseek(f, 0, SEEK_END);
if (err < 0) return {};
long size = ftell(f);
if (size < 0) return {};
err = fseek(f, 0, SEEK_SET);
if (err < 0) return {};
auto data = ator->template alloc<char>(size);
if (!data) return {};
size_t read_size = fread(data, 1, size, f);
if (read_size != (size_t) size && ferror(f)) {
ator->template dealloc<char>(data, size);
return {};
}
return some(String_View {static_cast<size_t>(size), static_cast<const char*>(data)});
}
void destroy(String_View sv)
{
mtor.dealloc(sv.data, sv.count);
}
////////////////////////////////////////////////////////////
// DYNAMIC ARRAY
////////////////////////////////////////////////////////////
template <typename... Args>
[[noreturn]] void panic(Args... args);
template <typename T, typename Ator = Mtor>
struct Dynamic_Array
{
size_t capacity;
size_t size;
T *data;
void expand_capacity()
{
size_t new_capacity = data ? 2 * capacity : 256;
T *new_data = mtor.alloc<T>(new_capacity);
memcpy(new_data, data, capacity);
data = new_data;
capacity = new_capacity;
}
void push(T item)
{
while (size + 1 > capacity) {
expand_capacity();
}
memcpy(data + size, &item, sizeof(T));
size += 1;
}
void concat(const T *items, size_t items_count)
{
while (size + 1 > capacity) {
expand_capacity();
}
memcpy(data + size, items, sizeof(T) * items_count);
size += items_count;
}
bool contains(T item)
{
for (size_t i = 0; i < size; ++i) {
if (item == data[i]) {
return true;
}
}
return false;
}
T &operator[](size_t index)
{
#ifndef AIDS_DISABLE_RANGE_CHECKS
if (index >= size) {
panic("Dynamic_Array: index out-of-bounds");
}
#endif // AIDS_DISABLE_RANGE_CHECKS
return data[index];
}
const T &operator[](size_t index) const
{
#ifndef AIDS_DISABLE_RANGE_CHECKS
if (index >= size) {
panic("Dynamic_Array: index out-of-bounds");
}
#endif // AIDS_DISABLE_RANGE_CHECKS
return data[index];
}
};
template <typename T>
void destroy(Dynamic_Array<T> dynamic_array)
{
if (dynamic_array.data) {
mtor.dealloc(dynamic_array.data, dynamic_array.capacity);
}
}
////////////////////////////////////////////////////////////
// ARGS
////////////////////////////////////////////////////////////
struct Args
{
int argc;
char **argv;
char *shift()
{
char *result = *argv;
argv += 1;
argc -= 1;
return result;
}
bool empty()
{
return argc == 0;
}
};
////////////////////////////////////////////////////////////
// SPRINT
////////////////////////////////////////////////////////////
struct String_Buffer
{
size_t capacity;
char *data;
size_t size;
String_View view() const
{
return {size, data};
}
};
void sprint1(String_Buffer *buffer, const char *cstr)
{
int n = snprintf(
buffer->data + buffer->size,
buffer->capacity - buffer->size,
"%s", cstr);
buffer->size = min(buffer->size + n, buffer->capacity - 1);
}
void sprint1(String_Buffer *buffer, String_View view)
{
int n = snprintf(
buffer->data + buffer->size,
buffer->capacity - buffer->size,
"%.*s", (int) view.count, view.data);
buffer->size = min(buffer->size + n, buffer->capacity - 1);
}
void sprint1(String_Buffer *buffer, char c)
{
int n = snprintf(
buffer->data + buffer->size,
buffer->capacity - buffer->size,
"%c", c);
buffer->size = min(buffer->size + n, buffer->capacity - 1);
}
void sprint1(String_Buffer *buffer, float f)
{
int n = snprintf(
buffer->data + buffer->size,
buffer->capacity - buffer->size,
"%f", f);
buffer->size = min(buffer->size + n, buffer->capacity - 1);
}
void sprint1(String_Buffer *buffer, unsigned long long x)
{
int n = snprintf(
buffer->data + buffer->size,
buffer->capacity - buffer->size,
"%llu", x);
buffer->size = min(buffer->size + n, buffer->capacity - 1);
}
void sprint1(String_Buffer *buffer, unsigned int x)
{
int n = snprintf(
buffer->data + buffer->size,
buffer->capacity - buffer->size,
"%u", x);
buffer->size = min(buffer->size + n, buffer->capacity - 1);
}
void sprint1(String_Buffer *buffer, long unsigned int x)
{
int n = snprintf(
buffer->data + buffer->size,
buffer->capacity - buffer->size,
"%lu", x);
buffer->size = min(buffer->size + n, buffer->capacity - 1);
}
void sprint1(String_Buffer *buffer, int x)
{
int n = snprintf(
buffer->data + buffer->size,
buffer->capacity - buffer->size,
"%d", x);
buffer->size = min(buffer->size + n, buffer->capacity - 1);
}
void sprint1(String_Buffer *buffer, long int x)
{
int n = snprintf(
buffer->data + buffer->size,
buffer->capacity - buffer->size,
"%ld", x);
buffer->size = min(buffer->size + n, buffer->capacity - 1);
}
void sprint1(String_Buffer *buffer, bool b)
{
sprint1(buffer, b ? "true" : "false");
}
template <typename ... Types>
void sprint(String_Buffer *buffer, Types... args)
{
(sprint1(buffer, args), ...);
}
template <typename T>
void sprint1(String_Buffer *buffer, Maybe<T> maybe)
{
if (!maybe.has_value) {
sprint(buffer, "None");
} else {
sprint(buffer, "Some(", maybe.unwrap, ")");
}
}
template <typename ... Types>
void sprintln(String_Buffer *buffer, Types... args)
{
(sprint1(buffer, args), ...);
sprint1(buffer, '\n');
}
struct Pad
{
size_t n;
char c;
};
void sprint1(String_Buffer *buffer, Pad pad)
{
for (size_t i = 0; i < pad.n; ++i) {
sprint1(buffer, pad.c);
}
}
struct Caps
{
String_View unwrap;
};
void sprint1(String_Buffer *buffer, Caps caps)
{
for (size_t i = 0; i < caps.unwrap.count; ++i) {
sprint1(buffer, (char) toupper(caps.unwrap.data[i]));
}
}
void sprint1(String_Buffer *buffer, String_Buffer another_buffer)
{
sprint1(buffer, another_buffer.view());
}
struct Escape
{
String_View unwrap;
};
////////////////////////////////////////////////////////////
// PRINT
////////////////////////////////////////////////////////////
void print1(FILE *stream, const char *s)
{
fwrite(s, 1, strlen(s), stream);
}
void print1(FILE *stream, char *s)
{
fwrite(s, 1, strlen(s), stream);
}
void print1(FILE *stream, char c)
{
fputc(c, stream);
}
void print1(FILE *stream, float f)
{
fprintf(stream, "%f", f);
}
void print1(FILE *stream, unsigned long long x)
{
fprintf(stream, "%llu", x);
}
void print1(FILE *stream, long unsigned int x)
{
fprintf(stream, "%lu", x);
}
void print1(FILE *stream, unsigned int x)
{
fprintf(stream, "%u", x);
}
void print1(FILE *stream, int x)
{
fprintf(stream, "%d", x);
}
void print1(FILE *stream, long int x)
{
fprintf(stream, "%ld", x);
}
template <typename ... Types>
void print(FILE *stream, Types... args)
{
(print1(stream, args), ...);
}
void print1(FILE *stream, bool b)
{
print1(stream, b ? "true" : "false");
}
template <typename T>
void print1(FILE *stream, Maybe<T> maybe)
{
if (!maybe.has_value) {
print(stream, "None");
} else {
print(stream, "Some(", maybe.unwrap, ")");
}
}
template <typename ... Types>
void println(FILE *stream, Types... args)
{
(print1(stream, args), ...);
print1(stream, '\n');
}
template <typename... Args>
[[noreturn]] void panic(Args... args)
{
println(stderr, args...);
exit(1);
}
template <typename... Args>
[[noreturn]] void unreachable(Args... args)
{
panic("Unreachable: ", args...);
}
template <typename... Args>
[[noreturn]] void todo(Args... args)
{
panic("TODO: ", args...);
}
template <typename T, typename... Args>
T unwrap_or_panic(Maybe<T> maybe, Args... args)
{
if (!maybe.has_value) {
panic(args...);
}
return maybe.unwrap;
}
void print1(FILE *stream, Escape escape)
{
for (size_t i = 0; i < escape.unwrap.count; ++i) {
switch (escape.unwrap.data[i]) {
case '\a': print(stream, "\\a"); break;
case '\b': print(stream, "\\b"); break;
case '\f': print(stream, "\\f"); break;
case '\n': print(stream, "\\n"); break;
case '\r': print(stream, "\\r"); break;
case '\t': print(stream, "\\t"); break;
case '\v': print(stream, "\\v"); break;
default: print(stream, escape.unwrap.data[i]);
}
}
}
void print1(FILE *stream, Pad pad)
{
for (size_t i = 0; i < pad.n; ++i) {
fputc(pad.c, stream);
}
}