forked from pyscripter/pyscripter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynUnicode.pas
3748 lines (3434 loc) · 103 KB
/
SynUnicode.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
{-------------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is SynUnicode.pas by Maλl Hφrz, released 2004-05-30.
All Rights Reserved.
TUnicodeStrings/TUnicodeStringList-code (originally written by Mike Lischke) is based
on JclUnicode.pas which is part of the JCL (www.delphi-jedi.org).
Contributors to the SynEdit and mwEdit projects are listed in the
Contributors.txt file.
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.
$Id: SynUnicode.pas,v 1.1.2.46 2009/09/28 17:54:20 maelh Exp $
You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net
Provides:
- Unicode(PWideChar) versions of the most important PAnsiChar-functions in
SysUtils and some functions unavailable in Delphi 5.
- An adapted and lighter version of TUnicodeStrings/TUnicodeStringList taken
from JCL, but made portable.
- function for loading and saving of Unicode files, and detecting the encoding
- Unicode clipboard support
- Unicode-version of TCanvas-methods
- Some character constants like CR&LF.
-------------------------------------------------------------------------------}
unit SynUnicode;
{$I SynEdit.inc}
interface
uses
{$IFDEF SYN_WIN32}
Windows,
{$ENDIF}
Messages,
Controls,
Forms,
Graphics,
Clipbrd,
{$IFDEF SYN_COMPILER_6_UP}
Types,
{$ENDIF}
Classes,
SysUtils,
TypInfo;
{$IFNDEF SYN_COMPILER_6_UP}
type
UTF8String = type string;
PUTF8String = ^UTF8String;
{$ENDIF}
{$IFNDEF UNICODE}
type
UnicodeString = WideString;
{$ENDIF}
const
SLineBreak = {$IFDEF SYN_LINUX} #10 {$ELSE} #13#10 {$ENDIF};
UTF8BOM: array[0..2] of Byte = ($EF, $BB, $BF);
UTF16BOMLE: array[0..1] of Byte = ($FF, $FE);
UTF16BOMBE: array[0..1] of Byte = ($FE, $FF);
UTF32BOMLE: array[0..3] of Byte = ($FF, $FE, $00, $00);
UTF32BOMBE: array[0..3] of Byte = ($00, $00, $FE, $FF);
const
// constants describing range of the Unicode Private Use Area (Unicode 3.2)
PrivateUseLow = WideChar($E000);
PrivateUseHigh = WideChar($F8FF);
// filler char: helper for painting wide glyphs
FillerChar = PrivateUseLow;
const
WideNull = WideChar(#0);
WideTabulator = WideChar(#9);
WideSpace = WideChar(#32);
// logical line breaks
WideLF = WideChar(#10);
WideLineFeed = WideChar(#10);
WideVerticalTab = WideChar(#11);
WideFormFeed = WideChar(#12);
WideCR = WideChar(#13);
WideCarriageReturn = WideChar(#13);
WideCRLF = UnicodeString(#13#10);
WideLineSeparator = WideChar($2028);
WideParagraphSeparator = WideChar($2029);
// byte order marks for Unicode files
// Unicode text files (in UTF-16 format) should contain $FFFE as first character to
// identify such a file clearly. Depending on the system where the file was created
// on this appears either in big endian or little endian style.
BOM_LSB_FIRST = WideChar($FEFF);
BOM_MSB_FIRST = WideChar($FFFE);
type
TSaveFormat = (sfUTF16LSB, sfUTF16MSB, sfUTF8, sfAnsi);
const
sfUnicodeLSB = sfUTF16LSB;
sfUnicodeMSB = sfUTF16MSB;
type
TFontCharSet = 0..255;
TSynEditFileFormat = (sffDos, sffUnix, sffMac, sffUnicode); // DOS: CRLF, UNIX: LF, Mac: CR, Unicode: LINE SEPARATOR
TUnicodeStrings = class;
// Event used to give the application a chance to switch the way of how to save
// the text in TUnicodeStrings if the text contains characters not only from the
// ANSI block but the save type is ANSI. On triggering the event the application
// can change the property SaveUnicode as needed. This property is again checked
// after the callback returns.
TConfirmConversionEvent = procedure (Sender: TUnicodeStrings; var Allowed: Boolean) of object;
{ TUnicodeStrings }
{$IFDEF UNICODE}
TUnicodeStrings = class(TStrings)
{$ELSE}
TUnicodeStrings = class(TPersistent)
{$ENDIF}
private
FUpdateCount: Integer;
FSaved: Boolean; // set in SaveToStream, True in case saving was successfull otherwise False
FFileFormat: TSynEditFileFormat;
FStreaming: Boolean;
FOnConfirmConversion: TConfirmConversionEvent;
fAppendNewLineAtEOF: Boolean;
FSaveFormat: TSaveFormat; // overrides the FSaveUnicode flag, initialized when a file is loaded,
// expect losses if it is set to sfAnsi before saving
{$IFNDEF UNICODE}
function GetCommaText: UnicodeString;
function GetName(Index: Integer): UnicodeString;
function GetValue(const Name: UnicodeString): UnicodeString;
procedure ReadData(Reader: TReader);
procedure WriteData(Writer: TWriter);
procedure SetCommaText(const Value: UnicodeString);
procedure SetValue(const Name, Value: UnicodeString);
{$ENDIF}
function GetSaveUnicode: Boolean;
procedure SetSaveUnicode(const Value: Boolean);
procedure SetFileFormat(const Value: TSynEditFileFormat);
protected
procedure DoConfirmConversion(var Allowed: Boolean); virtual;
{$IFDEF UNICODE}
function GetTextStr: UnicodeString; override;
procedure SetTextStr(const Value: UnicodeString); override;
{$ELSE}
function GetTextStr: UnicodeString; virtual;
procedure SetTextStr(const Value: UnicodeString); virtual;
procedure DefineProperties(Filer: TFiler); override;
procedure Error(const Msg: string; Data: Integer);
function Get(Index: Integer): UnicodeString; virtual; abstract;
function GetCapacity: Integer; virtual;
function GetCount: Integer; virtual; abstract;
function GetObject(Index: Integer): TObject; virtual;
procedure Put(Index: Integer; const S: UnicodeString); virtual; abstract;
procedure PutObject(Index: Integer; AObject: TObject); virtual; abstract;
procedure SetCapacity(NewCapacity: Integer); virtual;
procedure SetUpdateState(Updating: Boolean); virtual;
{$ENDIF}
public
constructor Create;
function GetSeparatedText(Separators: UnicodeString): UnicodeString; virtual;
{$IFDEF UNICODE}
procedure SaveToStream(Stream: TStream; WithBOM: Boolean = True);reintroduce; virtual;
procedure LoadFromStream(Stream: TStream); override;
procedure LoadFromFile(const FileName: string); override;
procedure SaveToFile(const FileName: string); override;
{$ELSE}
procedure SaveToStream(Stream: TStream; WithBOM: Boolean = True); virtual;
procedure LoadFromStream(Stream: TStream); virtual;
procedure AddStrings(Strings: TUnicodeStrings); overload; virtual;
procedure AddStrings(Strings: TStrings); overload; virtual;
function Add(const S: UnicodeString): Integer; virtual;
function AddObject(const S: UnicodeString; AObject: TObject): Integer; virtual;
procedure Append(const S: UnicodeString);
procedure Assign(Source: TPersistent); override;
procedure AssignTo(Dest: TPersistent); override;
procedure BeginUpdate;
procedure Clear; virtual; abstract;
procedure Delete(Index: Integer); virtual; abstract;
procedure EndUpdate;
function Equals(Strings: TUnicodeStrings): Boolean;
procedure Exchange(Index1, Index2: Integer); virtual;
function GetText: PWideChar; virtual;
function IndexOf(const S: UnicodeString): Integer; virtual;
function IndexOfName(const Name: UnicodeString): Integer;
function IndexOfObject(AObject: TObject): Integer;
procedure Insert(Index: Integer; const S: UnicodeString); virtual; abstract;
procedure InsertObject(Index: Integer; const S: UnicodeString; AObject: TObject);
procedure LoadFromFile(const FileName: TFileName); virtual;
procedure Move(CurIndex, NewIndex: Integer); virtual;
procedure SaveToFile(const FileName: TFileName); virtual;
property Capacity: Integer read GetCapacity write SetCapacity;
property CommaText: UnicodeString read GetCommaText write SetCommaText;
property Count: Integer read GetCount;
property Names[Index: Integer]: UnicodeString read GetName;
property Objects[Index: Integer]: TObject read GetObject write PutObject;
property Values[const Name: UnicodeString]: UnicodeString read GetValue write SetValue;
{$ENDIF}
property FileFormat: TSynEditFileFormat read fFileFormat write SetFileFormat;
property Saved: Boolean read FSaved;
property SaveUnicode: Boolean read GetSaveUnicode write SetSaveUnicode default True;
property SaveFormat: TSaveFormat read FSaveFormat write FSaveFormat default sfUnicodeLSB;
property AppendNewLineAtEOF: Boolean read fAppendNewLineAtEOF write fAppendNewLineAtEOF;
{$IFNDEF UNICODE}
property Strings[Index: Integer]: UnicodeString read Get write Put; default;
property Text: UnicodeString read GetTextStr write SetTextStr;
{$ENDIF}
property OnConfirmConversion: TConfirmConversionEvent read FOnConfirmConversion write FOnConfirmConversion;
end;
{ TUnicodeStringList }
//----- TUnicodeStringList class
TDynWideCharArray = array of WideChar;
TUnicodeStringItem = record
{$IFDEF OWN_UnicodeString_MEMMGR}
FString: PWideChar; // "array of WideChar";
{$ELSE}
FString: UnicodeString;
{$ENDIF OWN_UnicodeString_MEMMGR}
FObject: TObject;
end;
TUnicodeStringItemList = array of TUnicodeStringItem;
TUnicodeStringList = class(TUnicodeStrings)
private
FList: TUnicodeStringItemList;
FCount: Integer;
FSorted: Boolean;
FDuplicates: TDuplicates;
FOnChange: TNotifyEvent;
FOnChanging: TNotifyEvent;
procedure ExchangeItems(Index1, Index2: Integer);
procedure Grow;
procedure QuickSort(L, R: Integer);
procedure InsertItem(Index: Integer; const S: UnicodeString);
procedure SetSorted(Value: Boolean);
{$IFDEF OWN_UnicodeString_MEMMGR}
procedure SetListString(Index: Integer; const S: UnicodeString);
{$ENDIF OWN_UnicodeString_MEMMGR}
protected
procedure Changed; virtual;
procedure Changing; virtual;
function Get(Index: Integer): UnicodeString; override;
function GetCapacity: Integer; override;
function GetCount: Integer; override;
function GetObject(Index: Integer): TObject; override;
procedure Put(Index: Integer; const S: UnicodeString); override;
procedure PutObject(Index: Integer; AObject: TObject); override;
procedure SetCapacity(NewCapacity: Integer); override;
procedure SetUpdateState(Updating: Boolean); override;
public
destructor Destroy; override;
function Add(const S: UnicodeString): Integer; override;
procedure Clear; override;
procedure Delete(Index: Integer); override;
procedure Exchange(Index1, Index2: Integer); override;
function Find(const S: UnicodeString; var Index: Integer): Boolean; virtual;
function IndexOf(const S: UnicodeString): Integer; override;
procedure Insert(Index: Integer; const S: UnicodeString); override;
procedure Sort; virtual;
property Duplicates: TDuplicates read FDuplicates write FDuplicates;
property Sorted: Boolean read FSorted write SetSorted;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
end;
{$IFNDEF UNICODE}
{ PWideChar versions of important PAnsiChar functions from SysUtils }
function WStrLen(const Str: PWideChar): Cardinal;
function WStrEnd(const Str: PWideChar): PWideChar;
function WStrMove(Dest: PWideChar; const Source: PWideChar; Count: Integer): PWideChar;
function WStrCopy(Dest: PWideChar; const Source: PWideChar): PWideChar;
function WStrLCopy(Dest: PWideChar; const Source: PWideChar; MaxLen: Cardinal): PWideChar;
function WStrCat(Dest: PWideChar; const Source: PWideChar): PWideChar;
function WStrScan(const Str: PWideChar; Chr: WideChar): PWideChar;
function WStrAlloc(Size: Cardinal): PWideChar;
function WStrNew(const Str: PWideChar): PWideChar;
procedure WStrDispose(Str: PWideChar);
{$ENDIF}
{$IFNDEF SYN_COMPILER_6_UP}
{$IFDEF SYN_WIN32} // Kylix should have that from version 1 on
function UnicodeToUtf8(Dest: PAnsiChar; MaxDestBytes: Cardinal;
Source: PWideChar; SourceChars: Cardinal): Cardinal;
function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: Cardinal;
Source: PAnsiChar; SourceBytes: Cardinal): Cardinal;
function UTF8Encode(const WS: UnicodeString): UTF8String;
function UTF8Decode(const S: UTF8String): UnicodeString;
function AnsiToUtf8(const S: string): UTF8String;
function Utf8ToAnsi(const S: UTF8String): string;
function WideCompareStr(const S1, S2: UnicodeString): Integer;
function WideCompareText(const S1, S2: UnicodeString): Integer;
{$ENDIF}
{$ENDIF}
// Kylix has them, but Delphi 5 doesn't and Delphi 6&7 versions are buggy
// in Win9X (fix taken from Troy Wolbrinks TntUnicode-package)
{$IFDEF SYN_WIN32}
{$IFNDEF UNICODE}
var
DefaultSystemCodePage: Cardinal; // implicitly used when converting AnsiString <--> UnicodeString.
{$ENDIF}
function WCharUpper(lpsz: PWideChar): PWideChar;
function WCharUpperBuff(lpsz: PWideChar; cchLength: DWORD): DWORD;
function WCharLower(lpsz: PWideChar): PWideChar;
function WCharLowerBuff(lpsz: PWideChar; cchLength: DWORD): DWORD;
{$ENDIF}
function SynWideUpperCase(const S: UnicodeString): UnicodeString;
function SynWideLowerCase(const S: UnicodeString): UnicodeString;
function SynIsCharAlpha(const C: WideChar): Boolean;
function SynIsCharAlphaNumeric(const C: WideChar): Boolean;
{$IFNDEF UNICODE}
function CharInSet(C: AnsiChar; const CharSet: TSysCharSet): Boolean; overload; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function CharInSet(C: WideChar; const CharSet: TSysCharSet): Boolean; overload; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
{$ENDIF}
function WideLastDelimiter(const Delimiters, S: UnicodeString): Integer;
function UnicodeStringReplace(const S, OldPattern, NewPattern: UnicodeString;
Flags: TReplaceFlags): UnicodeString;
{ functions taken from JCLUnicode.pas }
function WStrComp(Str1, Str2: PWideChar): Integer;
function WStrLComp(Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
procedure StrSwapByteOrder(Str: PWideChar);
function WideQuotedStr(const S: UnicodeString; Quote: WideChar): UnicodeString;
function WideExtractQuotedStr(var Src: PWideChar; Quote: WideChar): UnicodeString;
function UnicodeStringOfChar(C: WideChar; Count: Cardinal): UnicodeString;
function WideTrim(const S: UnicodeString): UnicodeString;
function WideTrimLeft(const S: UnicodeString): UnicodeString;
function WideTrimRight(const S: UnicodeString): UnicodeString;
{$IFDEF SYN_WIN32}
function CharSetFromLocale(Language: LCID): TFontCharSet;
function CodePageFromLocale(Language: LCID): Integer;
function KeyboardCodePage: Word;
function KeyUnicode(C: AnsiChar): WideChar;
function StringToUnicodeStringEx(const S: AnsiString; CodePage: Word): UnicodeString;
function UnicodeStringToStringEx(const WS: UnicodeString; CodePage: Word): AnsiString;
{$ENDIF}
{ functions providing same behavior on Win9x and WinNT based systems}
function GetTextSize(DC: HDC; Str: PWideChar; Count: Integer): TSize;
{ Unicode versions of TCanvas-methods }
function TextExtent(ACanvas: TCanvas; const Text: UnicodeString): TSize;
function TextWidth(ACanvas: TCanvas; const Text: UnicodeString): Integer;
function TextHeight(ACanvas: TCanvas; const Text: UnicodeString): Integer;
procedure TextOut(ACanvas: TCanvas; X, Y: Integer; const Text: UnicodeString);
procedure TextRect(ACanvas: TCanvas; Rect: TRect; X, Y: Integer;
const Text: UnicodeString);
{ Unicode streaming-support }
type
TSynEncoding = (seUTF8, seUTF16LE, seUTF16BE, seAnsi);
TSynEncodings = set of TSynEncoding;
{$IFDEF UNICODE}
TWideFileStream = TFileStream;
{$ELSE}
TWideFileStream = class(THandleStream)
public
constructor Create(const FileName: UnicodeString; Mode: Word); overload;
constructor Create(const FileName: UnicodeString; Mode: Word; Rights: Cardinal); overload;
destructor Destroy; override;
end;
function WideFileOpen(const FileName: UnicodeString; Mode: LongWord): Integer;
function WideFileCreate(const FileName: UnicodeString): Integer; overload;
function WideFileCreate(const FileName: UnicodeString; Rights: Integer): Integer; overload;
{$ENDIF}
function IsAnsiOnly(const WS: UnicodeString): Boolean;
function IsUTF8(Stream: TStream; out WithBOM: Boolean): Boolean; overload;
function IsUTF8(const FileName: UnicodeString; out WithBOM: Boolean): Boolean; overload;
function GetEncoding(const FileName: UnicodeString; out WithBOM: Boolean): TSynEncoding; overload;
function GetEncoding(Stream: TStream; out WithBOM: Boolean): TSynEncoding; overload;
procedure SaveToFile(const WS: UnicodeString; const FileName: UnicodeString;
Encoding: TSynEncoding; WithBom: Boolean = True); overload;
procedure SaveToFile(UnicodeStrings: TUnicodeStrings; const FileName: UnicodeString;
Encoding: TSynEncoding; WithBom: Boolean = True); overload;
function LoadFromFile(UnicodeStrings: TUnicodeStrings; const FileName: UnicodeString;
out WithBOM: Boolean): TSynEncoding; overload;
function LoadFromFile(UnicodeStrings: TUnicodeStrings; const FileName: UnicodeString;
Encoding: TSynEncoding; out WithBOM: Boolean): TSynEncoding; overload;
procedure SaveToStream(const WS: UnicodeString; Stream: TStream;
Encoding: TSynEncoding; WithBom: Boolean = True); overload;
procedure SaveToStream(UnicodeStrings: TUnicodeStrings; Stream: TStream;
Encoding: TSynEncoding; WithBom: Boolean = True); overload;
function LoadFromStream(UnicodeStrings: TUnicodeStrings; Stream: TStream;
out WithBOM: Boolean): TSynEncoding; overload;
function LoadFromStream(UnicodeStrings: TUnicodeStrings; Stream: TStream;
Encoding: TSynEncoding; out WithBOM: Boolean): TSynEncoding; overload;
function LoadFromStream(UnicodeStrings: TUnicodeStrings; Stream: TStream;
Encoding: TSynEncoding): TSynEncoding; overload;
function ClipboardProvidesText: Boolean;
function GetClipboardText: UnicodeString;
procedure SetClipboardText(const Text: UnicodeString);
{ misc functions }
{$IFNDEF UNICODE}
{$IFNDEF SYN_COMPILER_6_UP}
function GetWideStrProp(Instance: TObject; PropInfo: PPropInfo): UnicodeString;
procedure SetWideStrProp(Instance: TObject; PropInfo: PPropInfo; const Value: UnicodeString);
{$ENDIF}
procedure UnicodeDefineProperties(Filer: TFiler; Instance: TPersistent);
{$ENDIF}
{$IFDEF SYN_WIN32}
function IsWideCharMappableToAnsi(const WC: WideChar): Boolean;
function IsUnicodeStringMappableToAnsi(const WS: UnicodeString): Boolean;
{$ENDIF}
{$IFDEF SYN_WIN32}
var
Win32PlatformIsUnicode: Boolean;
{$ENDIF}
implementation
uses
SynEditTextBuffer,
{$IFDEF SYN_UNISCRIBE}
SynUsp10,
{$ENDIF}
Math,
{$IFDEF SYN_LINUX}
Libc,
{$ENDIF}
{$IFDEF USE_TNT_RUNTIME_SUPPORT}
TntSysUtils, TntClasses,
{$ENDIF}
SysConst,
{$IFDEF SYN_COMPILER_6_UP}
RTLConsts;
{$ELSE}
Consts;
{$ENDIF}
{ TUnicodeStrings }
constructor TUnicodeStrings.Create;
begin
inherited;
SetFileFormat(sffDos);
FSaveFormat := sfUnicodeLSB;
end;
function TUnicodeStrings.GetSaveUnicode: Boolean;
begin
Result := SaveFormat in [sfUTF16LSB, sfUTF16MSB, sfUTF8];
end;
procedure TUnicodeStrings.SetSaveUnicode(const Value: Boolean);
begin
if Value then
SaveFormat := sfUnicodeLSB
else
SaveFormat := sfAnsi;
end;
procedure TUnicodeStrings.SetFileFormat(const Value: TSynEditFileFormat);
begin
fFileFormat := Value;
{$IFDEF UNICODE}
case FileFormat of
sffDos:
LineBreak := WideCRLF;
sffUnix:
LineBreak := WideLF;
sffMac:
LineBreak := WideCR;
sffUnicode:
LineBreak := WideLineSeparator;
end;
{$ENDIF}
end;
{$IFNDEF UNICODE}
function TUnicodeStrings.Add(const S: UnicodeString): Integer;
begin
Result := GetCount;
Insert(Result, S);
end;
function TUnicodeStrings.AddObject(const S: UnicodeString; AObject: TObject): Integer;
begin
Result := Add(S);
PutObject(Result, AObject);
end;
procedure TUnicodeStrings.Append(const S: UnicodeString);
begin
Add(S);
end;
procedure TUnicodeStrings.AddStrings(Strings: TStrings);
var
I: Integer;
{$IFDEF SYN_WIN32}
S: UnicodeString;
CP: Integer;
{$ENDIF}
begin
BeginUpdate;
try
{$IFDEF SYN_WIN32}
CP := CodePageFromLocale(GetThreadLocale);
for I := 0 to Strings.Count - 1 do
begin
S := StringToUnicodeStringEx(Strings[I], CP);
AddObject(S, Strings.Objects[I]);
end;
{$ELSE}
for I := 0 to Strings.Count - 1 do
AddObject(Strings[I], Strings.Objects[I]);
{$ENDIF}
finally
EndUpdate;
end;
end;
procedure TUnicodeStrings.AddStrings(Strings: TUnicodeStrings);
var
I: Integer;
begin
Assert(Strings <> nil);
BeginUpdate;
try
for I := 0 to Strings.Count - 1 do
AddObject(Strings[I], Strings.Objects[I]);
finally
EndUpdate;
end;
end;
procedure TUnicodeStrings.Assign(Source: TPersistent);
// usual assignment routine, but able to assign wide and small strings
begin
if Source is TUnicodeStrings then
begin
BeginUpdate;
try
Clear;
AddStrings(TUnicodeStrings(Source));
finally
EndUpdate;
end;
end
else
begin
if Source is TStrings then
begin
BeginUpdate;
try
Clear;
AddStrings(TStrings(Source));
finally
EndUpdate;
end;
end
else
inherited Assign(Source);
end;
end;
procedure TUnicodeStrings.AssignTo(Dest: TPersistent);
// need to do also assignment to old style TStrings, but this class doesn't know
// TUnicodeStrings, so we need to do it from here
var
I: Integer;
begin
if Dest is TStrings then
begin
with Dest as TStrings do
begin
BeginUpdate;
try
Clear;
for I := 0 to Self.Count - 1 do
AddObject(Self[I], Self.Objects[I]);
finally
EndUpdate;
end;
end;
end
else
begin
if Dest is TUnicodeStrings then
begin
with Dest as TUnicodeStrings do
begin
BeginUpdate;
try
Clear;
AddStrings(Self);
finally
EndUpdate;
end;
end;
end
else
inherited;
end;
end;
procedure TUnicodeStrings.BeginUpdate;
begin
if FUpdateCount = 0 then
SetUpdateState(True);
Inc(FUpdateCount);
end;
procedure TUnicodeStrings.DefineProperties(Filer: TFiler);
// Defines a private property for the content of the list.
// There's a bug in the handling of text DFMs in Classes.pas which prevents
// UnicodeStrings from loading under some circumstances. Zbysek Hlinka
// (zhlinka att login dott cz) brought this to my attention and supplied also a solution.
// See ReadData and WriteData methods for implementation details.
function DoWrite: Boolean;
begin
if Filer.Ancestor <> nil then
begin
Result := True;
if Filer.Ancestor is TUnicodeStrings then
Result := not Equals(TUnicodeStrings(Filer.Ancestor))
end
else
Result := Count > 0;
end;
begin
Filer.DefineProperty('UnicodeStrings', ReadData, WriteData, DoWrite);
end;
{$ENDIF}
procedure TUnicodeStrings.DoConfirmConversion(var Allowed: Boolean);
begin
if Assigned(FOnConfirmConversion) then
FOnConfirmConversion(Self, Allowed);
end;
{$IFNDEF UNICODE}
procedure TUnicodeStrings.EndUpdate;
begin
Dec(FUpdateCount);
if FUpdateCount = 0 then
SetUpdateState(False);
end;
function TUnicodeStrings.Equals(Strings: TUnicodeStrings): Boolean;
var
I, Count: Integer;
begin
Assert(Strings <> nil);
Result := False;
Count := GetCount;
if Count <> Strings.GetCount then
Exit;
for I := 0 to Count - 1 do
if Get(I) <> Strings.Get(I) then
Exit;
Result := True;
end;
procedure TUnicodeStrings.Error(const Msg: string; Data: Integer);
function ReturnAddr: Pointer;
asm
MOV EAX, [EBP + 4]
end;
begin
raise EStringListError.CreateFmt(Msg, [Data]) at ReturnAddr;
end;
procedure TUnicodeStrings.Exchange(Index1, Index2: Integer);
var
TempObject: TObject;
TempString: UnicodeString;
begin
BeginUpdate;
try
TempString := Strings[Index1];
TempObject := Objects[Index1];
Strings[Index1] := Strings[Index2];
Objects[Index1] := Objects[Index2];
Strings[Index2] := TempString;
Objects[Index2] := TempObject;
finally
EndUpdate;
end;
end;
function TUnicodeStrings.GetCapacity: Integer;
// Descendants may optionally override/replace this default implementation.
begin
Result := Count;
end;
function TUnicodeStrings.GetCommaText: UnicodeString;
var
S: UnicodeString;
P: PWideChar;
I, Count: Integer;
begin
Count := GetCount;
if (Count = 1) and (Get(0) = '') then
Result := '""'
else
begin
Result := '';
for I := 0 to Count - 1 do
begin
S := Get(I);
P := PWideChar(S);
while not (P^ in [WideNull..WideSpace, WideChar('"'), WideChar(',')]) do
Inc(P);
if P^ <> WideNull then
S := WideQuotedStr(S, '"');
Result := Result + S + ',';
end;
System.Delete(Result, Length(Result), 1);
end;
end;
function TUnicodeStrings.GetName(Index: Integer): UnicodeString;
var
P: Integer;
begin
Result := Get(Index);
P := Pos('=', Result);
if P > 0 then
SetLength(Result, P - 1)
else
Result := '';
end;
function TUnicodeStrings.GetObject(Index: Integer): TObject;
begin
Result := nil;
end;
{$ENDIF}
function TUnicodeStrings.GetSeparatedText(Separators: UnicodeString): UnicodeString;
// Same as GetText but with customizable separator characters.
var
I, L,
Size,
Count,
SepSize: Integer;
P: PWideChar;
S: UnicodeString;
begin
Count := GetCount;
SepSize := Length(Separators);
Size := 0;
for I := 0 to Count - 1 do
Inc(Size, Length(Get(I)) + SepSize);
// set one separator less, the last line does not need a trailing separator
SetLength(Result, Size - SepSize);
if Size > 0 then
begin
P := Pointer(Result);
I := 0;
while True do
begin
S := Get(I);
L := Length(S);
if L <> 0 then
begin
// add current string
System.Move(Pointer(S)^, P^, 2 * L);
Inc(P, L);
end;
Inc(I);
if I = Count then
Break;
// add separators
System.Move(Pointer(Separators)^, P^, SizeOf(WideChar) * SepSize);
Inc(P, SepSize);
end;
end;
end;
function TUnicodeStrings.GetTextStr: UnicodeString;
var
LB: UnicodeString;
begin
if not FStreaming then
Result := GetSeparatedText(SynUnicode.SLineBreak)
else
begin
case FileFormat of
sffDos:
LB := WideCRLF;
sffUnix:
LB := WideLF;
sffMac:
LB := WideCR;
sffUnicode:
if not SaveUnicode then
// Ansi-file cannot contain Unicode LINE SEPARATOR,
// so default to platform-specific Ansi-compatible SLineBreak
LB := SynUnicode.SLineBreak
else
LB := WideLineSeparator;
end;
Result := GetSeparatedText(LB);
end;
if AppendNewLineAtEOF then
Result := Result + LB;
end;
{$IFNDEF UNICODE}
function TUnicodeStrings.GetText: PWideChar;
begin
Result := WStrNew(PWideChar(GetTextStr));
end;
function TUnicodeStrings.GetValue(const Name: UnicodeString): UnicodeString;
var
I: Integer;
begin
I := IndexOfName(Name);
if I >= 0 then
Result := Copy(Get(I), Length(Name) + 2, MaxInt)
else
Result := '';
end;
function TUnicodeStrings.IndexOf(const S: UnicodeString): Integer;
begin
for Result := 0 to GetCount - 1 do
if WideCompareText(Get(Result), S) = 0 then
Exit;
Result := -1;
end;
function TUnicodeStrings.IndexOfName(const Name: UnicodeString): Integer;
var
P: Integer;
S: UnicodeString;
begin
for Result := 0 to GetCount - 1 do
begin
S := Get(Result);
P := Pos('=', S);
if (P > 0) and (WideCompareText(Copy(S, 1, P - 1), Name) = 0) then
Exit;
end;
Result := -1;
end;
function TUnicodeStrings.IndexOfObject(AObject: TObject): Integer;
begin
for Result := 0 to GetCount - 1 do
if GetObject(Result) = AObject then
Exit;
Result := -1;
end;
procedure TUnicodeStrings.InsertObject(Index: Integer; const S: UnicodeString; AObject: TObject);
begin
Insert(Index, S);
PutObject(Index, AObject);
end;
{$ENDIF}
procedure TUnicodeStrings.LoadFromFile(const FileName: string);
var
Stream: TStream;
begin
try
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
except
RaiseLastOSError;
end;
end;
procedure TUnicodeStrings.LoadFromStream(Stream: TStream);
// usual loader routine, but enhanced to handle byte order marks in stream
var
Size,
BytesRead: Integer;
ByteOrderMask: array[0..5] of Byte; // BOM size is max 5 bytes (cf: wikipedia)
// but it is easier to implement with a multiple of 2
Loaded: Boolean;
SW: UnicodeString;
SA: AnsiString;
begin
FStreaming := True;
BeginUpdate;
try
Loaded := False;
Size := Stream.Size - Stream.Position;
BytesRead := Stream.Read(ByteOrderMask[0], SizeOf(ByteOrderMask));
// UTF16 LSB = Unicode LSB/LE
if (BytesRead >= 2) and (ByteOrderMask[0] = UTF16BOMLE[0])
and (ByteOrderMask[1] = UTF16BOMLE[1]) then
begin
FSaveFormat := sfUTF16LSB;
SetLength(SW, (Size - 2) div SizeOf(WideChar));
Assert((Size and 1) <> 1, 'Number of chars must be a multiple of 2');
if BytesRead > 2 then
begin
System.Move(ByteOrderMask[2], SW[1], BytesRead - 2); // max 4 bytes = 2 widechars
if Size > BytesRead then
// first 2 chars (maximum) were copied by System.Move
Stream.Read(SW[3], Size - BytesRead);
end;
SetTextStr(SW);
Loaded := True;
end;
// UTF16 MSB = Unicode MSB/BE
if (BytesRead >= 2) and (ByteOrderMask[0] = UTF16BOMBE[0])
and (ByteOrderMask[1] = UTF16BOMBE[1]) then
begin
FSaveFormat := sfUTF16MSB;
SetLength(SW, (Size - 2) div SizeOf(WideChar));
Assert((Size and 1) <> 1, 'Number of chars must be a multiple of 2');
if BytesRead > 2 then
begin
System.Move(ByteOrderMask[2], SW[1] ,BytesRead - 2); // max 4 bytes = 2 widechars
if Size > BytesRead then
// first 2 chars (maximum) were copied by System.Move
Stream.Read(SW[3], Size - BytesRead);
StrSwapByteOrder(PWideChar(SW));
end;
SetTextStr(SW);
Loaded := True;
end;
// UTF8
if (BytesRead >= 3) and (ByteOrderMask[0] = UTF8BOM[0])
and (ByteOrderMask[1] = UTF8BOM[1]) and (ByteOrderMask[2] = UTF8BOM[2]) then
begin
FSaveFormat := sfUTF8;
SetLength(SA, (Size - 3) div SizeOf(AnsiChar));
if BytesRead > 3 then
begin
System.Move(ByteOrderMask[3], SA[1], BytesRead - 3); // max 3 bytes = 3 chars
if Size > BytesRead then
// first 3 chars were copied by System.Move
Stream.Read(SA[4], Size - BytesRead);
SW := UTF8Decode(SA);
end;
SetTextStr(SW);
Loaded := True;
end;
// default case (Ansi)
if not Loaded then
begin
FSaveFormat := sfAnsi;
SetLength(SA, Size div SizeOf(AnsiChar));
if BytesRead > 0 then
begin
System.Move(ByteOrderMask[0], SA[1], BytesRead); // max 6 bytes = 6 chars