-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStringConv.h
1217 lines (1071 loc) · 38.3 KB
/
StringConv.h
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
// Copyright Epic Games, Inc. All Rights Reserved.
// This file contains the classes used when converting strings between
// standards (ANSI, UNICODE, etc.)
#pragma once
#include "CoreTypes.h"
#include "Misc/AssertionMacros.h"
#include "Containers/ContainerAllocationPolicies.h"
#include "Containers/Array.h"
#include "Misc/CString.h"
#include "Templates/AndOrNot.h"
#include "Templates/EnableIf.h"
#include "Templates/IsArray.h"
#include "Templates/RemoveCV.h"
#include "Templates/RemoveReference.h"
#include "Templates/UnrealTemplate.h"
#include "Templates/UnrealTypeTraits.h"
#include "Traits/ElementType.h"
#include "Traits/IsCharEncodingCompatibleWith.h"
#include "Traits/IsContiguousContainer.h"
#include <type_traits>
#define DEFAULT_STRING_CONVERSION_SIZE 128u
template <typename From, typename To>
class TStringConvert
{
public:
typedef From FromType;
typedef To ToType;
template <
typename CharType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<CharType, FromType>>* = nullptr
>
FORCEINLINE static void Convert(To* Dest, int32 DestLen, const CharType* Source, int32 SourceLen)
{
To* Result = FPlatformString::Convert(Dest, DestLen, (const FromType*)Source, SourceLen);
check(Result);
}
template <
typename CharType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<CharType, FromType>>* = nullptr
>
static int32 ConvertedLength(const CharType* Source, int32 SourceLen)
{
return FPlatformString::ConvertedLength<To>((const FromType*)Source, SourceLen);
}
};
namespace StringConv
{
/** Is the provided Codepoint within the range of valid codepoints? */
static FORCEINLINE bool IsValidCodepoint(const uint32 Codepoint)
{
if ((Codepoint > 0x10FFFF) || // No Unicode codepoints above 10FFFFh, (for now!)
(Codepoint == 0xFFFE) || (Codepoint == 0xFFFF)) // illegal values.
{
return false;
}
return true;
}
/** Is the provided Codepoint within the range of the high-surrogates? */
static FORCEINLINE bool IsHighSurrogate(const uint32 Codepoint)
{
return Codepoint >= HIGH_SURROGATE_START_CODEPOINT && Codepoint <= HIGH_SURROGATE_END_CODEPOINT;
}
/** Is the provided Codepoint within the range of the low-surrogates? */
static FORCEINLINE bool IsLowSurrogate(const uint32 Codepoint)
{
return Codepoint >= LOW_SURROGATE_START_CODEPOINT && Codepoint <= LOW_SURROGATE_END_CODEPOINT;
}
static FORCEINLINE uint32 EncodeSurrogate(const uint16 HighSurrogate, const uint16 LowSurrogate)
{
return ((HighSurrogate - HIGH_SURROGATE_START_CODEPOINT) << 10) + (LowSurrogate - LOW_SURROGATE_START_CODEPOINT) + 0x10000;
}
static FORCEINLINE void DecodeSurrogate(const uint32 Codepoint, uint16& OutHighSurrogate, uint16& OutLowSurrogate)
{
const uint32 TmpCodepoint = Codepoint - 0x10000;
OutHighSurrogate = (uint16)((TmpCodepoint >> 10) + HIGH_SURROGATE_START_CODEPOINT);
OutLowSurrogate = (TmpCodepoint & 0x3FF) + LOW_SURROGATE_START_CODEPOINT;
}
/** Is the provided Codepoint outside of the range of the basic multilingual plane, but within the valid range of UTF8/16? */
static FORCEINLINE bool IsEncodedSurrogate(const uint32 Codepoint)
{
return Codepoint >= ENCODED_SURROGATE_START_CODEPOINT && Codepoint <= ENCODED_SURROGATE_END_CODEPOINT;
}
/** Inline combine any UTF-16 surrogate pairs in the given null-terminated character buffer, returning the updated length */
template<typename CharType>
static FORCEINLINE int32 InlineCombineSurrogates_Buffer(CharType* StrBuffer, int32 StrLen)
{
static_assert(sizeof(CharType) == 4, "CharType must be 4-bytes!");
for (int32 Index = 0; Index < StrLen; ++Index)
{
CharType Codepoint = StrBuffer[Index];
// Check if this character is a high-surrogate
if (StringConv::IsHighSurrogate(Codepoint))
{
// Ensure our string has enough characters to read from
if ((Index + 1) >= StrLen)
{
// Unpaired surrogate - replace with bogus char
StrBuffer[Index] = UNICODE_BOGUS_CHAR_CODEPOINT;
break;
}
const uint32 HighCodepoint = Codepoint;
Codepoint = StrBuffer[Index + 1];
// If our High Surrogate is set, check if this character is the matching low-surrogate
if (StringConv::IsLowSurrogate(Codepoint))
{
const uint32 LowCodepoint = Codepoint;
// Combine our high and low surrogates together to a single Unicode codepoint
Codepoint = StringConv::EncodeSurrogate(HighCodepoint, LowCodepoint);
StrBuffer[Index] = Codepoint;
{
const int32 ArrayNum = StrLen + 1;
const int32 IndexToRemove = Index + 1;
const int32 NumToMove = ArrayNum - IndexToRemove - 1;
if (NumToMove > 0)
{
FMemory::Memmove(StrBuffer + IndexToRemove, StrBuffer + IndexToRemove + 1, NumToMove * sizeof(CharType));
}
}
--StrLen;
continue;
}
// Unpaired surrogate - replace with bogus char
StrBuffer[Index] = UNICODE_BOGUS_CHAR_CODEPOINT;
}
else if (StringConv::IsLowSurrogate(Codepoint))
{
// Unpaired surrogate - replace with bogus char
StrBuffer[Index] = UNICODE_BOGUS_CHAR_CODEPOINT;
}
}
return StrLen;
}
/** Inline combine any UTF-16 surrogate pairs in the given null-terminated TCHAR array */
template<typename AllocatorType>
static FORCEINLINE void InlineCombineSurrogates_Array(TArray<TCHAR, AllocatorType>& StrBuffer)
{
#if PLATFORM_TCHAR_IS_4_BYTES
const int32 NewStrLen = InlineCombineSurrogates_Buffer(StrBuffer.GetData(), StrBuffer.Num() - 1);
StrBuffer.SetNum(NewStrLen + 1, /*bAllowShrinking*/false);
#endif // PLATFORM_TCHAR_IS_4_BYTES
}
struct FUnused;
// Helper functions for getting the conversion type from a converter
template <typename ConverterType>
typename ConverterType::LegacyFromType GetLegacyFromType(typename ConverterType::LegacyFromType*);
template <typename ConverterType>
FUnused GetLegacyFromType(...);
}
namespace UE4StringConv_Private
{
/**
* This is a basic object which counts how many times it has been incremented
*/
struct FCountingOutputIterator
{
FCountingOutputIterator()
: Counter(0)
{
}
const FCountingOutputIterator& operator* () const { return *this; }
const FCountingOutputIterator& operator++() { ++Counter; return *this; }
const FCountingOutputIterator& operator++(int) { ++Counter; return *this; }
const FCountingOutputIterator& operator+=(const int32 Amount) { Counter += Amount; return *this; }
template <typename T>
T operator=(T Val) const
{
return Val;
}
friend int32 operator-(FCountingOutputIterator Lhs, FCountingOutputIterator Rhs)
{
return Lhs.Counter - Rhs.Counter;
}
int32 GetCount() const { return Counter; }
private:
int32 Counter;
};
}
// This should be replaced with Platform stuff when FPlatformString starts to know about UTF-8.
class FTCHARToUTF8_Convert
{
// Leave this until we convert all the call sites and can change FromType to this.
using IntendedToType = UTF8CHAR;
public:
typedef TCHAR FromType;
typedef ANSICHAR ToType;
/**
* Convert Codepoint into UTF-8 characters.
*
* @param Codepoint Codepoint to expand into UTF-8 bytes
* @param OutputIterator Output iterator to write UTF-8 bytes into
* @param OutputIteratorByteSizeRemaining Maximum number of ANSI characters that can be written to OutputIterator
* @return Number of characters written for Codepoint
*/
template <typename BufferType>
static int32 Utf8FromCodepoint(uint32 Codepoint, BufferType OutputIterator, uint32 OutputIteratorByteSizeRemaining)
{
// Ensure we have at least one character in size to write
if (OutputIteratorByteSizeRemaining < sizeof(ToType))
{
return 0;
}
const BufferType OutputIteratorStartPosition = OutputIterator;
if (!StringConv::IsValidCodepoint(Codepoint))
{
Codepoint = UNICODE_BOGUS_CHAR_CODEPOINT;
}
else if (StringConv::IsHighSurrogate(Codepoint) || StringConv::IsLowSurrogate(Codepoint)) // UTF-8 Characters are not allowed to encode codepoints in the surrogate pair range
{
Codepoint = UNICODE_BOGUS_CHAR_CODEPOINT;
}
// Do the encoding...
if (Codepoint < 0x80)
{
*(OutputIterator++) = (ToType)Codepoint;
}
else if (Codepoint < 0x800)
{
if (OutputIteratorByteSizeRemaining >= 2)
{
*(OutputIterator++) = (ToType)((Codepoint >> 6) | 128 | 64);
*(OutputIterator++) = (ToType)((Codepoint & 0x3F) | 128);
}
}
else if (Codepoint < 0x10000)
{
if (OutputIteratorByteSizeRemaining >= 3)
{
*(OutputIterator++) = (ToType) ((Codepoint >> 12) | 128 | 64 | 32);
*(OutputIterator++) = (ToType)(((Codepoint >> 6) & 0x3F) | 128);
*(OutputIterator++) = (ToType) ((Codepoint & 0x3F) | 128);
}
}
else
{
if (OutputIteratorByteSizeRemaining >= 4)
{
*(OutputIterator++) = (ToType) ((Codepoint >> 18) | 128 | 64 | 32 | 16);
*(OutputIterator++) = (ToType)(((Codepoint >> 12) & 0x3F) | 128);
*(OutputIterator++) = (ToType)(((Codepoint >> 6 ) & 0x3F) | 128);
*(OutputIterator++) = (ToType) ((Codepoint & 0x3F) | 128);
}
}
return UE_PTRDIFF_TO_INT32(OutputIterator - OutputIteratorStartPosition);
}
/**
* Converts a Source string into UTF8 and stores it in Dest.
*
* @param Dest The destination output iterator. Usually ANSICHAR*, but you can supply your own output iterator.
* One can determine the number of characters written by checking the offset of Dest when the function returns.
* @param DestLen The length of the destination buffer.
* @param Source The source string to convert.
* @param SourceLen The length of the source string.
* @return The number of bytes written to Dest, up to DestLen, or -1 if the entire Source string could did not fit in DestLen bytes.
*/
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
static FORCEINLINE int32 Convert(IntendedToType* Dest, int32 DestLen, const SrcBufferType* Source, int32 SourceLen)
{
IntendedToType* Result = FPlatformString::Convert(Dest, DestLen, (const FromType*)Source, SourceLen);
if (!Result)
{
return -1;
}
return (int32)(Result - Dest);
}
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
static FORCEINLINE int32 Convert(ToType* Dest, int32 DestLen, const SrcBufferType* Source, int32 SourceLen)
{
// This helper function is only for backwards compatibility and won't be necessary once IntendedToType becomes the ToType
return FTCHARToUTF8_Convert::Convert((IntendedToType*)Dest, DestLen, Source, SourceLen);
}
/**
* Determines the length of the converted string.
*
* @return The length of the string in UTF-8 code units.
*/
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
static FORCEINLINE int32 ConvertedLength(const SrcBufferType* Source, int32 SourceLen)
{
return FPlatformString::ConvertedLength<IntendedToType>((const FromType*)Source, SourceLen);
}
};
class FUTF8ToTCHAR_Convert
{
public:
typedef ANSICHAR LegacyFromType;
typedef UTF8CHAR FromType;
typedef TCHAR ToType;
/**
* Converts the UTF-8 string to UTF-16 or UTF-32.
*
* @param Dest The destination buffer of the converted string.
* @param DestLen The length of the destination buffer.
* @param Source The source string to convert.
* @param SourceLen The length of the source string.
*/
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
static FORCEINLINE void Convert(ToType* Dest, const int32 DestLen, const SrcBufferType* Source, const int32 SourceLen)
{
FPlatformString::Convert(Dest, DestLen, (const FromType*)Source, SourceLen);
}
static FORCEINLINE void Convert(ToType* Dest, const int32 DestLen, const LegacyFromType* Source, const int32 SourceLen)
{
FPlatformString::Convert(Dest, DestLen, (const FromType*)Source, SourceLen);
}
/**
* Determines the length of the converted string.
*
* @param Source string to read and determine amount of TCHARs to represent
* @param SourceLen Length of source string; we will not read past this amount, even if the characters tell us to
* @return The length of the string in UTF-16 or UTF-32 characters.
*/
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
static int32 ConvertedLength(const SrcBufferType* Source, const int32 SourceLen)
{
return FPlatformString::ConvertedLength<ToType>((const FromType*)Source, SourceLen);
}
static int32 ConvertedLength(const LegacyFromType* Source, const int32 SourceLen)
{
return FPlatformString::ConvertedLength<ToType>((const FromType*)Source, SourceLen);
}
};
template<typename InFromType, typename InToType>
class TUTF32ToUTF16_Convert
{
static_assert(sizeof(InFromType) == 4, "FromType must be 4 bytes!");
static_assert(sizeof(InToType) == 2, "ToType must be 2 bytes!");
public:
typedef InFromType FromType;
typedef InToType ToType;
/**
* Convert Codepoint into UTF-16 characters.
*
* @param Codepoint Codepoint to expand into UTF-16 code units
* @param OutputIterator Output iterator to write UTF-16 code units into
* @param OutputIteratorNumRemaining Maximum number of UTF-16 code units that can be written to OutputIterator
* @return Number of characters written for Codepoint
*/
template <typename BufferType>
static int32 Utf16FromCodepoint(uint32 Codepoint, BufferType OutputIterator, uint32 OutputIteratorNumRemaining)
{
// Ensure we have at least one character in size to write
if (OutputIteratorNumRemaining < 1)
{
return 0;
}
const BufferType OutputIteratorStartPosition = OutputIterator;
if (!StringConv::IsValidCodepoint(Codepoint))
{
Codepoint = UNICODE_BOGUS_CHAR_CODEPOINT;
}
else if (StringConv::IsHighSurrogate(Codepoint) || StringConv::IsLowSurrogate(Codepoint)) // Surrogate pairs range should not be present in UTF-32
{
Codepoint = UNICODE_BOGUS_CHAR_CODEPOINT;
}
// We want to write out two chars
if (StringConv::IsEncodedSurrogate(Codepoint))
{
// We need two characters to write the surrogate pair
if (OutputIteratorNumRemaining >= 2)
{
uint16 HighSurrogate = 0;
uint16 LowSurrogate = 0;
StringConv::DecodeSurrogate(Codepoint, HighSurrogate, LowSurrogate);
*(OutputIterator++) = (ToType)HighSurrogate;
*(OutputIterator++) = (ToType)LowSurrogate;
}
}
else if (Codepoint > ENCODED_SURROGATE_END_CODEPOINT)
{
// Ignore values higher than the supplementary plane range
*(OutputIterator++) = UNICODE_BOGUS_CHAR_CODEPOINT;
}
else
{
// Normal codepoint
*(OutputIterator++) = (ToType)Codepoint;
}
return UE_PTRDIFF_TO_INT32(OutputIterator - OutputIteratorStartPosition);
}
/**
* Converts the string to the desired format.
*
* @param Dest The destination buffer of the converted string.
* @param DestLen The length of the destination buffer.
* @param Source The source string to convert.
* @param SourceLen The length of the source string.
*/
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
static FORCEINLINE void Convert(ToType* Dest, int32 DestLen, const SrcBufferType* Source, int32 SourceLen)
{
Convert_Impl(Dest, DestLen, (const FromType*)Source, SourceLen);
}
/**
* Determines the length of the converted string.
*
* @return The length of the string in UTF-16 code units.
*/
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
static FORCEINLINE int32 ConvertedLength(const SrcBufferType* Source, int32 SourceLen)
{
UE4StringConv_Private::FCountingOutputIterator Dest;
const int32 DestLen = SourceLen * 2;
Convert_Impl(Dest, DestLen, (const FromType*)Source, SourceLen);
return Dest.GetCount();
}
private:
template <typename DestBufferType>
static void Convert_Impl(DestBufferType& Dest, int32 DestLen, const FromType* Source, const int32 SourceLen)
{
for (int32 i = 0; i < SourceLen; ++i)
{
uint32 Codepoint = static_cast<uint32>(Source[i]);
if (!WriteCodepointToBuffer(Codepoint, Dest, DestLen))
{
// Could not write data, bail out
return;
}
}
}
template <typename DestBufferType>
static bool WriteCodepointToBuffer(const uint32 Codepoint, DestBufferType& Dest, int32& DestLen)
{
int32 WrittenChars = Utf16FromCodepoint(Codepoint, Dest, DestLen);
if (WrittenChars < 1)
{
return false;
}
Dest += WrittenChars;
DestLen -= WrittenChars;
return true;
}
};
template<typename InFromType, typename InToType>
class TUTF16ToUTF32_Convert
{
static_assert(sizeof(InFromType) == 2, "FromType must be 2 bytes!");
static_assert(sizeof(InToType) == 4, "ToType must be 4 bytes!");
public:
typedef InFromType FromType;
typedef InToType ToType;
/**
* Converts the UTF-16 string to UTF-32.
*
* @param Dest The destination buffer of the converted string.
* @param DestLen The length of the destination buffer.
* @param Source The source string to convert.
* @param SourceLen The length of the source string.
*/
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
static FORCEINLINE void Convert(ToType* Dest, const int32 DestLen, const SrcBufferType* Source, const int32 SourceLen)
{
Convert_Impl(Dest, DestLen, (const FromType*)Source, SourceLen);
}
/**
* Determines the length of the converted string.
*
* @param Source string to read and determine amount of ToType chars to represent
* @param SourceLen Length of source string; we will not read past this amount, even if the characters tell us to
* @return The length of the string in UTF-32 characters.
*/
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
static int32 ConvertedLength(const SrcBufferType* Source, const int32 SourceLen)
{
UE4StringConv_Private::FCountingOutputIterator Dest;
Convert_Impl(Dest, MAX_int32, (const FromType*)Source, SourceLen);
return Dest.GetCount();
}
private:
static uint32 CodepointFromUtf16(const FromType*& SourceString, const uint32 SourceLengthRemaining)
{
checkSlow(SourceLengthRemaining > 0);
const FromType* CodeUnitPtr = SourceString;
uint32 Codepoint = *CodeUnitPtr;
// Check if this character is a high-surrogate
if (StringConv::IsHighSurrogate(Codepoint))
{
// Ensure our string has enough characters to read from
if (SourceLengthRemaining < 2)
{
// Skip to end and write out a single char (we always have room for at least 1 char)
SourceString += SourceLengthRemaining;
return UNICODE_BOGUS_CHAR_CODEPOINT;
}
const uint16 HighSurrogate = (uint16)Codepoint;
Codepoint = *(++CodeUnitPtr);
// If our High Surrogate is set, check if this character is the matching low-surrogate
if (StringConv::IsLowSurrogate(Codepoint))
{
const uint16 LowSurrogate = (uint16)Codepoint;
// Combine our high and low surrogates together to a single Unicode codepoint
Codepoint = StringConv::EncodeSurrogate(HighSurrogate, LowSurrogate);
SourceString += 2; // skip to next possible start of codepoint.
return Codepoint;
}
else
{
++SourceString; // Did not find matching low-surrogate, write out a bogus character and continue
return UNICODE_BOGUS_CHAR_CODEPOINT;
}
}
else if (StringConv::IsLowSurrogate(Codepoint))
{
// Unpaired low surrogate
++SourceString;
return UNICODE_BOGUS_CHAR_CODEPOINT;
}
else
{
// Single codepoint
++SourceString;
return Codepoint;
}
}
/**
* Read Source string, converting the data from UTF-16 into UTF-32, and placing these in the Destination
*/
template <typename DestBufferType>
static void Convert_Impl(DestBufferType& ConvertedBuffer, int32 DestLen, const FromType* Source, const int32 SourceLen)
{
const FromType* SourceEnd = Source + SourceLen;
while (Source < SourceEnd && DestLen > 0)
{
// Read our codepoint, advancing the source pointer
uint32 Codepoint = CodepointFromUtf16(Source, SourceEnd - Source);
*(ConvertedBuffer++) = Codepoint;
--DestLen;
}
}
};
struct ENullTerminatedString
{
enum Type
{
No = 0,
Yes = 1
};
};
/**
* Class takes one type of string and converts it to another. The class includes a
* chunk of presized memory of the destination type. If the presized array is
* too small, it mallocs the memory needed and frees on the class going out of
* scope.
*/
template<typename Converter, int32 DefaultConversionSize = DEFAULT_STRING_CONVERSION_SIZE>
class TStringConversion : private Converter, private TInlineAllocator<DefaultConversionSize>::template ForElementType<typename Converter::ToType>
{
typedef typename TInlineAllocator<DefaultConversionSize>::template ForElementType<typename Converter::ToType> AllocatorType;
typedef typename Converter::FromType FromType;
typedef typename Converter::ToType ToType;
using LegacyFromType = decltype(StringConv::GetLegacyFromType<Converter>(nullptr));
/**
* Converts the data by using the Convert() method on the base class
*/
void Init(const FromType* Source, int32 SourceLen, ENullTerminatedString::Type NullTerminated)
{
StringLength = Converter::ConvertedLength(Source, SourceLen);
int32 BufferSize = StringLength + NullTerminated;
AllocatorType::ResizeAllocation(0, BufferSize, sizeof(ToType));
Ptr = (ToType*)AllocatorType::GetAllocation();
Converter::Convert(Ptr, BufferSize, Source, SourceLen + NullTerminated);
}
public:
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
explicit TStringConversion(const SrcBufferType* Source)
{
if (Source)
{
Init((const FromType*)Source, TCString<FromType>::Strlen((const FromType*)Source), ENullTerminatedString::Yes);
}
else
{
Ptr = nullptr;
StringLength = 0;
}
}
explicit TStringConversion(const LegacyFromType* Source)
: TStringConversion((const FromType*)Source)
{
}
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
TStringConversion(const SrcBufferType* Source, int32 SourceLen)
{
if (Source)
{
ENullTerminatedString::Type NullTerminated = ENullTerminatedString::No;
if (SourceLen > 0 && ((const FromType*)Source)[SourceLen-1] == 0)
{
// Given buffer is null-terminated
NullTerminated = ENullTerminatedString::Yes;
SourceLen -= 1;
}
Init((const FromType*)Source, SourceLen, NullTerminated);
}
else
{
Ptr = nullptr;
StringLength = 0;
}
}
TStringConversion(const LegacyFromType* Source, int32 SourceLen)
: TStringConversion((const FromType*)Source, SourceLen)
{
}
/**
* Construct from a compatible character range such as TStringView or TStringBuilder.
*/
template <
typename FromRangeType,
typename FromRangeCharType = std::remove_cv_t<std::remove_pointer_t<decltype(GetData(DeclVal<FromRangeType>()))>>,
std::enable_if_t<
TAnd<
TIsContiguousContainer<FromRangeType>,
TNot<TIsArray<typename TRemoveReference<FromRangeType>::Type>>
>::Value &&
TIsCharEncodingCompatibleWith<FromRangeCharType, FromType>()
>* = nullptr
>
TStringConversion(FromRangeType&& Source)
: TStringConversion((const FromType*)GetData(Source), GetNum(Source))
{
}
/**
* Move constructor
*/
TStringConversion(TStringConversion&& Other)
: Converter(MoveTemp(ImplicitConv<Converter&>(Other)))
, Ptr(Other.Ptr)
, StringLength(Other.StringLength)
{
AllocatorType::MoveToEmpty(Other);
Other.Ptr = nullptr;
Other.StringLength = 0;
}
/**
* Accessor for the converted string.
* @note The string may not be null-terminated if constructed from an explicitly sized buffer that didn't include the null-terminator.
*
* @return A const pointer to the converted string.
*/
FORCEINLINE const ToType* Get() const
{
return Ptr;
}
/**
* Length of the converted string.
*
* @return The number of characters in the converted string, excluding any null terminator.
*/
FORCEINLINE int32 Length() const
{
return StringLength;
}
private:
// Non-copyable
TStringConversion(const TStringConversion&) = delete;
TStringConversion& operator=(const TStringConversion&) = delete;
ToType* Ptr;
int32 StringLength;
};
template <typename Converter, int32 DefaultConversionSize>
inline auto GetData(const TStringConversion<Converter, DefaultConversionSize>& Conversion) -> decltype(Conversion.Get())
{
return Conversion.Get();
}
template <typename Converter, int32 DefaultConversionSize>
inline auto GetNum(const TStringConversion<Converter, DefaultConversionSize>& Conversion) -> decltype(Conversion.Length())
{
return Conversion.Length();
}
template <typename Converter, int32 DefaultConversionSize>
struct TIsContiguousContainer<TStringConversion<Converter, DefaultConversionSize>>
{
static constexpr bool Value = true;
};
template <typename Converter, int32 DefaultConversionSize>
struct TElementType<TStringConversion<Converter, DefaultConversionSize>>
{
using Type = typename Converter::ToType;
};
/**
* Class takes one type of string and and stores it as-is.
* For API compatibility with TStringConversion when the To and From types are already in the same format.
*/
template<typename InFromType, typename InToType = InFromType>
class TStringPointer
{
static_assert(sizeof(InFromType) == sizeof(InToType), "FromType must be the same size as ToType!");
public:
typedef InFromType FromType;
typedef InToType ToType;
public:
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
explicit TStringPointer(const SrcBufferType* Source)
{
if (Source)
{
Ptr = (const ToType*)Source;
StringLength = -1; // Length calculated on-demand
}
else
{
Ptr = nullptr;
StringLength = 0;
}
}
template <
typename SrcBufferType,
std::enable_if_t<TIsCharEncodingCompatibleWith_V<SrcBufferType, FromType>>* = nullptr
>
TStringPointer(const SrcBufferType* Source, int32 SourceLen)
{
if (Source)
{
if (SourceLen > 0 && ((const FromType*)Source)[SourceLen-1] == 0)
{
// Given buffer is null-terminated
SourceLen -= 1;
}
Ptr = (const ToType*)Source;
StringLength = SourceLen;
}
else
{
Ptr = nullptr;
StringLength = 0;
}
}
/**
* Construct from a compatible character range such as TStringView or TStringBuilder.
*/
template <
typename FromRangeType,
typename FromRangeCharType = std::remove_cv_t<std::remove_pointer_t<decltype(GetData(DeclVal<FromRangeType>()))>>,
std::enable_if_t<
TAnd<
TIsContiguousContainer<FromRangeType>,
TNot<TIsArray<typename TRemoveReference<FromRangeType>::Type>>
>::Value &&
TIsCharEncodingCompatibleWith<FromRangeCharType, FromType>()
>* = nullptr
>
TStringPointer(FromRangeType&& Source)
: TStringPointer((const FromType*)GetData(Source), GetNum(Source))
{
}
/**
* Move constructor
*/
TStringPointer(TStringPointer&& Other) = default;
/**
* Accessor for the string.
* @note The string may not be null-terminated if constructed from an explicitly sized buffer that didn't include the null-terminator.
*
* @return A const pointer to the string.
*/
FORCEINLINE const ToType* Get() const
{
return Ptr;
}
/**
* Length of the string.
*
* @return The number of characters in the string, excluding any null terminator.
*/
FORCEINLINE int32 Length() const
{
return StringLength == -1 ? TCString<ToType>::Strlen(Ptr) : StringLength;
}
private:
// Non-copyable
TStringPointer(const TStringPointer&) = delete;
TStringPointer& operator=(const TStringPointer&) = delete;
const ToType* Ptr;
int32 StringLength;
};
template <typename FromType, typename ToType>
inline auto GetData(const TStringPointer<FromType, ToType>& Pointer) -> decltype(Pointer.Get())
{
return Pointer.Get();
}
template <typename FromType, typename ToType>
inline auto GetNum(const TStringPointer<FromType, ToType>& Pointer) -> decltype(Pointer.Length())
{
return Pointer.Length();
}
template <typename FromType, typename ToType>
struct TIsContiguousContainer<TStringPointer<FromType, ToType>>
{
static constexpr bool Value = true;
};
template <typename FromType, typename ToType>
struct TElementType<TStringPointer<FromType, ToType>>
{
using Type = ToType;
};
/**
* NOTE: The objects these macros declare have very short lifetimes. They are
* meant to be used as parameters to functions. You cannot assign a variable
* to the contents of the converted string as the object will go out of
* scope and the string released.
*
* NOTE: The parameter you pass in MUST be a proper string, as the parameter
* is typecast to a pointer. If you pass in a char, not char* it will compile
* and then crash at runtime.
*
* Usage:
*
* SomeApi(TCHAR_TO_ANSI(SomeUnicodeString));
*
* const char* SomePointer = TCHAR_TO_ANSI(SomeUnicodeString); <--- Bad!!!
*/
// These should be replaced with StringCasts when FPlatformString starts to know about UTF-8.
typedef TStringConversion<FTCHARToUTF8_Convert> FTCHARToUTF8;
typedef TStringConversion<FUTF8ToTCHAR_Convert> FUTF8ToTCHAR;
// Usage of these should be replaced with StringCasts.
#define TCHAR_TO_ANSI(str) (ANSICHAR*)StringCast<ANSICHAR>(static_cast<const TCHAR*>(str)).Get()
#define ANSI_TO_TCHAR(str) (TCHAR*)StringCast<TCHAR>(static_cast<const ANSICHAR*>(str)).Get()
#define TCHAR_TO_UTF8(str) (ANSICHAR*)FTCHARToUTF8((const TCHAR*)str).Get()
#define UTF8_TO_TCHAR(str) (TCHAR*)FUTF8ToTCHAR((const ANSICHAR*)str).Get()
// special handling for platforms still using a 32-bit TCHAR
#if PLATFORM_TCHAR_IS_4_BYTES
typedef TStringConversion<TUTF32ToUTF16_Convert<TCHAR, UTF16CHAR>> FTCHARToUTF16;
typedef TStringConversion<TUTF16ToUTF32_Convert<UTF16CHAR, TCHAR>> FUTF16ToTCHAR;
#define TCHAR_TO_UTF16(str) (UTF16CHAR*)FTCHARToUTF16((const TCHAR*)str).Get()
#define UTF16_TO_TCHAR(str) (TCHAR*)FUTF16ToTCHAR((const UTF16CHAR*)str).Get()
static_assert(sizeof(TCHAR) == sizeof(UTF32CHAR), "TCHAR and UTF32CHAR are expected to be the same size for inline conversion! PLATFORM_TCHAR_IS_4_BYTES is not configured correctly for this platform.");
typedef TStringPointer<TCHAR, UTF32CHAR> FTCHARToUTF32;
typedef TStringPointer<UTF32CHAR, TCHAR> FUTF32ToTCHAR;
#define TCHAR_TO_UTF32(str) (UTF32CHAR*)(str)
#define UTF32_TO_TCHAR(str) (TCHAR*)(str)
#else
static_assert(sizeof(TCHAR) == sizeof(UTF16CHAR), "TCHAR and UTF16CHAR are expected to be the same size for inline conversion! PLATFORM_TCHAR_IS_4_BYTES is not configured correctly for this platform.");
typedef TStringPointer<TCHAR, UTF16CHAR> FTCHARToUTF16;
typedef TStringPointer<UTF16CHAR, TCHAR> FUTF16ToTCHAR;
#define TCHAR_TO_UTF16(str) (UTF16CHAR*)(str)
#define UTF16_TO_TCHAR(str) (TCHAR*)(str)
typedef TStringConversion<TUTF16ToUTF32_Convert<TCHAR, UTF32CHAR>> FTCHARToUTF32;
typedef TStringConversion<TUTF32ToUTF16_Convert<UTF32CHAR, TCHAR>> FUTF32ToTCHAR;
#define TCHAR_TO_UTF32(str) (UTF32CHAR*)FTCHARToUTF32((const TCHAR*)str).Get()
#define UTF32_TO_TCHAR(str) (TCHAR*)FUTF32ToTCHAR((const UTF32CHAR*)str).Get()