-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSF_Format.cpp
More file actions
1193 lines (1056 loc) · 43.5 KB
/
MSF_Format.cpp
File metadata and controls
1193 lines (1056 loc) · 43.5 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 "MSF_FormatStandardTypes.h"
#include "MSF_Assert.h"
#include "MSF_UTF.h"
#include "MSF_Utilities.h"
#include <new>
#if _MSC_VER
#define STRICT
#define WIN32_LEANER_AND_MEANER
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
template class MSF_StringFormatTemplate<char>;
template class MSF_StringFormatTemplate<char8_t>;
template class MSF_StringFormatTemplate<char16_t>;
template class MSF_StringFormatTemplate<char32_t>;
template class MSF_StringFormatTemplate<wchar_t>;
//-------------------------------------------------------------------------------------------------
// Types of errors that can occur during print validation
//-------------------------------------------------------------------------------------------------
enum MSF_PrintResultType
{
ER_NotEnoughSpace,
ER_TooManyInputs,
ER_InconsistentPrintType,
ER_DuplicateFlag,
ER_InvalidPrintCharacter,
ER_TypeMismatch,
ER_UnregisteredChar,
ER_WildcardType,
// csharp errors
ER_UnexpectedBrace,
ER_IndexOutOfRange,
ER_ExpectedWidth,
ER_ExpectedClosingBrace,
ER_UnsupportedType,
ER_Count
};
static char const* thePrintErrors[] =
{
"ER_NotEnoughSpace: {2} chars was not enough space",
"ER_TooManyInputs: Too many inputs {0} to printf system. Can only support " MSF_STR(MSF_MAX_ARGUMENTS),
"ER_InconsistentPrintType: Inconsistent print type '{0}' at {1}. 1=Auto(%% or {{}}), 2=Specific({{X}}). Strings shouldn't mix modes.",
"ER_DuplicateFlag: Duplicate Flag '{0:c}' found at {1}.",
"ER_InvalidPrintCharacter: Invalid print character '{0:c}' at {1}. Only lower and upper case ascii letters supported",
"ER_TypeMismatch: Type mismatch for print character '{0:c}' at {1}",
"ER_UnregisteredChar: Unregistered print character '{0:c}' at {1}",
"ER_WildcardType: Incorrect type specified for wildcard flag at {1}",
"ER_UnexpectedBrace: Unexpected '}}' at {1}. Did you forget to double up your braces?",
"ER_IndexOutOfRange: Requested print index is {1} is out of range",
"ER_ExpectedWidth: Expected a width value at {1}",
"ER_ExpectedClosingBrace: Expected a closing brace '}}' at {1}",
"ER_UnsupportedType: Unsupported type {0:x} at {1}",
};
//-------------------------------------------------------------------------------------------------
// Structure holding error information or length on success
//-------------------------------------------------------------------------------------------------
struct MSF_PrintResult
{
explicit MSF_PrintResult(size_t aNumChars) : MaxBufferLength(aNumChars) {}
explicit MSF_PrintResult(MSF_PrintResultType anError, uint64_t anErrorInfo = 0, uint64_t anErrorLocation = 0)
: MaxBufferLength(-(anError + 1))
, Error(anError)
, Info(anErrorInfo)
, Location(anErrorLocation)
{
}
intptr_t ToString(char(& anErrorBuffer)[256], size_t aBufferLength) const
{
static_assert((sizeof(thePrintErrors) / sizeof(thePrintErrors[0])) == ER_Count, "Number of error messages does not match number of error codes");
MSF_ASSERT(MaxBufferLength < 0);
return MSF_Format(anErrorBuffer, thePrintErrors[Error], Info, Location, aBufferLength);
}
bool HasError() const { return MaxBufferLength < 0; }
intptr_t MaxBufferLength;
MSF_PrintResultType Error;
uint64_t Info;
uint64_t Location;
};
//-------------------------------------------------------------------------------------------------
// Customized print interface, holds registered information about types that can be printed
//-------------------------------------------------------------------------------------------------
namespace MSF_CustomPrint
{
//-------------------------------------------------------------------------------------------------
// For each printf character registered we have a set of types that can be used as well
// as a validate and print functions.
//-------------------------------------------------------------------------------------------------
struct RegisteredChar
{
MSF_CustomPrinter Printer;
uint64_t SupportedTypes;
};
// a-z + A-Z
RegisteredChar theRegisteredChars[52];
// Currently we have a 32 limit based on bit field of registered types
char theDefaultPrintCharacters[64];
// helpers to decode character to a 0-52 index
int GetCharIndex(char aChar)
{
MSF_ASSERT(MSF_IsAsciiAlpha(aChar), "Invalid print character '%c'. Must be a-z or A-Z.", aChar);
int offset = aChar < 'a' ? 'A' - 26 : 'a';
return aChar - offset;
}
// helper to iterate bits in integer
int GetFirstSetBit(uint64_t aValue)
{
#if _MSC_VER
#if INTPTR_MAX == INT32_MAX
unsigned long result;
if (_BitScanForward(&result, (unsigned long)aValue))
return result;
if (_BitScanForward(&result, (unsigned long)(aValue >> 32)))
return result + 32;
return -1;
#else
unsigned long result;
if (_BitScanForward64(&result, aValue))
return result;
return -1;
#endif
#else
return aValue ? __builtin_ctzll(aValue) : -1;
#endif
}
//-------------------------------------------------------------------------------------------------
void RegisterPrintFunction(char aChar, uint64_t someSupportedTypes, MSF_CustomPrinter aPrinter)
{
MSF_ASSERT(someSupportedTypes != 0, "Invalid arguments");
MSF_ASSERT(
aPrinter.ValidateUTF8 && aPrinter.ValidateUTF16 && aPrinter.ValidateUTF32 &&
aPrinter.PrintUTF8 && aPrinter.PrintUTF16 && aPrinter.PrintUTF32, "Incomplete Printer");
int index = GetCharIndex(aChar);
RegisteredChar& registered = theRegisteredChars[index];
MSF_ASSERT(registered.Printer.ValidateUTF8 == nullptr || registered.Printer.ValidateUTF8 == aPrinter.ValidateUTF8,
"Custom print function for '%c' already registered", aChar);
MSF_ASSERT(registered.Printer.ValidateUTF16 == nullptr || registered.Printer.ValidateUTF16 == aPrinter.ValidateUTF16,
"Custom print function for '%c' already registered", aChar);
MSF_ASSERT(registered.Printer.ValidateUTF32 == nullptr || registered.Printer.ValidateUTF32 == aPrinter.ValidateUTF32,
"Custom print function for '%c' already registered", aChar);
MSF_ASSERT(registered.Printer.PrintUTF8 == nullptr || registered.Printer.PrintUTF8 == aPrinter.PrintUTF8,
"Custom print function for '%c' already registered", aChar);
MSF_ASSERT(registered.Printer.PrintUTF16 == nullptr || registered.Printer.PrintUTF16 == aPrinter.PrintUTF16,
"Custom print function for '%c' already registered", aChar);
MSF_ASSERT(registered.Printer.PrintUTF32 == nullptr || registered.Printer.PrintUTF32 == aPrinter.PrintUTF32,
"Custom print function for '%c' already registered", aChar);
MSF_ASSERT(registered.SupportedTypes == 0 || registered.SupportedTypes == someSupportedTypes,
"Custom print function for '%c' already registered", aChar);
registered.Printer = aPrinter;
registered.SupportedTypes = someSupportedTypes;
}
//-------------------------------------------------------------------------------------------------
void RegisterDefaultPrintFunction(char aChar, uint64_t someSupportedTypes, MSF_CustomPrinter aPrinter)
{
RegisterPrintFunction(aChar, someSupportedTypes, aPrinter);
RegisterTypesDefaultChar(someSupportedTypes, aChar);
}
//-------------------------------------------------------------------------------------------------
void RegisterTypesDefaultChar(uint64_t someSupportedTypes, char aChar)
{
int index = GetFirstSetBit(someSupportedTypes);
MSF_ASSERT(index >= 0);
while (index >= 0)
{
MSF_ASSERT(theDefaultPrintCharacters[index] == 0 || theDefaultPrintCharacters[index] == aChar, "Type %d(%x) is already set to %c", index, 1 << index, aChar);
if (theDefaultPrintCharacters[index] == 0)
theDefaultPrintCharacters[index] = aChar;
someSupportedTypes &= ~(1 << index);
index = GetFirstSetBit(someSupportedTypes);
}
}
//-------------------------------------------------------------------------------------------------
void OverrideTypesDefaultChar(uint64_t someSupportedTypes, char aChar)
{
int index = GetFirstSetBit(someSupportedTypes);
MSF_ASSERT(index >= 0);
while (index >= 0)
{
theDefaultPrintCharacters[index] = aChar;
someSupportedTypes &= ~(1 << index);
index = GetFirstSetBit(someSupportedTypes);
}
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
void RegisterTypeCopyLength(char aChar, size_t(*aCopyLength)(MSF_StringFormatType const& aValue))
{
RegisteredChar& registered = theRegisteredChars[GetCharIndex(aChar)];
MSF_ASSERT(registered.Printer.PrintUTF16, "Copy Length can only apply to already registered type");
MSF_ASSERT(registered.Printer.CopyLength == nullptr || registered.Printer.CopyLength == aCopyLength);
registered.Printer.CopyLength = aCopyLength;
}
//-------------------------------------------------------------------------------------------------
size_t GetTypeCopyLength(MSF_StringFormatType const& aValue)
{
char const printChar = theDefaultPrintCharacters[GetFirstSetBit(aValue.myType)];
RegisteredChar const& registered = theRegisteredChars[GetCharIndex(printChar)];
return registered.Printer.CopyLength ? registered.Printer.CopyLength(aValue) : 0;
}
//-------------------------------------------------------------------------------------------------
// For a given type, get the character used to print it by default.
// Special case for signed integers and char type.
//-------------------------------------------------------------------------------------------------
char GetTypeDefaultPrintCharacters(MSF_StringFormatType const& aValue)
{
if (aValue.myType < MSF_StringFormatType::TypeUser)
{
// special hacks
if (aValue.myUserData & MSF_StringFormatType::Char)
return 'c';
if (aValue.myUserData & MSF_StringFormatType::Pointer)
return 'p';
}
return theDefaultPrintCharacters[GetFirstSetBit(aValue.myType)];
}
//-------------------------------------------------------------------------------------------------
// Initialize standard printf types
//-------------------------------------------------------------------------------------------------
bool InitializeCustomPrint();
bool theCustomPrintInitialized = InitializeCustomPrint();
bool InitializeCustomPrint()
{
if (theCustomPrintInitialized)
return true;
{
MSF_CustomPrinter printer{
MSF_StringFormatChar::ValidateUTF8, MSF_StringFormatChar::ValidateUTF16, MSF_StringFormatChar::ValidateUTF32,
MSF_StringFormatChar::PrintUTF8, MSF_StringFormatChar::PrintUTF16, MSF_StringFormatChar::PrintUTF32
};
RegisterPrintFunction('c', MSF_StringFormatChar::ValidTypes, printer);
RegisterPrintFunction('C', MSF_StringFormatChar::ValidTypes, printer);
}
{
MSF_CustomPrinter printer{
MSF_StringFormatString::ValidateUTF8, MSF_StringFormatString::ValidateUTF16, MSF_StringFormatString::ValidateUTF32,
MSF_StringFormatString::PrintUTF8, MSF_StringFormatString::PrintUTF16, MSF_StringFormatString::PrintUTF32
};
RegisterDefaultPrintFunction('s', MSF_StringFormatString::ValidTypes, printer);
RegisterPrintFunction('S', MSF_StringFormatString::ValidTypes, printer);
RegisterTypeCopyLength('s', MSF_StringFormatString::CopyLength);
}
{
MSF_CustomPrinter printer{
MSF_StringFormatInt::Validate, MSF_StringFormatInt::Validate, MSF_StringFormatInt::Validate,
MSF_StringFormatInt::PrintUTF8, MSF_StringFormatInt::PrintUTF16, MSF_StringFormatInt::PrintUTF32
};
RegisterDefaultPrintFunction('d', MSF_StringFormatInt::ValidTypes, printer);
RegisterPrintFunction('i', MSF_StringFormatInt::ValidTypes, printer);
RegisterPrintFunction('u', MSF_StringFormatInt::ValidTypes, printer);
}
{
MSF_CustomPrinter printer{
MSF_StringFormatInt::ValidateOctal, MSF_StringFormatInt::ValidateOctal, MSF_StringFormatInt::ValidateOctal,
MSF_StringFormatInt::PrintUTF8, MSF_StringFormatInt::PrintUTF16, MSF_StringFormatInt::PrintUTF32
};
RegisterPrintFunction('o', MSF_StringFormatInt::ValidTypes, printer);
}
{
MSF_CustomPrinter printer{
MSF_StringFormatInt::ValidateHex, MSF_StringFormatInt::ValidateHex, MSF_StringFormatInt::ValidateHex,
MSF_StringFormatInt::PrintUTF8, MSF_StringFormatInt::PrintUTF16, MSF_StringFormatInt::PrintUTF32
};
RegisterPrintFunction('x', MSF_StringFormatInt::ValidTypes, printer);
RegisterPrintFunction('X', MSF_StringFormatInt::ValidTypes, printer);
}
{
MSF_CustomPrinter printer{
MSF_StringFormatInt::ValidatePointer, MSF_StringFormatInt::ValidatePointer, MSF_StringFormatInt::ValidatePointer,
MSF_StringFormatInt::PrintUTF8, MSF_StringFormatInt::PrintUTF16, MSF_StringFormatInt::PrintUTF32
};
RegisterPrintFunction('p', MSF_StringFormatInt::ValidTypes, printer);
RegisterPrintFunction('P', MSF_StringFormatInt::ValidTypes, printer);
}
{
MSF_CustomPrinter printer{
MSF_StringFormatFloat::Validate, MSF_StringFormatFloat::Validate, MSF_StringFormatFloat::Validate,
MSF_StringFormatFloat::PrintUTF8, MSF_StringFormatFloat::PrintUTF16, MSF_StringFormatFloat::PrintUTF32
};
RegisterPrintFunction('e', MSF_StringFormatFloat::ValidTypes, printer);
RegisterPrintFunction('E', MSF_StringFormatFloat::ValidTypes, printer);
RegisterPrintFunction('f', MSF_StringFormatFloat::ValidTypes, printer);
RegisterPrintFunction('F', MSF_StringFormatFloat::ValidTypes, printer);
RegisterDefaultPrintFunction('g', MSF_StringFormatFloat::ValidTypes, printer);
RegisterPrintFunction('G', MSF_StringFormatFloat::ValidTypes, printer);
}
return true;
}
//-------------------------------------------------------------------------------------------------
// Validate external assumed users are calling the correct types and will crash if invalid
//-------------------------------------------------------------------------------------------------
size_t ValidateTypeUTF8(char aChar, MSF_PrintData& aPrintData, MSF_StringFormatType const& aValue)
{
RegisteredChar const& registered = theRegisteredChars[GetCharIndex(aChar)];
return registered.Printer.ValidateUTF8(aPrintData, aValue);
}
size_t ValidateTypeUTF16(char aChar, MSF_PrintData& aPrintData, MSF_StringFormatType const& aValue)
{
RegisteredChar const& registered = theRegisteredChars[GetCharIndex(aChar)];
return registered.Printer.ValidateUTF16(aPrintData, aValue);
}
size_t ValidateTypeUTF32(char aChar, MSF_PrintData& aPrintData, MSF_StringFormatType const& aValue)
{
RegisteredChar const& registered = theRegisteredChars[GetCharIndex(aChar)];
return registered.Printer.ValidateUTF32(aPrintData, aValue);
}
//-------------------------------------------------------------------------------------------------
// Validate internal is safer than the external one since it depends on string format
//-------------------------------------------------------------------------------------------------
template <typename Char>
MSF_PrintResult ValidateTypeShared(MSF_PrintData& aPrintData, MSF_StringFormatType const& aValue)
{
RegisteredChar const& registered = theRegisteredChars[GetCharIndex(aPrintData.myPrintChar)];
if (!registered.Printer.ValidateUTF8)
{
// If called pre-main then we might be be initialized yet, so just do that now!
if (!theRegisteredChars[GetCharIndex('c')].Printer.ValidateUTF8)
theCustomPrintInitialized = InitializeCustomPrint();
if (!registered.Printer.ValidateUTF8)
return MSF_PrintResult(ER_UnregisteredChar, aPrintData.myPrintChar);
}
if (!(registered.SupportedTypes & aValue.myType))
return MSF_PrintResult(ER_TypeMismatch, aPrintData.myPrintChar);
size_t maxLength = registered.Printer.Validate<Char>(aPrintData, aValue);
MSF_ASSERT(maxLength >= 0, "Validates can't fail");
aPrintData.myMaxLength = maxLength;
return MSF_PrintResult(maxLength);
}
//-------------------------------------------------------------------------------------------------
// Print a character type, useful for chaining custom types into standard types
//-------------------------------------------------------------------------------------------------
template <typename Char>
size_t PrintTypeShared(char aChar, Char* aBuffer, Char const* aBufferEnd, MSF_PrintData const& aData, MSF_StringFormatType const& aValue)
{
RegisteredChar const& registered = theRegisteredChars[GetCharIndex(aChar)];
MSF_PrintData tmp = aData;
tmp.myPrintChar = aChar;
tmp.myValue = &aValue;
return registered.Printer.Print(aBuffer, aBufferEnd, tmp);
}
size_t PrintType(char aChar, char* aBuffer, char const* aBufferEnd, MSF_PrintData const& aData, MSF_StringFormatType const& aValue)
{
return PrintTypeShared(aChar, aBuffer, aBufferEnd, aData, aValue);
}
size_t PrintType(char aChar, char16_t* aBuffer, char16_t const* aBufferEnd, MSF_PrintData const& aData, MSF_StringFormatType const& aValue)
{
return PrintTypeShared(aChar, aBuffer, aBufferEnd, aData, aValue);
}
size_t PrintType(char aChar, char32_t* aBuffer, char32_t const* aBufferEnd, MSF_PrintData const& aData, MSF_StringFormatType const& aValue)
{
return PrintTypeShared(aChar, aBuffer, aBufferEnd, aData, aValue);
}
//-------------------------------------------------------------------------------------------------
// Error Handling
//-------------------------------------------------------------------------------------------------
MSF_ErrorMode theGlobalErrorMode = MSF_ErrorMode::Silent;
MSF_ErrorMode thread_local theLocalErrorMode = MSF_ErrorMode::UseGlobal;
MSF_ErrorFlags theGlobalErrorFlags;
MSF_ErrorFlags thread_local theLocalErrorFlags = MSF_ErrorFlags::UseGlobal;
void SetGlobalErrorMode(MSF_ErrorMode aMode)
{
theGlobalErrorMode = aMode;
}
void SetLocalErrorMode(MSF_ErrorMode aMode)
{
MSF_ASSERT(theLocalErrorMode == MSF_ErrorMode::UseGlobal, "Local error mode already in use");
theLocalErrorMode = aMode;
}
void ClearLocalErrorMode()
{
theLocalErrorMode = MSF_ErrorMode::UseGlobal;
}
MSF_ErrorMode GetErrorMode()
{
MSF_ErrorMode const localMode = theLocalErrorMode;
return localMode == MSF_ErrorMode::UseGlobal ? theGlobalErrorMode : localMode;
}
void SetGlobalErrorFlags(MSF_ErrorFlags someFlags)
{
theGlobalErrorFlags = someFlags;
}
void SetLocalErrorFlags(MSF_ErrorFlags someFlags)
{
MSF_ASSERT(theLocalErrorFlags == MSF_ErrorFlags::UseGlobal, "Local error mode already in use");
theLocalErrorFlags = someFlags;
}
void ClearLocalErrorFlags()
{
theLocalErrorFlags = MSF_ErrorFlags::UseGlobal;
}
MSF_ErrorFlags GetErrorFlags()
{
MSF_ErrorFlags const localFlags = theLocalErrorFlags;
return (localFlags & MSF_ErrorFlags::UseGlobal) ? theGlobalErrorFlags : localFlags;
}
bool IsRelaxedCSharpFormat()
{
return (GetErrorFlags() & MSF_ErrorFlags::RelaxedCSharpFormat) != 0;
}
//-------------------------------------------------------------------------------------------------
// Normal printf
//-------------------------------------------------------------------------------------------------
template <typename Char>
MSF_PrintResult SetupPrintfInfo(MSF_PrintData& aPrintData, Char const*& anInput, uint32_t& anInputIndex, uint32_t anInputCount)
{
// gather the parts
// [flags] [width] [.precision] type
// using the similar standards as Microsoft's printf
// we don't need additional size specifiers since the size
// is known in the format type. this also frees up
// more room for custom types
#if MSF_ERROR_PEDANTIC
#define SET_FLAG(flag) { if (aPrintData.myFlags & flag) return MSF_PrintResult(ER_DuplicateFlag, character); aPrintData.myFlags |= flag; }
#else
#define SET_FLAG(flag) aPrintData.myFlags |= flag;
#endif
// Read Flags
for (Char character = *anInput; character; character = *++anInput)
{
// read until non-flag is found
switch (character)
{
case '-':
SET_FLAG(PRINT_LEFTALIGN);
aPrintData.myFlags &= ~PRINT_ZERO;
continue;
case '+':
SET_FLAG(PRINT_SIGN);
continue;
case ' ':
SET_FLAG(PRINT_BLANK);
continue;
case '#':
SET_FLAG(PRINT_PREFIX);
continue;
case '0':
if (!(aPrintData.myFlags & PRINT_LEFTALIGN))
SET_FLAG(PRINT_ZERO);
continue;
}
// no more flags
break;
}
// Width and precision
uint32_t widthPrecision[2] = {};
for (uint32_t i = 0; i < 2; ++i)
{
uint32_t& value = widthPrecision[i];
Char character = *anInput;
if (character == '*')
{
++anInput;
if ((aPrintData.myValue->myType & MSF_StringFormatInt::ValidTypes) == 0)
return MSF_PrintResult(ER_WildcardType);
if (anInputIndex == anInputCount)
return MSF_PrintResult(ER_IndexOutOfRange, anInputIndex);
switch (aPrintData.myValue->myType)
{
case MSF_StringFormatType::Type8: value = aPrintData.myValue->myValue8; break;
case MSF_StringFormatType::Type16: value = aPrintData.myValue->myValue16; break;
case MSF_StringFormatType::Type32: value = aPrintData.myValue->myValue32; break;
case MSF_StringFormatType::Type64: value = (uint32_t)aPrintData.myValue->myValue32; break; // sure hope someone isn't trying 2gb+ width or precision O.o
}
++aPrintData.myValue;
++anInputIndex;
}
else
{
for (; MSF_IsDigit(character); character = *++anInput)
value = value * 10 + (character - '0');
}
if (!i && *anInput == '.')
{
aPrintData.myFlags |= PRINT_PRECISION;
++anInput;
}
else
break;
}
// Read print character
switch (Char character = *anInput++)
{
case 'I': // look for I[(32|64)](d|i|o|u|x|X)
{
int advance = 0;
if ((anInput[0] == '3' && anInput[1] == '2') ||
(anInput[0] == '6' && anInput[1] == '4'))
{
advance = 2;
}
Char const next = anInput[advance];
if (next == 'd' || next == 'i' || next == 'o' || next == 'u' || next == 'x' || next == 'X')
{
character = next;
anInput += advance + 1;
}
}
goto do_print;
case 'h': // look for (h|hh)(d|i|o|u|x|X|n) or h(s|c)
if (anInput[0] == 'h')
{
Char const next = anInput[1];
if (next == 'd' || next == 'i' || next == 'o' || next == 'u' || next == 'x' || next == 'X' || next == 'n')
{
// skip hh
character = next;
anInput += 2;
}
}
else
{
Char const next = anInput[0];
if (next == 'd' || next == 'i' || next == 'o' || next == 'u' || next == 'x' || next == 'X' || next == 'n' ||
next == 's' || next == 'c')
{
// skip h
character = next;
anInput += 1;
}
}
goto do_print;
case 'l': // look for (l|ll)(d|i|o|u|x|X|n) or l(s|c|f|F|e|E|a|A|g|G)
if (anInput[0] == 'l')
{
Char const next = anInput[1];
if (next == 'd' || next == 'i' || next == 'o' || next == 'u' || next == 'x' || next == 'X' || next == 'n')
{
// skip ll
character = next;
anInput += 2;
}
}
else
{
Char const next = anInput[0];
if (next == 'd' || next == 'i' || next == 'o' || next == 'u' || next == 'x' || next == 'X' || next == 'n' ||
next == 's' || next == 'c' ||
next == 'f' || next == 'F' || next == 'e' || next == 'E' || next == 'a' || next == 'A' || next == 'g' || next == 'G')
{
// skip l
character = next;
anInput += 1;
}
}
goto do_print;
case 'j':
case 't':
case 'z': // look for (j|t|z)(d|i|o|u|x|X)
{
Char const next = anInput[0];
if (next == 'd' || next == 'i' || next == 'o' || next == 'u' || next == 'x' || next == 'X')
{
// skip l
character = next;
anInput += 1;
}
}
goto do_print;
case 'L': // look for L(f|F|e|E|a|A|g|G)
{
Char const next = anInput[0];
if (next == 'f' || next == 'F' || next == 'e' || next == 'E' || next == 'a' || next == 'A' || next == 'g' || next == 'G')
{
// skip l
character = next;
anInput += 1;
}
}
goto do_print;
case 'w': // look for w(s|c)
{
Char const next = anInput[0];
if (next == 's' || next == 'c')
{
// skip l
character = next;
anInput += 1;
}
}
goto do_print;
default:
if (!MSF_IsAsciiAlpha(character))
{
return MSF_PrintResult(ER_InvalidPrintCharacter, character ? character : '?');
}
else
{
do_print:
aPrintData.myPrintChar = (char)character;
static_assert(sizeof(aPrintData.myWidth) == 2, "sizeof myWidth changed, update code");
MSF_ASSERT(widthPrecision[0] < UINT16_MAX);
aPrintData.myWidth = (uint16_t)widthPrecision[0];
static_assert(sizeof(aPrintData.myPrecision) == 2, "sizeof myPrecision changed, update code");
MSF_ASSERT(widthPrecision[1] < UINT16_MAX);
aPrintData.myPrecision = (uint16_t)widthPrecision[1];
return ValidateTypeShared<Char>(aPrintData, *aPrintData.myValue);
}
}
}
//-------------------------------------------------------------------------------------------------
// CSharp/hybrid style
//-------------------------------------------------------------------------------------------------
template <typename Char>
MSF_PrintResult SetupFormatInfo(MSF_PrintData& aPrintData, Char const*& anInput)
{
// gather the parts
// https://docs.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.8#controlling-formatting
// {[index][,[-]width][:formatString[precision]]}
// Read alignment / width
if (*anInput == ',')
{
++anInput;
if (*anInput == '-')
{
aPrintData.myFlags |= PRINT_LEFTALIGN;
++anInput;
}
if (!MSF_IsDigit(*anInput))
return MSF_PrintResult(ER_ExpectedWidth, 0);
uint32_t width = *(anInput++) - '0';
while (MSF_IsDigit(*anInput))
width = width * 10 + *(anInput++) - '0';
static_assert(sizeof(aPrintData.myWidth) == 2, "sizeof myWidth changed, update code");
MSF_ASSERT(width < UINT16_MAX);
aPrintData.myWidth = (uint16_t)width;
}
// Read formatting
if (*anInput == ':')
{
++anInput;
if (!MSF_IsAsciiAlpha(*anInput))
return MSF_PrintResult(ER_InvalidPrintCharacter, *anInput ? *anInput : '?');
aPrintData.myPrintChar = (char)*(anInput++);
uint32_t precision = 0;
while (MSF_IsDigit(*anInput))
precision = precision * 10 + *(anInput++) - '0';
static_assert(sizeof(aPrintData.myPrecision) == 2, "sizeof myPrecision changed, update code");
MSF_ASSERT(precision < UINT16_MAX);
aPrintData.myPrecision = (uint16_t)precision;
}
else if (aPrintData.myValue)
{
aPrintData.myPrintChar = GetTypeDefaultPrintCharacters(*aPrintData.myValue);
if (!aPrintData.myPrintChar)
return MSF_PrintResult(ER_UnsupportedType, aPrintData.myValue->myType, 0);
}
if (*anInput != '}')
return MSF_PrintResult(ER_ExpectedClosingBrace, 0);
++anInput;
return aPrintData.myValue ? ValidateTypeShared<Char>(aPrintData, *aPrintData.myValue) : MSF_PrintResult(0);
}
}
//-------------------------------------------------------------------------------------------------
// Helper class for constructing a formatted string
//-------------------------------------------------------------------------------------------------
template <typename Char>
class MSF_StringFormatter
{
public:
MSF_StringFormatter() : myPrintedCharacters(0) {}
MSF_PrintResult PrepareFormatter(MSF_StringFormatTemplate<Char> const& aStringFormat)
{
uint32_t const inputCount = aStringFormat.NumArgs();
if (inputCount > MSF_MAX_ARGUMENTS)
return MSF_PrintResult(ER_TooManyInputs, inputCount, 0);
uint32_t inputIndex = 0;
MSF_StringFormatType const* aData = aStringFormat.GetArgs();
myPrintString = aStringFormat.GetString();
Char const* str = myPrintString;
enum Mode { None, Auto, Specific };
Mode printMode = None;
Char character;
for ((character = *str++); character; (character = *str++))
{
switch (character)
{
case '%':
case '{':
{
switch (*str)
{
case '%':
case '{':
if (character != *str)
return MSF_PrintResult(ER_InvalidPrintCharacter, *str, int(str - myPrintString));
++str;
break;
default:
MSF_PrintData& printData = myPrintData[myPrintedCharacters++];
printData.myFlags = 0; // initialize the whole first block
printData.myPrecision = 0;
printData.myWidth = 0;
printData.myStart = str - 1;// account for '%' or '{'
// look for position argument and print mode
Mode thisMode = Auto;
if (character == '{')
{
// Read Index
if (MSF_IsDigit(*str))
{
thisMode = Specific;
uint32_t nextInputIndex = *(str++) - '0';
while (MSF_IsDigit(*str))
{
nextInputIndex = nextInputIndex * 10 + (*(str++) - '0');
// This is not a perf issue in the loop since 99.9% of the time it's going to be a single character
// we detect the moment it goes out of range to support relaxed errors in the case someone wrote "{12345667}".
if (nextInputIndex >= MSF_MAX_ARGUMENTS*2)
break;
}
if (nextInputIndex >= inputCount)
{
// Only relax the error if it's at least 5 past the last input. This is to try to disambiguate
// between a user error and and non-print text. i.e. {123} is clearly out of range where {7} might just be a mistaken print
// This does mean there might be some small edge case errors like {12} with 7 real inputs will trigger an error vs a relax
if (nextInputIndex - inputCount > 5 && MSF_CustomPrint::IsRelaxedCSharpFormat())
{
--myPrintedCharacters;
break;
}
return MSF_PrintResult(ER_IndexOutOfRange, nextInputIndex);
}
inputIndex = nextInputIndex;
}
}
// Check for inconsistent print modes
if (thisMode != printMode)
{
if (printMode == None)
printMode = thisMode;
else
return MSF_PrintResult(ER_InconsistentPrintType, printMode, int((Char const*)printData.myStart - myPrintString));
}
if (printMode == Auto && inputIndex == inputCount)
{
// Before considering out of range make sure this is a valid print statement and not an csharp looking one
if (character == '{' && MSF_CustomPrint::IsRelaxedCSharpFormat())
{
printData.myValue = nullptr;
if (MSF_CustomPrint::SetupFormatInfo(printData, str).HasError())
{
--myPrintedCharacters;
break;
}
}
return MSF_PrintResult(ER_IndexOutOfRange, inputIndex);
}
printData.myValue = aData + inputIndex;
// Post increment in auto mode
if (printMode == Auto)
{
++inputIndex;
}
// Process any format specifiers
MSF_PrintResult result = character == '%' ? MSF_CustomPrint::SetupPrintfInfo(printData, str, inputIndex, inputCount) : MSF_CustomPrint::SetupFormatInfo(printData, str);
if (result.HasError())
{
if (character == '{' && MSF_CustomPrint::IsRelaxedCSharpFormat())
{
if (result.Error == ER_ExpectedWidth ||
result.Error == ER_InvalidPrintCharacter ||
result.Error == ER_ExpectedClosingBrace)
{
--myPrintedCharacters;
break;
}
}
result.Location = int(str - myPrintString);
return result;
}
printData.myEnd = str;
break;
}
break;
}
case '}':
if (*str == '}')
++str;
else if (!MSF_CustomPrint::IsRelaxedCSharpFormat())
return MSF_PrintResult(ER_UnexpectedBrace, 0, int(str - myPrintString));
break;
}
}
// we actually end up 1 past the end of the string which is actually ok count wise
MSF_HEAVY_ASSERT(!*(str-1));
// get the max size required
size_t requiredLength = str - myPrintString;
for (uint32_t i = 0; i < myPrintedCharacters; ++i)
{
requiredLength += myPrintData[i].myMaxLength - ((Char const*)myPrintData[i].myEnd - (Char const*)myPrintData[i].myStart);
}
return MSF_PrintResult(requiredLength);
}
size_t FormatString(Char* aBuffer, size_t aBufferLength) const
{
Char const* start = aBuffer;
Char const* end = aBuffer + aBufferLength;
Char const* read = myPrintString;
for (uint32_t i = 0; i < myPrintedCharacters; ++i)
{
// copy segment between print markers
if (myPrintData[i].myStart > read)
{
while (read != myPrintData[i].myStart)
{
// if double control characters are found, skip one
if ((*read == '%' || *read == '{' || *read == '}') && read[0] == read[1])
{
++read;
}
MSF_HEAVY_ASSERT(aBuffer < end);
*aBuffer++ = *read++;
}
}
int index = MSF_CustomPrint::GetCharIndex(myPrintData[i].myPrintChar);
aBuffer += MSF_CustomPrint::theRegisteredChars[index].Printer.Print(aBuffer, (Char const*)end, myPrintData[i]);
MSF_ASSERT(aBuffer < end);
read = (Char const*)myPrintData[i].myEnd;
}
while (*read)
{
// if double control characters are found, skip one
if ((*read == '%' || *read == '{' || *read == '}') && read[0] == read[1])
{
++read;
}
MSF_HEAVY_ASSERT(aBuffer < end);
*aBuffer++ = *read++;
}
MSF_HEAVY_ASSERT(aBuffer < end);
*aBuffer = '\0';
// Don't include the null terminator since we want character printed, not bytes used
return aBuffer - start;
}
void ProcessError(MSF_PrintResult anError, Char* aBuffer, size_t aBufferLength, size_t anOffset, Char* (*aReallocFunction)(Char*, size_t, void*), void* aUserData)
{
MSF_ErrorMode errorMode = MSF_CustomPrint::GetErrorMode();
char errorMessage[256];
size_t errorLength = anError.ToString(errorMessage, aBufferLength);
// Null terminate buffer if possible
if (aBuffer && aBufferLength)
{
aBuffer[MSF_IntMin(anOffset, aBufferLength-1)] = 0;
}
if (errorMode == MSF_ErrorMode::Assert)
{
#if MSF_ASSERTS_ENABLED
if (!MSF_IsAsserting())
{
MSF_ASSERT(false, "Error formatting string: %s", errorMessage);
}
#endif
}
else if (errorMode == MSF_ErrorMode::WriteString)
{
if (errorLength + anOffset > aBufferLength)
{
if (aReallocFunction)
{
aBuffer = aReallocFunction(aBuffer, errorLength + anOffset, aUserData);
aBufferLength = errorLength + anOffset;
if (aBuffer == nullptr)
{
return;
}
}
else
{
errorLength = aBufferLength - anOffset;
}
}
MSF_UTFCopy(aBuffer + anOffset, aBufferLength - anOffset, errorMessage, errorLength);
}
// else do nothing
}
private:
MSF_PrintData myPrintData[MSF_MAX_ARGUMENTS];
Char const* myPrintString;
size_t myPrintedCharacters;
};
//-------------------------------------------------------------------------------------------------