/
Format.cpp
1283 lines (1182 loc) · 26.6 KB
/
Format.cpp
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 "Core.h"
//#BLITZ_APPROVE
namespace Upp {
// Old format ---------------------------
String VFormat(const char *fmt, va_list ptr) {
int limit = 2 * (int)strlen(fmt) + 1024;
if(limit < 1500) {
char buffer[1500];
vsprintf(buffer, fmt, ptr);
va_end(ptr);
int len = (int)strlen(buffer);
ASSERT(len <= limit);
return String(buffer, len);
}
else {
Buffer<char> buffer(limit);
vsprintf(buffer, fmt, ptr);
va_end(ptr);
int len = (int)strlen(buffer);
ASSERT(len <= limit);
return String(buffer, len);
}
}
// Formatting routines ---------------------------
// utoa32, utoa64 inspired by
// https://github.com/miloyip/itoa-benchmark/blob/940542a7770155ee3e9f2777ebc178dc899b43e0/src/branchlut.cpp
// by Milo Yip
namespace utoa_private {
const char s100[] =
"00010203040506070809"
"10111213141516171819"
"20212223242526272829"
"30313233343536373839"
"40414243444546474849"
"50515253545556575859"
"60616263646566676869"
"70717273747576777879"
"80818283848586878889"
"90919293949596979899"
;
force_inline
void Do2(char *t, dword d) {
#ifdef CPU_UNALIGNED
*(word *)t = *((word *)s100 + d);
#else
auto Copy2 = [](char *t, dword d) {
t[0] = s[2 * d];
t[1] = s[2 * d + 1];
};
#endif
};
force_inline
void Do4(char *t, dword value) {
Do2(t, value / 100);
Do2(t + 2, value % 100);
}
force_inline
void Do8(char *t, dword value) {
Do4(t, value / 10000);
Do4(t + 4, value % 10000);
}
};
int utoa32(dword value, char *buffer)
{
using namespace utoa_private;
if (value < 10000) {
if(value < 100) {
if(value < 10) {
*buffer = value + '0';
return 1;
}
Do2(buffer, value % 100);
return 2;
}
if(value < 1000) {
*buffer = value / 100 + '0';
Do2(buffer + 1, value % 100);
return 3;
}
Do4(buffer, value);
return 4;
}
else if (value < 100000000) {
if(value < 10000000) {
if(value < 100000) {
*buffer = value / 10000 + '0';
Do4(buffer + 1, value % 10000);
return 5;
}
if(value < 1000000) {
Do2(buffer, value / 10000);
Do4(buffer + 2, value % 10000);
return 6;
}
*buffer = value / 1000000 + '0';
Do2(buffer + 1, value / 10000 % 100);
Do4(buffer + 3, value % 10000);
return 7;
}
Do8(buffer, value);
return 8;
}
else {
dword a = value / 100000000; // 2 digits
value %= 100000000;
if(a < 10) {
*buffer = a + '0';
Do8(buffer + 1, value);
return 9;
}
Do2(buffer, a);
Do8(buffer + 2, value);
return 10;
}
}
int utoa64(uint64 value, char *buffer)
{
using namespace utoa_private;
if(value <= 0xffffffff)
return utoa32((dword)value, buffer);
if(value < (uint64)1000000000 * 100000000) {
int q = utoa32(value / 100000000, buffer);
Do8(buffer + q, value % 100000000);
return q + 8;
}
int q = utoa32(value / ((uint64)100000000 * 100000000), buffer);
Do8(buffer + q, value / 100000000 % 100000000);
Do8(buffer + 8 + q, value % 100000000);
return q + 16;
}
String FormatUInt64(uint64 w)
{
if(w < 100000000000000)
return String::MakeSmall([&](char *s) { return utoa64(w, s); });
char h[32];
return String(h, utoa64(w, h));
}
String FormatInt64(int64 i)
{
if(IsNull(i))
return String();
if(i < 0) {
i = -i;
if(i < 10000000000000) {
return String::MakeSmall([&](char *s) {
*s++ = '-';
return utoa64(i, s) + 1;
});
}
char h[32];
*h = '-';
return String(h, utoa64(i, h + 1) + 1);
}
if(i < 100000000000000) {
return String::MakeSmall([&](char *s) { return utoa64(i, s); });
}
char h[32];
return String(h, utoa64(i, h));
}
String FormatIntBase(int i, int base, int width, char lpad, int sign, bool upper)
{
enum { BUFFER = sizeof(int) * 8 + 1 };
if(base < 2 || base > 36)
return "<invalid base>";
char buffer[BUFFER];
char *const e = buffer + (int)BUFFER;
char *p = e;
const char *itoc = upper ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" : "0123456789abcdefghijklmnopqrstuvwxyz";
if(sign < 0 || !IsNull(i))
{
unsigned x = i;
if(sign >= 0 && i < 0)
x = -i;
do
*--p = itoc[x % base];
while(x /= base);
}
bool minus = (sign >= 0 && i < 0 && !IsNull(i));
bool do_sign = (sign > 0 || sign >= 0 && minus);
if(do_sign && lpad != '0')
*--p = (minus ? '-' : '+');
if(width > e - p)
{
char *b = e - min<int>(width, BUFFER);
while(p > b)
*--p = lpad;
}
if(do_sign && lpad == '0')
*--p = (minus ? '-' : '+');
int dwd = (int)(e - p);
int pad = (width = max(width, dwd)) - dwd;
StringBuffer out(width);
char *o = out;
if(dwd < width)
memset(o, lpad, pad);
memcpy8(o + pad, p, dwd);
return String(out);
}
String FormatIntDec(int i, int width, char lpad, bool always_sign)
{
return FormatIntBase(i, 10, width, lpad, always_sign ? 1 : 0);
}
String FormatIntHex(int i, int width, char lpad)
{
return FormatIntBase(i, 16, width, lpad, -1);
}
String FormatIntHexUpper(int i, int width, char lpad)
{
return FormatIntBase(i, 16, width, lpad, -1, true);
}
String FormatIntOct(int i, int width, char lpad)
{
return FormatIntBase(i, 8, width, lpad, -1);
}
String FormatIntAlpha(int i, bool upper)
{
if(IsNull(i) || i == 0)
return Null;
String out;
if(i < 0)
{
out << '-';
i = -i;
}
char temp[10], *p = temp + 10;
const char *itoc = upper ? "ZABCDEFGHIJKLMNOPQRSTUVWXYZ" : "zabcdefghijklmnopqrstuvwxyz";
do
*--p = itoc[i-- % 26];
while(i /= 26);
out.Cat(p, temp + 10);
return out;
}
String FormatIntRoman(int i, bool upper)
{
if(IsNull(i) || i == 0)
return Null;
String out;
if(i < 0)
{
out << '-';
i = -i;
}
int m = i / 1000;
if(m)
{
out.Cat('M', m);
i -= 1000 * m;
}
char shift = upper ? 0 : 'a' - 'A';
static const int value[] = { 1000, 500, 100, 50, 10, 5, 1 };
static const char letter[] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
for(int n = 0; i && n < __countof(value); n++)
{
int v = value[n];
while(i >= v)
{
out << (char)(letter[n] + shift);
i -= v;
}
if(n < __countof(value) - 1)
for(int j = n + (value[n + 1] * 2 >= v ? 2 : 1); j < __countof(value); j++)
if(i >= v - value[j])
{ // subtraction scheme
out << (char)(letter[j] + shift) << (char)(letter[n] + shift);
i -= v - value[j];
break;
}
}
return out;
}
String Format64Hex(uint64 a)
{
char b[50];
char *p = b + 50;
do {
*--p = "0123456789abcdef"[a & 15];
a >>= 4;
}
while(a);
return String(p, b + 50);
}
String FormatDouble(double a) { return IsNull(a) ? String() : IsNaN(a) || IsInf(a) ? "?" : FormatDouble(a, 17, FD_REL); }
String FormatBool(bool a) { return a ? "true" : "false"; }
String FormatPtr(const void *p) { return "0x" + FormatHex(p); }
static char *PutDigits(char *out, unsigned number, int count)
{
char temp[10];
char *t = temp;
do
*t++ = number % 10 + '0';
while(number /= 10);
ASSERT(t - temp <= __countof(temp));
if((count -= (int)(t - temp)) > 0)
{
char *e = out + count;
while(out < e)
*out++ = '0';
}
while(t > temp)
*out++ = *--t;
return out;
}
enum { DBL_DIGITS = (int)(DBL_MANT_DIG * M_LN2 / M_LN10) + 2 };
String FormatDoubleDigits(double d, int raw_digits, int flags, int& exponent)
{
if(IsNull(d))
{
exponent = Null;
return Null;
}
d = fabs(normalize(d, exponent));
if(IsNull(exponent))
return "0";
int digits = raw_digits + (flags & FD_REL ? 0 : exponent + 1);
int count = minmax<int>(digits, 1, DBL_DIGITS - 2);
Buffer<char> buffer(max(digits, 0) + 10);
char *p = buffer;
if(count == 0)
d /= 10;
else
{
static double powtbl[] = { 1e0, 1e1, 1e2, 1e3, 1e4 };
int part1 = min(count, 5);
unsigned i = (unsigned)(d * powtbl[part1 - 1]);
p = PutDigits(p, i, 0);
d = double((long double)d * (long double)powtbl[part1 - 1] - i);
count -= part1;
while(count >= 4)
{
i = minmax<unsigned>(0, (unsigned)(d * 10000), 9999);
p = PutDigits(p, i, 4);
d = double((long double)d * (long double)10000 - i);
count -= 4;
}
if(count > 0)
{
i = (unsigned)(d * powtbl[count]);
p = PutDigits(p, i, count);
d = d * powtbl[count] - i;
}
}
if(p - ~buffer < DBL_DIGITS && d >= 0.5)
{ // round
while(p > ~buffer && p[-1] == '9')
p--;
if(p == ~buffer)
{
*p++ = '1';
exponent++;
if(!(flags & FD_REL))
digits++;
}
else
p[-1]++;
}
if(flags & FD_ZEROS)
{
char *e = p;
while(e > ~buffer && e[-1] == '0')
e--;
if(e == buffer)
exponent = Null;
e = ~buffer + digits;
while(p < e)
*p++ = '0';
}
else
{
while(p > ~buffer && p[-1] == '0')
p--;
if(p == ~buffer)
{
exponent = Null;
*p++ = '0';
}
}
return String(~buffer, (int)(p - ~buffer));
}
String FormatDouble(double d, int digits, int flags, int pad_exp)
{
if(IsNull(d))
return Null;
double ad = fabs(d);
bool is_exp = (flags & FD_EXP);
if(!(flags & FD_FIX))
{
is_exp = ad && (ad <= 1e-15 || ad >= 1e15);
if(flags & FD_REL)
{
double bd = ipow10(2 * digits);
if(ad && (ad * bd <= 1 || ad >= bd))
is_exp = true;
}
}
if(is_exp)
return FormatDoubleExp(d, digits, flags, pad_exp);
else
return FormatDoubleFix(d, digits, flags);
}
String FormatDoubleFix(double d, int digits, int flags)
{
if(IsNull(d))
return Null;
int exp;
String dig = FormatDoubleDigits(d, digits, flags, exp);
if(flags & FD_REL)
digits = max(0, digits - Nvl(exp, 0) - 1);
String out;
if(flags & FD_SIGN || d < 0 && !IsNull(exp))
out.Cat(d >= 0 ? '+' : '-');
int pointchar = (flags & FD_COMMA) ? ',' : '.';
if(IsNull(exp) || exp < -digits) {
out.Cat('0');
if((flags & FD_ZEROS) && digits) {
out.Cat(pointchar);
out.Cat('0', digits);
}
}
else if(exp < 0) {
out.Cat('0');
out.Cat(pointchar);
out.Cat('0', -1 - exp);
int fill = digits + exp + 1;
if(!(flags & FD_ZEROS) || dig.GetLength() >= fill)
out.Cat(dig, min(fill, dig.GetLength()));
else {
out.Cat(dig);
out.Cat('0', fill - dig.GetLength());
}
}
else if(exp < dig.GetLength()) {
out.Cat(dig, ++exp);
if(digits > 0 && ((flags & FD_ZEROS) || dig.GetLength() > exp)) {
out.Cat(pointchar);
if(!(flags & FD_ZEROS) || dig.GetLength() - exp >= digits)
out.Cat(dig.Begin() + exp, min(dig.GetLength() - exp, digits));
else {
out.Cat(dig.Begin() + exp, dig.GetLength() - exp);
out.Cat('0', digits - (dig.GetLength() - exp));
}
}
}
else
{
out.Cat(dig);
out.Cat('0', exp - dig.GetLength() + 1);
if(digits > 0 && (flags & FD_ZEROS))
{
out.Cat(pointchar);
out.Cat('0', digits);
}
}
return out;
}
String FormatDoubleExp(double d, int digits, int flags, int fill_exp)
{
if(IsNull(d))
return Null;
int exp;
int pointchar = (flags & FD_COMMA) ? ',' : '.';
String dig = FormatDoubleDigits(d, digits, flags | FD_REL, exp);
exp = Nvl(exp, 0);
StringBuffer out;
if(flags & FD_SIGN || d < 0 && !IsNull(exp))
out.Cat(d >= 0 ? '+' : '-');
out.Cat(dig[0]);
if(dig.GetLength() > 1)
{
out.Cat(pointchar);
out.Cat(dig.Begin() + 1, dig.GetLength() - 1);
}
out.Cat(flags & FD_CAP_E ? 'E' : 'e');
out.Cat(FormatIntDec(exp, fill_exp, '0', flags & FD_SIGN_EXP));
return String(out);
}
String FormatDate(Date date, const char *format, int language)
{
if(IsNull(date))
return Null;
if(!format || !*format)
return Format(date);
return FormatTime(ToTime(date), format, language);
}
String FormatTime(Time t, const char *s, int language)
{
if(IsNull(t))
return Null;
String result;
if(!s || !*s)
return Format(t);
while(*s) {
int q = 0;
if(*s == 'M') {
while(*s == 'M') { s++; q++; }
if(q == 1)
result.Cat(Format("%d", t.month));
else
result.Cat(Format("%02d", t.month));
}
else
if(*s == 'D') {
while(*s == 'D') { s++; q++; }
if(q == 1)
result.Cat(Format("%d", t.day));
else
result.Cat(Format("%02d", t.day));
}
else
if(*s == 'Y') {
while(*s == 'Y') { s++; q++; }
if(q == 1)
result.Cat(Format("%d", t.year % 100));
else
if(q == 2)
result.Cat(Format("%02d", t.year % 100));
else
result.Cat(Format("%d", t.year));
}
else
if(*s == 'h') {
while(*s == 'h') { s++; q++; }
if(q == 1)
result.Cat(Format("%d", t.hour));
else
result.Cat(Format("%02d", t.hour));
}
else
if(*s == 'H') {
while(*s == 'H') { s++; q++; }
int h = ((t.hour + 11) % 12 + 1);
if(q == 1)
result.Cat(Format("%d", h));
else
result.Cat(Format("%02d", h));
}
else
if(*s == '<') {
s++;
while(*s && *s != '/') {
if(t.hour <= 12)
result.Cat(*s);
s++;
}
if(!*s) break;
s++;
while(*s && *s != '>') {
if(t.hour > 12)
result.Cat(*s);
s++;
}
if(!*s) break;
s++;
}
else
if(*s == 'm') {
while(*s == 'm') { s++; q++; }
if(q == 1)
result.Cat(Format("%d", t.minute));
else
result.Cat(Format("%02d", t.minute));
}
else
if(*s == 's') {
while(*s == 's') { s++; q++; }
if(q == 1)
result.Cat(Format("%d", t.second));
else
result.Cat(Format("%02d", t.second));
}
else
if(*s == '`' && s[1]) {
s++;
result.Cat(*s++);
}
else
result.Cat(*s++);
}
return result;
}
// New format ----------------------------
void TrimChar(String& s, int n)
{
if(GetDefaultCharset() == CHARSET_UTF8) {
WString h(s);
h.Trim(n);
s = h.ToString();
}
else
s.Trim(n);
}
struct FormId : Moveable<FormId> {
FormId(String id, int type) : id(id), type(type) {}
String id;
int type;
};
hash_t GetHashValue(const FormId& fid)
{
return CombineHash(fid.type, GetHashValue(fid.id));
}
bool operator==(const FormId& a, const FormId& b)
{
return a.type == b.type && a.id == b.id;
}
VectorMap<FormId, Formatter>& formatmap()
{
return Single< VectorMap<FormId, Formatter> > ();
}
void RegisterFormatter(int type, const char *id, Formatter f)
{
AssertST();
INTERLOCKED {
FormId fid(id, type);
formatmap().FindAdd(fid, f);
formatmap().Find(fid);
}
}
void RegisterValueFormatter(const char *id, Formatter f)
{
RegisterFormatter(VALUE_V, id, f);
}
void RegisterNullFormatter(const char *id, Formatter f)
{
RegisterFormatter(VOID_V, id, f);
RegisterFormatter(ERROR_V, id, f);
}
void RegisterNumberFormatter(const char *id, Formatter f)
{
RegisterFormatter(DOUBLE_V, id, f);
RegisterFormatter(INT64_V, id, f);
RegisterFormatter(INT_V, id, f);
RegisterFormatter(BOOL_V, id, f);
RegisterNullFormatter(id, f);
}
void RegisterStringFormatter(const char *id, Formatter f)
{
RegisterFormatter(WSTRING_V, id, f);
RegisterFormatter(STRING_V, id, f);
RegisterNullFormatter(id, f);
}
void RegisterDateTimeFormatter(const char *id, Formatter f)
{
RegisterFormatter(TIME_V, id, f);
RegisterFormatter(DATE_V, id, f);
RegisterNullFormatter(id, f);
}
String IntFormatter(const Formatting& f)
{
if(f.format.GetCount() == 0 && f.id[0] == 'd' && f.id[1] == 0)
return AsString((int)f.arg);
StringBuffer q;
q.SetLength(1000);
q.SetLength(sprintf(q, '%' + f.format + f.id, (int)f.arg));
return String(q);
}
String Int64Formatter(const Formatting& f)
{
StringBuffer q;
q.SetLength(1000);
q.SetLength(sprintf(q, '%' + f.format + f.id, (int64)f.arg));
return String(q);
}
String IntLowerAlphaFormatter(const Formatting& f)
{
return FormatIntAlpha(f.arg, false);
}
String IntUpperAlphaFormatter(const Formatting& f)
{
return FormatIntAlpha(f.arg, true);
}
String IntLowerRomanFormatter(const Formatting& f)
{
return FormatIntRoman(f.arg, false);
}
String IntUpperRomanFormatter(const Formatting& f)
{
return FormatIntRoman(f.arg, true);
}
String RealFormatter(const Formatting& f)
{
if(IsNull(f.arg))
return Null;
double value = f.arg;
const char *s = f.format;
int digits = 6, fill_exp = 0;
const char *id = f.id;
int flags = (*id++ == 'v' ? FD_REL : 0);
if(*s == '+') {
flags |= FD_SIGN;
s++;
}
if(IsDigit(*s) || *s == '-' && IsDigit(s[1])) {
digits = (int)strtol(s, NULL, 10);
while(IsDigit(*++s))
;
}
if(*s == '@') { s++; flags |= FD_NOTHSEPS; }
if(*s == ',') { s++; flags |= FD_COMMA; }
if(*s == '!') { s++; flags |= FD_ZEROS; }
if(*s == '^') {
if(*++s == '+') {
flags |= FD_SIGN_EXP;
s++;
}
if(IsDigit(*s)) {
fill_exp = (int)strtol(s, NULL, 10);
while(IsDigit(*++s))
;
}
}
bool lng = false;
if(*id == 'l') {
lng = true;
id++;
}
if(*id == 'e') flags |= FD_EXP;
else if(*id == 'f') flags |= FD_FIX;
if(lng)
return GetLanguageInfo(f.language).FormatDouble(value, digits, flags, fill_exp);
else
return FormatDouble(value, digits, flags, fill_exp);
}
String StringFormatter(const Formatting& f)
{
const String& s = f.arg;
if(f.format.GetCount() == 0 && f.id[0] == 's' && f.id[1] == 0)
return s;
int len = s.GetCharCount();
int width = len;
int precision = len;
bool lpad = false;
CParser p(f.format);
if(p.Char('-'))
lpad = true;
if(p.IsNumber())
width = p.ReadInt();
if(p.Char('.') && p.IsNumber())
precision = p.ReadInt();
// if(precision > len)
// return WString(~s, precision).ToString();
String q = s;
if(precision < len) {
TrimChar(q, precision);
len = precision;
}
String r;
if(lpad)
r.Cat(q);
if(width > len)
r.Cat(' ', width - len);
if(!lpad)
r.Cat(q);
return r;
}
static inline
void sFixPoint(char *s) // We do not want locale to affect decimal point, convert ',' to '.'
{
while(*s) {
if(*s == ',')
*s = '.';
s++;
}
}
String FloatFormatter(const Formatting& f)
{
double d = f.arg;
String fmt = '%' + f.format + f.id;
char h[256];
#ifdef COMPILER_MSC
int n = _snprintf(h, 256, fmt, d);
if(n < 0)
#else
int n = snprintf(h, 255, fmt, d);
if(n >= 254)
#endif
{
#ifdef COMPILER_MSC
n = _scprintf(fmt, d);
#endif
Buffer<char> ah(n + 1);
sprintf(ah, fmt, d);
sFixPoint(ah);
return String(ah, n);
}
if(n < 0)
return String();
sFixPoint(h);
return String(h, n);
}
String DateFormatter(const Formatting& f)
{
return GetLanguageInfo(f.language).FormatDate(f.arg);
}
String TimeFormatter(const Formatting& f)
{
return GetLanguageInfo(f.language).FormatTime(f.arg);
}
String SwitchFormatter(const Formatting& f)
{
const char *s = f.format;
int i = f.arg;
int o = i;
for(;;) {
int n = 0;
while(IsDigit(*s))
n = 10 * n + *s++ - '0';
if(!*s) return Null;
if(*s == '%') {
o = i % max(n, 1);
s++;
}
else
if(*s == ',' || *s == ':') {
if(o == n) {
while(*s && *s != ':')
s++;
if(!*s) return Null;
++s;
const char *b = s;
while(*s && *s != ';')
s++;
return String(b, s);
}
if(*s == ':')
while(*s && *s != ';')
s++;
if(!*s) return Null;
s++;
}
else
return s;
}
return String();
}
String StdFormatFormatter(const Formatting& f)
{
String out = StdFormat(f.arg);
if(!IsNull(out))
return out;
return f.format;
}
String MonthFormatter(const Formatting& f)
{
return MonthName((int)f.arg - 1, f.language);
}
String MONTHFormatter(const Formatting& f)
{
return ToUpper(MonthFormatter(f), GetLNGCharset(f.language));
}
String monthFormatter(const Formatting& f)
{
return ToLower(MonthFormatter(f), GetLNGCharset(f.language));
}
String MonFormatter(const Formatting& f)
{
return MonName((int)f.arg - 1, f.language);
}
String MONFormatter(const Formatting& f)
{
return ToUpper(MonFormatter(f), GetLNGCharset(f.language));
}
String monFormatter(const Formatting& f)
{
return ToLower(MonFormatter(f), GetLNGCharset(f.language));
}
String DayFormatter(const Formatting& f)
{
return DayName((int)f.arg, f.language);
}
String DAYFormatter(const Formatting& f)
{
return ToUpper(DayFormatter(f), GetLNGCharset(f.language));
}
String dayFormatter(const Formatting& f)
{
return ToLower(DayFormatter(f), GetLNGCharset(f.language));
}
String DyFormatter(const Formatting& f)
{
return DyName((int)f.arg, f.language);
}
String DYFormatter(const Formatting& f)
{
return ToUpper(DyFormatter(f), GetLNGCharset(f.language));
}
String dyFormatter(const Formatting& f)
{
return ToLower(DyFormatter(f), GetLNGCharset(f.language));
}
String twFormatter(const Formatting& f)
{
int q = f.arg;
return Sprintf(*f.format == '0' ? "%02d" : "%d", q ? q % 12 : 12);
}
String NumberFormatter(const Formatting& f)
{
int q = f.arg;
return AsString(q);
}
void IntDoubleRegister(int type)
{
RegisterFormatter(type, "", &NumberFormatter);
RegisterFormatter(type, "c", &IntFormatter);
RegisterFormatter(type, "d", &IntFormatter);
RegisterFormatter(type, "i", &IntFormatter);
RegisterFormatter(type, "o", &IntFormatter);
RegisterFormatter(type, "x", &IntFormatter);
RegisterFormatter(type, "X", &IntFormatter);
RegisterFormatter(type, "ld", &IntFormatter);
RegisterFormatter(type, "li", &IntFormatter);
RegisterFormatter(type, "lo", &IntFormatter);
RegisterFormatter(type, "lx", &IntFormatter);
RegisterFormatter(type, "lX", &IntFormatter);
RegisterFormatter(type, "lld", &Int64Formatter);
RegisterFormatter(type, "lli", &Int64Formatter);