-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSF_FormatStandardTypes.cpp
More file actions
666 lines (582 loc) · 21 KB
/
MSF_FormatStandardTypes.cpp
File metadata and controls
666 lines (582 loc) · 21 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
#include "MSF_FormatStandardTypes.h"
#include "MSF_Assert.h"
#include "MSF_PlatformConfig.h"
#include "MSF_ToString.h"
#include "MSF_UTF.h"
#include "MSF_Utilities.h"
//-------------------------------------------------------------------------------------------------
// Windows printf respects the 0 flag, posix doesn't
//-------------------------------------------------------------------------------------------------
static char locStringLeadingCharacter(MSF_PrintData const& aData)
{
#if MSF_STRING_ALLOW_LEADING_ZERO
return ((aData.myFlags & PRINT_ZERO) && aData.myWidth) ? '0' : ' ';
#else
(void)aData;
return ' ';
#endif
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
namespace MSF_StringFormatChar
{
struct CharData
{
uint32_t Length;
union
{
char UTF8[4];
char16_t UTF16[2];
char32_t UTF32[1];
} Data;
};
size_t ValidateUTF8(MSF_PrintData& aData, MSF_StringFormatType const& aValue)
{
MSF_ASSERT(aValue.myType & ValidTypes);
static_assert(sizeof(CharData) <= sizeof(aData.myUserData), "Not enough storage space for temp data");
CharData* utf8Char = (CharData*)&aData.myUserData;
if (aValue.myType == MSF_StringFormatType::Type8)
{
utf8Char->Data.UTF8[0] = aValue.myValue8;
utf8Char->Length = 1;
}
else
{
uint32_t codePoint;
if (aValue.myType == MSF_StringFormatType::Type16)
{
char16_t data[2] = { aValue.myValue16, 0 };
codePoint = MSF_ReadCodePoint(data).CodePoint;
}
else
{
// If not a wchar then just assume the value is a unicode code point
codePoint = aValue.myValue32;
}
utf8Char->Length = MSF_WriteCodePoint(codePoint, utf8Char->Data.UTF8);
}
return MSF_IntMax<uint32_t>(aData.myWidth, utf8Char->Length);
}
size_t ValidateUTF16(MSF_PrintData& aData, MSF_StringFormatType const& aValue)
{
MSF_ASSERT(aValue.myType & ValidTypes);
static_assert(sizeof(CharData) <= sizeof(aData.myUserData), "Not enough storage space for temp data");
CharData* utf8Char = (CharData*)&aData.myUserData;
if (aValue.myType == MSF_StringFormatType::Type8)
{
utf8Char->Data.UTF16[0] = aValue.myValue8;
utf8Char->Length = 1;
}
else
{
if (aValue.myType == MSF_StringFormatType::Type16)
{
utf8Char->Data.UTF16[0] = aValue.myValue16;
utf8Char->Length = 1;
}
else
{
// If not a wchar then just assume the value is a unicode code point
utf8Char->Length = MSF_WriteCodePoint(aValue.myValue32, utf8Char->Data.UTF16);
}
}
return MSF_IntMax<uint32_t>(aData.myWidth, utf8Char->Length);
}
size_t ValidateUTF32(MSF_PrintData& aData, MSF_StringFormatType const& aValue)
{
MSF_ASSERT(aValue.myType & ValidTypes);
static_assert(sizeof(CharData) <= sizeof(aData.myUserData), "Not enough storage space for temp data");
CharData* utf8Char = (CharData*)&aData.myUserData;
utf8Char->Length = 1;
if (aValue.myType == MSF_StringFormatType::Type8)
{
utf8Char->Data.UTF32[0] = aValue.myValue8;
}
else if (aValue.myType == MSF_StringFormatType::Type16)
{
utf8Char->Data.UTF32[0] = aValue.myValue16;
}
else
{
utf8Char->Data.UTF32[0] = aValue.myValue16;
}
return MSF_IntMax<uint32_t>(aData.myWidth, utf8Char->Length);
}
template <typename Char>
size_t Print(Char* aBuffer, Char const* aBufferEnd, MSF_PrintData const& aData)
{
Char* bufferWrite = aBuffer;
CharData const charData = *(CharData const*)&aData.myUserData;
if (!(aData.myFlags & PRINT_LEFTALIGN) && aData.myWidth > charData.Length)
{
size_t const splatChars = aData.myWidth - charData.Length;
MSF_SplatChars(bufferWrite, aBufferEnd, locStringLeadingCharacter(aData), splatChars);
bufferWrite += splatChars;
}
MSF_CopyChars(bufferWrite, aBufferEnd, (Char const*)charData.Data.UTF8, charData.Length);
bufferWrite += charData.Length;
if ((aData.myFlags & PRINT_LEFTALIGN) && aData.myWidth > charData.Length)
{
size_t const splatChars = aData.myWidth - charData.Length;
MSF_SplatChars(bufferWrite, aBufferEnd, ' ', splatChars);
bufferWrite += splatChars;
}
return bufferWrite - aBuffer;
}
size_t PrintUTF8(char* aBuffer, char const* aBufferEnd, MSF_PrintData const& aData) { return Print(aBuffer, aBufferEnd, aData); }
size_t PrintUTF16(char16_t* aBuffer, char16_t const* aBufferEnd, MSF_PrintData const& aData) { return Print(aBuffer, aBufferEnd, aData); }
size_t PrintUTF32(char32_t* aBuffer, char32_t const* aBufferEnd, MSF_PrintData const& aData) { return Print(aBuffer, aBufferEnd, aData); }
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
namespace MSF_StringFormatString
{
template <typename CharTo, typename CharFrom>
struct Helper
{
static size_t Validate(MSF_PrintData& aData, CharFrom const* aString, size_t aLength)
{
MSF_CharactersWritten written;
if (aData.myFlags & PRINT_PRECISION)
{
#if MSF_STRING_PRECISION_IS_CHARACTERS
if (aLength == SIZE_MAX)
written = MSF_UTFCopyLength<CharTo>(aString, aData.myPrecision);
else
written = MSF_UTFCopy((CharTo*)nullptr, aLength, aString, aData.myPrecision);
#else
written = MSF_UTFCopy((CharTo*)nullptr, MSF_IntMin<size_t>(aLength, aData.myPrecision), aString);
#endif
}
else
{
written = MSF_UTFCopyLength<CharTo>(aString);
}
#if MSF_STRING_PRECISION_IS_CHARACTERS
aData.myUserData = written.Characters;
size_t elementsRequired = written.Elements;
if (aData.myWidth > written.Characters)
{
elementsRequired += aData.myWidth - written.Characters;
}
return elementsRequired;
#else
aData.myUserData = written.Elements;
return MSF_IntMax<size_t>(written.Elements, aData.myWidth);
#endif
}
static size_t Print(CharTo* aBuffer, CharTo const* aBufferEnd, MSF_PrintData const& aData, CharFrom const* aString)
{
CharTo* bufferWrite = aBuffer;
if (!(aData.myFlags & PRINT_LEFTALIGN) && aData.myWidth > aData.myUserData)
{
size_t const splatChars = size_t(aData.myWidth - aData.myUserData);
MSF_SplatChars(bufferWrite, aBufferEnd, locStringLeadingCharacter(aData), splatChars);
bufferWrite += splatChars;
}
if (aData.myUserData > 0)
{
#if MSF_STRING_PRECISION_IS_CHARACTERS
bufferWrite += MSF_UTFCopy(bufferWrite, aBufferEnd - bufferWrite, aString, (size_t)aData.myUserData).Elements;
#else
bufferWrite += MSF_UTFCopy(bufferWrite, (size_t)aData.myUserData, aString).Elements;
#endif
}
if (aData.myFlags & PRINT_LEFTALIGN && aData.myWidth > aData.myUserData)
{
size_t const splatChars = size_t(aData.myWidth - aData.myUserData);
MSF_SplatChars(bufferWrite, aBufferEnd, ' ', splatChars);
bufferWrite += splatChars;
}
return bufferWrite - aBuffer;
}
};
template <typename Char>
struct Helper<Char, Char>
{
static size_t Validate(MSF_PrintData& aData, Char const* aString, size_t aLength)
{
aData.myUserData = aLength == SIZE_MAX ? MSF_Strlen(aString) : aLength;
if (aData.myFlags & PRINT_PRECISION)
{
aData.myUserData = MSF_IntMin<uint64_t>(aData.myUserData, aData.myPrecision);
}
return MSF_IntMax<size_t>((size_t)aData.myUserData, aData.myWidth);
}
static size_t Print(Char* aBuffer, Char const* aBufferEnd, MSF_PrintData const& aData, Char const* aString)
{
Char* bufferWrite = aBuffer;
if (!(aData.myFlags & PRINT_LEFTALIGN) && aData.myWidth > aData.myUserData)
{
size_t const splatChars = size_t(aData.myWidth - aData.myUserData);
MSF_SplatChars(bufferWrite, aBufferEnd, locStringLeadingCharacter(aData), splatChars);
bufferWrite += splatChars;
}
if (aData.myUserData > 0)
{
size_t const copyChars = (size_t)aData.myUserData;
MSF_CopyChars(bufferWrite, aBufferEnd, aString, copyChars);
bufferWrite += copyChars;
}
if (aData.myFlags & PRINT_LEFTALIGN && aData.myWidth > aData.myUserData)
{
size_t const splatChars = size_t(aData.myWidth - aData.myUserData);
MSF_SplatChars(bufferWrite, aBufferEnd, locStringLeadingCharacter(aData), splatChars);
bufferWrite += splatChars;
}
return bufferWrite - aBuffer;
}
};
template <typename Char>
size_t ValidateShared(MSF_PrintData& aData, MSF_StringFormatType const& aValue, size_t aLength = SIZE_MAX)
{
MSF_ASSERT(aValue.myType & ValidTypes);
if (aValue.myString == nullptr)
{
#if MSF_STRING_NULL_ALL_OR_NOTHING
if (!(aData.myFlags & PRINT_PRECISION) || aData.myPrecision > 5)
return Helper<Char, char>::Validate(aData, "(null)", 6);
return Helper<Char, char>::Validate(aData, "", 0);
#else
return Helper<Char, char>::Validate(aData, "(null)", 6);
#endif
}
if ((aValue.myUserData & (MSF_StringFormatType::UTF16 | MSF_StringFormatType::UTF32)) == 0)
return Helper<Char, char>::Validate(aData, aValue.myString, aLength);
if (aValue.myUserData & MSF_StringFormatType::UTF16)
return Helper<Char, char16_t>::Validate(aData, aValue.myUTF16String, aLength);
return Helper<Char, char32_t>::Validate(aData, aValue.myUTF32String, aLength);
}
size_t ValidateUTF8(MSF_PrintData& aData, MSF_StringFormatType const& aValue)
{
return ValidateShared<char>(aData, aValue);
}
size_t ValidateUTF16(MSF_PrintData& aData, MSF_StringFormatType const& aValue)
{
return ValidateShared<char16_t>(aData, aValue);
}
size_t ValidateUTF32(MSF_PrintData& aData, MSF_StringFormatType const& aValue)
{
return ValidateShared<char32_t>(aData, aValue);
}
size_t ValidateUTF8(MSF_PrintData& aData, MSF_StringFormatType const& aValue, size_t aLength)
{
return ValidateShared<char>(aData, aValue, aLength);
}
size_t ValidateUTF16(MSF_PrintData& aData, MSF_StringFormatType const& aValue, size_t aLength)
{
return ValidateShared<char16_t>(aData, aValue, aLength);
}
size_t ValidateUTF32(MSF_PrintData& aData, MSF_StringFormatType const& aValue, size_t aLength)
{
return ValidateShared<char32_t>(aData, aValue, aLength);
}
template <typename Char>
size_t PrintShared(Char* aBuffer, Char const* aBufferEnd, MSF_PrintData const& aData)
{
if (aData.myValue->myString == nullptr)
{
#if MSF_STRING_NULL_ALL_OR_NOTHING
if (!(aData.myFlags & PRINT_PRECISION) || aData.myPrecision > 5)
return Helper<Char, char>::Print(aBuffer, aBufferEnd, aData, "(null)");
return Helper<Char, char>::Print(aBuffer, aBufferEnd, aData, "");
#else
return Helper<Char, char>::Print(aBuffer, aBufferEnd, aData, "(null)");
#endif
}
if ((aData.myValue->myUserData & (MSF_StringFormatType::UTF16 | MSF_StringFormatType::UTF32)) == 0)
return Helper<Char, char>::Print(aBuffer, aBufferEnd, aData, aData.myValue->myString);
if (aData.myValue->myUserData & MSF_StringFormatType::UTF16)
return Helper<Char, char16_t>::Print(aBuffer, aBufferEnd, aData, aData.myValue->myUTF16String);
return Helper<Char, char32_t>::Print(aBuffer, aBufferEnd, aData, aData.myValue->myUTF32String);
}
size_t PrintUTF8(char* aBuffer, char const* aBufferEnd, MSF_PrintData const& aData)
{
return PrintShared(aBuffer, aBufferEnd, aData);
}
size_t PrintUTF16(char16_t* aBuffer, char16_t const* aBufferEnd, MSF_PrintData const& aData)
{
return PrintShared(aBuffer, aBufferEnd, aData);
}
size_t PrintUTF32(char32_t* aBuffer, char32_t const* aBufferEnd, MSF_PrintData const& aData)
{
return PrintShared(aBuffer, aBufferEnd, aData);
}
size_t CopyLength(MSF_StringFormatType const& aValue)
{
if (aValue.myString == nullptr)
return 0;
if (aValue.myUserData & MSF_StringFormatType::UTF32)
return (MSF_Strlen(aValue.myUTF32String) + 1) * 4;
if (aValue.myUserData & MSF_StringFormatType::UTF16)
return (MSF_Strlen(aValue.myUTF16String) + 1) * 2;
return MSF_Strlen(aValue.myString) + 1;
}
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
namespace MSF_StringFormatInt
{
size_t Validate(MSF_PrintData& aData, MSF_StringFormatType const& aValue)
{
MSF_ASSERT(aValue.myType & ValidTypes);
static_assert(MSF_StringFormatType::Type8 == 2, "Format type indexes changed, this formula no longer works");
// type = (2/4/8/16) target = (3/5/10/20)
// type / 2 * 3 = (3/6/12/24) - type >> 2 (0/1/2/4) = (3/5/10/20), exactly what we need, + 1 for sign, just in case its negative
size_t maxLength = size_t(aValue.myType / 2 * 3 - (aValue.myType >> 2));
maxLength = MSF_IntMax<size_t>(maxLength, aData.myPrecision);
return MSF_IntMax<size_t>(maxLength, aData.myWidth) + 1;
}
size_t ValidateOctal(MSF_PrintData& aData, MSF_StringFormatType const& aValue)
{
MSF_ASSERT(aValue.myType & ValidTypes);
// type = (2/4/8/16) target = (3/6/11/23)
// type / 2 * 3 = (3/6/12/24) which is close enough for me, + prefix 'o'
size_t maxLength = size_t((aValue.myType / 2 * 3));
maxLength = MSF_IntMax<size_t>(maxLength, aData.myPrecision) + (aData.myFlags & PRINT_PREFIX);
return MSF_IntMax<size_t>(maxLength, aData.myWidth);
}
size_t ValidateHex(MSF_PrintData& aData, MSF_StringFormatType const& aValue)
{
MSF_ASSERT(aValue.myType & ValidTypes);
// make sure these calculations stay the same as those in GetTypeMaxLength
// type = (2/4/8/16)... perfect, add prefix of '0x'.
size_t maxLength = size_t((aValue.myType));
maxLength = MSF_IntMax<size_t>(maxLength, aData.myPrecision) + (aData.myFlags & PRINT_PREFIX) * 2;
return MSF_IntMax<size_t>(maxLength, aData.myWidth);
}
size_t ValidatePointer(MSF_PrintData& aData, MSF_StringFormatType const& aValue)
{
(void)aValue;
MSF_ASSERT(aValue.myType & ValidTypes);
// always print pointer sized
size_t maxLength = sizeof(void*) * 2;
maxLength = MSF_IntMax<size_t>(maxLength, aData.myPrecision);
#if MSF_POINTER_ADD_PREFIX
maxLength += 2;
#endif // MSF_POINTER_ADD_PREFIX
#if MSF_POINTER_ADD_SIGN_OR_BLANK
maxLength += !!(aData.myFlags & (PRINT_SIGN | PRINT_BLANK));
#endif // MSF_POINTER_ADD_SIGN_OR_BLANK
return MSF_IntMax<size_t>(maxLength, aData.myWidth);
}
template <typename Char, typename Type, typename SignedType>
size_t Print(Char* aBuffer, Char const* aBufferEnd, MSF_PrintData const& aData, Type aValue)
{
bool isSigned = aData.myPrintChar == 'd' || aData.myPrintChar == 'i';
bool addPrefix = false;
bool printSign = false;
Char sign = ' ';
char start = (aData.myPrintChar == 'X' || aData.myPrintChar == 'P' || (MSF_POINTER_PRINT_CAPS && aData.myPrintChar == 'p')) ? 'A' : 'a';
auto radix = 10;
auto precision = aData.myPrecision;
auto flags = aData.myFlags;
Type value;
if (isSigned)
{
SignedType signedValue = aValue;
if (signedValue < 0)
{
value = -signedValue;
printSign = true;
sign = '-';
}
else
{
value = signedValue;
if (flags & PRINT_SIGN)
{
printSign = true;
sign = '+';
}
else if (flags & PRINT_BLANK)
{
printSign = true;
sign = ' ';
}
}
}
else
value = aValue;
switch (aData.myPrintChar)
{
case 'o':
radix = 8;
addPrefix = (flags & PRINT_PREFIX) != 0;
break;
case 'X':
case 'x':
radix = 16;
addPrefix = (flags & PRINT_PREFIX) != 0;
break;
case 'P':
case 'p':
#if MSF_POINTER_PRINT_NIL
if (value == 0)
{
Char nil[] = { '(', 'n', 'i', 'l', ')', 0 };
aData.myUserData = 5;
return MSF_CustomPrint::PrintType('s', aBuffer, aBufferEnd, aData, MSF_StringFormatType(nil));
}
#endif
radix = 16;
#if MSF_POINTER_FORCE_PRECISION
precision = sizeof(void*) * 2;
flags |= PRINT_PRECISION;
#endif // MSF_POINTER_FORCE_PRECISION
#if MSF_POINTER_ADD_PREFIX
addPrefix = true;
#endif // MSF_POINTER_ADD_PREFIX
#if MSF_POINTER_ADD_SIGN_OR_BLANK
if (flags & PRINT_SIGN)
{
printSign = true;
sign = '+';
}
else if (flags & PRINT_BLANK)
{
printSign = true;
sign = ' ';
}
#endif // MSF_POINTER_ADD_SIGN_OR_BLANK
break;
}
if (flags & PRINT_PRECISION)
{
flags &= ~PRINT_ZERO;
}
auto utoa = MSF_UnsignedToString<Type, Char>(value, radix, start);
Char const* string = utoa;
auto length = utoa.Length();
Char const prefix[2] = { '0', Char(start + ('x' - 'a')) };
uint32_t prefixLen = 0;
if (string[0] != '0' && addPrefix)
{
if (radix == 8)
{
prefixLen = 1;
if (precision)
{
--precision;
}
}
else if (radix == 16)
{
prefixLen = 2;
}
}
else if (string[0] == '0' && (flags & PRINT_PRECISION) && precision == 0)
{
// Odd hack where we're actually printing the octal prefix 0 not the value 0
if (aData.myPrintChar != 'o' || !addPrefix)
--length;
}
uint32_t requiredLength = MSF_IntMax<uint32_t>(length, precision) + prefixLen + printSign;
Char* bufferWrite = aBuffer;
// pad with space if we're right aligned, have space left and are using blanks
if (requiredLength < aData.myWidth && !(flags & (PRINT_LEFTALIGN | PRINT_ZERO)))
{
size_t const splatChars = aData.myWidth - requiredLength;
MSF_SplatChars(bufferWrite, aBufferEnd, ' ', splatChars);
bufferWrite += splatChars;
}
// if there's a +/- or space before the number write it now (before padding with zeros)
if (printSign)
{
*bufferWrite++ = sign;
}
// write the prefix before padding with zeros
if (prefixLen)
{
MSF_CopyChars(bufferWrite, aBufferEnd, prefix, prefixLen);
bufferWrite += prefixLen;
}
// if we're right aligned and have space and are using zeros write them now
if (requiredLength < aData.myWidth && !(flags & PRINT_LEFTALIGN) && flags & PRINT_ZERO)
{
size_t const splatChars = aData.myWidth - requiredLength;
MSF_SplatChars(bufferWrite, aBufferEnd, '0', splatChars);
bufferWrite += splatChars;
}
// if we're less than the wanted precision pad with some zeros
if (length < precision)
{
size_t const splatChars = precision - length;
MSF_SplatChars(bufferWrite, aBufferEnd, '0', splatChars);
bufferWrite += splatChars;
}
// write the value
MSF_CopyChars(bufferWrite, aBufferEnd, string, length);
bufferWrite += length;
// if we're left aligned, have space and are using spaces then write them now
if (requiredLength < aData.myWidth && flags & PRINT_LEFTALIGN)
{
size_t const splatChars = aData.myWidth - requiredLength;
MSF_SplatChars(bufferWrite, aBufferEnd, ' ', splatChars);
bufferWrite += splatChars;
}
return bufferWrite - aBuffer;
}
template <typename Char>
size_t PrintShared(Char* aBuffer, Char const* aBufferEnd, MSF_PrintData const& aData)
{
switch (aData.myValue->myType)
{
case MSF_StringFormatType::Type64:
return Print<Char, uint64_t, int64_t>(aBuffer, aBufferEnd, aData, aData.myValue->myValue64);
case MSF_StringFormatType::Type32:
return Print<Char, uint32_t, int32_t>(aBuffer, aBufferEnd, aData, aData.myValue->myValue32);
case MSF_StringFormatType::Type16:
return Print<Char, uint16_t, int16_t>(aBuffer, aBufferEnd, aData, aData.myValue->myValue16);
case MSF_StringFormatType::Type8:
return Print<Char, uint8_t, int8_t>(aBuffer, aBufferEnd, aData, aData.myValue->myValue8);
}
MSF_ASSERT(false);
return 0;
}
size_t PrintUTF8(char* aBuffer, char const* aBufferEnd, MSF_PrintData const& aData)
{
return PrintShared(aBuffer, aBufferEnd, aData);
}
size_t PrintUTF16(char16_t* aBuffer, char16_t const* aBufferEnd, MSF_PrintData const& aData)
{
return PrintShared(aBuffer, aBufferEnd, aData);
}
size_t PrintUTF32(char32_t* aBuffer, char32_t const* aBufferEnd, MSF_PrintData const& aData)
{
return PrintShared(aBuffer, aBufferEnd, aData);
}
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
namespace MSF_StringFormatFloat
{
size_t Validate(MSF_PrintData& aData, MSF_StringFormatType const& aValue)
{
MSF_ASSERT(aValue.myType & ValidTypes);
static_assert(MSF_StringFormatType::Typefloat == 32, "Format type indexes changed, this formula no longer works");
// make sure these calculations stay the same as those in GetTypeMaxLength
int const extra = 7; // add sign, decimal place, and extra for exponent (e+999)
if (!(aData.myFlags & PRINT_PRECISION)) aData.myPrecision = 6;
size_t maxLength = size_t(aValue.myType / 2); // max length of floats is 16/32 chars for 32 bit and 64 bit respectively, + '.' '-'
return MSF_IntMax<size_t>(aData.myWidth + aData.myPrecision, maxLength) + extra;
}
template <typename Char>
size_t PrintShared(Char* aBuffer, Char const* aBufferEnd, MSF_PrintData const& aData)
{
double value = aData.myValue->myType == MSF_StringFormatType::Typefloat ? aData.myValue->myfloat : aData.myValue->mydouble;
return MSF_DoubleToString(value, aBuffer, MSF_IntMin<size_t>(aData.myMaxLength, aBufferEnd - aBuffer), aData.myPrintChar, aData.myWidth, aData.myPrecision, aData.myFlags);
}
size_t PrintUTF8(char* aBuffer, char const* aBufferEnd, MSF_PrintData const& aData)
{
return PrintShared(aBuffer, aBufferEnd, aData);
}
size_t PrintUTF16(char16_t* aBuffer, char16_t const* aBufferEnd, MSF_PrintData const& aData)
{
return PrintShared(aBuffer, aBufferEnd, aData);
}
size_t PrintUTF32(char32_t* aBuffer, char32_t const* aBufferEnd, MSF_PrintData const& aData)
{
return PrintShared(aBuffer, aBufferEnd, aData);
}
}