forked from baldurk/renderdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf8printf.cpp
More file actions
1395 lines (1203 loc) · 39.9 KB
/
utf8printf.cpp
File metadata and controls
1395 lines (1203 loc) · 39.9 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
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014-2017 Baldur Karlsson
*
* 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.
******************************************************************************/
#include "common/common.h"
// grisu2 double-to-string function, returns number of digits written to digits array
int grisu2(uint64_t mantissa, int exponent, char digits[18], int &kout);
///////////////////////////////////////////////////////////////////////////////
// functions for appending to output (handling running out of buffer space)
void addchar(char *&output, size_t &actualsize, char *end, char c)
{
actualsize++;
if(output == end)
return;
*(output++) = c;
}
void addchars(char *&output, size_t &actualsize, char *end, size_t num, char c)
{
actualsize += num;
for(size_t i = 0; output != end && i < num; i++)
*(output++) = c;
}
void appendstring(char *&output, size_t &actualsize, char *end, const char *str, size_t len)
{
for(size_t i = 0; i < len; i++)
{
if(str[i] == 0)
return;
actualsize++;
if(output != end)
*(output++) = str[i];
}
}
void appendstring(char *&output, size_t &actualsize, char *end, const char *str)
{
for(size_t i = 0; *str; i++)
{
actualsize++;
if(output != end)
*(output++) = *str;
str++;
}
}
///////////////////////////////////////////////////////////////////////////////
// Flags and general formatting parameters
enum FormatterFlags
{
LeftJustify = 0x1,
PrependPos = 0x2,
PrependSpace = 0x4,
AlternateForm = 0x8,
PadZeroes = 0x10,
// non standard
AlwaysDecimal = 0x20,
};
enum LengthModifier
{
None,
HalfHalf,
Half,
Long,
LongLong,
SizeT,
};
struct FormatterParams
{
FormatterParams() : Flags(0), Width(NoWidth), Precision(NoPrecision), Length(None) {}
int Flags;
int Width;
int Precision;
LengthModifier Length;
static const int NoWidth = -1; // can't set negative width, so -1 indicates no width specified
static const int NoPrecision =
-1; // can't set negative precision, so -1 indicates no precision specified
};
///////////////////////////////////////////////////////////////////////////////
// Print a number in a specified base (16, 8, 10 or 2 supported)
void PrintInteger(bool typeUnsigned, uint64_t argu, int base, uint64_t numbits,
FormatterParams formatter, bool uppercaseDigits, char *&output,
size_t &actualsize, char *end)
{
int64_t argi = 0;
union
{
uint64_t *u64;
signed int *i;
signed char *c;
signed short *s;
int64_t *i64;
} typepun;
typepun.u64 = &argu;
// cast the appropriate size to signed version
switch(formatter.Length)
{
default:
case None:
case Long: argi = (int64_t)*typepun.i; break;
case HalfHalf: argi = (int64_t)*typepun.c; break;
case Half: argi = (int64_t)*typepun.s; break;
case LongLong: argi = (int64_t)*typepun.i64; break;
}
bool negative = false;
if(base == 10 && !typeUnsigned)
{
negative = argi < 0;
}
int digwidth = 0;
int numPad0s = 0;
int numPadWidth = 0;
{
int intwidth = 0;
int digits = 0;
// work out the number of decimal digits in the integer
if(!negative)
{
uint64_t accum = argu;
while(accum)
{
digits += 1;
accum /= base;
}
}
else
{
int64_t accum = argi;
while(accum)
{
digits += 1;
accum /= base;
}
}
intwidth = digwidth = RDCMAX(1, digits);
// printed int is 2 chars larger for 0x or 0b, and 1 char for 0 (octal)
if(base == 16 || base == 2)
intwidth += formatter.Flags & AlternateForm ? 2 : 0;
if(base == 8)
intwidth += formatter.Flags & AlternateForm ? 1 : 0;
if(formatter.Precision != FormatterParams::NoPrecision && formatter.Precision > intwidth)
numPad0s = formatter.Precision - intwidth;
intwidth += numPad0s;
// for decimal we can have a negative sign (or placeholder)
if(base == 10)
{
if(negative)
intwidth++;
else if(formatter.Flags & (PrependPos | PrependSpace))
intwidth++;
}
if(formatter.Width != FormatterParams::NoWidth && formatter.Width > intwidth)
numPadWidth = formatter.Width - intwidth;
}
// pad with spaces if necessary
if((formatter.Flags & (LeftJustify | PadZeroes)) == 0 && numPadWidth > 0)
addchars(output, actualsize, end, (size_t)numPadWidth, ' ');
if(base == 16)
{
if(formatter.Flags & AlternateForm)
{
if(uppercaseDigits)
appendstring(output, actualsize, end, "0X");
else
appendstring(output, actualsize, end, "0x");
}
// pad with 0s as appropriate
if((formatter.Flags & (LeftJustify | PadZeroes)) == PadZeroes && numPadWidth > 0)
addchars(output, actualsize, end, (size_t)numPadWidth, '0');
if(numPad0s > 0)
addchars(output, actualsize, end, (size_t)numPad0s, '0');
bool left0s = true;
// mask off each hex digit and print
for(uint64_t i = 0; i < numbits; i += 4)
{
uint64_t shift = numbits - 4 - i;
uint64_t mask = 0xfULL << shift;
char digit = char((argu & mask) >> shift);
if(digit == 0 && left0s && i + 4 < numbits)
continue;
left0s = false;
if(digit < 10)
addchar(output, actualsize, end, '0' + digit);
else if(uppercaseDigits)
addchar(output, actualsize, end, 'A' + digit - 10);
else
addchar(output, actualsize, end, 'a' + digit - 10);
}
}
else if(base == 8)
{
if(formatter.Flags & AlternateForm)
appendstring(output, actualsize, end, "0");
if((formatter.Flags & (LeftJustify | PadZeroes)) == PadZeroes && numPadWidth > 0)
addchars(output, actualsize, end, (size_t)numPadWidth, '0');
if(numPad0s > 0)
addchars(output, actualsize, end, (size_t)numPad0s, '0');
// octal digits don't quite fit into typical integer sizes,
// so instead we pretend the number is a little bigger, then
// the shift just fills out the upper bits with 0s.
uint64_t offs = 0;
if(numbits % 3 == 1)
offs = 2;
if(numbits % 3 == 2)
offs = 1;
bool left0s = true;
for(uint64_t i = 0; i < numbits; i += 3)
{
uint64_t shift = numbits - 3 - i + offs;
uint64_t mask = 0x7ULL << shift;
char digit = char((argu & mask) >> shift);
if(digit == 0 && left0s && i + 3 < numbits)
continue;
left0s = false;
addchar(output, actualsize, end, '0' + digit);
}
}
else if(base == 2)
{
if(formatter.Flags & AlternateForm)
{
if(uppercaseDigits)
appendstring(output, actualsize, end, "0B");
else
appendstring(output, actualsize, end, "0b");
}
if((formatter.Flags & (LeftJustify | PadZeroes)) == PadZeroes && numPadWidth > 0)
addchars(output, actualsize, end, (size_t)numPadWidth, '0');
if(numPad0s > 0)
addchars(output, actualsize, end, (size_t)numPad0s, '0');
bool left0s = true;
for(uint64_t i = 0; i < numbits; i++)
{
uint64_t shift = numbits - 1 - i;
uint64_t mask = 0x1ULL << shift;
char digit = char((argu & mask) >> shift);
if(digit == 0 && left0s && i + 1 < numbits)
continue;
left0s = false;
addchar(output, actualsize, end, '0' + digit);
}
}
else
{
// buffer large enough for any int (up to 64bit unsigned)
char intbuf[32] = {0};
// handle edge case of INT_MIN so we can negate the number and be sure we
// won't actualsize
if(argu == 0x8000000000000000)
{
addchar(output, actualsize, end, '-');
if((formatter.Flags & (LeftJustify | PadZeroes)) == PadZeroes && numPadWidth > 0)
addchars(output, actualsize, end, (size_t)numPadWidth, '0');
if(numPad0s > 0)
addchars(output, actualsize, end, (size_t)numPad0s, '0');
appendstring(output, actualsize, end, "9223372036854775808");
}
else
{
// we know we can negate without loss of precision because we handled 64bit INT_MIN above
if(negative)
{
addchar(output, actualsize, end, '-');
argi = -argi;
}
else if(formatter.Flags & PrependPos)
addchar(output, actualsize, end, '+');
else if(formatter.Flags & PrependSpace)
addchar(output, actualsize, end, ' ');
if((formatter.Flags & (LeftJustify | PadZeroes)) == PadZeroes && numPadWidth > 0)
addchars(output, actualsize, end, (size_t)numPadWidth, '0');
if(numPad0s > 0)
addchars(output, actualsize, end, (size_t)numPad0s, '0');
if(typeUnsigned)
{
uint64_t accum = argu;
for(int i = 0; i < digwidth; i++)
{
int digit = accum % 10;
accum /= 10;
intbuf[digwidth - 1 - i] = char('0' + digit);
}
}
else
{
int64_t accum = argi;
for(int i = 0; i < digwidth; i++)
{
int digit = accum % 10;
accum /= 10;
intbuf[digwidth - 1 - i] = char('0' + digit);
}
}
char *istr = intbuf;
while(*istr == '0')
istr++;
if(*istr == 0 && istr > intbuf)
istr--;
appendstring(output, actualsize, end, istr);
}
}
// if we were left justifying, pad on the right with spaces
if((formatter.Flags & LeftJustify) && numPadWidth > 0)
{
addchars(output, actualsize, end, (size_t)numPadWidth, ' ');
}
}
void PrintFloat0(bool e, bool f, FormatterParams formatter, char prepend, char *&output,
size_t &actualsize, char *end)
{
int numwidth = 0;
if(e)
numwidth = formatter.Precision + 1 + 5; // 0 plus precision plus e+000
else if(f || formatter.Flags & AlternateForm)
numwidth = formatter.Precision + 1; // 0 plus precision
else
numwidth = 1;
// alternate form means . is included even if no digits after .
if(((e || f) && formatter.Precision > 0) || (formatter.Flags & AlternateForm))
numwidth++; // .
if(!e && !f && (formatter.Flags & AlwaysDecimal))
{
numwidth += 2; // .0
}
// sign space
if(prepend)
numwidth++;
int padlen = 0;
if(formatter.Width != FormatterParams::NoWidth && formatter.Width > numwidth)
padlen = formatter.Width - numwidth;
if(formatter.Flags & PadZeroes)
{
if(prepend)
addchar(output, actualsize, end, prepend);
addchars(output, actualsize, end, size_t(padlen), '0');
}
else if(padlen > 0 && (formatter.Flags & LeftJustify) == 0)
{
addchars(output, actualsize, end, size_t(padlen), ' ');
if(prepend)
addchar(output, actualsize, end, prepend);
}
else
{
if(prepend)
addchar(output, actualsize, end, prepend);
}
// print a .0 for all cases except non-alternate %g
if(e || f || formatter.Flags & AlternateForm)
{
addchar(output, actualsize, end, '0');
if(formatter.Precision > 0 || (formatter.Flags & AlternateForm))
addchar(output, actualsize, end, '.');
addchars(output, actualsize, end, size_t(formatter.Precision), '0');
if(e)
appendstring(output, actualsize, end, "e+000");
}
else
{
addchar(output, actualsize, end, '0');
if(!e && !f && (formatter.Flags & AlwaysDecimal))
{
addchar(output, actualsize, end, '.');
addchar(output, actualsize, end, '0');
}
}
if(padlen > 0 && (formatter.Flags & LeftJustify))
{
addchars(output, actualsize, end, size_t(padlen), ' ');
}
}
void PrintFloat(double argd, FormatterParams &formatter, bool e, bool f, bool g,
bool uppercaseDigits, char *&output, size_t &actualsize, char *end)
{
// extract the pieces out of the double
uint64_t *arg64 = (uint64_t *)&argd;
bool signbit = (*arg64 & 0x8000000000000000) ? true : false;
uint64_t rawexp = (*arg64 & 0x7ff0000000000000) >> 52;
int exponent = int(rawexp) - 1023;
uint64_t mantissa = (*arg64 & 0x000fffffffffffff);
char prepend = '\0';
if(signbit)
prepend = '-';
else if(formatter.Flags & PrependPos)
prepend = '+';
else if(formatter.Flags & PrependSpace)
prepend = ' ';
// special-case handling of printing 0
if(rawexp == 0 && mantissa == 0)
{
PrintFloat0(e, f, formatter, prepend, output, actualsize, end);
}
// handle 'special' values, inf and nan
else if(rawexp == 0x7ff)
{
if(mantissa == 0)
{
if(signbit)
appendstring(output, actualsize, end, uppercaseDigits ? "-INF" : "-inf");
else
appendstring(output, actualsize, end, uppercaseDigits ? "+INF" : "-inf");
}
else
{
appendstring(output, actualsize, end, uppercaseDigits ? "NAN" : "nan");
}
}
else
{
// call out to grisu2 to generate digits + exponent
char digits[18] = {0};
int K = 0;
int ndigits = grisu2(mantissa, exponent, digits, K);
// this is the decimal exponent (ie. 0 if the digits are 1.2345)
int expon = K + ndigits - 1;
// number of digits after the decimal
int decdigits = ndigits - expon - 1;
// for exponential form, this is always 1 less than the total number of digits
if(e)
decdigits = RDCMAX(0, ndigits - 1);
// see if we need to trim some digits (for %g, the precision is the number of
// significant figures which is just ndigits at the moment, will be padded with 0s
// later).
if(decdigits > formatter.Precision || (g && ndigits > formatter.Precision))
{
int removedigs = decdigits - formatter.Precision;
if(g)
removedigs = RDCMAX(0, ndigits - formatter.Precision);
// if we're removing all digits, just check the first to see if it should be
// rounded up or down
if(removedigs == ndigits)
{
ndigits = 1;
if(digits[0] < '5')
{
digits[0] = '0';
}
else
{
// round up to "1" on the next exponent
digits[0] = '1';
expon++;
}
}
else
{
// remove the specified number of digits
ndigits -= removedigs;
// round up the last digit (continually rolling up if necessary)
// note this will look 'ahead' into the last removed digits at first
bool carry = true;
for(int i = ndigits - 1; i >= 0; i--)
{
// should we round up?
if(digits[i + 1] >= '5')
{
digits[i + 1] = 0;
// unless current digit is a 9, we can just increment it and stop
if(digits[i] < '9')
{
digits[i]++;
carry = false;
break;
}
// continue (carry to next digit)
}
else
{
// didn't need to round up, everything's fine.
carry = false;
break;
}
// trim off a digit (was a 9)
ndigits--;
continue;
}
// we only get here with carry still true if digits are 9999999
if(carry)
{
// round up to "1" on the next exponent
ndigits = 1;
digits[0] = '1';
expon++;
}
}
}
// recalculate decimal digits with new ndigits
decdigits = ndigits - expon - 1;
if(e)
decdigits = RDCMAX(0, ndigits - 1);
// number of trailing 0s we need to pad after decimal point determined by
// the precision
int padtrailing0s = formatter.Precision - RDCMAX(0, decdigits);
if(g)
{
// for %g if the exponent is too far out of range, we revert to exponential form
if(expon >= formatter.Precision || expon < -4)
{
e = true;
// if not alternate form, all trailing 0 digits are removed and there is no padding.
if((formatter.Flags & AlternateForm) == 0)
{
while(ndigits > 1 && digits[ndigits - 1] == '0')
ndigits--;
padtrailing0s = 0;
}
else
padtrailing0s = formatter.Precision - RDCMAX(0, ndigits);
}
else
{
padtrailing0s = formatter.Precision - RDCMAX(0, ndigits);
}
}
// exponential display
if(e)
{
int numwidth = 0;
// first calculate the width of the produced output, so we can calculate any padding
numwidth = ndigits; // digits
if(ndigits > 1 || (formatter.Flags & AlternateForm) || padtrailing0s > 0)
numwidth++; // '.'
numwidth += padtrailing0s;
numwidth += 2; // 'e+' or 'e-'
if(expon >= 1000 || expon <= -1000)
numwidth += 4;
else
numwidth += 3;
if(prepend)
numwidth++; // +, - or ' '
int padlen = 0;
if(formatter.Width != FormatterParams::NoWidth && formatter.Width > numwidth)
padlen = formatter.Width - numwidth;
// pad with 0s or ' 's and insert the sign character
if(formatter.Flags & PadZeroes)
{
if(prepend)
addchar(output, actualsize, end, prepend);
addchars(output, actualsize, end, size_t(padlen), '0');
}
else if(padlen > 0 && (formatter.Flags & LeftJustify) == 0)
{
addchars(output, actualsize, end, size_t(padlen), ' ');
if(prepend)
addchar(output, actualsize, end, prepend);
}
else
{
if(prepend)
addchar(output, actualsize, end, prepend);
}
// insert the mantissa as a 1.23456 decimal
addchar(output, actualsize, end, digits[0]);
if(ndigits > 1 || (formatter.Flags & AlternateForm) || padtrailing0s > 0)
addchar(output, actualsize, end, '.');
for(int i = 1; i < ndigits; i++)
addchar(output, actualsize, end, digits[i]);
// add the trailing 0s here
if(padtrailing0s > 0)
addchars(output, actualsize, end, size_t(padtrailing0s), '0');
// print the e-XXX exponential
addchar(output, actualsize, end, uppercaseDigits ? 'E' : 'e');
if(expon >= 0)
addchar(output, actualsize, end, '+');
else
addchar(output, actualsize, end, '-');
int exponaccum = expon >= 0 ? expon : -expon;
if(exponaccum >= 1000)
addchar(output, actualsize, end, '0' + char(exponaccum / 1000));
exponaccum %= 1000;
addchar(output, actualsize, end, '0' + char(exponaccum / 100));
exponaccum %= 100;
addchar(output, actualsize, end, '0' + char(exponaccum / 10));
exponaccum %= 10;
addchar(output, actualsize, end, '0' + char(exponaccum));
if(padlen > 0 && (formatter.Flags & LeftJustify))
{
addchars(output, actualsize, end, size_t(padlen), ' ');
}
}
else if(digits[0] == '0' && ndigits == 1)
{
// if we rounded off to a 0.0, print it with special handling
PrintFloat0(e, f, formatter, prepend, output, actualsize, end);
}
else
{
// we're printing as a normal decimal, e.g. 12345.6789
// if %g and not in alternate form, all 0s after the decimal point are stripped
if(g && (formatter.Flags & AlternateForm) == 0)
while(ndigits > 1 && ndigits - 1 > expon && digits[ndigits - 1] == '0')
ndigits--;
int numwidth = 0;
// first calculate the width of the produced output, so we can calculate any padding
// always all digits are printed (after trailing 0s optionally removed above)
numwidth = ndigits;
if(prepend)
numwidth++; // prefix +, - or ' '
// if the exponent is exactly the number of digits we have, we have one 0 to pad
// before the decimal point, and special handling of whether to display the decimal
// point for %g. (note that exponent 0 is mantissa x 10^0 which is 1.2345
if(expon == ndigits)
{
numwidth++; // 0 before decimal place
// if in alternate form for %g we print a . and any trailing 0s necessary to make
// up the precision (number of significant figures)
if(g && (formatter.Flags & AlternateForm))
{
numwidth++; // .
if(padtrailing0s > 1)
numwidth += (padtrailing0s - 1);
}
else if(!g)
{
// otherwise we only print the . if alternate form is specified or we need to
// print trailing 0s
if(padtrailing0s > 0 || (formatter.Flags & AlternateForm))
numwidth++; // .
if(padtrailing0s > 0)
numwidth += padtrailing0s;
}
}
// exponent greater than ndigits means we have padding before the decimal place
// and no values after the decimal place
else if(expon > ndigits)
{
numwidth += (expon + 1 - ndigits); // 0s between digits and decimal place
if((!g || (formatter.Flags & AlternateForm)))
numwidth++; // .
if(padtrailing0s > 0 && (!g || (formatter.Flags & AlternateForm)))
numwidth += padtrailing0s;
}
else if(expon >= 0)
{
// expon < ndigits is true here
if(expon < ndigits - 1 || !g || (formatter.Flags & AlternateForm))
numwidth++; // .
if(g && (formatter.Flags & AlwaysDecimal))
numwidth += 2; // .0
if(padtrailing0s > 0 && (!g || (formatter.Flags & AlternateForm)))
numwidth += padtrailing0s;
}
else // if(expon < 0)
{
numwidth += 2; // 0.;
numwidth += (-1 - expon); // 0s before digits
if(!g || (formatter.Flags & AlternateForm))
numwidth += padtrailing0s;
}
int padlen = 0;
// calculate padding and print it (0s or ' 's) with the sign character
if(formatter.Width != FormatterParams::NoWidth && formatter.Width > numwidth)
padlen = formatter.Width - numwidth;
if(formatter.Flags & PadZeroes)
{
if(prepend)
addchar(output, actualsize, end, prepend);
addchars(output, actualsize, end, size_t(padlen), '0');
}
else if(padlen > 0 && (formatter.Flags & LeftJustify) == 0)
{
addchars(output, actualsize, end, size_t(padlen), ' ');
if(prepend)
addchar(output, actualsize, end, prepend);
}
else
{
if(prepend)
addchar(output, actualsize, end, prepend);
}
// if the exponent is greater than 0 we have to handle padding,
// placing it correctly, whether to show the decimal place or not, etc
if(expon >= 0)
{
// print the digits, adding the . at the right column, as long as it's not
// after the last column AND we are in %g that's not alternate form (ie.
// trailing 0s and . are stripped)
for(int i = 0; i < ndigits; i++)
{
addchar(output, actualsize, end, digits[i]);
if(i == expon)
{
if(i < ndigits - 1 || !g || (formatter.Flags & AlternateForm))
addchar(output, actualsize, end, '.');
}
}
// handle printing trailing 0s here as well as a trailing. if it
// wasn't printed above, and is needed for the print form.
if(expon == ndigits)
{
addchar(output, actualsize, end, '0');
if(g && (formatter.Flags & AlternateForm))
{
addchar(output, actualsize, end, '.');
if(padtrailing0s > 1)
addchars(output, actualsize, end, size_t(padtrailing0s - 1), '0');
}
else if(!g)
{
if(padtrailing0s > 0 || (formatter.Flags & AlternateForm))
addchar(output, actualsize, end, '.');
if(padtrailing0s > 0)
addchars(output, actualsize, end, size_t(padtrailing0s), '0');
}
else if(g && (formatter.Flags & AlwaysDecimal))
{
addchar(output, actualsize, end, '.');
addchar(output, actualsize, end, '0');
}
}
else if(expon > ndigits)
{
addchars(output, actualsize, end, size_t(expon + 1 - ndigits), '0');
if((!g || (formatter.Flags & AlternateForm)))
addchar(output, actualsize, end, '.');
if(padtrailing0s > 0 && (!g || (formatter.Flags & AlternateForm)))
addchars(output, actualsize, end, size_t(padtrailing0s), '0');
if(g && (formatter.Flags & AlwaysDecimal))
{
addchar(output, actualsize, end, '.');
addchar(output, actualsize, end, '0');
}
}
else
{
if(padtrailing0s > 0 && (!g || (formatter.Flags & AlternateForm)))
addchars(output, actualsize, end, size_t(padtrailing0s), '0');
if(ndigits - 1 <= expon && g && (formatter.Flags & AlwaysDecimal))
{
addchar(output, actualsize, end, '.');
addchar(output, actualsize, end, '0');
}
}
}
// if exponent is less than 0 it's much easier - just print the number as
// digits at the right column, then any trailing 0s necessary
else
{
appendstring(output, actualsize, end, "0.");
addchars(output, actualsize, end, size_t(-1 - expon), '0');
appendstring(output, actualsize, end, digits, size_t(ndigits));
if(padtrailing0s > 0 && (!g || (formatter.Flags & AlternateForm)))
addchars(output, actualsize, end, size_t(padtrailing0s), '0');
}
if(padlen > 0 && (formatter.Flags & LeftJustify))
{
addchars(output, actualsize, end, size_t(padlen), ' ');
}
}
}
}
void formatargument(char type, void *rawarg, FormatterParams formatter, char *&output,
size_t &actualsize, char *end)
{
// print a single character (ascii or wide)
if(type == 'c')
{
int arg = *(int *)rawarg;
// left padding - character is always by definition one space wide
if(formatter.Width != FormatterParams::NoWidth && !(formatter.Flags & LeftJustify))
addchars(output, actualsize, end, (size_t)formatter.Width - 1, ' ');
if(formatter.Length == Long)
{
wchar_t chr = (wchar_t)arg;
// convert single wide character to UTF-8 sequence, at most
// 4 characters
char mbchr[4];
int seqlen = StringFormat::Wide2UTF8(chr, mbchr);
appendstring(output, actualsize, end, mbchr, seqlen);
}
else
{
char chr = (char)arg;
addchar(output, actualsize, end, chr);
}
// right padding
if(formatter.Width != FormatterParams::NoWidth && (formatter.Flags & LeftJustify))
addchars(output, actualsize, end, (size_t)formatter.Width - 1, ' ');
}
else if(type == 's')
{
void *arg = *(void **)rawarg;
if(formatter.Length == Long)
{
const wchar_t *ws = (const wchar_t *)arg;
if(arg == NULL)
ws = L"(null)";
size_t width = (size_t)formatter.Width;
size_t precision = (size_t)formatter.Precision;
size_t len = wcslen(ws);
// clip length to precision
if(formatter.Precision != FormatterParams::NoPrecision)
len = RDCMIN(len, precision);
// convert the substring to UTF-8
string str = StringFormat::Wide2UTF8(std::wstring(ws, ws + len));
// add left padding, if necessary
if(formatter.Width != FormatterParams::NoWidth && len < width &&
!(formatter.Flags & LeftJustify))
addchars(output, actualsize, end, width - len, ' ');
appendstring(output, actualsize, end, str.c_str());
// add right padding
if(formatter.Width != FormatterParams::NoWidth && len < width && (formatter.Flags & LeftJustify))
addchars(output, actualsize, end, width - len, ' ');
}
else
{
const char *s = (const char *)arg;
if(arg == NULL)
s = "(null)";
size_t len = 0;
size_t clipoffs = 0;
size_t width = (size_t)formatter.Width;
size_t precision = (size_t)formatter.Precision;
// iterate through UTF-8 string to find its length (for padding in case
// format width is longer than the string) or where to clip off a substring
// (if the precision is shorter than the string)
const char *si = s;
while(*si)
{
if((*si & 0x80) == 0) // ascii character
{
si++;
}
else if((*si & 0xC0) == 0xC0) // first byte of a sequence
{
si++;
// skip past continuation bytes (if we hit a NULL terminator this loop will break out)
while((*si & 0xC0) == 0x80)
si++;
}
else
{
// invalid UTF-8 byte to encounter, bail out here.
clipoffs = 0;
len = 0;
s = "";
break;
}
len++; // one more codepoint
if(len == precision && formatter.Precision != FormatterParams::NoPrecision)
{