@@ -340,10 +340,10 @@ limitations under the License.
340
340
else if c == ' ' then
341
341
consume(str, j + 1 , v { blank: true })
342
342
else if c == '+' then
343
- consume(str, j + 1 , v { sign : true })
343
+ consume(str, j + 1 , v { plus : true })
344
344
else
345
345
{ i: j, v: v };
346
- consume(str, i, { alt: false , zero: false , left: false , blank: false , sign : false });
346
+ consume(str, i, { alt: false , zero: false , left: false , blank: false , plus : false });
347
347
348
348
local try_parse_field_width(str, i) =
349
349
if i < std.length (str) && str[i] == '*' then
@@ -483,23 +483,38 @@ limitations under the License.
483
483
local pad_right(str, w, s) =
484
484
str + padding(w - std.length (str), s);
485
485
486
- // Render an integer (e.g., decimal or octal).
487
- local render_int(n__, min_chars, min_digits, blank, sign, radix, zero_prefix) =
488
- local n_ = std.abs (n__);
489
- local aux(n) =
490
- if n == 0 then
491
- zero_prefix
486
+ // Render a sign & magnitude integer (radix ranges from decimal to binary).
487
+ // neg should be a boolean, and when true indicates that we should render a negative number.
488
+ // mag must always be a whole number >= 0, it's the magnitude of the integer to render
489
+ // min_chars must be a whole number >= 0
490
+ // It is the field width, i.e. std.length() of the result should be >= min_chars
491
+ // min_digits must be a whole number >= 0. It's the number of zeroes to pad with.
492
+ // blank must be a boolean, if true adds an additional ' ' in front of a positive number, so
493
+ // that it is aligned with negative numbers with the same number of digits.
494
+ // plus must be a boolean, if true adds a '+' in front of a postive number, so that it is
495
+ // aligned with negative numbers with the same number of digits. This takes precedence over
496
+ // blank, if both are true.
497
+ // radix must be a whole number >1 and <= 10. It is the base of the system of numerals.
498
+ // zero_prefix is a string prefixed before the sign to all numbers that are not 0.
499
+ local render_int(neg, mag, min_chars, min_digits, blank, plus, radix, zero_prefix) =
500
+ // dec is the minimal string needed to represent the number as text.
501
+ local dec =
502
+ if mag == 0 then
503
+ '0'
492
504
else
493
- aux(std.floor (n / radix)) + (n % radix);
494
- local dec = if std.floor (n_) == 0 then '0' else aux(std.floor (n_));
495
- local neg = n__ < 0 ;
496
- local zp = min_chars - (if neg || blank || sign then 1 else 0 );
505
+ local aux(n) =
506
+ if n == 0 then
507
+ zero_prefix
508
+ else
509
+ aux(std.floor (n / radix)) + (n % radix);
510
+ aux(mag);
511
+ local zp = min_chars - (if neg || blank || plus then 1 else 0 );
497
512
local zp2 = std.max (zp, min_digits);
498
513
local dec2 = pad_left(dec, zp2, '0' );
499
- (if neg then '-' else if sign then '+' else if blank then ' ' else '' ) + dec2;
514
+ (if neg then '-' else if plus then '+' else if blank then ' ' else '' ) + dec2;
500
515
501
516
// Render an integer in hexadecimal.
502
- local render_hex(n__, min_chars, min_digits, blank, sign , add_zerox, capitals) =
517
+ local render_hex(n__, min_chars, min_digits, blank, plus , add_zerox, capitals) =
503
518
local numerals = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
504
519
+ if capitals then ['A' , 'B' , 'C' , 'D' , 'E' , 'F' ]
505
520
else ['a' , 'b' , 'c' , 'd' , 'e' , 'f' ];
@@ -511,12 +526,12 @@ limitations under the License.
511
526
aux(std.floor (n / 16 )) + numerals[n % 16 ];
512
527
local hex = if std.floor (n_) == 0 then '0' else aux(std.floor (n_));
513
528
local neg = n__ < 0 ;
514
- local zp = min_chars - (if neg || blank || sign then 1 else 0 )
529
+ local zp = min_chars - (if neg || blank || plus then 1 else 0 )
515
530
- (if add_zerox then 2 else 0 );
516
531
local zp2 = std.max (zp, min_digits);
517
532
local hex2 = (if add_zerox then (if capitals then '0X' else '0x' ) else '' )
518
533
+ pad_left(hex, zp2, '0' );
519
- (if neg then '-' else if sign then '+' else if blank then ' ' else '' ) + hex2;
534
+ (if neg then '-' else if plus then '+' else if blank then ' ' else '' ) + hex2;
520
535
521
536
local strip_trailing_zero(str) =
522
537
local aux(str, i) =
@@ -530,35 +545,35 @@ limitations under the License.
530
545
aux(str, std.length (str) - 1 );
531
546
532
547
// Render floating point in decimal form
533
- local render_float_dec(n__, zero_pad, blank, sign , ensure_pt, trailing, prec) =
548
+ local render_float_dec(n__, zero_pad, blank, plus , ensure_pt, trailing, prec) =
534
549
local n_ = std.abs (n__);
535
550
local whole = std.floor (n_);
536
551
local dot_size = if prec == 0 && !ensure_pt then 0 else 1 ;
537
552
local zp = zero_pad - prec - dot_size;
538
- local str = render_int(std.sign ( n__) * whole, zp, 0 , blank, sign , 10 , '' );
553
+ local str = render_int(n__ < 0 , whole, zp, 0 , blank, plus , 10 , '' );
539
554
if prec == 0 then
540
555
str + if ensure_pt then '.' else ''
541
556
else
542
557
local frac = std.floor ((n_ - whole) * std.pow (10 , prec) + 0.5 );
543
558
if trailing || frac > 0 then
544
- local frac_str = render_int(frac, prec, 0 , false , false , 10 , '' );
559
+ local frac_str = render_int(false , frac, prec, 0 , false , false , 10 , '' );
545
560
str + '.' + if !trailing then strip_trailing_zero(frac_str) else frac_str
546
561
else
547
562
str;
548
563
549
564
// Render floating point in scientific form
550
- local render_float_sci(n__, zero_pad, blank, sign , ensure_pt, trailing, caps, prec) =
565
+ local render_float_sci(n__, zero_pad, blank, plus , ensure_pt, trailing, caps, prec) =
551
566
local exponent = if n__ == 0 then 0 else std.floor (std.log (std.abs (n__)) / std.log (10 ));
552
567
local suff = (if caps then 'E' else 'e' )
553
- + render_int(exponent, 3 , 0 , false , true , 10 , '' );
568
+ + render_int(exponent < 0 , std.abs (exponent) , 3 , 0 , false , true , 10 , '' );
554
569
local mantissa = if exponent == -324 then
555
570
// Avoid a rounding error where std.pow(10, -324) is 0
556
571
// -324 is the smallest exponent possible.
557
572
n__ * 10 / std.pow (10 , exponent + 1 )
558
573
else
559
574
n__ / std.pow (10 , exponent);
560
575
local zp2 = zero_pad - std.length (suff);
561
- render_float_dec(mantissa, zp2, blank, sign , ensure_pt, trailing, prec) + suff;
576
+ render_float_dec(mantissa, zp2, blank, plus , ensure_pt, trailing, prec) + suff;
562
577
563
578
// Render a value with an arbitrary format code.
564
579
local format_code(val, code, fw, prec_or_null, i) =
@@ -573,24 +588,24 @@ limitations under the License.
573
588
error 'Format required number at '
574
589
+ i + ', got ' + std.type (val)
575
590
else
576
- render_int(val, zp, iprec, cflags.blank, cflags.sign , 10 , '' )
591
+ render_int(val <= - 1 , std.floor ( std.abs (val)), zp, iprec, cflags.blank, cflags.plus , 10 , '' )
577
592
else if code.ctype == 'o' then
578
593
if std.type (val) != 'number' then
579
594
error 'Format required number at '
580
595
+ i + ', got ' + std.type (val)
581
596
else
582
597
local zero_prefix = if cflags.alt then '0' else '' ;
583
- render_int(val, zp, iprec, cflags.blank, cflags.sign , 8 , zero_prefix)
598
+ render_int(val <= - 1 , std.floor ( std.abs (val)), zp, iprec, cflags.blank, cflags.plus , 8 , zero_prefix)
584
599
else if code.ctype == 'x' then
585
600
if std.type (val) != 'number' then
586
601
error 'Format required number at '
587
602
+ i + ', got ' + std.type (val)
588
603
else
589
- render_hex(val,
604
+ render_hex(std.floor ( val) ,
590
605
zp,
591
606
iprec,
592
607
cflags.blank,
593
- cflags.sign ,
608
+ cflags.plus ,
594
609
cflags.alt,
595
610
code.caps)
596
611
else if code.ctype == 'f' then
@@ -601,7 +616,7 @@ limitations under the License.
601
616
render_float_dec(val,
602
617
zp,
603
618
cflags.blank,
604
- cflags.sign ,
619
+ cflags.plus ,
605
620
cflags.alt,
606
621
true ,
607
622
fpprec)
@@ -613,7 +628,7 @@ limitations under the License.
613
628
render_float_sci(val,
614
629
zp,
615
630
cflags.blank,
616
- cflags.sign ,
631
+ cflags.plus ,
617
632
cflags.alt,
618
633
true ,
619
634
code.caps,
@@ -628,7 +643,7 @@ limitations under the License.
628
643
render_float_sci(val,
629
644
zp,
630
645
cflags.blank,
631
- cflags.sign ,
646
+ cflags.plus ,
632
647
cflags.alt,
633
648
cflags.alt,
634
649
code.caps,
@@ -638,7 +653,7 @@ limitations under the License.
638
653
render_float_dec(val,
639
654
zp,
640
655
cflags.blank,
641
- cflags.sign ,
656
+ cflags.plus ,
642
657
cflags.alt,
643
658
cflags.alt,
644
659
fpprec - digits_before_pt)
0 commit comments