-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
StrRect.pas
2155 lines (1907 loc) · 57.9 KB
/
StrRect.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
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
StrRect - String rectification utility
Main aim of this library is to simplify conversions in Lazarus when passing
strings to RTL or WinAPI - mainly to ensure the same code can be used in all
compilers (Delphi, FPC 2.x.x, FPC 3.x.x) without a need for symbol checks.
It also provides set of functions for string comparison that encapsulates
some of the intricacies given by different approach in different compilers,
functions returning index of first and last character (can be used when
iterating the string) and functions for character presence tests.
For details about encodings refer to file encoding_notes.txt that should
be distributed with this library.
Library was tested in following IDE/compilers:
Delphi 7 Personal (non-unicode, Windows)
Delphi 10.1 Berlin Personal (unicode, Windows)
Lazarus 1.4.4 - FPC 2.6.4 (non-unicode, Windows)
Lazarus 2.0.8 - FPC 3.0.4 (non-unicode, Windows)
Lazarus 2.0.8 - FPC 3.0.4 (non-unicode, Linux)
Lazarus 2.0.8 - FPC 3.0.4 (unicode, Windows)
Lazarus 2.0.8 - FPC 3.0.4 (unicode, Linux)
Lazarus 2.2.6 - FPC 3.2.2 (non-unicode, Windows)
Lazarus 2.2.6 - FPC 3.2.2 (non-unicode, Linux)
Lazarus 2.2.6 - FPC 3.2.2 (unicode, Windows)
Lazarus 2.2.6 - FPC 3.2.2 (unicode, Linux)
Tested compatible FPC modes:
Delphi, DelphiUnicode, FPC (default), ObjFPC, TurboPascal
WARNING - a LOT of assumptions were made when creating this library (most
of it was written "blind", with no access to internet and
documentation), so if you find any error or mistake, please
contact the author.
Also, if you are certain that some part, in here marked as
dubious, is actually correct, let the author know.
Version 1.6.1 (2024-02-29)
Last change 2024-03-05
©2017-2024 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.StrRect
Dependencies:
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
===============================================================================}
{
I do not have any good information on non-windows Delphi, therefore this
entire unit is marked as platform in those systems as a warning.
}
unit StrRect{$IF not Defined(FPC) and not(Defined(WINDOWS) or Defined(MSWINDOWS))} platform{$IFEND};
{$IF Defined(WINDOWS) or Defined(MSWINDOWS)}
{$DEFINE Windows}
{$ELSEIF Defined(LINUX) and Defined(FPC)}
{$DEFINE Linux}
{$ELSE}
{$MESSAGE FATAL 'Unsupported operating system.'}
{$IFEND}
{$IFDEF FPC}
// do not set $MODE, leave the unit mode-agnostic
{$MODESWITCH Result+}
{$MODESWITCH DefaultParameters+}
{$MODESWITCH InitFinal+}
{$INLINE ON}
{$DEFINE CanInline}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{
Activate symbol BARE_FPC if you want to compile this unit outside of
Lazarus.
Non-unicode default strings are assumed to be encoded using current CP when
defined, otherwise they are assumed to be UTF8-encoded.
Has effect only in FPC older than 2.7.1, in newer versions there are ways
of how to discern the encoding automatically.
It is automatically undefined if you compile the program in Lazarus
with LCL (lazarus defines symbol LCL, this symbol is observed).
Not defined by default.
}
{.$DEFINE BARE_FPC}
{$IFDEF LCL} // clearly not bare FPC...
{$UNDEF BARE_FPC}
{$ENDIF}
{$ELSE}
{$IF CompilerVersion >= 17} // Delphi 2005+
{$DEFINE CanInline}
{$ELSE}
{$UNDEF CanInline}
{$IFEND}
{$ENDIF}
{$H+} // explicitly activate long strings
interface
uses
SysUtils,
{$IFNDEF Windows}
cwstring,
{$ENDIF}
AuxTypes;
{$IFNDEF FPC}
const
FPC_FULLVERSION = Integer(0);
{$ENDIF}
{===============================================================================
Auxiliary public functions
===============================================================================}
{
Following two functions are present in newer Delphi where they replace
deprecated UTF8Decode/UTF8Encode.
They are here for use in older compilers.
}
{$IF not Declared(UTF8ToString)}
Function UTF8ToString(const Str: UTF8String): UnicodeString;{$IFDEF CanInline} inline; {$ENDIF}
{$DEFINE Implement_UTF8ToString}
{$IFEND}
{$IF not Declared(StringToUTF8)}
Function StringToUTF8(const Str: UnicodeString): UTF8String;{$IFDEF CanInline} inline; {$ENDIF}
{$DEFINE Implement_StringToUTF8}
{$IFEND}
{
UTF8AnsiStrings
Returns true when single-byte strings (AnsiString, ShortString, String in
some cases) are encoded using UTF-8 encoding, false otherwise.
}
Function UTF8AnsiStrings: Boolean;{$IFDEF CanInline} inline; {$ENDIF}
{
UTF8AnsiDefaultStrings
Returns true when default strings (type String) are UTF-8 ecoded ansi strings,
false in all other cases (eg. when in Unicode).
}
Function UTF8AnsiDefaultStrings: Boolean;{$IFDEF CanInline} inline; {$ENDIF}
{
UCS4Encode
Converts UTF-16 encoded string into an UCS4 (UTF-32) encoded string.
This conversion is completely lossless.
NOTE - Lone surrogates, though being an invalid codepoints, are encoded
directly as they are (with no change in numerical value).
For example lone #$DCAB will be directly encoded as #$0000DCAB.
}
Function UCS4Encode(const Str: UnicodeString): UCS4String;
{
UCS4Encode
Converts UCS4 (UTF-32) encoded string into an UTF-16 encoded string.
This conversion is completely lossless.
NOTE - UCS4 character equal to a surrogate are directly copied, producing
lone surrogates in the resulting string.
WARNING - Values beyod the allowed range (abowe $10FFFF) are converted to an
unicode replacement character ($FFFD).
}
Function UCS4Decode(const Str: UCS4String): UnicodeString;
{===============================================================================
Default string <-> explicit string conversions
===============================================================================}
{
Depending on some use cases, the actually used string type might change,
therefore following type aliases are introduced for such cases.
}
type
{$IF Defined(FPC) and Defined(Unicode)}
TRTLString = AnsiString; TRTLChar = AnsiChar; PRTLChar = PAnsiChar;
{$ELSE}
TRTLString = String; TRTLChar = Char; PRTLChar = PChar;
{$IFEND}
{$IF not Defined(FPC) and Defined(Windows) and Defined(Unicode)}
TWinString = WideString; TWinChar = Widechar; PWinChar = PWideChar;
TSysString = WideString; TSysChar = WideChar; PSysChar = PWideChar;
{$ELSE}
TWinString = AnsiString; TWinChar = Ansichar; PWinChar = PAnsiChar;
TSysString = AnsiString; TSysChar = Ansichar; PSysChar = PAnsichar;
{$IFEND}
{$IFDEF FPC}
{$IF FPC_FULLVERSION >= 20701}
TGUIString = type AnsiString(CP_UTF8); TGUIChar = AnsiChar; PGUIChar = PAnsiChar;
{$ELSE}
TGUIString = AnsiString; TGUIChar = AnsiChar; PGUIChar = PAnsiChar;
{$IFEND}
{$ELSE}
TGUIString = String; TGUIChar = Char; PGUIChar = PChar;
{$ENDIF}
{$IF Defined(FPC) and not Defined(Windows) and Defined(Unicode)}
TCSLString = AnsiString; TCSLChar = AnsiChar; PCSLChar = PAnsiChar;
{$ELSE}
TCSLString = String; TCSLChar = Char; PCSLChar = PChar;
{$IFEND}
//------------------------------------------------------------------------------
Function StrToShort(const Str: String): ShortString;
Function ShortToStr(const Str: ShortString): String;
Function StrToAnsi(const Str: String): AnsiString;
Function AnsiToStr(const Str: AnsiString): String;
Function StrToUTF8(const Str: String): UTF8String;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function UTF8ToStr(const Str: UTF8String): String;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function StrToWide(const Str: String): WideString;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function WideToStr(const Str: WideString): String;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function StrToUnicode(const Str: String): UnicodeString;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function UnicodeToStr(const Str: UnicodeString): String;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function StrToUCS4(const Str: String): UCS4String;{$IFDEF CanInline} inline; {$ENDIF}
Function UCS4ToStr(const Str: UCS4String): String;{$IFDEF CanInline} inline; {$ENDIF}
Function StrToRTL(const Str: String): TRTLString;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function RTLToStr(const Str: TRTLString): String;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function StrToGUI(const Str: String): TGUIString;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function GUIToStr(const Str: TGUIString): String;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function StrToWinA(const Str: String): AnsiString;
Function WinAToStr(const Str: AnsiString): String;
Function StrToWinW(const Str: String): WideString;{$IFDEF CanInline} inline; {$ENDIF}
Function WinWToStr(const Str: WideString): String;{$IFDEF CanInline} inline; {$ENDIF}
Function StrToWin(const Str: String): TWinString;{$IFDEF CanInline} inline; {$ENDIF}
Function WinToStr(const Str: TWinString): String;{$IFDEF CanInline} inline; {$ENDIF}
Function StrToCsl(const Str: String): TCSLString;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function CslToStr(const Str: TCSLString): String;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function StrToSys(const Str: String): TSysString;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function SysToStr(const Str: TSysString): String;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
{===============================================================================
Explicit string comparison
===============================================================================}
Function ShortStringCompare(const A,B: ShortString; CaseSensitive: Boolean): Integer;
Function AnsiStringCompare(const A,B: AnsiString; CaseSensitive: Boolean): Integer;
Function UTF8StringCompare(const A,B: UTF8String; CaseSensitive: Boolean): Integer;
Function WideStringCompare(const A,B: WideString; CaseSensitive: Boolean): Integer;
Function UnicodeStringCompare(const A,B: UnicodeString; CaseSensitive: Boolean): Integer;
Function UCS4StringCompare(const A,B: UCS4String; CaseSensitive: Boolean): Integer;{$IFDEF CanInline} inline; {$ENDIF}
Function StringCompare(const A,B: String; CaseSensitive: Boolean): Integer;
{===============================================================================
String indexing helpers
===============================================================================}
Function ShortStrLow(const Str: ShortString): TStrOff;
Function AnsiStrLow(const Str: AnsiString): TStrOff;
Function UTF8StrLow(const Str: UTF8String): TStrOff;
Function WideStrLow(const Str: WideString): TStrOff;
Function UnicodeStrLow(const Str: UnicodeString): TStrOff;
Function UCS4StrLow(const Str: UCS4String): TStrOff; {$IFDEF CanInline} inline; {$ENDIF}
Function StrLow(const Str: String): TStrOff;
//------------------------------------------------------------------------------
Function ShortStrHigh(const Str: ShortString): TStrOff;
Function AnsiStrHigh(const Str: AnsiString): TStrOff;
Function UTF8StrHigh(const Str: UTF8String): TStrOff;
Function WideStrHigh(const Str: WideString): TStrOff;
Function UnicodeStrHigh(const Str: UnicodeString): TStrOff;
Function UCS4StrHigh(const Str: UCS4String): TStrOff;
Function StrHigh(const Str: String): TStrOff;
{===============================================================================
Character presence tests
===============================================================================}
Function AnsiCharInSet(C: AnsiChar; const CharSet: TSysCharSet): Boolean;{$IFDEF CanInline} inline; {$ENDIF}
Function UTF8CharInSet(C: UTF8Char; const CharSet: TSysCharSet): Boolean;{$IFDEF CanInline} inline; {$ENDIF}
Function WideCharInSet(C: WideChar; const CharSet: TSysCharSet): Boolean;
Function UnicodeCharInSet(C: UnicodeChar; const CharSet: TSysCharSet): Boolean;
Function UCS4CharInSet(C: UCS4Char; const CharSet: TSysCharSet): Boolean;
Function CharInSet(C: Char; const CharSet: TSysCharSet): Boolean;
Function AnsiCharInArr(C: AnsiChar; const Arr: array of AnsiChar): Boolean;
Function UTF8CharInArr(C: UTF8Char; const Arr: array of UTF8Char): Boolean;
Function WideCharInArr(C: WideChar; const Arr: array of WideChar): Boolean;
Function UnicodeCharInArr(C: UnicodeChar; const Arr: array of UnicodeChar): Boolean;
Function UCS4CharInArr(C: UCS4Char; const Arr: array of UCS4Char): Boolean;
Function CharInArr(C: Char; const Arr: array of Char): Boolean;
Function AnsiCharInStr(C: AnsiChar; const Str: AnsiString): Boolean;
Function UTF8CharInStr(C: UTF8Char; const Str: UTF8String): Boolean;
Function WideCharInStr(C: WideChar; const Str: WideString): Boolean;
Function UnicodeCharInStr(C: WideChar; const Str: UnicodeString): Boolean;
Function UCS4CharInStr(C: UCS4Char; const Str: UCS4String): Boolean;
Function CharInStr(C: Char; const Str: String): Boolean;
implementation
{$IF (not Defined(FPC) and (CompilerVersion >= 20)) or Defined(Windows)}
uses
{$IF not Defined(FPC) and (CompilerVersion >= 20)}(* Delphi2009+ *)
AnsiStrings{$IFDEF Windows},{$ENDIF}
{$IFEND}
{$IFDEF Windows}
Windows
{$ENDIF};
{$IFEND}
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W4045:={$WARN 4045 OFF}} // Comparison might be always true due to range of constant and expression
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$DEFINE W6018:={$WARN 6018 OFF}} // unreachable code
{$PUSH}{$WARN 2005 OFF} // Comment level $1 found
{$IF Defined(FPC) and (FPC_FULLVERSION >= 30200)}
{$DEFINE W6058:={$WARN 6058 OFF}} // Call to subroutine "$1" marked as inline is not inlined
{$ELSE}
{$DEFINE W6058:=}
{$IFEND}
{$POP}
{$ENDIF}
{===============================================================================
Auxiliary public functions
===============================================================================}
{$IFDEF Implement_UTF8ToString}
Function UTF8ToString(const Str: UTF8String): UnicodeString;
begin
Result := UTF8Decode(Str);
end;
{$ENDIF}
//------------------------------------------------------------------------------
{$IFDEF Implement_StringToUTF8}
Function StringToUTF8(const Str: UnicodeString): UTF8String;
begin
Result := UTF8Encode(Str);
end;
{$ENDIF}
//------------------------------------------------------------------------------
Function UTF8AnsiStrings: Boolean;
begin
{$IFDEF FPC}
// FPC
{$IFDEF Windows}
// check for FPC is there because of Delphi :/
{$IF FPC_FULLVERSION >= 20701}
// new FPC, version 2.7.1 and newer
Result := StringCodePage(AnsiString('')) = CP_UTF8;
{$ELSE}
// old FPC, before version 2.7.1
Result := False; // old FPC (prior to 2.7.1), everything is assumed to be ansi CP
{$IFEND}
{$ELSE}
Result := True; // linux, everything is assumed to be UTF-8
{$ENDIF}
{$ELSE}
// delphi
Result := {$IFDEF Windows}False{$ELSE}True{$ENDIF};
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function UTF8AnsiDefaultStrings: Boolean;
begin
{$IFDEF Unicode}
Result := False; // strings are encoded using UTF-16
{$ELSE}
{$IFDEF FPC}
// FPC
{$IFDEF Windows}
{$IF FPC_FULLVERSION >= 20701}
Result := StringCodePage(AnsiString('')) = CP_UTF8;
{$ELSE}
{$IFDEF BARE_FPC}
Result := GetACP = CP_UTF8;
{$ELSE}
Result := True;
{$ENDIF}
{$IFEND}
{$ELSE}
Result := True;
{$ENDIF}
{$ELSE}
// delphi
Result := {$IFDEF Windows}False{$ELSE}True{$ENDIF};
{$ENDIF}
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function UCS4Encode(const Str: UnicodeString): UCS4String;
var
i: Integer;
ResLen: Integer;
begin
// get full resulting length for one-shot preallocation
ResLen := 0;
If Length(Str) > 0 then
begin
i := 1;
while i <= Length(Str) do
begin
If (Ord(Str[i]) >= $D800) and (Ord(Str[i]) < $E000) then
// surrogates
If Ord(Str[i]) < $DC00 then
// high surrogate, check if next is low surrogate
If i < Length(Str) then
If (Ord(Str[i + 1]) >= $DC00) and (Ord(Str[i + 1]) < $E000) then
Inc(i);
Inc(i);
Inc(ResLen);
end;
end;
Result := nil;
SetLength(Result,ResLen + 1);
Result[High(Result)] := 0; // explicitly set terminating zero
// do conversion
i := 1;
ResLen := 0;
while i <= Length(Str) do
begin
Result[ResLen] := UCS4Char(Str[i]);
If (Ord(Str[i]) >= $D800) and (Ord(Str[i]) < $E000) then
// surrogates
If Ord(Str[i]) < $DC00 then
// high surrogate, next should be low surrogate
If i < Length(Str) then
If (Ord(Str[i + 1]) >= $DC00) and (Ord(Str[i + 1]) < $E000) then
begin
// combine surrogates and replace current result char
Result[ResLen] := UCS4Char(((Ord(Str[i]) - $D800) shl 10) +
((Ord(Str[i + 1]) - $DC00) + $10000));
Inc(i);
end;
Inc(i);
Inc(ResLen);
end;
end;
//------------------------------------------------------------------------------
Function UCS4Decode(const Str: UCS4String): UnicodeString;
var
TermZero: Boolean;
i: Integer;
ResLen: Integer;
CurrChar: UCS4Char;
Function IfThenElse(Value: Boolean; RetOnTrue,RetOnFalse: Integer): Integer;
begin
If Value then
Result := RetOnTrue
else
Result := RetOnFalse;
end;
begin
If Length(Str) > 0 then
begin
// get full resulting length for one-shot preallocation
TermZero := Ord(Str[High(Str)]) = 0;
ResLen := 0;
For i := Low(Str) to IfThenElse(TermZero,Pred(High(Str)),High(Str)) do
{$IFDEF FPCDWM}{$PUSH}W4045{$ENDIF}
Inc(ResLen, 1 + IfThenElse((Str[i] > $FFFF) and (Str[i] <= $10FFFF),1,0));
{$IFDEF FPCDWM}{$POP}{$ENDIF}
// preallocation
Result := '';
SetLength(Result,ResLen);
// do conversion
ResLen := 1;
For i := Low(Str) to IfThenElse(TermZero,Pred(High(Str)),High(Str)) do
begin
CurrChar := Str[i];
If CurrChar <= $FFFF then
Result[ResLen] := WideChar(CurrChar) // lone surrogates are just copied
{$IFDEF FPCDWM}{$PUSH}W4045{$ENDIF}
else If CurrChar <= $10FFFF then
{$IFDEF FPCDWM}{$POP}{$ENDIF}
begin
// create surrogate pair
Result[ResLen] := WideChar(((CurrChar - $10000) shr 10) + $D800); // high surrogate
Inc(ResLen);
Result[ResLen] := WideChar(((CurrChar - $10000) and $3FF) + $DC00); // low surrogate
end
{$IFDEF FPCDWM}{$PUSH}W6018{$ENDIF}
else
Result[ResLen] := WideChar($FFFD); // unicode replacement character
{$IFDEF FPCDWM}{$POP}{$ENDIF}
Inc(ResLen);
end;
end
else Result := '';
end;
{===============================================================================
Internal functions
===============================================================================}
{$IFDEF Windows}
type
PUnicodeChar = PWideChar;
//------------------------------------------------------------------------------
Function UnicodeToAnsiCP(const Str: UnicodeString; CodePage: UINT = CP_ACP): AnsiString;
begin
If Length (Str) > 0 then
begin
Result := '';
SetLength(Result,WideCharToMultiByte(CodePage,0,PUnicodeChar(Str),Length(Str),nil,0,nil,nil));
WideCharToMultiByte(CodePage,0,PUnicodeChar(Str),Length(Str),PAnsiChar(Result),Length(Result) * SizeOf(AnsiChar),nil,nil);
{$IF Defined(FPC) and (FPC_FULLVERSION >= 20701)}
SetCodePage(RawByteString(Result),CodePage,False);
{$IFEND}
end
else Result := '';
end;
//------------------------------------------------------------------------------
Function AnsiCPToUnicode(const Str: AnsiString; CodePage: UINT = CP_ACP): UnicodeString;
const
ExclCodePages: array[0..19] of Word = (50220,50221,50222,50225,50227,50229,52936,
54936,57002,57003,57004,57005,57006,57007,57008,57009,57010,57011,65000,42);
var
i: Integer;
Flags: DWORD;
begin
If Length (Str) > 0 then
begin
Flags := MB_PRECOMPOSED;
{
In wast majority of cases, the code page will be CP_ACP (0), so testing for
all excluded CPs is nonsense - limit number of necessary comparisons to
speed things up.
}
If not(((CodePage < 50220) and (CodePage <> 42)) or
((CodePage > 57011) and (CodePage <> 65000))) then
For i := Low(ExclCodePages) to High(ExclCodePages) do
If CodePage = ExclCodePages[i] then
begin
Flags := 0;
Break{For i};
end;
Result := '';
SetLength(Result,MultiByteToWideChar(CodePage,Flags,PAnsiChar(Str),Length(Str) * SizeOf(AnsiChar),nil,0));
MultiByteToWideChar(CodePage,Flags,PAnsiChar(Str),Length(Str) * SizeOf(AnsiChar),PUnicodeChar(Result),Length(Result));
end
else Result := '';
end;
//------------------------------------------------------------------------------
Function UTF8ToAnsiCP(const Str: UTF8String; CodePage: UINT = CP_ACP): AnsiString;{$IFDEF CanInline} inline; {$ENDIF}
begin
Result := UnicodeToAnsiCP(UTF8ToString(Str),CodePage);
end;
//------------------------------------------------------------------------------
Function AnsiCPToUTF8(const Str: AnsiString; CodePage: UINT = CP_ACP): UTF8String;{$IFDEF CanInline} inline; {$ENDIF}
begin
Result := StringToUTF8(AnsiCPToUnicode(Str,CodePage));
end;
{$ENDIF}
//------------------------------------------------------------------------------
{$IF Defined(Windows) and not Defined(Unicode)}
Function AnsiToConsole(const Str: AnsiString): AnsiString;
begin
If Length (Str) > 0 then
begin
Result := StrToWinA(Str);
UniqueString(Result);
If not CharToOEMBuff(PAnsiChar(Result),PAnsiChar(Result),Length(Result)) then
Result := '';
{$IF Defined(FPC) and (FPC_FULLVERSION >= 20701)}
SetCodePage(RawByteString(Result),CP_OEMCP,False);
{$IFEND}
end
else Result := '';
end;
//------------------------------------------------------------------------------
Function ConsoleToAnsi(const Str: AnsiString): AnsiString;
begin
If Length (Str) > 0 then
begin
Result := Str;
UniqueString(Result);
If OEMToCharBuff(PAnsiChar(Result),PAnsiChar(Result),Length(Result)) then
Result := WinAToStr(Result)
else
Result := '';
{$IF Defined(FPC) and (FPC_FULLVERSION >= 20701)}
SetCodePage(RawByteString(Result),CP_ACP,False);
{$IFEND}
end
else Result := '';
end;
{$IFEND}
//------------------------------------------------------------------------------
Function UTF8ToUTF16(const Str: UTF8String): UnicodeString;{$IFDEF CanInline} inline; {$ENDIF}
begin
Result := UTF8ToString(Str);
end;
//------------------------------------------------------------------------------
Function UTF16ToUTF8(const Str: UnicodeString): UTF8String;{$IFDEF CanInline} inline; {$ENDIF}
begin
Result := StringToUTF8(Str);
end;
{===============================================================================
Default string <-> explicit string conversion
===============================================================================}
Function StrToShort(const Str: String): ShortString;
begin
{$IFDEF FPC}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode FPC on Windows
If UTF8AnsiStrings then
Result := ShortString(UTF16ToUTF8(Str))
else
Result := ShortString(UnicodeToAnsiCP(Str));
{$ELSE}
// non-unicode FPC on Windows
If UTF8AnsiDefaultStrings and not UTF8AnsiStrings then
Result := ShortString(UTF8ToAnsiCP(Str))
else
Result := ShortString(Str);
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode FPC on Linux
Result := ShortString(UTF16ToUTF8(Str));
{$ELSE}
// non-unicode FPC on Linux
Result := ShortString(Str);
{$ENDIF}
{$ENDIF}
{$ELSE}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode Delphi on Windows
Result := ShortString(UnicodeToAnsiCP(Str));
{$ELSE}
// non-unicode Delphi on Windows
Result := ShortString(Str);
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode Delphi on Linux
Result := ShortString(UTF16ToUTF8(Str));
{$ELSE}
// non-unicode Delphi on Linux
Result := ShortString(Str);
{$ENDIF}
{$ENDIF}
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function ShortToStr(const Str: ShortString): String;
begin
{$IFDEF FPC}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode FPC on Windows
If UTF8AnsiStrings then
Result := String(UTF8ToUTF16(Str))
else
Result := String(AnsiCPToUnicode(Str));
{$ELSE}
// non-unicode FPC on Windows
If UTF8AnsiDefaultStrings and not UTF8AnsiStrings then
Result := String(AnsiCPToUTF8(Str))
else
Result := String(Str);
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode FPC on Linux
Result := String(UTF8ToUTF16(Str));
{$ELSE}
// non-unicode FPC on Linux
Result := String(Str);
{$ENDIF}
{$ENDIF}
{$ELSE}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode Delphi on Windows
Result := String(AnsiCPToUnicode(Str));
{$ELSE}
// non-unicode Delphi on Windows
Result := String(Str);
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode Delphi on Linux
Result := String(UTF8ToUTF16(Str));
{$ELSE}
// non-unicode Delphi on Linux
Result := String(Str);
{$ENDIF}
{$ENDIF}
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function StrToAnsi(const Str: String): AnsiString;
begin
{$IFDEF FPC}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode FPC on Windows
If UTF8AnsiStrings then
Result := UTF16ToUTF8(Str)
else
Result := UnicodeToAnsiCP(Str);
{$ELSE}
// non-unicode FPC on Windows
If UTF8AnsiDefaultStrings and not UTF8AnsiStrings then
Result := UTF8ToAnsiCP(Str)
else
Result := Str;
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode FPC on Linux
Result := UTF16ToUTF8(Str);
{$ELSE}
// non-unicode FPC on Linux
Result := Str;
{$ENDIF}
{$ENDIF}
{$ELSE}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode Delphi on Windows
Result := UnicodeToAnsiCP(Str);
{$ELSE}
// non-unicode Delphi on Windows
Result := Str;
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode Delphi on Linux
Result := UTF16ToUTF8(Str);
{$ELSE}
// non-unicode Delphi on Linux
Result := Str;
{$ENDIF}
{$ENDIF}
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function AnsiToStr(const Str: AnsiString): String;
begin
{$IFDEF FPC}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode FPC on Windows
If UTF8AnsiStrings then
Result := UTF8ToUTF16(Str)
else
Result := AnsiCPToUnicode(Str);
{$ELSE}
// non-unicode FPC on Windows
If UTF8AnsiDefaultStrings and not UTF8AnsiStrings then
Result := AnsiCPToUTF8(Str)
else
Result := Str;
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode FPC on Linux
Result := UTF8ToUTF16(Str);
{$ELSE}
// non-unicode FPC on Linux
Result := Str;
{$ENDIF}
{$ENDIF}
{$ELSE}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode Delphi on Windows
Result := AnsiCPToUnicode(Str);
{$ELSE}
// non-unicode Delphi on Windows
Result := Str;
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode Delphi on Linux
Result := UTF8ToUTF16(Str);
{$ELSE}
// non-unicode Delphi on Linux
Result := Str;
{$ENDIF}
{$ENDIF}
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function StrToUTF8(const Str: String): UTF8String;
begin
{$IFDEF FPC}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode FPC on Windows
Result := UTF16ToUTF8(Str);
{$ELSE}
// non-unicode FPC on Windows
If UTF8AnsiDefaultStrings then
Result := Str
else
Result := AnsiCPToUTF8(Str);
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode FPC on Linux
Result := UTF16ToUTF8(Str);
{$ELSE}
// non-unicode FPC on Linux
Result := Str;
{$ENDIF}
{$ENDIF}
{$ELSE}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode Delphi on Windows
Result := UTF16ToUTF8(Str);
{$ELSE}
// non-unicode Delphi on Windows
Result := AnsiCPToUTF8(Str);
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode Delphi on Linux
Result := UTF16ToUTF8(Str);
{$ELSE}
// non-unicode Delphi on Linux
Result := Str;
{$ENDIF}
{$ENDIF}
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function UTF8ToStr(const Str: UTF8String): String;
begin
{$IFDEF FPC}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode FPC on Windows
Result := UTF8ToUTF16(Str);
{$ELSE}
// non-unicode FPC on Windows
If UTF8AnsiDefaultStrings then
Result := Str
else
Result := UTF8ToAnsiCP(Str);
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode FPC on Linux
Result := UTF8ToUTF16(Str);
{$ELSE}
// non-unicode FPC on Linux
Result := Str;
{$ENDIF}
{$ENDIF}
{$ELSE}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode Delphi on Windows
Result := UTF8ToUTF16(Str);
{$ELSE}
// non-unicode Delphi on Windows
Result := UTF8ToAnsiCP(Str);
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode Delphi on Linux
Result := UTF8ToUTF16(Str);
{$ELSE}
// non-unicode Delphi on Linux
Result := Str;
{$ENDIF}
{$ENDIF}
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function StrToWide(const Str: String): WideString;
begin
{$IFDEF FPC}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode FPC on Windows
Result := Str;
{$ELSE}
// non-unicode FPC on Windows
If UTF8AnsiDefaultStrings then
Result := UTF8ToUTF16(Str)
else
Result := AnsiCPToUnicode(Str);
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode FPC on Linux
Result := Str;
{$ELSE}
// non-unicode FPC on Linux
Result := UTF8ToUTF16(Str);
{$ENDIF}
{$ENDIF}
{$ELSE}
{$IFDEF Windows}
{$IFDEF Unicode}
// unicode Delphi on Windows
Result := Str;
{$ELSE}
// non-unicode Delphi on Windows
Result := AnsiCPToUnicode(Str);
{$ENDIF}
{$ELSE}
{$IFDEF Unicode}
// unicode Delphi on Linux
Result := Str;
{$ELSE}
// non-unicode Delphi on Linux
Result := UTF8ToUTF16(Str);
{$ENDIF}