-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathmormot.core.unicode.pas
11243 lines (10325 loc) · 368 KB
/
mormot.core.unicode.pas
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
/// Framework Core Low-Level Unicode UTF-8 UTF-16 Ansi Conversion
// - this unit is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
unit mormot.core.unicode;
{
*****************************************************************************
Efficient Unicode Conversion Classes shared by all framework units
- UTF-8 Efficient Encoding / Decoding
- Cross-Platform Charset and CodePage Support
- UTF-8 / UTF-16 / Ansi Conversion Classes
- Text File Loading with BOM/Unicode Support
- Low-Level String Conversion Functions
- Text Case-(in)sensitive Conversion and Comparison
- UTF-8 String Manipulation Functions
- TRawUtf8DynArray Processing Functions
- Operating-System Independent Unicode Process
*****************************************************************************
}
interface
{$I ..\mormot.defines.inc}
uses
classes,
sysutils,
mormot.core.base,
mormot.core.os;
{ *************** UTF-8 Efficient Encoding / Decoding }
// some constants used for UTF-8 conversion, including surrogates
type
/// define a lookup table for efficient UTF-8 processing
// - supports the full original UTF-8 range, even if only the U+0000..U+10FFFF
// range (the UTF-16 accessible range) is defined since RFC 3629 (Nov 2003)
// - see http://floodyberry.wordpress.com/2007/04/14/utf-8-conversion-tricks
{$ifdef USERECORDWITHMETHODS}
TUtf8Table = record
{$else}
TUtf8Table = object
{$endif USERECORDWITHMETHODS}
public
/// allow GetHighUtf8Ucs4() to validate and decode an UTF-8 sequence
Extra: array[0..5] of record
offset, minimum: cardinal;
end;
/// the first UTF-8 byte depending on its target length (extra + 1)
FirstByte: array[2..6] of byte;
/// the number of extra bytes in addition to the first UTF-8 byte
// - since RFC 3629, only values within the 0..3 range should appear, i.e.
// up to UTF8_MAXUTF16 within the UTF-16 surrogates range
Lookup: TByteToByte;
/// retrieve a >127 UCS-4 CodePoint from an UTF-8 sequence
function GetHighUtf8Ucs4(var U: PUtf8Char): Ucs4CodePoint;
end;
PUtf8Table = ^TUtf8Table;
const
/// TUtf8Table.Lookup[] value for a 7-bit ASCII character
UTF8_ASCII = 0;
/// maximum TUtf8Table.Lookup[] value within UTF-16 accessible range
// - this unit support the full original UTF-8 range, but this constant could
// be used to ensure RFC 3629 expectations, as used e.g. by IsValidUtf8() and
// most UTF-16 software or language (e.g. Windows, Java, JavaScript...)
UTF8_MAXUTF16 = 3;
/// impossible TUtf8Table.Lookup[] value
UTF8_INVALID = 6;
/// special encoding of ending #0 in TUtf8Table.Lookup[]
UTF8_ZERO = 7;
/// constant lookup table for efficient UTF-8 processing
UTF8_TABLE: TUtf8Table = (
Extra: (
(offset: $00000000; minimum: $00010000), // 0: 0000 0000 - 0000 007F
(offset: $00003080; minimum: $00000080), // 1: 0000 0080 - 0000 07FF
(offset: $000e2080; minimum: $00000800), // 2: 0000 0800 - 0000 FFFF
(offset: $03c82080; minimum: $00010000), // 3: 0001 0000 - 0010 FFFF
(offset: $fa082080; minimum: $00200000), // 4: outside UTF-16 range
(offset: $82082080; minimum: $04000000)); // 5: outside UTF-16 range
FirstByte: (
$c0, $e0, $f0, $f8, $fc);
Lookup: (
7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6)
);
UTF8_NEED_UTF16_SURROGATES = 3; // 4 UTF-8 bytes trigger surrogates in UTF-16
UTF8_EXTRA1_OFFSET = $00003080; // = UTF8_TABLE.Extra[1].offset for $0..$7ff
UTF16_HISURROGATE_MIN = $d800;
UTF16_HISURROGATE_MAX = $dbff;
UTF16_LOSURROGATE_MIN = $dc00;
UTF16_LOSURROGATE_MAX = $dfff;
UTF16_SURROGATE_OFFSET = $d7c0;
/// replace any incoming UCS-4 which is unrepresentable as a single WideChar
// - i.e. which would need a UTF-16 surrogates pair for proper encoding
// - set e.g. by GetUtf8WideChar(), Utf8UpperReference() or
// RawUnicodeToUtf8() when ccfReplacementCharacterForUnmatchedSurrogate is set
// - encoded as $ef $bf $bd bytes in UTF-8
UNICODE_REPLACEMENT_CHARACTER = $fffd;
/// internal function, used to retrieve a >127 US4 CodePoint from UTF-8
// - not to be called directly, but from inlined higher-level functions
// - here U^ shall be always >= #80
// - typical use is as such:
// ! ch := ord(P^);
// ! if ch and $80=0 then
// ! inc(P) else
// ! ch := GetHighUtf8Ucs4(P);
function GetHighUtf8Ucs4(var U: PUtf8Char): Ucs4CodePoint;
{$ifdef HASINLINE}inline;{$endif}
/// decode UTF-16 WideChar from UTF-8 input buffer
// - any surrogate (Ucs4>$ffff) is returned as UNICODE_REPLACEMENT_CHARACTER=$fffd
function GetUtf8WideChar(P: PUtf8Char): cardinal;
{$ifdef HASINLINE}inline;{$endif}
/// get the UCS-4 CodePoint stored in P^ (decode UTF-8 if necessary)
function NextUtf8Ucs4(var P: PUtf8Char): Ucs4CodePoint;
{$ifdef HASINLINE}inline;{$endif}
/// UTF-8 encode one UTF-16 encoded UCS-4 CodePoint into Dest
// - return the number of bytes written into Dest (i.e. from 1 up to 6)
// - Source will contain the next UTF-16 character
// - this method DOES properly handle UTF-16 surrogate pairs
function Utf16CharToUtf8(Dest: PUtf8Char; var Source: PWord): integer;
/// UTF-8 encode one UCS-4 CodePoint into Dest
// - return the number of bytes written into Dest (i.e. from 1 up to 6)
// - this method DOES properly handle UTF-16 surrogate pairs
function Ucs4ToUtf8(ucs4: Ucs4CodePoint; Dest: PUtf8Char): PtrInt;
{$ifdef HASINLINE}inline;{$endif}
type
/// option set for RawUnicodeToUtf8() conversion
TCharConversionFlags = set of (
ccfNoTrailingZero,
ccfReplacementCharacterForUnmatchedSurrogate);
/// convert a UTF-16 PWideChar buffer into a UTF-8 string
procedure RawUnicodeToUtf8(WideChar: PWideChar; WideCharCount: integer;
var result: RawUtf8; Flags: TCharConversionFlags = [ccfNoTrailingZero]); overload;
/// convert a UTF-16 PWideChar buffer into a UTF-8 temporary buffer
procedure RawUnicodeToUtf8(WideChar: PWideChar; WideCharCount: integer;
var result: TSynTempBuffer; Flags: TCharConversionFlags); overload;
/// convert a UTF-16 PWideChar buffer into a UTF-8 string
function RawUnicodeToUtf8(WideChar: PWideChar; WideCharCount: integer;
Flags: TCharConversionFlags = [ccfNoTrailingZero]): RawUtf8; overload;
{$ifdef HASINLINE}inline;{$endif}
/// convert a UTF-16 PWideChar buffer into a UTF-8 buffer
// - replace system.UnicodeToUtf8 implementation, which is rather slow
// since Delphi 2009+
// - append a #0 terminator to the ending PUtf8Char, unless ccfNoTrailingZero is set
// - if ccfReplacementCharacterForUnmatchedSurrogate is set, this function will identify
// unmatched surrogate pairs and replace them with UNICODE_REPLACEMENT_CHARACTER -
// see https://en.wikipedia.org/wiki/Specials_(Unicode_block)
function RawUnicodeToUtf8(Dest: PUtf8Char; DestLen: PtrInt;
Source: PWideChar; SourceLen: PtrInt; Flags: TCharConversionFlags): PtrInt; overload;
/// convert a UTF-16 PWideChar buffer into a UTF-8 string
// - this version doesn't resize the resulting RawUtf8 string, but return
// the new resulting RawUtf8 byte count into Utf8Length
function RawUnicodeToUtf8(WideChar: PWideChar; WideCharCount: integer;
out Utf8Length: integer): RawUtf8; overload;
/// convert an UTF-8 encoded text into a WideChar (UTF-16) buffer
// - faster than System.Utf8ToUnicode
// - sourceBytes can by 0, therefore length is computed from zero terminated source
// - enough place must be available in dest buffer (guess is sourceBytes*3+2)
// - a WideChar(#0) is added at the end (if something is written) unless
// NoTrailingZero is TRUE
// - returns the BYTE count written in dest, excluding the ending WideChar(#0)
function Utf8ToWideChar(dest: PWideChar; source: PUtf8Char; sourceBytes: PtrInt = 0;
NoTrailingZero: boolean = false): PtrInt; overload;
/// convert an UTF-8 encoded text into a WideChar (UTF-16) buffer
// - faster than System.Utf8ToUnicode
// - this overloaded function expect a MaxDestChars parameter
// - sourceBytes can not be 0 for this function
// - enough place must be available in dest buffer (guess is sourceBytes*3+2)
// - a WideChar(#0) is added at the end (if something is written) unless
// NoTrailingZero is TRUE
// - returns the BYTE COUNT (not WideChar count) written in dest, excluding the
// ending WideChar(#0)
function Utf8ToWideChar(dest: PWideChar; source: PUtf8Char;
MaxDestChars, sourceBytes: PtrInt; NoTrailingZero: boolean = false): PtrInt; overload;
/// direct conversion of a UTF-8 encoded buffer into a WinAnsi ShortString buffer
// - non WinAnsi chars are replaced by '?' placeholders
procedure Utf8ToShortString(var dest: ShortString; source: PUtf8Char);
/// calculate the UTF-16 Unicode characters count, UTF-8 encoded in source^
// - count may not match the UCS-4 CodePoint, in case of UTF-16 surrogates
// - faster than System.Utf8ToUnicode with dest=nil
function Utf8ToUnicodeLength(source: PUtf8Char): PtrUInt;
/// returns TRUE if the supplied buffer has valid UTF-8 encoding
// - on Haswell AVX2 Intel/AMD CPUs, will use very efficient ASM
// - warning: AVX2 version won't refuse #0 characters within the buffer
// - follows RFC 3629 requirements, i.e. up to 4-bytes UTF-8 sequences, to
// stay within U+0000..U+10FFFF UTF-16 accessible range with surrogates
var
IsValidUtf8Buffer: function(source: PUtf8Char; sourcelen: PtrInt): boolean;
/// returns TRUE if the supplied buffer has valid UTF-8 encoding
// - could be called directly on small input, if #0 characters should be refused
function IsValidUtf8Pas(source: PUtf8Char; len: PtrInt): boolean;
/// returns TRUE if the supplied RawUtf8 has valid UTF-8 encoding
// - could be called directly on small input, if #0 characters should be refused
function IsValidUtf8Small(const source: RawByteString): boolean;
{$ifdef HASINLINE}inline;{$endif}
/// returns TRUE if the supplied buffer has valid UTF-8 encoding
// - on Haswell AVX2 Intel/AMD CPUs, will use very efficient ASM, reaching e.g.
// 21 GB/s parsing speed on a Core i5-13500
// - warning: AVX2 version won't refuse #0 characters within the buffer - use
// IsValidUtf8NotVoid() if you are not sure that your input is pure text
function IsValidUtf8(const source: RawByteString): boolean; overload;
{$ifdef HASINLINE}inline;{$endif}
/// returns TRUE if the supplied buffer has valid UTF-8 encoding and no #0 within
// - will also refuse #0 characters within the buffer even on AVX2
function IsValidUtf8NotVoid(source: PUtf8Char; len: PtrInt): boolean; overload;
{$ifdef HASINLINE}{$ifndef ASMX64AVXNOCONST}inline;{$endif}{$endif}
/// returns TRUE if the supplied buffer has valid UTF-8 encoding and no #0 within
// - will also refuse #0 characters within the buffer even on AVX2
function IsValidUtf8NotVoid(const source: RawByteString): boolean; overload;
{$ifdef HASINLINE}inline;{$endif}
/// returns TRUE if the supplied buffer has valid UTF-8 encoding
// - will stop when the buffer contains #0
// - just a wrapper around IsValidUtf8Buffer(source, StrLen(source)) so if you
// know the source length, you would better call IsValidUtf8Buffer() directly
// - on Haswell AVX2 Intel/AMD CPUs, will use very efficient ASM, reaching e.g.
// 15 GB/s parsing speed on a Core i5-13500 - StrLen() itself runs at 37 GB/s
function IsValidUtf8(source: PUtf8Char): boolean; overload;
{$ifdef HASINLINE}inline;{$endif}
/// detect UTF-8 content and mark the variable with the CP_UTF8 codepage
// - to circumvent FPC concatenation bug with CP_UTF8 and CP_RAWBYTESTRING
procedure DetectRawUtf8(var source: RawByteString);
{$ifndef HASCODEPAGE}{$ifdef HASINLINE}inline;{$endif}{$endif}
/// returns TRUE if the supplied buffer has valid UTF-8 encoding with no #1..#31
// control characters
// - supplied input is a pointer to a #0 ended text buffer
function IsValidUtf8WithoutControlChars(source: PUtf8Char): boolean; overload;
/// returns TRUE if the supplied buffer has valid UTF-8 encoding with no #0..#31
// control characters
// - supplied input is a RawUtf8 variable
function IsValidUtf8WithoutControlChars(const source: RawUtf8): boolean; overload;
/// check if any forbidden 7-bit char appears in the supplied text
// - is a wrapper around strcspn()
function ContainsChars(const text, forbidden: RawUtf8): boolean;
/// will truncate the supplied UTF-8 value if its length exceeds the specified
// UTF-16 Unicode characters count
// - count may not match the UCS-4 CodePoint, in case of UTF-16 surrogates
// - returns FALSE if text was not truncated, TRUE otherwise
function Utf8TruncateToUnicodeLength(var text: RawUtf8; maxUtf16: integer): boolean;
/// will truncate the supplied UTF-8 value if its length exceeds the specified
// bytes count
// - this function will ensure that the returned content will contain only valid
// UTF-8 sequence, i.e. will trim the whole trailing UTF-8 sequence
// - returns FALSE if text was not truncated, TRUE otherwise
function Utf8TruncateToLength(var text: RawUtf8; maxBytes: PtrUInt): boolean;
/// compute the truncated length of the supplied UTF-8 value if it exceeds the
// specified bytes count
// - this function will ensure that the returned content will contain only valid
// UTF-8 sequence, i.e. will trim the whole trailing UTF-8 sequence
// - returns maxBytes if text was not truncated, or the number of fitting bytes
function Utf8TruncatedLength(const text: RawUtf8; maxBytes: PtrUInt): PtrInt; overload;
{$ifdef HASINLINE}inline;{$endif}
/// compute the truncated length of the supplied UTF-8 value if it exceeds the
// specified bytes count
// - this function will ensure that the returned content will contain only valid
// UTF-8 sequence, i.e. will trim the whole trailing UTF-8 sequence
// - returns maxBytes if text was not truncated, or the number of fitting bytes
function Utf8TruncatedLength(text: PAnsiChar; textlen, maxBytes: PtrUInt): PtrInt; overload;
/// calculate the UTF-16 Unicode characters count of the UTF-8 encoded first line
// - count may not match the UCS-4 CodePoint, in case of UTF-16 surrogates
// - end the parsing at first #13 or #10 character
function Utf8FirstLineToUtf16Length(source: PUtf8Char): PtrInt;
{ ************** Cross-Platform Charset and CodePage Support }
const
ANSI_CHARSET = 0;
DEFAULT_CHARSET = 1;
SYMBOL_CHARSET = 2;
SHIFTJIS_CHARSET = 128;
HANGEUL_CHARSET = 129;
JOHAB_CHARSET = 130;
GB2312_CHARSET = 134;
CHINESEBIG5_CHARSET = 136;
GREEK_CHARSET = 161;
TURKISH_CHARSET = 162;
VIETNAMESE_CHARSET = 163;
HEBREW_CHARSET = 177;
ARABIC_CHARSET = 178;
BALTIC_CHARSET = 186;
RUSSIAN_CHARSET = 204;
THAI_CHARSET = 222;
EASTEUROPE_CHARSET = 238;
OEM_CHARSET = 255;
/// convert a char set to a code page
function CharSetToCodePage(CharSet: integer): cardinal;
/// convert a code page to a char set
function CodePageToCharSet(CodePage: cardinal): integer;
/// check if a code page is known to be of fixed width, i.e. not MBCS
// - i.e. will be implemented as a TSynAnsiFixedWidth
function IsFixedWidthCodePage(aCodePage: cardinal): boolean;
/// return a code page number into human-friendly text
// - e.g. 'shift_jis' for aCodePage = 932, or 'ms1252' for 1252
// - returns the lowercased Unicode_CodePageName(aCodePage) value
function CodePageToText(aCodePage: cardinal): TShort16;
{ **************** UTF-8 / UTF-16 / Ansi Conversion Classes }
type
/// Exception raised by this unit in case of fatal conversion issue
ESynUnicode = class(ExceptionWithProps);
/// an abstract class to handle Ansi to/from Unicode translation
// - implementations of this class will handle efficiently all CharSets
// - this default implementation will use the Operating System APIs
// - NEVER call Create() constructor directly: use the Engine() factory instead
TSynAnsiConvert = class
protected
fCodePage: cardinal;
fAnsiCharMbcs: boolean;
fAnsiCharShift: byte;
public
/// returns the engine corresponding to a given code page
// - a global list of TSynAnsiConvert instances is handled by the unit -
// therefore, caller should not release the returned instance
// - will return nil in case of unhandled code page
// - is aCodePage is 0, will return CurrentAnsiConvert value
class function Engine(aCodePage: cardinal): TSynAnsiConvert;
{$ifdef HASINLINE} static; {$endif}
/// initialize the internal conversion engine
// - NEVER call this constructor directly: use the Engine() factory instead
constructor Create(aCodePage: cardinal); reintroduce; virtual;
/// direct conversion of a PAnsiChar buffer into an Unicode buffer
// - Dest^ buffer must be reserved with at least SourceChars*2 bytes
// - this default implementation will use the Operating System APIs
// - will append a #0 terminator to the returned PWideChar, unless
// NoTrailingZero is set
function AnsiBufferToUnicode(Dest: PWideChar; Source: PAnsiChar;
SourceChars: cardinal; NoTrailingZero: boolean = false): PWideChar; overload; virtual;
/// direct conversion of a PAnsiChar buffer into a UTF-8 encoded buffer
// - Dest^ buffer must be reserved with at least SourceChars*3 bytes
// - will append a #0 terminator to the returned PUtf8Char, unless
// NoTrailingZero is set
// - this default implementation will use the Operating System APIs
function AnsiBufferToUtf8(Dest: PUtf8Char; Source: PAnsiChar;
SourceChars: cardinal; NoTrailingZero: boolean = false): PUtf8Char; overload; virtual;
{$ifndef PUREMORMOT2}
/// convert any Ansi Text into an UTF-16 Unicode String
// - returns a value using our RawUnicode kind of string
function AnsiToRawUnicode(const AnsiText: RawByteString): RawUnicode; overload;
/// convert any Ansi buffer into an Unicode String
// - returns a value using our RawUnicode kind of string
function AnsiToRawUnicode(
Source: PAnsiChar; SourceChars: cardinal): RawUnicode; overload; virtual;
{$endif PUREMORMOT2}
/// convert any Ansi buffer into an Unicode String
// - returns a SynUnicode, i.e. Delphi 2009+ UnicodeString or a WideString
procedure AnsiToUnicodeStringVar(
Source: PAnsiChar; SourceChars: cardinal; var Result: SynUnicode);
/// convert any Ansi buffer into an Unicode String
// - returns a SynUnicode, i.e. Delphi 2009+ UnicodeString or a WideString
function AnsiToUnicodeString(const Source: RawByteString): SynUnicode;
{$ifdef HASINLINE} inline; {$endif}
/// convert any Ansi Text into an UTF-8 encoded String
// - internally calls AnsiBufferToUtf8 virtual method
function AnsiToUtf8(const AnsiText: RawByteString): RawUtf8; virtual;
/// direct conversion of a PAnsiChar buffer into a UTF-8 encoded string
// - will call AnsiBufferToUnicode() overloaded virtual method
procedure AnsiBufferToRawUtf8(Source: PAnsiChar;
SourceChars: cardinal; out Value: RawUtf8); overload; virtual;
/// direct conversion of an Unicode buffer into a PAnsiChar buffer
// - Dest^ buffer must be reserved with at least SourceChars * 3 bytes
// - will detect and ignore any trailing UTF-16LE BOM marker
// - this default implementation will rely on the Operating System for
// all non ASCII-7 chars
function UnicodeBufferToAnsi(Dest: PAnsiChar; Source: PWideChar;
SourceChars: cardinal): PAnsiChar; virtual;
/// direct conversion of an Unicode buffer into an Ansi Text
procedure UnicodeBufferToAnsiVar(Source: PWideChar;
SourceChars: cardinal; var Result: RawByteString); virtual;
/// convert any Unicode-encoded String into Ansi Text
// - internally calls UnicodeBufferToAnsi virtual method
function UnicodeStringToAnsi(const Source: SynUnicode): RawByteString;
{$ifdef HASINLINE}inline;{$endif}
{$ifndef PUREMORMOT2}
/// convert any Unicode-encoded String into Ansi Text
// - internally calls UnicodeBufferToAnsi virtual method
function RawUnicodeToAnsi(const Source: RawUnicode): RawByteString;
{$endif PUREMORMOT2}
/// direct conversion of an UTF-8 encoded buffer into a PAnsiChar buffer
// - Dest^ buffer must be reserved with at least SourceChars bytes
// - no #0 terminator is appended to the buffer
function Utf8BufferToAnsi(Dest: PAnsiChar;
Source: PUtf8Char; SourceChars: cardinal): PAnsiChar; overload; virtual;
/// convert any UTF-8 encoded buffer into Ansi Text
// - internally calls Utf8BufferToAnsi virtual method
function Utf8BufferToAnsi(Source: PUtf8Char;
SourceChars: cardinal): RawByteString; overload;
{$ifdef HASINLINE}inline;{$endif}
/// convert any UTF-8 encoded buffer into Ansi Text
// - internally calls Utf8BufferToAnsi virtual method
procedure Utf8BufferToAnsi(Source: PUtf8Char; SourceChars: cardinal;
var result: RawByteString); overload; virtual;
/// convert any UTF-8 encoded String into Ansi Text
// - internally calls Utf8BufferToAnsi virtual method
function Utf8ToAnsi(const u: RawUtf8): RawByteString; virtual;
/// direct conversion of a UTF-8 encoded string into a WinAnsi <2KB buffer
// - will truncate the destination string to DestSize bytes (including the
// #0 terminator), with a maximum handled size of 2048 bytes
// - returns the number of bytes stored in Dest^ (i.e. the position of #0)
function Utf8ToAnsiBuffer2K(const S: RawUtf8;
Dest: PAnsiChar; DestSize: integer): integer;
/// convert any Ansi Text (providing a From converted) into Ansi Text
function AnsiToAnsi(From: TSynAnsiConvert;
const Source: RawByteString): RawByteString; overload;
/// convert any Ansi buffer (providing a From converted) into Ansi Text
function AnsiToAnsi(From: TSynAnsiConvert; Source: PAnsiChar;
SourceChars: cardinal): RawByteString; overload;
/// corresponding code page
property CodePage: cardinal
read fCodePage;
/// corresponding length binary shift used for worst conversion case
property AnsiCharShift: byte
read fAnsiCharShift;
/// detect complex MBCS asiatic charsets with escape codes
// - e.g. CP_HZ with ~} ~{ or IEC-2022 with $1b ESC [I..] F
// - i.e. to disable chars < $80 direct assignement optimization
property AnsiCharMbcs: boolean
read fAnsiCharMbcs;
end;
/// a class to handle Ansi to/from Unicode translation of fixed width encoding
// - this class will handle efficiently all Code Page availables without MBCS
// encoding - like WinAnsi (1252) or Russian (1251)
// - it will use internal fast look-up tables for such encodings
// - each instance will consume a bit more than 64 KB of memory
// - this class has some additional methods (e.g. IsValid*) which take
// advantage of the internal lookup tables to provide some fast process
// - NEVER call Create() constructor directly: use the Engine() factory instead
TSynAnsiFixedWidth = class(TSynAnsiConvert)
protected
fAnsiToWide: TWordDynArray;
fWideToAnsi: TByteDynArray;
public
/// initialize the internal conversion engine
// - NEVER call this constructor directly: use the Engine() factory instead
constructor Create(aCodePage: cardinal); override;
/// direct conversion of a PAnsiChar buffer into an Unicode buffer
// - Dest^ buffer must be reserved with at least SourceChars*2 bytes
// - will append a #0 terminator to the returned PWideChar, unless
// NoTrailingZero is set
function AnsiBufferToUnicode(Dest: PWideChar; Source: PAnsiChar;
SourceChars: cardinal; NoTrailingZero: boolean = false): PWideChar; override;
/// direct conversion of a PAnsiChar buffer into a UTF-8 encoded buffer
// - Dest^ buffer must be reserved with at least SourceChars*3 bytes
// - will append a #0 terminator to the returned PUtf8Char, unless
// NoTrailingZero is set
function AnsiBufferToUtf8(Dest: PUtf8Char; Source: PAnsiChar;
SourceChars: cardinal; NoTrailingZero: boolean = false): PUtf8Char; override;
{$ifndef PUREMORMOT2}
/// convert any Ansi buffer into an Unicode String
// - returns a value using our RawUnicode kind of string
function AnsiToRawUnicode(Source: PAnsiChar;
SourceChars: cardinal): RawUnicode; override;
{$endif PUREMORMOT2}
/// direct conversion of an Unicode buffer into a PAnsiChar buffer
// - Dest^ buffer must be reserved with at least SourceChars * 3 bytes
// - will detect and ignore any trailing UTF-16LE BOM marker
// - this overridden version will use internal lookup tables for fast process
function UnicodeBufferToAnsi(Dest: PAnsiChar;
Source: PWideChar; SourceChars: cardinal): PAnsiChar; override;
/// direct conversion of an UTF-8 encoded buffer into a PAnsiChar buffer
// - Dest^ buffer must be reserved with at least SourceChars bytes
// - no #0 terminator is appended to the buffer
// - non Ansi compatible characters are replaced as '?'
function Utf8BufferToAnsi(Dest: PAnsiChar; Source: PUtf8Char;
SourceChars: cardinal): PAnsiChar; override;
/// conversion of a wide char into the corresponding Ansi character
// - return -1 for an unknown WideChar in the current code page
function WideCharToAnsiChar(wc: cardinal): integer;
/// return TRUE if the supplied unicode buffer only contains characters of
// the corresponding Ansi code page
// - i.e. if the text can be displayed using this code page
function IsValidAnsi(WideText: PWideChar; Length: PtrInt): boolean; overload;
/// return TRUE if the supplied unicode buffer only contains characters of
// the corresponding Ansi code page
// - i.e. if the text can be displayed using this code page
function IsValidAnsi(WideText: PWideChar): boolean; overload;
/// return TRUE if the supplied UTF-8 buffer only contains characters of
// the corresponding Ansi code page
// - i.e. if the text can be displayed using this code page
function IsValidAnsiU(Utf8Text: PUtf8Char): boolean;
/// return TRUE if the supplied UTF-8 buffer only contains 8-bit characters
// of the corresponding Ansi code page
// - i.e. if the text can be displayed with only 8-bit unicode characters
// (e.g. no "tm" or such) within this code page
function IsValidAnsiU8Bit(Utf8Text: PUtf8Char): boolean;
/// direct access to the Ansi-To-Unicode lookup table
// - use this array like AnsiToWide: array[byte] of word
property AnsiToWide: TWordDynArray
read fAnsiToWide;
/// direct access to the Unicode-To-Ansi lookup table
// - use this array like WideToAnsi: array[word] of byte
// - any unhandled WideChar will return ord('?')
property WideToAnsi: TByteDynArray
read fWideToAnsi;
end;
/// a class to handle UTF-8 to/from Unicode translation
// - this class is mostly a non-operation for conversion to/from UTF-8
// - NEVER call Create() constructor directly: use the Engine() factory instead
TSynAnsiUtf8 = class(TSynAnsiConvert)
public
/// initialize the internal conversion engine
// - NEVER call this constructor directly: use the Engine() factory instead
constructor Create(aCodePage: cardinal); override;
/// direct conversion of a PAnsiChar UTF-8 buffer into an Unicode buffer
// - Dest^ buffer must be reserved with at least SourceChars*2 bytes
// - will append a #0 terminator to the returned PWideChar, unless
// NoTrailingZero is set
function AnsiBufferToUnicode(Dest: PWideChar; Source: PAnsiChar;
SourceChars: cardinal; NoTrailingZero: boolean = false): PWideChar; override;
/// direct conversion of a PAnsiChar UTF-8 buffer into a UTF-8 encoded buffer
// - Dest^ buffer must be reserved with at least SourceChars*3 bytes
// - will append a #0 terminator to the returned PUtf8Char, unless
// NoTrailingZero is set
function AnsiBufferToUtf8(Dest: PUtf8Char; Source: PAnsiChar;
SourceChars: cardinal; NoTrailingZero: boolean = false): PUtf8Char; override;
{$ifndef PUREMORMOT2}
/// convert any UTF-8 Ansi buffer into an Unicode String
// - returns a value using our RawUnicode kind of string
function AnsiToRawUnicode(Source: PAnsiChar;
SourceChars: cardinal): RawUnicode; override;
{$endif PUREMORMOT2}
/// direct conversion of an Unicode buffer into a PAnsiChar UTF-8 buffer
// - will detect and ignore any trailing UTF-16LE BOM marker
// - Dest^ buffer must be reserved with at least SourceChars * 3 bytes
function UnicodeBufferToAnsi(Dest: PAnsiChar; Source: PWideChar;
SourceChars: cardinal): PAnsiChar; override;
/// direct conversion of an Unicode buffer into an Ansi Text
procedure UnicodeBufferToAnsiVar(Source: PWideChar;
SourceChars: cardinal; var Result: RawByteString); override;
/// direct conversion of an UTF-8 encoded buffer into a PAnsiChar UTF-8 buffer
// - Dest^ buffer must be reserved with at least SourceChars bytes
// - no #0 terminator is appended to the buffer
function Utf8BufferToAnsi(Dest: PAnsiChar; Source: PUtf8Char;
SourceChars: cardinal): PAnsiChar; override;
/// convert any UTF-8 encoded buffer into Ansi Text
procedure Utf8BufferToAnsi(Source: PUtf8Char; SourceChars: cardinal;
var result: RawByteString); override;
/// convert any UTF-8 encoded String into Ansi Text
// - directly assign the input as result, since no conversion is needed
function Utf8ToAnsi(const u: RawUtf8): RawByteString; override;
/// convert any Ansi Text into an UTF-8 encoded String
// - directly assign the input as result, since no conversion is needed
function AnsiToUtf8(const AnsiText: RawByteString): RawUtf8; override;
/// direct conversion of a PAnsiChar buffer into a UTF-8 encoded string
procedure AnsiBufferToRawUtf8(Source: PAnsiChar;
SourceChars: cardinal; out Value: RawUtf8); override;
end;
/// a class to handle UTF-16 to/from Unicode translation
// - even if UTF-16 is not an Ansi format, code page CP_UTF16 may have been
// used to store UTF-16 encoded binary content
// - this class is mostly a non-operation for conversion to/from Unicode
// - NEVER call Create() constructor directly: use the Engine() factory instead
TSynAnsiUtf16 = class(TSynAnsiConvert)
public
/// initialize the internal conversion engine
// - NEVER call this constructor directly: use the Engine() factory instead
constructor Create(aCodePage: cardinal); override;
/// direct conversion of a PAnsiChar UTF-16 buffer into an Unicode buffer
// - Dest^ buffer must be reserved with at least SourceChars*2 bytes
// - will append a #0 terminator to the returned PWideChar, unless
// NoTrailingZero is set
function AnsiBufferToUnicode(Dest: PWideChar; Source: PAnsiChar;
SourceChars: cardinal; NoTrailingZero: boolean = false): PWideChar; override;
/// direct conversion of a PAnsiChar UTF-16 buffer into a UTF-8 encoded buffer
// - Dest^ buffer must be reserved with at least SourceChars*3 bytes
// - will append a #0 terminator to the returned PUtf8Char, unless
// NoTrailingZero is set
function AnsiBufferToUtf8(Dest: PUtf8Char; Source: PAnsiChar;
SourceChars: cardinal; NoTrailingZero: boolean = false): PUtf8Char; override;
{$ifndef PUREMORMOT2}
/// convert any UTF-16 Ansi buffer into an Unicode String
// - returns a value using our RawUnicode kind of string
function AnsiToRawUnicode(Source: PAnsiChar;
SourceChars: cardinal): RawUnicode; override;
{$endif PUREMORMOT2}
/// direct conversion of an Unicode buffer into a PAnsiChar UTF-16 buffer
// - Dest^ buffer must be reserved with at least SourceChars * 3 bytes
function UnicodeBufferToAnsi(Dest: PAnsiChar; Source: PWideChar;
SourceChars: cardinal): PAnsiChar; override;
/// direct conversion of an UTF-8 encoded buffer into a PAnsiChar UTF-16 buffer
// - Dest^ buffer must be reserved with at least SourceChars bytes
// - no #0 terminator is appended to the buffer
function Utf8BufferToAnsi(Dest: PAnsiChar; Source: PUtf8Char;
SourceChars: cardinal): PAnsiChar; override;
end;
var
/// global TSynAnsiConvert instance to handle WinAnsi encoding (code page 1252)
// - this instance is global and created during this unit's initialization
// - it will be created from hard-coded values, and not using the system API,
// since it appeared that some systems (e.g. in Russia) did tweak the registry
// so that 1252 code page maps 1251 code page
WinAnsiConvert: TSynAnsiFixedWidth;
/// global TSynAnsiConvert instance to handle current system encoding
// - this instance is global and created during this unit's initialization
// - this is the encoding as used by the AnsiString type, so will be used
// before Delphi 2009 to speed-up RTL string handling (especially for UTF-8)
CurrentAnsiConvert: TSynAnsiConvert;
/// global TSynAnsiConvert instance to handle UTF-8 encoding (code page CP_UTF8)
// - this instance is global and created during this unit's initialization
Utf8AnsiConvert: TSynAnsiUtf8;
/// global TSynAnsiConvert instance with no encoding (RawByteString/RawBlob)
// - this instance is global and created during this unit's initialization
RawByteStringConvert: TSynAnsiFixedWidth;
{ *************** Text File Loading with BOM/Unicode Support }
type
/// text file layout, as returned by BomFile() and StringFromBomFile()
// - bomNone means there was no BOM recognized
// - bomUtf16LE stands for UTF-16 Little-Endian encoding (as in Windows)
// - bomUtf16BE stands for UTF-16 Big-Endian encoding
// - bomUtf8 stands for a UTF-8 BOM (as on Windows products)
TBomFile = (
bomNone,
bomUtf16LE,
bomUtf16BE,
bomUtf8);
const
/// UTF-8 BOM marker three bytes value (in little-endian)
BOM_UTF8 = $bfbbef;
/// UTF-16LE BOM WideChar marker, as existing e.g. in some UTF-16 Windows files
BOM_UTF16LE = #$feff;
/// UTF-16BE BOM WideChar marker, which is not supported
BOM_UTF16BE = #$fffe;
/// check the file BOM at the beginning of a file buffer
// - BOM is common only with Microsoft products
// - returns bomNone if no BOM was recognized
// - returns bomUtf16LE or bomUtf8 if UTF-16LE or UTF-8 BOM were recognized:
// and will adjust Buffer/BufferSize to ignore the leading 2 or 3 bytes
function BomFile(var Buffer: pointer; var BufferSize: PtrInt): TBomFile;
/// read a file into a temporary variable, check the BOM, and adjust the buffer
// - for bomUtf16LE and bomUtf16BE, returns BufferSize as WideChar count (not bytes)
function StringFromBomFile(const FileName: TFileName; var FileContent: RawByteString;
out Buffer: pointer; out BufferSize: PtrInt): TBomFile;
/// read a File content into a RawUtf8, detecting any leading BOM
// - will assume text file with no BOM is already UTF-8 encoded
// - an alternative to StringFromFile() if you want to handle UTF-8 content
// and the files are likely to be natively UTF-8 encoded, or with a BOM
function RawUtf8FromFile(const FileName: TFileName): RawUtf8;
{$ifdef HASINLINE} inline; {$endif}
/// read a File content into a RawUtf8, detecting any leading BOM
// - assume file with no BOM is encoded with the current Ansi code page, not
// UTF-8, unless AssumeUtf8IfNoBom is true and it behaves like RawUtf8FromFile()
function AnyTextFileToRawUtf8(const FileName: TFileName;
AssumeUtf8IfNoBom: boolean = false): RawUtf8;
/// read a File content into a RTL string, detecting any leading BOM
// - assume file with no BOM is encoded with the current Ansi code page, not UTF-8
// - if ForceUtf8 is true, won't detect the BOM but assume whole file is UTF-8
function AnyTextFileToString(const FileName: TFileName;
ForceUtf8: boolean = false): string;
{$ifdef UNICODE} inline; {$endif}
/// read a File content into SynUnicode string, detecting any leading BOM
// - assume file with no BOM is encoded with the current Ansi code page, not UTF-8
// - if ForceUtf8 is true, won't detect the BOM but assume whole file is UTF-8
function AnyTextFileToSynUnicode(const FileName: TFileName;
ForceUtf8: boolean = false): SynUnicode;
{ *************** Low-Level String Conversion Functions }
/// will fast replace all #0 chars as ~
// - could be used after UniqueRawUtf8() on a in-placed modified JSON buffer,
// in which all values have been ended with #0
// - you can optionally specify a maximum size, in bytes (this won't reallocate
// the string, but just add a #0 at some point in the UTF-8 buffer)
// - could allow logging of parsed input e.g. after an exception
procedure UniqueRawUtf8ZeroToTilde(var u: RawUtf8; MaxSize: PtrInt = maxInt);
/// convert a binary buffer into a fake ASCII/UTF-8 content without any #0 input
// - will use ~ char to escape any #0 as ~0 pair (and plain ~ as ~~ pair)
// - output is just a bunch of non 0 bytes, so not trully valid UTF-8 content
// - may be used as an alternative to Base64 encoding if 8-bit chars are allowed
// - call ZeroedRawUtf8() as reverse function
function UnZeroed(const bin: RawByteString): RawUtf8;
/// convert a fake UTF-8 buffer without any #0 input back into its original binary
// - may be used as an alternative to Base64 decoding if 8-bit chars are allowed
// - call UnZeroedRawUtf8() as reverse function
function Zeroed(const u: RawUtf8): RawByteString;
/// conversion of a wide char into a WinAnsi (CodePage 1252) char
// - return '?' for an unknown WideChar in code page 1252
function WideCharToWinAnsiChar(wc: cardinal): AnsiChar;
{$ifdef HASINLINE}inline;{$endif}
/// conversion of a wide char into a WinAnsi (CodePage 1252) char index
// - return -1 for an unknown WideChar in code page 1252
function WideCharToWinAnsi(wc: cardinal): integer;
{$ifdef HASINLINE}inline;{$endif}
/// return TRUE if the supplied unicode buffer only contains WinAnsi characters
// - i.e. if the text can be displayed using ANSI_CHARSET
function IsWinAnsi(WideText: PWideChar): boolean; overload;
{$ifdef HASINLINE}inline;{$endif}
/// return TRUE if the supplied unicode buffer only contains WinAnsi characters
// - i.e. if the text can be displayed using ANSI_CHARSET
function IsWinAnsi(WideText: PWideChar; Length: integer): boolean; overload;
{$ifdef HASINLINE}inline;{$endif}
/// return TRUE if the supplied UTF-8 buffer only contains WinAnsi characters
// - i.e. if the text can be displayed using ANSI_CHARSET
function IsWinAnsiU(Utf8Text: PUtf8Char): boolean;
{$ifdef HASINLINE}inline;{$endif}
/// return TRUE if the supplied UTF-8 buffer only contains WinAnsi 8-bit characters
// - i.e. if the text can be displayed using ANSI_CHARSET with only 8-bit unicode
// characters (e.g. no "tm" or such)
function IsWinAnsiU8Bit(Utf8Text: PUtf8Char): boolean;
{$ifdef HASINLINE}inline;{$endif}
/// direct conversion of an AnsiString with an unknown code page into an
// UTF-8 encoded String, as mainly used by VariantToUtf8() or VarRecToUtf8()
// - FPC and Unicode versions of Delphi will retrieve the code page from s
// - Delphi 7/2007 calls IsValidUtf8() then assume CurrentAnsiConvert.CodePage
procedure AnyAnsiToUtf8Var(const s: RawByteString; var result: RawUtf8);
/// direct conversion of an AnsiString with an unknown code page into an
// UTF-8 encoded String
// - FPC and Unicode versions of Delphi will retrieve the code page from s
// - Delphi 7/2007 calls IsValidUtf8() then assume CurrentAnsiConvert.CodePage
// - use AnsiToUtf8() if you want to specify the codepage
function AnyAnsiToUtf8(const s: RawByteString): RawUtf8;
{$ifdef HASINLINE}inline;{$endif}
/// convert an AnsiString (of a given code page) into a UTF-8 string
// - use AnyAnsiToUtf8() if you want to use the codepage of the input string
// - wrapper around TSynAnsiConvert.Engine(CodePage).AnsiToUtf8()
function AnsiToUtf8(const Ansi: RawByteString; CodePage: integer): RawUtf8;
{$ifdef HASINLINE}inline;{$endif}
/// convert an AnsiChar buffer (of a given code page) into a UTF-8 string
// - the destination code page should be supplied
// - wrapper around TSynAnsiConvert.Engine(CodePage).AnsiBufferToRawUtf8()
procedure AnsiCharToUtf8(P: PAnsiChar; L: integer; var result: RawUtf8;
CodePage: integer);
{$ifdef HASINLINE}inline;{$endif}
/// convert an AnsiString (of a given code page) into a RTL string
// - the destination code page should be supplied
// - wrapper around TSynAnsiConvert.Engine(CodePage) and string conversion
function AnsiToString(const Ansi: RawByteString; CodePage: integer): string;
{$ifdef HASINLINE}inline;{$endif}
/// direct conversion of a WinAnsi (CodePage 1252) string into a UTF-8 encoded String
// - faster than SysUtils: don't use Utf8Encode(WideString) -> no Windows.Global(),
// and use a fixed pre-calculated array for individual chars conversion
function WinAnsiToUtf8(const S: WinAnsiString): RawUtf8; overload;
{$ifdef HASINLINE}inline;{$endif}
/// direct conversion of a WinAnsi (CodePage 1252) string into a UTF-8 encoded String
// - faster than SysUtils: don't use Utf8Encode(WideString) -> no Windows.Global(),
// and use a fixed pre-calculated array for individual chars conversion
function WinAnsiToUtf8(WinAnsi: PAnsiChar; WinAnsiLen: PtrInt): RawUtf8; overload;
{$ifdef HASINLINE}inline;{$endif}
/// direct conversion of a WinAnsi PAnsiChar buffer into a UTF-8 encoded buffer
// - Dest^ buffer must be reserved with at least SourceChars*3
// - call internally WinAnsiConvert fast conversion class
function WinAnsiBufferToUtf8(Dest: PUtf8Char;
Source: PAnsiChar; SourceChars: cardinal): PUtf8Char;
{$ifdef HASINLINE}inline;{$endif}
/// direct conversion of a WinAnsi ShortString into a UTF-8 text
// - call internally WinAnsiConvert fast conversion class
function ShortStringToUtf8(const source: ShortString): RawUtf8;
{$ifdef HASINLINE}inline;{$endif}
/// direct conversion of a WinAnsi (CodePage 1252) string into a Unicode buffer
// - very fast, by using a fixed pre-calculated array for individual chars conversion
// - text will be truncated if necessary to avoid buffer overflow in Dest[]
procedure WinAnsiToUnicodeBuffer(const S: WinAnsiString;
Dest: PWordArray; DestLen: PtrInt);
{$ifdef HASINLINE}inline;{$endif}
/// direct conversion of a UTF-8 encoded string into a WinAnsi String
function Utf8ToWinAnsi(const S: RawUtf8): WinAnsiString; overload;
{$ifdef HASINLINE}inline;{$endif}
/// direct conversion of a UTF-8 encoded zero terminated buffer into a WinAnsi String
function Utf8ToWinAnsi(P: PUtf8Char): WinAnsiString; overload;
{$ifdef HASINLINE}inline;{$endif}
/// direct conversion of a UTF-8 encoded zero terminated buffer into a RawUtf8 String
procedure Utf8ToRawUtf8(P: PUtf8Char; var result: RawUtf8);
{$ifdef HASINLINE}inline;{$endif}
/// direct conversion of a UTF-8 encoded buffer into a WinAnsi PAnsiChar buffer
function Utf8ToWinPChar(dest: PAnsiChar; source: PUtf8Char; count: integer): integer;
{$ifdef HASINLINE}inline;{$endif}
{$ifndef PUREMORMOT2}
/// direct conversion of a WinAnsi (CodePage 1252) string into a Unicode encoded String
// - very fast, by using a fixed pre-calculated array for individual chars conversion
function WinAnsiToRawUnicode(const S: WinAnsiString): RawUnicode;
/// convert a UTF-16 string into a WinAnsi (code page 1252) string
function RawUnicodeToWinAnsi(const Unicode: RawUnicode): WinAnsiString; overload;
{$ifdef HASINLINE}inline;{$endif}
/// convert a UTF-8 encoded buffer into a RawUnicode string
// - if L is 0, L is computed from zero terminated P buffer
// - RawUnicode is ended by a WideChar(#0)
// - faster than System.Utf8Decode() which uses slow widestrings
function Utf8DecodeToRawUnicode(P: PUtf8Char; L: integer): RawUnicode; overload;
/// convert a UTF-8 string into a RawUnicode string
function Utf8DecodeToRawUnicode(const S: RawUtf8): RawUnicode; overload;
{$ifdef HASINLINE}inline;{$endif}
/// convert a UTF-8 string into a RawUnicode string
// - this version doesn't resize the length of the result RawUnicode
// and is therefore useful before a Win32 Unicode API call (with nCount=-1)
// - if DestLen is not nil, the resulting length (in bytes) will be stored within
// - see also Utf8DecodeToUnicode() which uses a TSynTempBuffer for storage
function Utf8DecodeToRawUnicodeUI(const S: RawUtf8;
DestLen: PInteger = nil): RawUnicode; overload;
/// convert a UTF-8 string into a RawUnicode string
// - returns the resulting length (in bytes) will be stored within Dest
// - see also Utf8DecodeToUnicode() which uses a TSynTempBuffer for storage
function Utf8DecodeToRawUnicodeUI(const S: RawUtf8;
var Dest: RawUnicode): integer; overload;
/// convert a RawUnicode string into a UTF-8 string
function RawUnicodeToUtf8(const Unicode: RawUnicode): RawUtf8; overload;
/// convert any RawUnicode String into a generic SynUnicode Text
function RawUnicodeToSynUnicode(const Unicode: RawUnicode): SynUnicode; overload;
{$ifdef HASINLINE}inline;{$endif}
/// convert any RTL string into a RawUnicode encoded String
// - it's prefered to use TLanguageFile.StringToUtf8() method in mORMoti18n,
// which will handle full i18n of your application
// - it will work as is with Delphi 2009+ (direct unicode conversion)
// - under older version of Delphi (no unicode), it will use the
// current RTL codepage, as with WideString conversion (but without slow
// WideString usage)
function StringToRawUnicode(const S: string): RawUnicode; overload;
/// convert any RTL string into a RawUnicode encoded String
// - it's prefered to use TLanguageFile.StringToUtf8() method in mORMoti18n,
// which will handle full i18n of your application
// - it will work as is with Delphi 2009+ (direct unicode conversion)
// - under older version of Delphi (no unicode), it will use the
// current RTL codepage, as with WideString conversion (but without slow
// WideString usage)
function StringToRawUnicode(P: PChar; L: integer): RawUnicode; overload;
/// convert any RawUnicode encoded string into a RTL string
// - uses StrLenW() and not length(U) to handle case when was used as buffer
function RawUnicodeToString(const U: RawUnicode): string; overload;
{$endif PUREMORMOT2}
/// convert a SynUnicode string into a UTF-8 string
function SynUnicodeToUtf8(const Unicode: SynUnicode): RawUtf8;
/// convert a WideString into a UTF-8 string
function WideStringToUtf8(const aText: WideString): RawUtf8;
{$ifdef HASINLINE}inline;{$endif}
/// direct conversion of a UTF-16 encoded buffer into a WinAnsi PAnsiChar buffer
procedure RawUnicodeToWinPChar(dest: PAnsiChar;
source: PWideChar; WideCharCount: integer);
{$ifdef HASINLINE}inline;{$endif}
/// convert a UTF-16 PWideChar buffer into a WinAnsi (code page 1252) string
function RawUnicodeToWinAnsi(
WideChar: PWideChar; WideCharCount: integer): WinAnsiString; overload;
{$ifdef HASINLINE}inline;{$endif}
/// convert a WideString into a WinAnsi (code page 1252) string
function WideStringToWinAnsi(const Wide: WideString): WinAnsiString;
{$ifdef HASINLINE}inline;{$endif}
/// convert any UTF-16 buffer into a generic SynUnicode Text
function RawUnicodeToSynUnicode(
WideChar: PWideChar; WideCharCount: integer): SynUnicode; overload;
{$ifdef HASINLINE}inline;{$endif}
/// convert an Unicode buffer into a WinAnsi (code page 1252) string
procedure UnicodeBufferToWinAnsi(source: PWideChar; out Dest: WinAnsiString);
/// convert an Unicode buffer into a RTL string
function UnicodeBufferToString(source: PWideChar): string;
/// convert an Unicode buffer into a UTF-8 string
function UnicodeBufferToUtf8(source: PWideChar): RawUtf8;
{$ifdef HASINLINE} inline; {$endif}
/// convert an Unicode buffer into a variant storing a UTF-8 string
// - could be used e.g. as TDocVariantData.AddValue() parameter
function UnicodeBufferToVariant(source: PWideChar): variant;
/// convert any RTL string into a variant storing a UTF-8 string
// - could be used e.g. as TDocVariantData.AddValue() parameter
function StringToVariant(const Txt: string): variant; overload;
/// convert any RTL string into a variant storing a UTF-8 string
// - could be used e.g. as TDocVariantData.AddValue() parameter
procedure StringToVariant(const Txt: string; var result: variant); overload;
{$ifdef HASVARUSTRING}
/// convert a Delphi 2009+ or FPC Unicode string into our UTF-8 string
function UnicodeStringToUtf8(const S: UnicodeString): RawUtf8; inline;
// this function is the same as direct RawUtf8=AnsiString(CP_UTF8) assignment
// but is faster, since it uses no Win32 API call
function Utf8DecodeToUnicodeString(const S: RawUtf8): UnicodeString; overload; inline;
/// convert an UTF-8 encoded buffer into a Delphi 2009+ or FPC Unicode string
// - this function is the same as direct assignment, since RawUtf8=AnsiString(CP_UTF8),
// but is faster, since use no Win32 API call
procedure Utf8DecodeToUnicodeString(P: PUtf8Char; L: integer;
var result: UnicodeString); overload;
/// convert a Delphi 2009+ or FPC Unicode string into a WinAnsi (code page 1252) string
function UnicodeStringToWinAnsi(const S: UnicodeString): WinAnsiString; inline;
/// convert our UTF-8 encoded buffer into a Delphi 2009+ or FPC Unicode string
// - this function is the same as direct assignment, since RawUtf8=AnsiString(CP_UTF8),
// but is faster, since use no Win32 API call
function Utf8DecodeToUnicodeString(P: PUtf8Char; L: integer): UnicodeString; overload; inline;