-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrb_u_string_format.c
More file actions
1745 lines (1558 loc) · 64.6 KB
/
Copy pathrb_u_string_format.c
File metadata and controls
1745 lines (1558 loc) · 64.6 KB
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 "rb_includes.h"
#include <limits.h>
#include <math.h>
#ifdef HAVE_RUBY_INTERN_H
# include <ruby/intern.h>
#else
# include <intern.h>
#endif
#ifdef HAVE_RUBY_ENCODING_H
# include <ruby/encoding.h>
#endif
#include "rb_u_buffer.h"
#include "rb_u_string_to_inum.h"
enum directive_flags {
DIRECTIVE_FLAGS_NONE = 0,
DIRECTIVE_FLAGS_SPACE = 1,
DIRECTIVE_FLAGS_SHARP = 2,
DIRECTIVE_FLAGS_PLUS = 4,
DIRECTIVE_FLAGS_MINUS = 8,
DIRECTIVE_FLAGS_ZERO = 16,
DIRECTIVE_FLAGS_DECIMAL =
DIRECTIVE_FLAGS_SPACE |
DIRECTIVE_FLAGS_PLUS |
DIRECTIVE_FLAGS_MINUS |
DIRECTIVE_FLAGS_ZERO,
DIRECTIVE_FLAGS_NUMBER =
DIRECTIVE_FLAGS_DECIMAL |
DIRECTIVE_FLAGS_SHARP
};
struct format_arguments {
int argc;
const VALUE *argv;
int i;
bool absolute;
VALUE names;
};
#if !(defined(HAVE_RB_LONG2INT) || defined(rb_long2int))
# if SIZEOF_INT < SIZEOF_LONG
static void
rb_out_of_int(long l)
{
rb_u_raise(rb_eRangeError,
l < 0 ?
"integer %ld too small to convert to C type int" :
"integer %ld too big to convert to C type int",
l);
}
# if defined(__GNUC__) && __GNUC__ > 2
# define rb_long2int(l) __extension__({ \
long _rb_long2int_l = (l); \
int _rb_long2int_i = (int)_rb_long2int_l; \
if ((long)_rb_long2int_i != _rb_long2int_l) \
rb_out_of_int(_rb_long2int_l); \
_rb_long2int_i; \
})
# else
static inline int
rb_long2int(long l)
{
int i = (int)l;
if ((long)i != l)
rb_out_of_int(l);
return i;
}
# endif
# else
# define rb_long2int(l) ((int)(l))
# endif
#endif
#ifndef RARRAY_LENINT
# define RARRAY_LENINT(ary) rb_long2int(RARRAY_LEN(ary))
#endif
#ifndef HAVE_RB_HASH_LOOKUP2
# include <st.h>
static VALUE
rb_hash_lookup2(VALUE hash, VALUE key, VALUE default_value)
{
VALUE value;
if (st_lookup(RHASH(hash)->tbl, key, &value))
return value;
return default_value;
}
#endif
#ifndef HAVE_RB_EKEYERROR
# define rb_eKeyError rb_eArgError
#endif
static int
directive_parse_int(const char **p, const char *end, const char *type)
{
const char *q = *p;
int n = 0;
while (q < end) {
const char *r;
uint32_t c = u_decode(&r, q, end);
if (!u_char_isdigit(c)) {
*p = q;
return n;
}
q = r;
int m = 10 * n + u_char_digit_value(c);
if (m / 10 != n) {
for ( ; q < end; q = r) {
if (!u_char_isdigit(u_decode(&r, q, end)))
break;
}
rb_u_raise(rb_eArgError,
"%s too large: %*s > %d",
type, (int)(q - *p), *p, INT_MAX);
}
n = m;
}
rb_u_raise(rb_eArgError, "directive missing after %s", type);
}
static bool
directive_argument_number(const char **p, const char *end, const char *type,
int *argument_number)
{
if (*argument_number != 0)
rb_u_raise(rb_eArgError, "%s already given", type);
const char *q = *p;
int n = directive_parse_int(&q, end, type);
if (*q != '$')
return false;
*p = q + 1;
*argument_number = n;
if (*p == end)
rb_u_raise(rb_eArgError, "directive missing after %s", type);
return true;
}
static void
directive_argument_name(const char **p, const char *end, char right,
ID *argument_id)
{
if (*argument_id != 0)
rb_u_raise(rb_eArgError, "argument name already given");
const char *q = *p;
const char *r = q;
while (r < end && u_decode(&r, q, end) != (uint32_t)right)
q = r;
if (q == end)
rb_u_raise(rb_eArgError,
"missing argument name end delimiter ‘%c’: %s",
right, *p);
const char *base = *p + 1;
long length = q - base;
#ifdef HAVE_RUBY_ENCODING_H
*argument_id = rb_intern3(base, length, rb_utf8_encoding());
#else
char name[length + 1];
memcpy(name, base, length);
name[length] = '\0';
*argument_id = rb_intern(name);
#endif
*p = r;
if (*p == end)
rb_u_raise(rb_eArgError, "directive missing after argument name");
}
static VALUE
format_arguments_absolute(struct format_arguments *arguments, int absolute)
{
if (arguments->i > 0)
rb_u_raise(rb_eArgError,
"cannot use absolute argument number %d: relative argument number already used",
absolute);
if (arguments->names != Qundef)
rb_u_raise(rb_eArgError,
"cannot use absolute argument number %d: named argument already used",
absolute);
arguments->absolute = true;
if (absolute > arguments->argc)
rb_u_raise(rb_eArgError,
"absolute argument number beyond end of argument list: %d > %d",
absolute, arguments->argc);
return arguments->argv[absolute - 1];
}
static VALUE
format_arguments_id(struct format_arguments *arguments, ID id)
{
if (arguments->i > 0)
rb_u_raise(rb_eArgError,
"cannot use named argument “%s”: relative argument number already used",
rb_id2name(id));
if (arguments->absolute)
rb_u_raise(rb_eArgError,
"cannot use named argument “%s”: absolute argument number already used",
rb_id2name(id));
if (arguments->names == Qundef) {
VALUE tmp;
if (arguments->argc != 1 ||
NIL_P(tmp = rb_check_convert_type(arguments->argv[0], T_HASH, "Hash", "to_hash")))
rb_u_raise(rb_eArgError,
"one Hash argument required when using named arguments in format");
arguments->names = tmp;
}
VALUE argument = rb_hash_lookup2(arguments->names, ID2SYM(id), Qundef);
if (argument == Qundef)
rb_u_raise(rb_eKeyError, "named argument not found: %s", rb_id2name(id));
return argument;
}
static VALUE
format_arguments_next(struct format_arguments *arguments)
{
if (arguments->absolute)
rb_u_raise(rb_eArgError,
"cannot use positional argument numbers after absolute argument numbers");
if (arguments->names != Qundef)
rb_u_raise(rb_eArgError,
"cannot use positional argument numbers after named arguments");
arguments->i++;
if (arguments->i > arguments->argc)
need_at_least_n_arguments(arguments->argc, arguments->i);
return arguments->argv[arguments->i - 1];
}
static int
directive_flags(const char **p, const char *end,
struct format_arguments *arguments,
VALUE *argument, bool *argument_to_s)
{
int flags = DIRECTIVE_FLAGS_NONE;
int argument_number = 0;
ID argument_id = 0;
while (*p < end) {
const char *q;
uint32_t c = u_decode(&q, *p, end);
int flag;
switch (c) {
case ' ':
flag = DIRECTIVE_FLAGS_SPACE;
break;
case '#':
flag = DIRECTIVE_FLAGS_SHARP;
break;
case '+':
flag = DIRECTIVE_FLAGS_PLUS;
break;
case '-':
flag = DIRECTIVE_FLAGS_MINUS;
break;
case '0':
flag = DIRECTIVE_FLAGS_ZERO;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (!directive_argument_number(p, end, "absolute argument number", &argument_number))
goto setup_argument;
if (argument_id != 0)
rb_u_raise(rb_eArgError,
"cannot use absolute argument number: argument name already given");
continue;
case '<':
if (argument_number != 0)
rb_u_raise(rb_eArgError,
"cannot use argument name: absolute argument number already given");
directive_argument_name(p, end, '>', &argument_id);
*argument_to_s = false;
continue;
case '{':
if (argument_number != 0)
rb_u_raise(rb_eArgError,
"cannot use argument name: absolute argument number already given");
directive_argument_name(p, end, '}', &argument_id);
*argument_to_s = true;
goto setup_argument;
default:
goto setup_argument;
}
if (flags & flag)
rb_warning("repeated flag in format: ‘%c’", c);
flags |= flag;
*p = q;
}
if (flags != DIRECTIVE_FLAGS_NONE && *p == end)
rb_u_raise(rb_eArgError, "directive missing after flags");
setup_argument:
if (argument_number != 0)
*argument = format_arguments_absolute(arguments, argument_number);
else if (argument_id != 0)
*argument = format_arguments_id(arguments, argument_id);
return flags;
}
static int
directive_width(const char **p, const char *end,
struct format_arguments *arguments,
enum directive_flags *flags)
{
if (*p == end)
return 0;
const char *q;
if (u_decode(&q, *p, end) != '*')
return directive_parse_int(p, end, "field width");
*p = q;
int argument_number = 0;
VALUE argument = directive_argument_number(p, end,
"absolute field width argument number",
&argument_number) ?
format_arguments_absolute(arguments, argument_number) :
format_arguments_next(arguments);
int width = NUM2INT(argument);
if (width < 0) {
*flags |= DIRECTIVE_FLAGS_MINUS;
width = -width;
}
return width;
}
static int
directive_precision(const char **p, const char *end)
{
if (*p == end)
return -1;
const char *q;
if (u_decode(&q, *p, end) != '.')
return -1;
*p = q;
return directive_parse_int(p, end, "field precision");
}
static void
directive_validate_flags(uint32_t c, int flags, int valid)
{
char invalid[6];
int n = 0;
if ((flags & DIRECTIVE_FLAGS_SPACE) && !(valid & DIRECTIVE_FLAGS_SPACE))
invalid[n++] = ' ';
if ((flags & DIRECTIVE_FLAGS_SHARP) && !(valid & DIRECTIVE_FLAGS_SHARP))
invalid[n++] = '#';
if ((flags & DIRECTIVE_FLAGS_PLUS) && !(valid & DIRECTIVE_FLAGS_PLUS))
invalid[n++] = '+';
if ((flags & DIRECTIVE_FLAGS_MINUS) && !(valid & DIRECTIVE_FLAGS_MINUS))
invalid[n++] = '-';
if ((flags & DIRECTIVE_FLAGS_ZERO) && !(valid & DIRECTIVE_FLAGS_ZERO))
invalid[n++] = '0';
if (n == 0)
return;
invalid[n] = '\0';
char buf[U_CHAR_MAX_BYTE_LENGTH];
int length = u_char_to_u(c, buf);
if (n == 1)
rb_u_raise(rb_eArgError,
"invalid flag ‘%s’ given to directive ‘%.*s’",
invalid, length, buf);
rb_u_raise(rb_eArgError,
"invalid flags “%s” given to directive ‘%.*s’",
invalid, length, buf);
}
NORETURN(static void
directive_simple_error(uint32_t c, const char *message))
{
char buf[U_CHAR_MAX_BYTE_LENGTH];
int length = u_char_to_u(c, buf);
rb_u_raise(rb_eArgError, "%s: %.*s", message, length, buf);
}
static void
directive_validate_argument_not_given(uint32_t c, VALUE argument)
{
if (argument != Qundef)
directive_simple_error(c, "directive does not take an argument");
}
static void
directive_validate_width_not_given(uint32_t c, int width)
{
if (width != 0)
directive_simple_error(c, "directive does not allow specifying a width");
}
static void
directive_validate_precision_not_given(uint32_t c, int precision)
{
if (precision >= 0)
directive_simple_error(c, "directive does not allow specifying a precision");
}
static void
directive_escape(uint32_t c, VALUE result)
{
rb_u_buffer_append_char(result, c);
}
static void
directive_pad(int flags, int padding, const char *str, long length, VALUE result)
{
if (flags & DIRECTIVE_FLAGS_MINUS) {
rb_u_buffer_append(result, str, length);
rb_u_buffer_append_char_n(result, ' ', padding);
} else {
rb_u_buffer_append_char_n(result, ' ', padding);
rb_u_buffer_append(result, str, length);
}
}
static void
directive_character(UNUSED(uint32_t directive), int flags, int width, UNUSED(int precision), VALUE argument, VALUE result)
{
VALUE tmp = rb_u_string_check_type(argument);
uint32_t c;
const char *p;
int length;
if (!NIL_P(tmp)) {
const struct rb_u_string *string = RVAL2USTRING_ANY(tmp);
p = USTRING_STR(string);
const char *end = USTRING_END(string);
if (p == end)
rb_u_raise(rb_eArgError, "%%c requires a character");
const char *q;
c = u_decode(&q, p, end);
length = (int)(q - p);
} else {
char buf[U_CHAR_MAX_BYTE_LENGTH];
p = buf;
c = NUM2INT(argument);
length = rb_u_char_to_u(c, buf);
}
int padding = width - (int)u_char_width(c);
if (padding < 0) {
rb_u_buffer_append_char(result, c);
return;
}
directive_pad(flags, padding, p, length, result);
}
static void
directive_string(UNUSED(uint32_t directive), int flags, int width, int precision, VALUE argument, VALUE result)
{
VALUE str = rb_u_string_object_as_string(argument);
if (OBJ_TAINTED(str))
OBJ_TAINT(result);
const struct rb_u_string *string = RVAL2USTRING_ANY(str);
const char *p = USTRING_STR(string);
long length = USTRING_LENGTH(string);
if (precision > 0) {
int i = 0;
const char *q = p, *end = p + length;
while (i < precision && q < end)
i += (int)u_char_width(u_decode(&q, q, end));
length = q - p;
}
long n_cells;
if (width == 0 || width < (n_cells = u_width_n(p, length))) {
rb_u_buffer_append(result, p, length);
return;
}
directive_pad(flags, width - (int)n_cells, p, length, result);
}
static void
directive_inspect(uint32_t directive, int flags, int width, int precision, VALUE argument, VALUE result)
{
directive_string(directive, flags, width, precision, rb_inspect(argument), result);
}
static long
directive_integer_value_m(VALUE argument, VALUE *bignum)
{
switch (TYPE(argument)) {
case T_FLOAT: {
if (FIXABLE(RFLOAT_VALUE(argument)))
return (long)RFLOAT_VALUE(argument);
VALUE result = rb_dbl2big(RFLOAT_VALUE(argument));
if (FIXNUM_P(result))
return FIX2LONG(result);
*bignum = result;
return 0;
}
case T_STRING:
return directive_integer_value_m(rb_str_to_inum(argument, 0, true), bignum);
case T_BIGNUM:
*bignum = argument;
return 0;
case T_FIXNUM:
return FIX2LONG(argument);
default:
if (RTEST(rb_obj_is_kind_of(argument, rb_cUString)))
return directive_integer_value_m(rb_u_string_to_inum(argument, 0, true), bignum);
return directive_integer_value_m(rb_Integer(argument), bignum);
}
}
static long
directive_integer_value(VALUE argument, int base, VALUE *bignum)
{
long value = directive_integer_value_m(argument, bignum);
if (base == 2 && *bignum == Qundef)
return directive_integer_value_m(rb_int2big(value), bignum);
return value;
}
static void
directive_number_output(int flags, int width, int precision,
const char *prefix, uint32_t precision_filler, const char *digits, int length,
VALUE result)
{
int prefix_length = (int)strlen(prefix);
width -= prefix_length;
if (precision >= 0)
flags &= ~DIRECTIVE_FLAGS_ZERO;
if (precision < length)
precision = length;
width -= precision;
if (!(flags & DIRECTIVE_FLAGS_MINUS) && !(flags & DIRECTIVE_FLAGS_ZERO))
rb_u_buffer_append_char_n(result, ' ', width);
rb_u_buffer_append(result, prefix, prefix_length);
if (!(flags & DIRECTIVE_FLAGS_MINUS) && (flags & DIRECTIVE_FLAGS_ZERO))
rb_u_buffer_append_char_n(result, '0', width);
rb_u_buffer_append_char_n(result, precision_filler, precision - length);
rb_u_buffer_append(result, digits, length);
if (flags & DIRECTIVE_FLAGS_MINUS)
rb_u_buffer_append_char_n(result, ' ', width);
}
#define BITS2DECIMALDIGITS(n) (((long)(n) * 146) / 485 + 1) /* lg(10)⁻¹ ≈ 146/485 */
#define BITS2OCTALDIGITS(n) (((long)(n) * 1) / 3 + 1) /* lg(8)⁻¹ = 3 */
#define DIGITS_BUFFER_SIZE (BITS2OCTALDIGITS(sizeof(long) * CHAR_BIT) + 1)
#define BASE2FORMAT(base) \
((base) == 10 ? "%ld" : ((base) == 16 ? "%lx" : "%lo"))
static void
directive_conflicting_flags_warning(uint32_t directive, int ignored, int when)
{
char buf[U_CHAR_MAX_BYTE_LENGTH];
int length = u_char_to_u(directive, buf);
rb_warning("‘%.*s’ directive ignores ‘%c’ flag when ‘%c’ flag has been specified",
length, buf, ignored, when);
}
static bool
directive_number_sign(uint32_t directive, bool negative, int flags, char *sign)
{
if (flags & DIRECTIVE_FLAGS_PLUS && flags & DIRECTIVE_FLAGS_SPACE)
directive_conflicting_flags_warning(directive, ' ', '+');
if (negative) {
sign[0] = '-';
return true;
}
if (flags & DIRECTIVE_FLAGS_PLUS)
sign[0] = '+';
else if (flags & DIRECTIVE_FLAGS_SPACE)
sign[0] = ' ';
else
sign[0] = '\0';
return false;
}
static inline int
directive_number_long_signed(uint32_t directive, int flags, long argument,
int base, char *sign, const char **digits, char *buffer)
{
if (directive_number_sign(directive, argument < 0, flags, sign))
argument = -argument;
*digits = buffer;
return snprintf(buffer, DIGITS_BUFFER_SIZE, BASE2FORMAT(base), argument);
}
static inline int
directive_number_bignum_signed(uint32_t directive, int flags, VALUE argument,
int base, char *sign, const char **digits, VALUE *str)
{
*str = rb_big2str(argument, base);
*digits = RSTRING_PTR(*str);
if (directive_number_sign(directive, *digits[0] == '-', flags, sign))
(*digits)++;
return rb_long2int(RSTRING_END(*str) - *digits);
}
static void
directive_number_check_flags(uint32_t directive, int flags, int precision)
{
if ((flags & DIRECTIVE_FLAGS_MINUS) && (flags & DIRECTIVE_FLAGS_ZERO))
directive_conflicting_flags_warning(directive, '0', '-');
if (precision >= 0 && (flags & DIRECTIVE_FLAGS_ZERO)) {
char buf[U_CHAR_MAX_BYTE_LENGTH];
int length = u_char_to_u(directive, buf);
rb_warning("‘%.*s’ directive ignores ‘0’ flag when precision (%d) has been specified",
length, buf, precision);
}
}
static int
directive_signed_number(uint32_t directive, int flags, int precision, VALUE argument,
int base, char *sign, const char **digits, char *buffer, VALUE *str)
{
directive_number_check_flags(directive, flags, precision);
VALUE bignum = Qundef;
long lvalue = directive_integer_value(argument, base, &bignum);
return (bignum == Qundef) ?
directive_number_long_signed(directive, flags, lvalue, base, sign, digits, buffer) :
directive_number_bignum_signed(directive, flags, bignum, base, sign, digits, str);
}
static U_PURE const char *
directive_number_skip_bits(const char *digits, int base)
{
const char *p = digits;
if (base == 16)
while (*p == 'f' || *p == 'F')
p++;
else if (base == 8) {
p++;
while (*p == '7')
p++;
} else if (base == 2)
while (*p == '1')
p++;
return p - 1;
}
static inline int
directive_number_long_unsigned(long argument,
int base, char *prefix, const char **digits, char *buffer)
{
*digits = buffer;
int length = snprintf(buffer, DIGITS_BUFFER_SIZE, BASE2FORMAT(base), (unsigned long)argument);
if (argument < 0) {
*digits = directive_number_skip_bits(buffer, base);
length -= (int)(*digits - buffer);
strcat(prefix, "..");
}
return length;
}
static inline int
directive_number_bignum_unsigned(VALUE argument,
int base, char *prefix, const char **digits, VALUE *str)
{
if (!RBIGNUM_SIGN(argument)) {
argument = rb_big_clone(argument);
rb_big_2comp(argument);
}
*str = rb_big2str0(argument, base, RBIGNUM_SIGN(argument));
*digits = RSTRING_PTR(*str);
if (*digits[0] == '-') {
*digits = directive_number_skip_bits(*digits + 1, base);
strcat(prefix, "..");
}
return rb_long2int(RSTRING_END(*str) - *digits);
}
static inline int
directive_unsigned_number(uint32_t directive, int flags, int precision, VALUE argument,
int base, char *prefix, const char **digits, char *buffer, VALUE *str)
{
directive_number_check_flags(directive, flags, precision);
VALUE bignum = Qundef;
long lvalue = directive_integer_value(argument, base, &bignum);
return (bignum == Qundef) ?
directive_number_long_unsigned(lvalue, base, prefix, digits, buffer) :
directive_number_bignum_unsigned(bignum, base, prefix, digits, str);
}
static int
directive_signed_or_unsigned_number(uint32_t directive, int flags, int precision, VALUE argument,
int base, char *prefix, const char **digits, char *buffer, VALUE *str)
{
if (!(flags & DIRECTIVE_FLAGS_SHARP))
for (char *p = prefix; *p != '\0'; p++)
*p = '\0';
if (!(flags & (DIRECTIVE_FLAGS_PLUS | DIRECTIVE_FLAGS_SPACE)))
return directive_unsigned_number(directive, flags, precision, argument, base, prefix, digits, buffer, str);
/* Move prefix forward one position to make room for sign. */
for (char *p = prefix + strlen(prefix); p > prefix; p--)
*p = *(p - 1);
return directive_signed_number(directive, flags, precision, argument, base, prefix, digits, buffer, str);
}
static void
directive_integer(uint32_t directive, int flags, int width, int precision, VALUE argument, VALUE result)
{
char sign[] = "\0\0";
char buffer[DIGITS_BUFFER_SIZE];
VALUE str;
const char *digits;
int length = directive_signed_number(directive, flags, precision, argument, 10, sign, &digits, buffer, &str);
directive_number_output(flags, width, precision, sign, '0', digits, length, result);
}
static bool
directive_number_is_unsigned(char *prefix)
{
return strstr(prefix, "..") != NULL;
}
static void
directive_ignored_flag_on_negative_argument_warning(uint32_t directive, int ignored)
{
char buf[U_CHAR_MAX_BYTE_LENGTH];
int length = u_char_to_u(directive, buf);
rb_warning("‘%.*s’ directive ignores ‘%c’ flag when given a negative argument",
length, buf, ignored);
}
static void
directive_unsigned_number_output(uint32_t directive, int flags, int width, int precision,
char *prefix, const char *digits, int length,
VALUE result)
{
if (directive_number_is_unsigned(prefix)) {
if (flags & DIRECTIVE_FLAGS_ZERO)
directive_ignored_flag_on_negative_argument_warning(directive, '0');
directive_number_output(flags & ~DIRECTIVE_FLAGS_ZERO,
width,
precision < 0 ? precision :
(precision - 2 >= 0 ? precision - 2 : 0),
prefix, digits[0], digits, length,
result);
} else
directive_number_output(flags, width, precision, prefix, '0', digits, length, result);
}
static void
directive_octal(uint32_t directive, int flags, int width, int precision, VALUE argument, VALUE result)
{
char prefix[] = "0\0\0\0\0";
char buffer[DIGITS_BUFFER_SIZE];
VALUE str;
const char *digits;
int length = directive_signed_or_unsigned_number(directive, flags, precision, argument, 8, prefix, &digits, buffer, &str);
if ((flags & DIRECTIVE_FLAGS_SHARP) &&
(precision >= 0 ||
directive_number_is_unsigned(prefix) ||
(length == 1 && digits[0] == '0'))) {
if (directive_number_is_unsigned(prefix))
directive_ignored_flag_on_negative_argument_warning(directive, '#');
for (char *p = prefix; *p != '\0'; p++)
*p = *(p + 1);
}
directive_unsigned_number_output(directive, flags, width, precision, prefix, digits, length, result);
}
static void
directive_hexadecimal(uint32_t directive, int flags, int width, int precision, VALUE argument, VALUE result)
{
char prefix[] = "0x\0\0\0\0";
if (directive == 'X')
prefix[1] = 'X';
char buffer[DIGITS_BUFFER_SIZE];
VALUE str;
const char *digits;
int length = directive_signed_or_unsigned_number(directive, flags, precision, argument, 16, prefix, &digits, buffer, &str);
if ((flags & DIRECTIVE_FLAGS_SHARP) && (length == 1 && digits[0] == '0'))
for (char *p = prefix; *p != '\0'; p++)
*p = '\0';
if (directive == 'X')
for (char *p = (char *)digits; *p != '\0'; p++)
*p = u_char_upcase(*p);
directive_unsigned_number_output(directive, flags, width, precision, prefix, digits, length, result);
}
static void
directive_binary(uint32_t directive, int flags, int width, int precision, VALUE argument, VALUE result)
{
char prefix[] = "0b\0\0\0\0";
if (directive == 'B')
prefix[1] = 'B';
char buffer[DIGITS_BUFFER_SIZE];
VALUE str;
const char *digits;
int length = directive_signed_or_unsigned_number(directive, flags, precision, argument, 2, prefix, &digits, buffer, &str);
if ((flags & DIRECTIVE_FLAGS_SHARP) && (length == 1 && digits[0] == '0'))
for (char *p = prefix; *p != '\0'; p++)
*p = '\0';
directive_unsigned_number_output(directive, flags, width, precision, prefix, digits, length, result);
}
static inline void
directive_float_nan(uint32_t directive, int flags, int width, VALUE result)
{
/* sign? + NaN + \0 */
char buffer[1 + 3 + 1] = "\0";
int length = 3;
directive_number_sign(directive, false, flags, buffer);
if (buffer[0] != '\0')
length++;
strcat(buffer, "NaN");
directive_pad(flags, width - length, buffer, length, result);
}
static inline void
directive_float_inf(uint32_t directive, int flags, int width, double argument, VALUE result)
{
/* sign? + Inf + \0 */
char buffer[1 + 3 + 1] = "\0";
int length = 3;
directive_number_sign(directive, argument < 0.0, flags, buffer);
if (buffer[0] != '\0')
length++;
strcat(buffer, "Inf");
directive_pad(flags, width - length, buffer, length, result);
}
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
static void
directive_float_format(uint32_t directive, int flags, int width, int precision, double argument, VALUE result)
{
char format[1 + /* '%' */
5 + /* flags{0,5} */
BITS2DECIMALDIGITS(sizeof(width) * CHAR_BIT) + /* width_digits? */
1 + /* ('.' */
BITS2DECIMALDIGITS(sizeof(precision) * CHAR_BIT) + /* precision_digits)? */
1 + /* directive */
1]; /* '\0' */
char *p = format;
const char *end = format + lengthof(format);
*p++ = '%';
if (flags & DIRECTIVE_FLAGS_SHARP)
*p++ = '#';
if (flags & DIRECTIVE_FLAGS_PLUS)
*p++ = '+';
if (flags & DIRECTIVE_FLAGS_MINUS)
*p++ = '-';
if (flags & DIRECTIVE_FLAGS_ZERO)
*p++ = '0';
if (flags & DIRECTIVE_FLAGS_SPACE)
*p++ = ' ';
if (width > 0)
p += snprintf(p, end - p, "%d", width);
if (precision >= 0)
p += snprintf(p, end - p, ".%d", precision);
*p++ = directive;
*p = '\0';
int exponent = 0;
frexp(argument, &exponent);
/* sign? +
* prefix? +
* character-before-separator? +
* (decimal_digits or characters after exponent) +
* separator? +
* precision? +
* exponent-char?
* exponent-±?
*/
size_t needed = 1 +
2 +
1 +
abs((int)BITS2DECIMALDIGITS(exponent)) +
1 +
(size_t)(precision >= 0 ? precision : 0) +
1 +
1;
if (needed < (size_t)width)
needed = width;
needed += 1;
rb_u_buffer_append_printf(result, needed, format, argument);
}
#pragma GCC diagnostic warning "-Wformat-nonliteral"
static void
directive_float(uint32_t directive, int flags, int width, int precision, VALUE argument, VALUE result)
{
double value = RFLOAT_VALUE(rb_Float(argument));
if (isnan(value))
directive_float_nan(directive, flags, width, result);
else if (isinf(value))
directive_float_inf(directive, flags, width, value, result);
else
directive_float_format(directive, flags, width, precision, value, result);
}
static void
directive(const char **p, const char *end, struct format_arguments *arguments, VALUE result)
{
VALUE argument = Qundef;
bool argument_to_s = false;
enum directive_flags flags = directive_flags(p, end, arguments, &argument, &argument_to_s);
if (argument_to_s) {
directive_validate_flags('s', flags, DIRECTIVE_FLAGS_MINUS);