-
Notifications
You must be signed in to change notification settings - Fork 3
/
Unicode.pas
2517 lines (2116 loc) · 71 KB
/
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
unit Unicode;
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CODE OFF}
interface
uses
Windows, Classes;
const
// definitions of often used characters:
// Note: Use them only for tests of a certain character not to determine character classes like
// white spaces as in Unicode are often many code points defined being in a certain class.
// Hence your best option is to use the various UnicodeIs* functions.
// can't use identifier "Null" here as this is already in a special Variant identifier
WideNull = WideChar(#0);
Tabulator = WideChar(#9);
Space = WideChar(#32);
// logical line breaks
LF = WideChar($A);
LineFeed = WideChar($A);
VerticalTab = WideChar($B);
FormFeed = WideChar($C);
CR = WideChar($D);
CarriageReturn = WideChar($D);
CRLF: WideString = #$D#$A;
LineSeparator = WideChar($2028);
ParagraphSeparator = WideChar($2029);
// byte order marks for strings
// Unicode text files 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); // this is how the BOM appears on x86 systems when written by a x86 system
BOM_MSB_FIRST = WideChar($FFFE);
type
// Unicode transformation formats (UTF) data types
UTF7 = Char;
UTF8 = Char;
UTF16 = WideChar;
UTF32 = Cardinal;
// UTF conversion schemes (UCS) data types
PUCS4 = ^UCS4;
UCS4 = Cardinal;
PUCS2 = PWideChar;
UCS2 = WideChar;
const
ReplacementCharacter: UCS4 = $0000FFFD;
MaximumUCS2: UCS4 = $0000FFFF;
MaximumUTF16: UCS4 = $0010FFFF;
MaximumUCS4: UCS4 = $7FFFFFFF;
SurrogateHighStart: UCS4 = $D800;
SurrogateHighEnd: UCS4 = $DBFF;
SurrogateLowStart: UCS4 = $DC00;
SurrogateLowEnd: UCS4 = $DFFF;
type
PCardinal = ^Cardinal;
TWideStrings = class;
// Event used to give the application a chance to switch the way of how to save the text in TWideStrings
// 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: TWideStrings; var Allowed: Boolean) of object;
TWideStrings = class(TPersistent)
private
FUpdateCount: Integer;
FLanguage: LCID; // language can usually left alone, the system's default is used
FSaved, // set in SaveToStream, True in case saving was successfull otherwise False
FSaveUnicode: Boolean; // flag set on loading to keep track in which format to save
// (can be set explicitely, but expect losses if there's true Unicode content
// and this flag is set to False)
FOnConfirmConversion: TConfirmConversionEvent;
function GetCommaText: WideString;
function GetName(Index: Integer): WideString;
function GetValue(const Name: WideString): WideString;
procedure ReadData(Reader: TReader);
procedure SetCommaText(const Value: WideString);
procedure SetValue(const Name, Value: WideString);
procedure WriteData(Writer: TWriter);
protected
procedure DefineProperties(Filer: TFiler); override;
procedure Error(const Msg: String; Data: Integer);
function Get(Index: Integer): WideString; virtual; abstract;
function GetCapacity: Integer; virtual;
function GetCount: Integer; virtual; abstract;
function GetObject(Index: Integer): TObject; virtual;
function GetTextStr: WideString; virtual;
procedure Put(Index: Integer; const S: WideString); virtual;
procedure PutObject(Index: Integer; AObject: TObject); virtual;
procedure SetCapacity(NewCapacity: Integer); virtual;
procedure SetTextStr(const Value: WideString); virtual;
procedure SetUpdateState(Updating: Boolean); virtual;
procedure SetLanguage(Value: LCID); virtual;
function CompareStrings(const S1, S2: WideString): Integer; virtual;
public
constructor Create;
destructor Destroy; override;
function Add(const S: WideString): Integer; virtual;
function AddObject(const S: WideString; AObject: TObject): Integer; virtual;
procedure Append(const S: WideString);
procedure AddStrings(Strings: TStrings); overload; virtual;
procedure AddStrings(Strings: TWideStrings); overload; virtual;
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: TWideStrings): Boolean;
procedure Exchange(Index1, Index2: Integer); virtual;
function GetText: PWideChar; virtual;
function IndexOf(const S: WideString): Integer; virtual;
function IndexOfName(const Name: WideString): Integer;
function IndexOfObject(AObject: TObject): Integer;
procedure Insert(Index: Integer; const S: WideString); virtual; abstract;
procedure InsertObject(Index: Integer; const S: WideString; AObject: TObject);
procedure LoadFromFile(const FileName: String); virtual;
procedure LoadFromStream(Stream: TStream); virtual;
procedure Move(CurIndex, NewIndex: Integer); virtual;
procedure SaveToFile(const FileName: String); virtual;
procedure SaveToStream(Stream: TStream); virtual;
procedure SetText(Text: PWideChar); virtual;
property Capacity: Integer read GetCapacity write SetCapacity;
property CommaText: WideString read GetCommaText write SetCommaText;
property Count: Integer read GetCount;
property Language: LCID read FLanguage write SetLanguage;
property Names[Index: Integer]: WideString read GetName;
property Objects[Index: Integer]: TObject read GetObject write PutObject;
property Values[const Name: WideString]: WideString read GetValue write SetValue;
property Saved: Boolean read FSaved;
property SaveUnicode: Boolean read FSaveUnicode write FSaveUnicode;
property Strings[Index: Integer]: WideString read Get write Put; default;
property Text: WideString read GetTextStr write SetTextStr;
property OnConfirmConversion: TConfirmConversionEvent read FOnConfirmConversion write FOnConfirmConversion;
end;
// TWideStringList class
TWideStringList = class;
TWideStringItem = record
FString: WideString;
FObject: TObject;
end;
TWideStringItemList = array of TWideStringItem;
TWideStringListSortCompare = function(List: TWideStringList; Index1, Index2: Integer): Integer;
TWideStringList = class(TWideStrings)
private
FList: TWideStringItemList;
FCount: Integer;
FSorted: Boolean;
FDuplicates: TDuplicates;
FCaseSensitive: Boolean;
FOnChange: TNotifyEvent;
FOnChanging: TNotifyEvent;
procedure ExchangeItems(Index1, Index2: Integer);
procedure Grow;
procedure QuickSort(L, R: Integer; SCompare: TWideStringListSortCompare);
procedure InsertItem(Index: Integer; const S: WideString);
procedure SetSorted(Value: Boolean);
procedure SetCaseSensitive(const Value: Boolean);
protected
procedure Changed; virtual;
procedure Changing; virtual;
function Get(Index: Integer): WideString; override;
function GetCapacity: Integer; override;
function GetCount: Integer; override;
function GetObject(Index: Integer): TObject; override;
procedure Put(Index: Integer; const S: WideString); override;
procedure PutObject(Index: Integer; AObject: TObject); override;
procedure SetCapacity(NewCapacity: Integer); override;
procedure SetUpdateState(Updating: Boolean); override;
procedure SetLanguage(Value: LCID); override;
function CompareStrings(const S1, S2: WideString): Integer; override;
public
destructor Destroy; override;
function Add(const S: WideString): Integer; override;
procedure Clear; override;
procedure Delete(Index: Integer); override;
procedure Exchange(Index1, Index2: Integer); override;
function Find(const S: WideString; var Index: Integer): Boolean; virtual;
function IndexOf(const S: WideString): Integer; override;
procedure Insert(Index: Integer; const S: WideString); override;
procedure Sort; virtual;
procedure CustomSort(Compare: TWideStringListSortCompare); virtual;
property Duplicates: TDuplicates read FDuplicates write FDuplicates;
property Sorted: Boolean read FSorted write SetSorted;
property CaseSensitive: Boolean read FCaseSensitive write SetCaseSensitive;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
end;
// utility functions
function StrNewW(Str: PWideChar): PWideChar;
function StrEndW(Str: PWideChar): PWideChar;
function StrLenW(Str: PWideChar): Cardinal;
function StrMoveW(Dest, Source: PWideChar; Count: Cardinal): PWideChar;
function StrScanW(Str: PWideChar; Chr: WideChar): PWideChar; overload;
function StrScanW(Str: PWideChar; Chr: WideChar; StrLen: Cardinal): PWideChar; overload;
function StrAllocW(Size: Cardinal): PWideChar;
procedure StrSwapByteOrder(Str: PWideChar);
function UnicodeIsWhiteSpace(C: UCS4): Boolean;
function UnicodeIsNonSpacing(C: UCS4): Boolean;
function UnicodeIsHangul(C: UCS4): Boolean;
function UnicodeIsControl(C: UCS4): Boolean;
function UnicodeIsComposite(C: UCS4): Boolean;
function CodePageFromLocale(Language: LCID): Integer;
function WideQuotedStr(const S: WideString; Quote: WideChar): WideString;
function WideExtractQuotedStr(var Src: PWideChar; Quote: WideChar): WideString;
function WideStringToString(var ws: WideString; codePage: Word): AnsiString;
function StringToWideString(const s: AnsiString; codePage: Word): WideString;
function GetDefaultAnsiCodepage(LangID: LCID; defCP: integer): word;
{ functions taken from JCLUnicode.pas }
function WStrComp(Str1, Str2: PWideChar): Integer;
function WStrLComp(Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
function UnicodeStringOfChar(C: WideChar; Count: Cardinal): WideString;
function WideTrim(const S: WideString): WideString;
function WideTrimLeft(const S: WideString): WideString;
function WideTrimRight(const S: WideString): WideString;
{$IFNDEF UNICODE}
var
DefaultSystemCodePage: Cardinal; // implicitly used when converting AnsiString <--> WideString.
{$ENDIF}
{$IFNDEF Win64}
var
Win32PlatformIsUnicode: Boolean;
function WCharUpper(lpsz: PWideChar): PWideChar;
function WCharUpperBuff(lpsz: PWideChar; cchLength: DWORD): DWORD;
function WCharLower(lpsz: PWideChar): PWideChar;
function WCharLowerBuff(lpsz: PWideChar; cchLength: DWORD): DWORD;
function IsWideCharMappableToAnsi(const WC: WideChar): Boolean;
function IsUnicodeStringMappableToAnsi(const WS: WideString): Boolean;
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: WideString): UTF8String;
function UTF8Decode(const S: UTF8String): WideString;
function AnsiToUtf8(const S: string): UTF8String;
function Utf8ToAnsi(const S: UTF8String): string;
function WideCompareStr(const S1, S2: WideString): Integer;
function WideCompareText_(const S1, S2: WideString): Integer;
{$ENDIF}
//----------------------------------------------------------------------------------------------------------------------
implementation
// ~67K Unicode data for case mapping, decomposition, numbers etc.
// This data is loaded on demand which means only those parts will be put in memory which are needed
// by one of the lookup functions.
{$IfDEF QR4}
{$R Unicode.res}
{$endif}
uses
SyncObjs, SysUtils;
resourcestring
SUREBaseString = 'Error in regular expression: %s' + #13;
SUREUnexpectedEOS = 'Unexpected end of pattern.';
SURECharacterClassOpen = 'Character class not closed, '']'' is missing.';
SUREUnbalancedGroup = 'Unbalanced group expression, '')'' is missing.';
SUREInvalidCharProperty = 'A character property is invalid';
SUREInvalidRepeatRange = 'Invalid repeation range.';
SURERepeatRangeOpen = 'Repeation range not closed, ''}'' is missing.';
SUREExpressionEmpty = 'Expression is empty.';
type
TCompareFunc = function (W1, W2: WideString; Locale: LCID): Integer;
var
WideCompareText: TCompareFunc;
//----------------- Loader routines for resource data ------------------------------------------------------------------
const
// Values that can appear in the Mask1 parameter of the IsProperty function.
UC_MN = $00000001; // Mark, Non-Spacing
UC_MC = $00000002; // Mark, Spacing Combining
UC_ME = $00000004; // Mark, Enclosing
UC_ND = $00000008; // Number, Decimal Digit
UC_NL = $00000010; // Number, Letter
UC_NO = $00000020; // Number, Other
UC_ZS = $00000040; // Separator, Space
UC_ZL = $00000080; // Separator, Line
UC_ZP = $00000100; // Separator, Paragraph
UC_CC = $00000200; // Other, Control
UC_CF = $00000400; // Other, Format
UC_OS = $00000800; // Other, Surrogate
UC_CO = $00001000; // Other, private use
UC_CN = $00002000; // Other, not assigned
UC_LU = $00004000; // Letter, Uppercase
UC_LL = $00008000; // Letter, Lowercase
UC_LT = $00010000; // Letter, Titlecase
UC_LM = $00020000; // Letter, Modifier
UC_LO = $00040000; // Letter, Other
UC_PC = $00080000; // Punctuation, Connector
UC_PD = $00100000; // Punctuation, Dash
UC_PS = $00200000; // Punctuation, Open
UC_PE = $00400000; // Punctuation, Close
UC_PO = $00800000; // Punctuation, Other
UC_SM = $01000000; // Symbol, Math
UC_SC = $02000000; // Symbol, Currency
UC_SK = $04000000; // Symbol, Modifier
UC_SO = $08000000; // Symbol, Other
UC_L = $10000000; // Left-To-Right
UC_R = $20000000; // Right-To-Left
UC_EN = $40000000; // European Number
UC_ES = $80000000; // European Number Separator
// Values that can appear in the Mask2 parameter of the IsProperty function
UC_ET = $00000001; // European Number Terminator
UC_AN = $00000002; // Arabic Number
UC_CS = $00000004; // Common Number Separator
UC_B = $00000008; // Block Separator
UC_S = $00000010; // Segment (unit) Separator (this includes tab and vertical tab)
UC_WS = $00000020; // Whitespace
UC_ON = $00000040; // Other Neutrals
// Implementation specific character properties.
UC_CM = $00000080; // Composite
UC_NB = $00000100; // Non-Breaking
UC_SY = $00000200; // Symmetric
UC_HD = $00000400; // Hex Digit
UC_QM = $00000800; // Quote Mark
UC_MR = $00001000; // Mirroring
UC_SS = $00002000; // Space, other
UC_CP = $00004000; // Defined
// Added for UnicodeData-2.1.3.
UC_PI = $00008000; // Punctuation, Initial
UC_PF = $00010000; // Punctuation, Final
type
TUHeader = record
BOM: WideChar;
Count: Word;
case Boolean of
True:
(Bytes: Cardinal);
False:
(Len: array[0..1] of Word);
end;
TWordArray = array of Word;
TCardinalArray = array of Cardinal;
var
// As the global data can be accessed by several threads it should be guarded
// while the data is loaded.
LoadInProgress: TCriticalSection;
//----------------- internal support routines --------------------------------------------------------------------------
function SwapCardinal(C: Cardinal): Cardinal;
// swaps all bytes in C from MSB to LSB order
// EAX contains both parameter as well as result
asm
BSWAP EAX
end;
//----------------- support for character properties -------------------------------------------------------------------
var
PropertyOffsets: TWordArray;
PropertyRanges: TCardinalArray;
procedure LoadUnicodeTypeData;
// loads the character property data (as saved by the Unicode database extractor into the ctype.dat file)
var
I, Size: Integer;
Header: TUHeader;
Stream: TResourceStream;
begin
// make sure no other code is currently modifying the global data area
if LoadInProgress = nil then LoadInProgress := TCriticalSection.Create;
LoadInProgress.Enter;
// Data already loaded?
if PropertyOffsets = nil then
begin
Stream := TResourceStream.Create(HInstance, 'TYPE', 'UNICODE');
Stream.Read(Header, SizeOf(Header));
if Header.BOM = BOM_MSB_FIRST then
begin
Header.Count := Swap(Header.Count);
Header.Bytes := SwapCardinal(Header.Bytes);
end;
// Calculate the offset into the storage for the ranges. The offsets
// array is on a 4-byte boundary and one larger than the value provided in
// the header count field. This means the offset to the ranges must be
// calculated after aligning the count to a 4-byte boundary.
Size := (Header.Count + 1) * SizeOf(Word);
if (Size and 3) <> 0 then Inc(Size, 4 - (Size and 3));
// fill offsets array
SetLength(PropertyOffsets, Size div SizeOf(Word));
Stream.Read(PropertyOffsets[0], Size);
// Do an endian swap if necessary. Don't forget there is an extra node on the end with the final index.
if Header.BOM = BOM_MSB_FIRST then
begin
for I := 0 to Header.Count do
PropertyOffsets[I] := Swap(PropertyOffsets[I]);
end;
// Load the ranges. The number of elements is in the last array position of the offsets.
SetLength(PropertyRanges, PropertyOffsets[Header.Count]);
Stream.Read(PropertyRanges[0], PropertyOffsets[Header.Count] * SizeOf(Cardinal));
// Do an endian swap if necessary.
if Header.BOM = BOM_MSB_FIRST then
begin
for I := 0 to PropertyOffsets[Header.Count] - 1 do
PropertyRanges[I] := SwapCardinal(PropertyRanges[I]);
end;
Stream.Free;
end;
LoadInProgress.Leave;
end;
//----------------------------------------------------------------------------------------------------------------------
function PropertyLookup(Code, N: Cardinal): Boolean;
var
L, R, M: Integer;
begin
// load property data if not already done
if PropertyOffsets = nil then LoadUnicodeTypeData;
Result := False;
// There is an extra node on the end of the offsets to allow this routine
// to work right. If the index is 0xffff, then there are no nodes for the property.
L := PropertyOffsets[N];
if L <> $FFFF then
begin
// Locate the next offset that is not 0xffff. The sentinel at the end of
// the array is the max index value.
M := 1;
while ((Integer(N) + M) < High(PropertyOffsets)) and (PropertyOffsets[Integer(N) + M] = $FFFF) do Inc(M);
R := PropertyOffsets[Integer(N) + M] - 1;
while L <= R do
begin
// Determine a "mid" point and adjust to make sure the mid point is at
// the beginning of a range pair.
M := (L + R) shr 1;
Dec(M, M and 1);
if Code > PropertyRanges[M + 1] then L := M + 2
else
if Code < PropertyRanges[M] then R := M - 2
else
if (Code >= PropertyRanges[M]) and (Code <= PropertyRanges[M + 1]) then
begin
Result := True;
Break;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function IsProperty(Code, Mask1, Mask2: Cardinal): Boolean;
var
I: Cardinal;
Mask: Cardinal;
begin
Result := False;
if Mask1 <> 0 then
begin
Mask := 1;
for I := 0 to 31 do
begin
if ((Mask1 and Mask) <> 0) and PropertyLookup(Code, I) then
begin
Result := True;
Exit;
end;
Mask := Mask shl 1;
end;
end;
if Mask2 <> 0 then
begin
I := 32;
Mask := 1;
while I < Cardinal(High(PropertyOffsets)) do
begin
if ((Mask2 and Mask) <> 0) and PropertyLookup(Code, I) then
begin
Result := True;
Exit;
end;
Inc(I);
Mask := Mask shl 1;
end;
end;
end;
//----------------- TWideStrings ---------------------------------------------------------------------------------------
constructor TWideStrings.Create;
begin
inherited;
// there should seldom be the need to use a language other than the one of the system
FLanguage := GetUserDefaultLCID;
end;
//----------------------------------------------------------------------------------------------------------------------
destructor TWideStrings.Destroy;
begin
inherited;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TWideStrings.SetLanguage(Value: LCID);
begin
FLanguage := Value;
end;
function TWideStrings.CompareStrings(const S1, S2: WideString): Integer;
begin
Result := WideCompareText(S1, S2, FLanguage);
end;
//----------------------------------------------------------------------------------------------------------------------
function TWideStrings.Add(const S: WideString): Integer;
begin
Result := GetCount;
Insert(Result, S);
end;
//----------------------------------------------------------------------------------------------------------------------
function TWideStrings.AddObject(const S: WideString; AObject: TObject): Integer;
begin
Result := Add(S);
PutObject(Result, AObject);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TWideStrings.Append(const S: WideString);
begin
Add(S);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TWideStrings.AddStrings(Strings: TStrings);
var
I: Integer;
begin
BeginUpdate;
try
for I := 0 to Strings.Count - 1 do AddObject(Strings[I], Strings.Objects[I]);
finally
EndUpdate;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TWideStrings.AddStrings(Strings: TWideStrings);
var
I: Integer;
begin
BeginUpdate;
try
for I := 0 to Strings.Count - 1 do AddObject(Strings[I], Strings.Objects[I]);
finally
EndUpdate;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TWideStrings.Assign(Source: TPersistent);
// usual assignment routine, but able to assign wide and small strings
var
I: Integer;
begin
if Source is TWideStrings then
begin
BeginUpdate;
try
Clear;
AddStrings(TWideStrings(Source));
finally
EndUpdate;
end;
end
else
if Source is TStrings then
begin
BeginUpdate;
try
Clear;
for I := 0 to TStrings(Source).Count - 1 do AddObject(TStrings(Source)[I], TStrings(Source).Objects[I]);
finally
EndUpdate;
end;
end
else inherited Assign(Source);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TWideStrings.AssignTo(Dest: TPersistent);
// need to do also assignment to old style TStrings, but this class doesn't know TWideStrings, so
// we need to do it from here
var
I: Integer;
begin
if Dest is TStrings then
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
else
if Dest is TWideStrings then
with Dest as TWideStrings do
begin
BeginUpdate;
try
Clear;
AddStrings(Self);
finally
EndUpdate;
end;
end
else inherited;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TWideStrings.BeginUpdate;
begin
if FUpdateCount = 0 then SetUpdateState(True);
Inc(FUpdateCount);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TWideStrings.DefineProperties(Filer: TFiler);
function DoWrite: Boolean;
begin
if Filer.Ancestor <> nil then
begin
Result := True;
if Filer.Ancestor is TWideStrings then Result := not Equals(TWideStrings(Filer.Ancestor))
end
else Result := Count > 0;
end;
begin
Filer.DefineProperty('WideStrings', ReadData, WriteData, DoWrite);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TWideStrings.EndUpdate;
begin
Dec(FUpdateCount);
if FUpdateCount = 0 then SetUpdateState(False);
end;
//----------------------------------------------------------------------------------------------------------------------
function TWideStrings.Equals(Strings: TWideStrings): Boolean;
var
I, Count: Integer;
begin
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 TWideStrings.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 TWideStrings.Exchange(Index1, Index2: Integer);
var
TempObject: TObject;
TempString: WideString;
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 TWideStrings.GetCapacity: Integer;
begin // descendants may optionally override/replace this default implementation
Result := Count;
end;
//----------------------------------------------------------------------------------------------------------------------
function TWideStrings.GetCommaText: WideString;
var
S: WideString;
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..Space, 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 TWideStrings.GetName(Index: Integer): WideString;
var
P: Integer;
begin
Result := Get(Index);
P := Pos('=', Result);
if P > 0 then SetLength(Result, P - 1)
else Result := '';
end;
//----------------------------------------------------------------------------------------------------------------------
function TWideStrings.GetObject(Index: Integer): TObject;
begin
Result := nil;
end;
//----------------------------------------------------------------------------------------------------------------------
function TWideStrings.GetText: PWideChar;
begin
Result := StrNewW(PWideChar(GetTextStr));
end;
//----------------------------------------------------------------------------------------------------------------------
function TWideStrings.GetTextStr: WideString;
var
I, L,
Size,
Count: Integer;
P: PWideChar;
S: WideString;
begin
Count := GetCount;
Size := 0;
for I := 0 to Count - 1 do Inc(Size, Length(Get(I)) + 2);
SetLength(Result, Size);
P := Pointer(Result);
for I := 0 to Count - 1 do
begin
S := Get(I);
L := Length(S);
if L <> 0 then
begin
System.Move(Pointer(S)^, P^, 2 * L);
Inc(P, L);
end;
P^ := CarriageReturn;
Inc(P);
P^ := LineFeed;
Inc(P);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TWideStrings.GetValue(const Name: WideString): WideString;
var
I: Integer;
begin
I := IndexOfName(Name);
if I >= 0 then Result := Copy(Get(I), Length(Name) + 2, MaxInt)
else Result := '';
end;
//----------------------------------------------------------------------------------------------------------------------
function TWideStrings.IndexOf(const S: WideString): Integer;
begin
for Result := 0 to GetCount - 1 do
if WideCompareText(Get(Result), S, FLanguage) = 0 then Exit;
Result := -1;
end;
//----------------------------------------------------------------------------------------------------------------------
function TWideStrings.IndexOfName(const Name: WideString): Integer;
var
P: Integer;
S: WideString;
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, FLanguage) = 0) then Exit;
end;
Result := -1;
end;
//----------------------------------------------------------------------------------------------------------------------
function TWideStrings.IndexOfObject(AObject: TObject): Integer;
begin
for Result := 0 to GetCount - 1 do
if GetObject(Result) = AObject then Exit;
Result := -1;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TWideStrings.InsertObject(Index: Integer; const S: WideString; AObject: TObject);
begin
Insert(Index, S);
PutObject(Index, AObject);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TWideStrings.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 TWideStrings.LoadFromStream(Stream: TStream);
// usual loader routine, but enhanced to handle byte order marks in stream
var
Size,
BytesRead: Integer;
Order: WideChar;
SW: WideString;
SA: String;
begin
BeginUpdate;
try
Size := Stream.Size - Stream.Position;
BytesRead := Stream.Read(Order, 2);
if (Order = BOM_LSB_FIRST) or (Order = BOM_MSB_FIRST) then
begin
FSaveUnicode := True;
SetLength(SW, (Size - 2) div 2);
Stream.Read(PWideChar(SW)^, Size - 2);
if Order = BOM_MSB_FIRST then StrSwapByteOrder(PWideChar(SW));
SetTextStr(SW);
end
else
begin
// without byte order mark it is assumed that we are loading ANSI text
FSaveUnicode := False;