-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_printf.c
1109 lines (946 loc) · 33.2 KB
/
parse_printf.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
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "parse_printf.h"
struct string_view {
const char *begin;
uint32_t length;
};
struct va_list_struct {
va_list list;
};
#define c_string_foreach(c_str, name) \
for (typeof(*c_str) *name = c_str; *name != '\0'; name++)
#define check_add(lhs, rhs, result) (!__builtin_add_overflow(lhs, rhs, result))
#define check_mul(lhs, rhs, result) (!__builtin_mul_overflow(lhs, rhs, result))
// Add 2 for a int-prefix, and one for a sign, to each length
#define BINARY_BUFFER_LENGTH 68
#define OCTAL_BUFFER_LENGTH 26
#define DECIMAL_BUFFER_LENGTH 22
#define HEXADECIMAL_BUFFER_LENGTH 20
#define LARGEST_BUFFER_LENGTH BINARY_BUFFER_LENGTH
/******* PRIVATE FUNCTIONS *******/
static inline struct string_view
sv_create_length(const char *const string, const uint32_t length) {
const struct string_view sv = {
.begin = string,
.length = length
};
return sv;
}
static inline struct string_view
sv_create_end(const char *const begin, const char *const end) {
return sv_create_length(begin, (uint64_t)(end - begin));
}
#define SV_STATIC(c_str) sv_create_length(c_str, sizeof(c_str) - 1)
#define SV_EMPTY() (struct string_view){ .begin = NULL, .length = 0 };
static struct string_view sv_drop_front(const struct string_view sv) {
if (sv.length != 0) {
return sv_create_length(sv.begin + 1, sv.length - 1);
}
return SV_EMPTY();
}
static const char *const lower_alphadigit_string = "0123456789abcdef";
static const char *const upper_alphadigit_string = "0123456789ABCDEF";
enum numeric_base {
NUMERIC_BASE_2 = 2,
NUMERIC_BASE_8 = 8,
NUMERIC_BASE_10 = 10,
NUMERIC_BASE_16 = 16,
};
struct num_to_str_options {
bool include_prefix : 1;
bool include_pos_sign : 1;
bool capitalize : 1;
};
#define NUM_TO_STR_OPTIONS_INIT() \
((struct num_to_str_options){ \
.include_prefix = false, \
.include_pos_sign = false, \
.capitalize = false, \
})
static inline struct string_view
unsigned_to_string_view(uint64_t number,
const enum numeric_base base,
char buffer_in[static const LARGEST_BUFFER_LENGTH],
const struct num_to_str_options options)
{
// Subtract one from the buffer-size to convert ordinal to index.
int i = LARGEST_BUFFER_LENGTH - 1;
const char *const alphadigit_string =
(options.capitalize) ?
upper_alphadigit_string : lower_alphadigit_string;
buffer_in[i] = '\0';
i--;
do {
buffer_in[i] = alphadigit_string[number % base];
if (number < base) {
break;
}
i--;
number /= base;
} while (true);
if (options.include_prefix) {
buffer_in[i - 2] = '0';
switch (base) {
case NUMERIC_BASE_2:
buffer_in[i - 1] = 'b';
break;
case NUMERIC_BASE_8:
buffer_in[i - 1] = 'o';
break;
case NUMERIC_BASE_10:
// This should never be reached.
break;
case NUMERIC_BASE_16:
buffer_in[i - 1] = 'x';
break;
}
i -= 2;
}
if (options.include_pos_sign) {
i -= 1;
buffer_in[i] = '+';
}
/* Make end point to the null-terminator */
const char *const end = buffer_in + (LARGEST_BUFFER_LENGTH - 1);
return sv_create_end(buffer_in + i, end);
}
static struct string_view
convert_neg_64int_to_string(int64_t number,
const enum numeric_base base,
char buffer_in[static const LARGEST_BUFFER_LENGTH],
const struct num_to_str_options options)
{
// Subtract one from the buffer-size to convert ordinal to index.
int i = LARGEST_BUFFER_LENGTH - 1;
const char *const alphadigit_string =
(options.capitalize) ?
upper_alphadigit_string : lower_alphadigit_string;
buffer_in[i] = '\0';
i--;
const int64_t neg_base = 0 - (int64_t)base;
do {
buffer_in[i] = alphadigit_string[0 - (number % base)];
if (number > neg_base) {
break;
}
i--;
number /= base;
} while (true);
if (options.include_prefix) {
buffer_in[i - 2] = '0';
switch (base) {
case NUMERIC_BASE_2:
buffer_in[i - 1] = 'b';
break;
case NUMERIC_BASE_8:
buffer_in[i - 1] = 'o';
break;
case NUMERIC_BASE_10:
// This should never be reached.
break;
case NUMERIC_BASE_16:
buffer_in[i - 1] = 'x';
break;
}
i -= 2;
}
buffer_in[i - 1] = '-';
i -= 1;
/* Make end point to the null-terminator */
const char *const end = buffer_in + (LARGEST_BUFFER_LENGTH - 1);
return sv_create_end(buffer_in + i, end);
}
static inline struct string_view
signed_to_string_view(const int64_t number,
const enum numeric_base base,
char buffer_in[static const LARGEST_BUFFER_LENGTH],
const struct num_to_str_options options)
{
struct string_view result = {};
if (number >= 0) {
result = unsigned_to_string_view(number, base, buffer_in, options);
} else {
result = convert_neg_64int_to_string(number, base, buffer_in, options);
}
return result;
}
static bool
parse_flags(struct printf_spec_info *const curr_spec,
const char *iter,
const char **const iter_out)
{
do {
switch (*iter) {
case ' ':
curr_spec->add_one_space_for_sign = true;
break;
case '-':
curr_spec->left_justify = true;
break;
case '+':
curr_spec->add_pos_sign = true;
break;
case '#':
curr_spec->add_base_prefix = true;
break;
case '0':
curr_spec->leftpad_zeros = true;
break;
default:
goto done;
}
iter++;
if (__builtin_expect(*iter == '\0', 0)) {
return false;
}
} while (true);
done:
*iter_out = iter;
return true;
}
static inline int
read_int_from_fmt_string(const char *const c_str, const char **const iter_out) {
int result = 0;
c_string_foreach(c_str, iter) {
const uint8_t digit = *iter - '0';
if (digit >= 10) {
*iter_out = iter;
break;
}
if (__builtin_expect(
!check_mul(result, 10, &result)
|| !check_add(result, digit, &result), 0))
{
*iter_out = iter;
return -1;
}
}
return result;
}
static bool
parse_width(struct printf_spec_info *const curr_spec,
struct va_list_struct *const list_struct,
const char *iter,
const char **const iter_out)
{
if (*iter != '*') {
const int width = read_int_from_fmt_string(iter, &iter);
if (__builtin_expect(width == -1, 0)) {
return false;
}
curr_spec->width = (uint32_t)width;
} else {
const int value = va_arg(list_struct->list, int);
curr_spec->width = value >= 0 ? (uint32_t)value : 0;
iter++;
}
if (__builtin_expect(*iter == '\0', 0)) {
// If we have an incomplete spec, then we exit without writing anything.
return false;
}
*iter_out = iter;
return true;
}
static bool
parse_precision(struct printf_spec_info *const curr_spec,
const char *iter,
struct va_list_struct *const list_struct,
const char **const iter_out)
{
if (*iter != '.') {
curr_spec->precision = -1;
return true;
}
iter++;
switch (*iter) {
case '\0':
return false;
case '*':
// Don't bother reading if we have an incomplete spec
if (__builtin_expect(iter[1] == '\0', 0)) {
return false;
}
curr_spec->precision = va_arg(list_struct->list, int);
iter++;
break;
default:
curr_spec->precision = read_int_from_fmt_string(iter, &iter);
if (__builtin_expect(curr_spec->precision == -1, 0)) {
return false;
}
if (__builtin_expect(*iter == '\0', 0)) {
return false;
}
break;
}
*iter_out = iter;
return true;
}
static bool
parse_length(struct printf_spec_info *const curr_spec,
const char *iter,
const char **const iter_out,
struct va_list_struct *const list_struct,
uint64_t *const number_out,
bool *const is_zero_out)
{
switch (*iter) {
case '\0':
// If we get an incomplete spec, then we exit without writing
// anything.
return false;
case 'h': {
iter++;
switch (*iter) {
case '\0':
// If we get an incomplete spec, then we exit without
// writing anything.
return false;
case 'h': {
const uint64_t number =
(uint64_t)va_arg(list_struct->list, int);
*number_out = number;
*is_zero_out = number == 0;
curr_spec->length_info = iter - 2;
curr_spec->length_info_len = 2;
iter++;
break;
}
default: {
const uint64_t number =
(uint64_t)va_arg(list_struct->list, int);
*number_out = number;
*is_zero_out = number == 0;
curr_spec->length_info = iter - 1;
curr_spec->length_info_len = 1;
break;
}
}
break;
}
case 'l':
iter++;
switch (*iter) {
case '\0':
return false;
case 'l': {
const uint64_t number =
(uint64_t)va_arg(list_struct->list, long long int);
*number_out = number;
*is_zero_out = number == 0;
curr_spec->length_info = iter - 2;
curr_spec->length_info_len = 2;
iter++;
break;
}
default: {
const uint64_t number =
(uint64_t)va_arg(list_struct->list, long int);
*number_out = number;
*is_zero_out = number == 0;
curr_spec->length_info = iter - 1;
curr_spec->length_info_len = 1;
break;
}
}
break;
case 'j': {
const uint64_t number =
(uint64_t)va_arg(list_struct->list, intmax_t);
*number_out = number;
*is_zero_out = number == 0;
curr_spec->length_info = iter;
curr_spec->length_info_len = 1;
iter++;
break;
}
case 'z': {
const uint64_t number = (uint64_t)va_arg(list_struct->list, size_t);
*number_out = number;
*is_zero_out = number == 0;
curr_spec->length_info = iter;
curr_spec->length_info_len = 1;
iter++;
break;
}
case 't': {
const uint64_t number =
(uint64_t)va_arg(list_struct->list, ptrdiff_t);
*number_out = number;
*is_zero_out = number == 0;
curr_spec->length_info = iter;
curr_spec->length_info_len = 1;
iter++;
break;
}
default:
curr_spec->length_info = NULL;
curr_spec->length_info_len = 0;
return true;
}
*iter_out = iter;
return true;
}
enum handle_spec_result {
E_HANDLE_SPEC_OK,
E_HANDLE_SPEC_REACHED_END,
E_HANDLE_SPEC_CONTINUE
};
static enum handle_spec_result
handle_spec(struct printf_spec_info *const curr_spec,
char *const buffer,
uint64_t number,
struct va_list_struct *const list_struct,
const uint32_t written_out,
struct string_view *const parsed_out,
bool *const is_zero_out,
bool *const is_null_out)
{
switch (curr_spec->spec) {
case '\0':
return E_HANDLE_SPEC_REACHED_END;
case 'b':
if (curr_spec->length_info_len == 0) {
number = (uint64_t)va_arg(list_struct->list, unsigned);
*is_zero_out = number == 0;
}
*parsed_out =
unsigned_to_string_view(number,
NUMERIC_BASE_2,
buffer,
NUM_TO_STR_OPTIONS_INIT());
break;
case 'B':
if (curr_spec->length_info_len == 0) {
number = (uint64_t)va_arg(list_struct->list, unsigned);
*is_zero_out = number == 0;
}
*parsed_out =
unsigned_to_string_view(number,
NUMERIC_BASE_2,
buffer,
(struct num_to_str_options){
.capitalize = true
});
break;
case 'd':
case 'i':
if (curr_spec->length_info_len == 0) {
number = (uint64_t)va_arg(list_struct->list, int);
*is_zero_out = number == 0;
}
*parsed_out =
signed_to_string_view((int64_t)number,
NUMERIC_BASE_10,
buffer,
(struct num_to_str_options){
.include_pos_sign =
curr_spec->add_pos_sign,
});
break;
case 'u':
if (curr_spec->length_info_len == 0) {
number = va_arg(list_struct->list, unsigned);
*is_zero_out = number == 0;
}
*parsed_out =
unsigned_to_string_view(number,
NUMERIC_BASE_10,
buffer,
NUM_TO_STR_OPTIONS_INIT());
break;
case 'o':
if (curr_spec->length_info_len == 0) {
number = va_arg(list_struct->list, unsigned);
*is_zero_out = number == 0;
}
*parsed_out =
unsigned_to_string_view(number,
NUMERIC_BASE_8,
buffer,
NUM_TO_STR_OPTIONS_INIT());
break;
case 'x':
if (curr_spec->length_info_len == 0) {
number = va_arg(list_struct->list, unsigned);
*is_zero_out = number == 0;
}
*parsed_out =
unsigned_to_string_view(number,
NUMERIC_BASE_16,
buffer,
NUM_TO_STR_OPTIONS_INIT());
break;
case 'X':
if (curr_spec->length_info_len == 0) {
number = va_arg(list_struct->list, unsigned);
*is_zero_out = number == 0;
}
*parsed_out =
unsigned_to_string_view(number,
NUMERIC_BASE_16,
buffer,
(struct num_to_str_options){
.capitalize = true
});
break;
case 'c':
buffer[0] = (char)va_arg(list_struct->list, int);
*parsed_out = sv_create_length(buffer, 1);
break;
case 's': {
const char *const str = va_arg(list_struct->list, const char *);
if (str != NULL) {
uint32_t length = 0;
if (curr_spec->precision != -1) {
length = strnlen(str, (size_t)curr_spec->precision);
} else {
length = strlen(str);
}
*parsed_out = sv_create_length(str, length);
} else {
*parsed_out = SV_STATIC("(null)");
*is_null_out = true;
}
break;
}
case 'p': {
const void *const arg = va_arg(list_struct->list, const void *);
if (arg != NULL) {
const struct num_to_str_options options = {
.capitalize = true,
.include_prefix = true
};
*parsed_out =
unsigned_to_string_view((uint64_t)arg,
NUMERIC_BASE_16,
buffer,
options);
} else {
*parsed_out = SV_STATIC("(nil)");
*is_null_out = true;
}
break;
}
case 'n':
if (curr_spec->length_info_len == 0) {
*va_arg(list_struct->list, int *) = (int)written_out;
return E_HANDLE_SPEC_CONTINUE;
}
switch (*curr_spec->length_info) {
case 'h':
// case 'hh'
if (curr_spec->length_info_len == 2) {
if (curr_spec->length_info[1] == 'h') {
*va_arg(list_struct->list, signed char *) =
written_out;
return E_HANDLE_SPEC_CONTINUE;
}
} else if (curr_spec->length_info_len == 1) {
*va_arg(list_struct->list, short int *) = written_out;
return E_HANDLE_SPEC_CONTINUE;
}
break;
case 'l':
// case 'll'
if (curr_spec->length_info_len == 2) {
if (curr_spec->length_info[1] == 'l') {
*va_arg(list_struct->list, signed char *) =
written_out;
return E_HANDLE_SPEC_CONTINUE;
}
} else if (curr_spec->length_info_len == 1) {
*va_arg(list_struct->list, long int *) =
(long int)written_out;
return E_HANDLE_SPEC_CONTINUE;
}
break;
case 'j':
*va_arg(list_struct->list, intmax_t *) =
(intmax_t)written_out;
return E_HANDLE_SPEC_CONTINUE;
case 'z':
*va_arg(list_struct->list, size_t *) = written_out;
return E_HANDLE_SPEC_CONTINUE;
case 't':
*va_arg(list_struct->list, ptrdiff_t *) =
(ptrdiff_t)written_out;
return E_HANDLE_SPEC_CONTINUE;
}
break;
case '%':
buffer[0] = '%';
*parsed_out = sv_create_length(buffer, 1);
break;
default:
return E_HANDLE_SPEC_CONTINUE;
}
return E_HANDLE_SPEC_OK;
}
static inline bool is_int_specifier(const char spec) {
switch (spec) {
case 'b':
case 'B':
case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
return true;
}
return false;
}
static inline uint32_t
write_prefix_for_spec(struct printf_spec_info *const info,
const printf_write_char_callback_t write_ch_cb,
void *const ch_cb_info,
const printf_write_string_callback_t write_sv_cb,
void *const sv_cb_info,
bool *const cont_out)
{
uint32_t out = 0;
if (!info->add_base_prefix) {
return out;
}
switch (info->spec) {
case 'b':
out += write_sv_cb(info, sv_cb_info, "0b", /*length=*/2, cont_out);
break;
case 'B':
out += write_sv_cb(info, sv_cb_info, "0B", /*length=*/2, cont_out);
break;
case 'o':
out += write_ch_cb(info, ch_cb_info, '0', /*times=*/1, cont_out);
break;
case 'x':
out += write_sv_cb(info, sv_cb_info, "0x", /*length=*/2, cont_out);
break;
case 'X':
out += write_sv_cb(info, sv_cb_info, "0X", /*length=*/2, cont_out);
break;
}
return out;
}
static uint32_t
pad_with_lead_zeros(struct printf_spec_info *const info,
struct string_view *const parsed,
const uint32_t zero_count,
const bool is_null,
const printf_write_char_callback_t write_char_cb,
void *const cb_info,
const printf_write_string_callback_t write_sv_cb,
void *const write_sv_cb_info,
bool *const cont_out)
{
uint32_t out = 0;
if (!is_null) {
out +=
write_prefix_for_spec(info,
write_char_cb,
cb_info,
write_sv_cb,
write_sv_cb_info,
cont_out);
}
if (zero_count == 0) {
return out;
}
const char front = *parsed->begin;
if (front == '+') {
out += write_char_cb(info, cb_info, '+', /*amount=*/1, cont_out);
if (!cont_out) {
return out;
}
*parsed = sv_drop_front(*parsed);
} else if (front == '-') {
out += write_char_cb(info, cb_info, '-', /*amount=*/1, cont_out);
if (!cont_out) {
return out;
}
*parsed = sv_drop_front(*parsed);
}
out += write_char_cb(info, cb_info, '0', zero_count, cont_out);
if (!cont_out) {
return out;
}
return out;
}
static inline uint32_t
call_cb(struct printf_spec_info *const info,
const struct string_view sv,
const printf_write_char_callback_t write_char_cb,
void *const char_cb_info,
const printf_write_string_callback_t write_sv_cb,
void *const sv_cb_info,
bool *const cont_out)
{
if (sv.length == 0) {
}
if (sv.length == 1) {
const char ch = *sv.begin;
return write_char_cb(info, char_cb_info, ch, /*amount=*/1, cont_out);
}
return write_sv_cb(info, sv_cb_info, sv.begin, sv.length, cont_out);
}
/******* PUBLIC FUNCTIONS *******/
uint32_t
parse_printf_format(const printf_write_char_callback_t write_char_cb,
void *const write_char_cb_info,
const printf_write_string_callback_t write_string_cb,
void *const write_string_cb_info,
const char *const fmt,
va_list list)
{
struct va_list_struct list_struct = {0};
va_copy(list_struct.list, list);
char buffer[LARGEST_BUFFER_LENGTH];
bzero(buffer, sizeof(buffer));
struct printf_spec_info curr_spec = PRINTF_SPEC_INFO_INIT();
const char *unformatted_start = fmt;
uint32_t written_out = 0;
bool should_continue = true;
const char *iter = strchr(fmt, '%');
for (; iter != NULL; iter = strchr(iter, '%')) {
const struct string_view unformatted =
sv_create_end(unformatted_start, iter);
written_out +=
call_cb(NULL,
unformatted,
write_char_cb,
write_char_cb_info,
write_string_cb,
write_string_cb_info,
&should_continue);
if (!should_continue) {
break;
}
iter++;
if (*iter == '\0') {
// If we only got a percent sign, then we don't print anything
va_end(list_struct.list);
return written_out;
}
// Format is %[flags][width][.precision][length]specifier
if (!parse_flags(&curr_spec, iter, &iter)) {
// If we have an incomplete spec, then we exit without writing
// anything.
va_end(list_struct.list);
return written_out;
}
if (!parse_width(&curr_spec, &list_struct, iter, &iter)) {
// If we have an incomplete spec, then we exit without writing
// anything.
va_end(list_struct.list);
return written_out;
}
if (!parse_precision(&curr_spec, iter, &list_struct, &iter)) {
// If we have an incomplete spec, then we exit without writing
// anything.
va_end(list_struct.list);
return written_out;
}
uint64_t number = 0;
bool is_zero = false;
if (!parse_length(&curr_spec,
iter,
&iter,
&list_struct,
&number,
&is_zero))
{
// If we have an incomplete spec, then we exit without writing
// anything.
va_end(list_struct.list);
return written_out;
}
// Parse specifier
struct string_view parsed = SV_EMPTY();
bool is_null = false;
curr_spec.spec = *iter;
unformatted_start = iter + 1;
const enum handle_spec_result handle_spec_result =
handle_spec(&curr_spec,
buffer,
number,
&list_struct,
written_out,
&parsed,
&is_zero,
&is_null);
switch (handle_spec_result) {
case E_HANDLE_SPEC_OK:
break;
case E_HANDLE_SPEC_REACHED_END:
va_end(list_struct.list);
return written_out;
case E_HANDLE_SPEC_CONTINUE:
curr_spec = PRINTF_SPEC_INFO_INIT();
continue;
}
// Move past specifier
iter++;
uint32_t padded_zero_count = 0;
uint8_t parsed_length = parsed.length;
// is_zero being true implies spec is an integer.
// We don't write anything if we have a '0' and precision is 0.
const bool should_write_parsed = !(is_zero && curr_spec.precision == 0);
if (should_write_parsed) {
if (curr_spec.add_base_prefix) {
switch (curr_spec.spec) {
case 'b':
case 'B':
parsed_length += 2;
break;
case 'o':
parsed_length += 1;
break;
case 'x':
case 'X':
parsed_length += 2;
break;
}
}
} else {
parsed_length = 0;
}
// We have to pad with either spaces or zeroes if we're not wider than
// the specified width,
uint32_t space_pad_count = 0;
if (is_int_specifier(curr_spec.spec)) {
if (curr_spec.precision != -1) {
// The case for the string-spec was already handled above
// Total digit count doesn't include the sign/prefix.
uint8_t total_digit_count = parsed.length;
if (*parsed.begin == '-' || *parsed.begin == '+') {
total_digit_count -= 1;
}
if (total_digit_count < curr_spec.precision) {
padded_zero_count =
(uint32_t)curr_spec.precision - total_digit_count;
parsed_length += padded_zero_count;
}
}
if (parsed_length != 0 && curr_spec.add_one_space_for_sign) {
// Only add a sign if we have neither a '+' or '-'
if (*parsed.begin != '+' && *parsed.begin != '-') {
space_pad_count += 1;
}
}
}
if (parsed_length < curr_spec.width) {
const bool pad_with_zeros =
curr_spec.leftpad_zeros
&& is_int_specifier(curr_spec.spec)
&& curr_spec.precision == -1
&& !curr_spec.left_justify; // Zeros are never left-justified
if (pad_with_zeros) {
// We're always resetting padded_zero_count if it was set before
padded_zero_count = curr_spec.width - parsed_length;
} else {
space_pad_count += curr_spec.width - parsed_length;
}
}
if (curr_spec.left_justify) {
written_out +=
pad_with_lead_zeros(&curr_spec,
&parsed,
padded_zero_count,