-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathProjectCommons.pas
1272 lines (1152 loc) · 35.2 KB
/
ProjectCommons.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
/// commons types, functions and tables for fast data processing
// - this unit is part of SynProject, under GPL 3.0 license; version 1.7
unit ProjectCommons;
(*
This file is part of SynProject.
Synopse SynProject. Copyright (C) 2008-2023 Arnaud Bouchez
Synopse Informatique - https://synopse.info
SynProject is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
SynProject is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU General Public License
along with SynProject. If not, see <http://www.gnu.org/licenses/>.
*)
{
Contains some commons types, functions and tables:
type TIntegerDynArray TStringDynArray TNormToUpper TTwoDigit
PosEx() AnsiPosEx() StringReplaceAll() ValAt()
FastFindInteger[Index]()
AddInteger() AddString() IdemPChar() WinAnsiToUnicodeBuffer()
NormToLower[] NormToUpper[] TwoDigitLookup[] HexChars[]
Version history:
1.0 20080708 AB Initial release
1.1 20080820 AB FastCodeCPUID + FastMove + FastFillChar units integration
1.2 20081201 AB code review and Delphi 2009 adaptation
1.3 20090101 AB Linux compatibility added
1.4 20090202 AB new System/SysUtils uses fastcode -> no redirection anymore
FastCode unit now contains only some useful types/functions
1.9 20101015 AB
}
{ $D-,L-}
{$I Synopse.inc} // define HASINLINE CPU32 CPU64 OWNNORMTOUPPER
{$ifndef ENHANCEDRTL}
{$define NOENHANCEDRTL}
{$endif}
interface
uses
SysUtils,
Classes,
{$ifdef MSWINDOWS}
Windows;
{$else}
LibC, Types;
{$endif}
{$ifdef NOENHANCEDRTL}
type
PNormTable = ^TNormTable;
TNormTable = packed array[char] of char;
PNormTableByte = ^TNormTableByte;
TNormTableByte = packed array[byte] of byte;
var
// values locked to WinAnsi, with only A..Z ('Å'->'A' e.g.)
NormToLower, // for Lower*
NormToUpper: TNormTable; // for Upper*
NormToLowerByte: TNormTableByte absolute NormToLower;
NormToUpperByte: TNormTableByte absolute NormToUpper;
type
TTwoDigit = packed array[1..2] of char;
const
TwoDigitLookup: packed array[0..99] of TTwoDigit =
('00','01','02','03','04','05','06','07','08','09',
'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');
/// very fast copy with Table-case transform
procedure TabledPCharCpy(src, dest: pAnsiChar; Count: integer; var Table: TNormTable);
{$endif}
/// this version will use NormToUpper[]
function UpperCase(const S: string): string;
/// Faster Equivalent of Delphi 7 StrUtils.PosEx
function PosEx(const SubStr, S: AnsiString; Offset: Cardinal = 1): Integer;
/// fast PosEx, with ignoreCase (use NormToUpper)
function AnsiPosEx(const SubStr, S: AnsiString; Offset: Cardinal = 1): Integer;
/// fast replacement of StringReplace(S, OldPattern, NewPattern,[rfReplaceAll]);
function StringReplaceAll(const S, OldPattern, NewPattern: AnsiString): AnsiString;
/// read and fill a string from a filename
function StringFromFile(const FN: TFileName): AnsiString;
/// ValAt('un,deux',1)='deux'
function ValAt(const value: AnsiString; index: integer; charLimit: AnsiChar = ','): AnsiString;
/// ValAt('un,deux',1,','res) -> res='deux'
procedure ValAtPChar(pc: pAnsiChar; index: integer; charLimit: AnsiChar; var result: AnsiString);
/// 'mormot.core.toto.pas' -> 'mormot.core.toto'
function WithoutExt(const s: AnsiString): AnsiString;
type
/// wrapper to a dynamic array of integer
TIntegerDynArray = array of integer;
/// wrapper to a dynamic array of string
TStringDynArray = array of AnsiString;
PIntegers = ^TIntegers;
/// old-fashion object to store a dynamic array of integer
// - old-fashion objects are stored as record -> don't need to call free
// - memory is allocated by chunk: it's more efficient than plain
// TIntegerDynArray to store a growing array of integer
// - use internaly FastFindInteger*(), AddInteger(), etc...
TIntegers = object
Value: TIntegerDynArray;
Count: integer;
procedure Init(InitialCapacity: integer=50);
function Find(aValue: integer): boolean;
function FindIndex(aValue: integer): integer;
function Add(aValue: integer; DontDuplicate: boolean = false): integer;
/// force Value[] having exactly Count as Length
procedure SetLength;
end;
PAnsiStrings = ^TAnsiStrings;
/// old-fashion object to store a dynamic array of string
// - old-fashion objects are stored as record -> don't need to call free
// - memory is allocated by chunk: it's more efficient than plain
// TStringDynArray to store a growing array of string
TAnsiStrings = object
Value: TStringDynArray;
Count: integer;
procedure Init(InitialCapacity: integer=50);
function Find(const aValue: AnsiString; CaseSensitive: boolean=false): boolean;
function FindIndex(const aValue: AnsiString; CaseSensitive: boolean=false): integer;
/// find aName=???
function FindValue(const aName: AnsiString; const aSep: AnsiString='='): AnsiString;
function Add(const aValue: AnsiString; DontDuplicate: boolean=false; CaseSensitive: boolean=false): integer;
procedure Delete(aIndex: integer);
procedure SetLength;
end;
/// find the index of the first Value found in an array of integer
// - p points to the first integer
// - Count is the number of total integer (not integer memory size)
function FastFindIntegerIndex(p: PInteger; Count: Cardinal; Value: integer): integer;
/// return true if the Value is found in an array of integer
// - p points to the first integer
// - Count is the number of total integer (not integer memory size)
function FastFindInteger(p: PInteger; Count: Cardinal; Value: integer): boolean;
/// wrapper to add an integer in Res[] + Count
function AddInteger(var Res: TIntegerDynArray; var Count: integer; aValue: integer;
DontDuplicate: boolean = false): integer;
/// wrapper to add a string in Res[] + Count
function AddString(var Res: TStringDynArray; var Count: integer; aValue: AnsiString): integer;
/// if the beginning of p^ is same as up^ (ignore case - up^ must be already Upper)
function IdemPChar(p, up: pAnsiChar): boolean;
/// GetStringIndex(['un','deux','trois'],'deux') -> 1
function GetStringIndex(const Values: array of AnsiString; const Value: AnsiString): integer;
/// return next CSV string in P, nil if no more
function GetNextItem(var P: PAnsiChar; Sep: AnsiChar= ','): AnsiString;
/// return trim(next CSV string) from P, set P=nil if no more string available
function GetNextItemTrimed(var P: PAnsiChar; Sep: AnsiChar= ','): AnsiString;
/// return the next value as an integer, 0 if not a positive integer
function GetNextItemCardinal(var P: PAnsiChar): cardinal;
/// s := 'un,deux'; CSVAdd(s,'trois') -> s='un,deux,trois'
procedure CSVAdd(var aCSV: AnsiString; const aValue: AnsiString);
/// s := 'un,deux'; CSVAddOnce(s,'deux') -> s='un,deux'
procedure CSVAddOnce(var aCSV: AnsiString; const aValue: AnsiString);
/// s := 'un,deux'; CSVAddOnce(s,'deux,trois') -> s='un,deux,trois'
procedure CSVAddOnceCSV(var aCSV: AnsiString; const aCSVValue: AnsiString);
/// CSVContains('un,deux,trois','deux')=true
function CSVContains(const aCSV, aValue: AnsiString; Sep: AnsiChar=','): boolean;
/// CSVIndexOf('un,deux,trois','deux')=1
function CSVIndexOf(const aCSV, aValue: AnsiString; Sep: AnsiChar=','): integer;
/// v := 'un,deux,trois','deux' -> CSVDelete(v,'deux')=true and v='un,trois'
function CSVDelete(var aCSV: AnsiString; const aValue: AnsiString; Sep: AnsiChar=','): boolean;
/// LastCSV('un,deux,trois')='trois'
function LastCSV(const aCSV: AnsiString): AnsiString;
/// GetStringIndexCSV(['un','deux','trois']) -> 'un,deux,trois'
function GetStringIndexCSV(const Values: array of AnsiString): AnsiString;
/// Result[n]=true if any CSV in Values[n]
// - leave previous value of Result[] if no match
procedure SetStringIndexCSV(const CSV: AnsiString; const Values: array of AnsiString;
var Result: array of Boolean);
/// can be used in any TStrings.CustomSort() to class a list from "5.1"-like indexes
function SectionNumbersCompare(List: TStringList; Index1, Index2: Integer): Integer;
/// CSVToIntegers('1,2,3',Ints) -> Ints[]=1,2,3
procedure CSVToIntegers(const aCSV: AnsiString; var Integers: TIntegerDynArray);
var
Hex2Dec: array[AnsiChar] of byte; // for UrlDecode
/// convert a number stored in ascii in p^ to a cardinal value
// - return 0 if p^ is not a digit
// - stop at the first non-digit char in p^
// - if d is not nil, it will contain the next char to be parsed (the first one
// after the last digit converted)
function PCharToHex32(p: PAnsiChar; d: ppChar = nil): cardinal;
/// convert a number stored in ascii to a cardinal value
// - stop at the first non-digit char
// - don't raise any exception, but return zero if no digit is available
function HexToInt(const s: AnsiString): cardinal;
/// add a char to an AnsiString
// - this is faster than S := S+c (in which an AnsiString is allocated for c)
// - with FastMM4, performance is very good (ReallocMem is optimized)
procedure AddCharLS(var S: AnsiString; c: AnsiChar);
/// get an adress for a stub procedure
// function FastcodeGetAddress(AStub: Pointer): Pointer;
/// in-memory code redirection: Func will call RedirectFunc procedure/function
procedure Redirect(Func, RedirectFunc: Pointer);
implementation
{$ifdef NOENHANCEDRTL}
procedure TabledPCharCpy(src, dest: pAnsiChar; Count: integer; var Table: TNormTable); assembler;
// very fast copy with Table-case transform
// eax=src edx=dest ecx=Count
asm // generates a push ebp; mov esp,ebp
push ebx
push edi
test edx,edx
jz @z // avoid GPF
mov edi,ecx
xor ebx,ebx
xor ecx,ecx
mov ebp,Table // generates mov ebp,[ebp+8]
cmp edi,8
jae @loop
jmp dword ptr [edi*4+@Table]
nop; nop // align @Table
@Table:
dd @0,@1,@2,@3,@4,@5,@6,@7
@loop:
sub edi,8
mov bl,[eax]; mov cl,[eax+1] // use bl+cl: 2 chars each time (pairing on i686)
mov bl,[ebx+ebp]; mov cl,[ecx+ebp]
mov [edx],bl; mov [edx+1],cl
mov bl,[eax+2]; mov cl,[eax+3]
mov bl,[ebx+ebp]; mov cl,[ecx+ebp]
mov [edx+2],bl; mov [edx+3],cl
mov bl,[eax+4]; mov cl,[eax+5]
mov bl,[ebx+ebp]; mov cl,[ecx+ebp]
mov [edx+4],bl; mov [edx+5],cl
mov bl,[eax+6]; mov cl,[eax+7]
mov bl,[ebx+ebp]; mov cl,[ecx+ebp]
mov [edx+6],bl; mov [edx+7],cl
add eax,8
add edx,8
cmp edi,8
jae @loop
jmp dword ptr [edi*4+@Table]
@7: mov cl,[eax+6]; mov cl,[ecx+ebp]; mov [edx+6],cl
@6: mov bl,[eax+5]; mov bl,[ebx+ebp]; mov [edx+5],bl
@5: mov cl,[eax+4]; mov cl,[ecx+ebp]; mov [edx+4],cl
@4: mov bl,[eax+3]; mov bl,[ebx+ebp]; mov [edx+3],bl
@3: mov cl,[eax+2]; mov cl,[ecx+ebp]; mov [edx+2],cl
@2: mov bl,[eax+1]; mov bl,[ebx+ebp]; mov [edx+1],bl
@1: mov cl,[eax+0]; mov cl,[ecx+ebp]; mov [edx+0],cl
@0: mov byte ptr [edx+edi],0
@z: pop edi
pop ebx
end;
{$endif}
function PosEx(const SubStr, S: AnsiString; Offset: Cardinal = 1): Integer;
// Faster Equivalent of Delphi 7 StrUtils.PosEx
asm
push ebx
push esi
push edx {@Str}
test eax, eax
jz @@NotFound {Exit if SubStr = ''}
test edx, edx
jz @@NotFound {Exit if Str = ''}
mov esi, ecx
mov ecx,[edx-4] {Length(Str)}
mov ebx,[eax-4] {Length(SubStr)}
add ecx, edx
sub ecx, ebx {Max Start Pos for Full Match}
lea edx,[edx+esi-1] {Set Start Position}
cmp edx, ecx
jg @@NotFound {StartPos > Max Start Pos}
cmp ebx, 1 {Length(SubStr)}
jle @@SingleChar {Length(SubStr) <= 1}
push edi
push ebp
lea edi,[ebx-2] {Length(SubStr) - 2}
mov esi, eax
movzx ebx,[eax] {Search Character}
@@Loop: {Compare 2 Characters per Loop}
cmp bl,[edx]
jne @@NotChar1
mov ebp, edi {Remainder}
@@Char1Loop:
movzx eax, word ptr [esi+ebp]
cmp ax,[edx+ebp]
jne @@NotChar1
sub ebp, 2
jnc @@Char1Loop
pop ebp
pop edi
jmp @@SetResult
@@NotChar1:
cmp bl,[edx+1]
jne @@NotChar2
mov ebp, edi {Remainder}
@@Char2Loop:
movzx eax, word ptr [esi+ebp]
cmp ax,[edx+ebp+1]
jne @@NotChar2
sub ebp, 2
jnc @@Char2Loop
pop ebp
pop edi
jmp @@CheckResult
@@NotChar2:
lea edx,[edx+2]
cmp edx, ecx {Next Start Position <= Max Start Position}
jle @@Loop
pop ebp
pop edi
jmp @@NotFound
@@SingleChar:
jl @@NotFound {Needed for Zero-Length Non-NIL Strings}
movzx eax,[eax] {Search Character}
@@CharLoop:
cmp al,[edx]
je @@SetResult
cmp al,[edx+1]
je @@CheckResult
lea edx,[edx+2]
cmp edx, ecx
jle @@CharLoop
@@NotFound:
xor eax, eax
pop edx
pop esi
pop ebx
ret
@@CheckResult: {Check within AnsiString}
cmp edx, ecx
jge @@NotFound
add edx, 1
@@SetResult:
pop ecx {@Str}
pop esi
pop ebx
neg ecx
lea eax,[edx+ecx+1]
end;
function AnsiPosEx(const SubStr, S: AnsiString; Offset: Cardinal = 1): Integer;
// fast PosEx, with ignoreCase (use NormToUpper)
asm
push ebx
push esi
push edx {@Str}
test eax, eax
jz @@NotFound {Exit if SubStr = ''}
test edx, edx
jz @@NotFound {Exit if Str = ''}
mov esi, ecx
mov ecx, [edx-4] {Length(Str)}
mov ebx, [eax-4] {Length(SubStr)}
add ecx, edx
sub ecx, ebx {Max Start Pos for Full Match}
lea edx, [edx+esi-1] {Set Start Position}
cmp edx, ecx
jg @@NotFound {StartPos > Max Start Pos}
cmp ebx, 1 {Length(SubStr)}
jle @@SingleChar {Length(SubStr) <= 1}
push edi
push ebp
lea edi, [ebx-2] {Length(SubStr) - 2}
mov esi, eax
push edi {Save Remainder to Check = Length(SubStr) - 2}
push ecx {Save Max Start Position}
lea edi, NormToUpper {Uppercase Lookup Table}
movzx ebx, [eax] {Search Character = 1st Char of SubStr}
movzx ebx, [edi+ebx] {Convert to Uppercase}
@@Loop: {Loop Comparing 2 Characters per Loop}
movzx eax, [edx] {Get Next Character}
movzx eax, [edi+eax] {Convert to Uppercase}
cmp eax, ebx
jne @@NotChar1
mov ebp, [esp+4] {Remainder to Check}
@@Char1Loop:
movzx eax, [esi+ebp]
movzx ecx, [edx+ebp]
movzx eax, [edi+eax] {Convert to Uppercase}
movzx ecx, [edi+ecx] {Convert to Uppercase}
cmp eax, ecx
jne @@NotChar1
movzx eax, [esi+ebp+1]
movzx ecx, [edx+ebp+1]
movzx eax, [edi+eax] {Convert to Uppercase}
movzx ecx, [edi+ecx] {Convert to Uppercase}
cmp eax, ecx
jne @@NotChar1
sub ebp, 2
jnc @@Char1Loop
pop ecx
pop edi
pop ebp
pop edi
jmp @@SetResult
@@NotChar1:
movzx eax, [edx+1] {Get Next Character}
movzx eax, [edi+eax] {Convert to Uppercase}
cmp bl, al
jne @@NotChar2
mov ebp, [esp+4] {Remainder to Check}
@@Char2Loop:
movzx eax, [esi+ebp]
movzx ecx, [edx+ebp+1]
movzx eax, [edi+eax] {Convert to Uppercase}
movzx ecx, [edi+ecx] {Convert to Uppercase}
cmp eax, ecx
jne @@NotChar2
movzx eax, [esi+ebp+1]
movzx ecx, [edx+ebp+2]
movzx eax, [edi+eax] {Convert to Uppercase}
movzx ecx, [edi+ecx] {Convert to Uppercase}
cmp eax, ecx
jne @@NotChar2
sub ebp, 2
jnc @@Char2Loop
pop ecx
pop edi
pop ebp
pop edi
jmp @@CheckResult {Check Match is within AnsiString Data}
@@NotChar2:
add edx,2
cmp edx, [esp] {Compate to Max Start Position}
jle @@Loop {Loop until Start Position > Max Start Position}
pop ecx {Dump Start Position}
pop edi {Dump Remainder to Check}
pop ebp
pop edi
jmp @@NotFound
@@SingleChar:
jl @@NotFound {Needed for Zero-Length Non-NIL Strings}
lea esi, NormToUpper
movzx ebx, [eax] {Search Character = 1st Char of SubStr}
movzx ebx, [esi+ebx] {Convert to Uppercase}
@@CharLoop:
movzx eax, [edx]
movzx eax, [esi+eax] {Convert to Uppercase}
cmp eax, ebx
je @@SetResult
movzx eax, [edx+1]
movzx eax, [esi+eax] {Convert to Uppercase}
cmp eax, ebx
je @@CheckResult
add edx, 2
cmp edx, ecx
jle @@CharLoop
@@NotFound:
xor eax, eax
pop edx
pop esi
pop ebx
ret
@@CheckResult: {Check Match is within AnsiString Data}
cmp edx, ecx
jge @@NotFound
add edx, 1 {OK - Adjust Result}
@@SetResult: {Set Result Position}
pop ecx {@Str}
pop esi
pop ebx
neg ecx
lea eax, [edx+ecx+1]
end;
function FastFindInteger(p: PInteger; Count: Cardinal; Value: integer): boolean;
// test if Value is present in the PInteger[0..n-1]
// eax=p edx=n ecx=value
asm
test eax,eax
jz @end
cmp edx,8
jae @s1
jmp dword ptr [edx*4+@Table]
nop // align @Table
@Table:
dd @z, @1, @2, @3, @4, @5, @6, @7
@s1: // fast search by 8 integers (pipelined instructions)
sub edx,8
cmp [eax],ecx; je @ok
cmp [eax+4],ecx; je @ok
cmp [eax+8],ecx; je @ok
cmp [eax+12],ecx; je @ok
cmp [eax+16],ecx; je @ok
cmp [eax+20],ecx; je @ok
cmp [eax+24],ecx; je @ok
cmp [eax+28],ecx; je @ok
add eax,32
cmp edx,8
@s2:jae @s1
jmp dword ptr [edx*4+@Table]
@7: cmp [eax+24],ecx; je @ok
@6: cmp [eax+20],ecx; je @ok
@5: cmp [eax+16],ecx; je @ok
@4: cmp [eax+12],ecx; je @ok
@3: cmp [eax+8],ecx; je @ok
@2: cmp [eax+4],ecx; je @ok
@1: cmp [eax],ecx; je @ok
@z: xor eax,eax
@end: ret
@ok:mov al,1
end;
function FastFindIntegerIndex(p: PInteger; Count: Cardinal; Value: integer): integer;
// eax=p edx=n ecx=value
asm
push eax
test eax,eax
jz @z
cmp edx,8
jmp @s2
@ok0: pop ecx; sub eax,ecx; shr eax,2; ret
@ok4: add eax,4; pop ecx; sub eax,ecx; shr eax,2; ret
@ok8: add eax,8; pop ecx; sub eax,ecx; shr eax,2; ret
@ok12: add eax,12; pop ecx; sub eax,ecx; shr eax,2; ret
@ok16: add eax,16; pop ecx; sub eax,ecx; shr eax,2; ret
@ok20: add eax,20; pop ecx; sub eax,ecx; shr eax,2; ret
@ok24: add eax,24; pop ecx; sub eax,ecx; shr eax,2; ret
@ok28: add eax,28; pop ecx; sub eax,ecx; shr eax,2; ret
@s1:
sub edx,8
cmp [eax],ecx; je @ok0
cmp [eax+4],ecx; je @ok4
cmp [eax+8],ecx; je @ok8
cmp [eax+12],ecx; je @ok12
cmp [eax+16],ecx; je @ok16
cmp [eax+20],ecx; je @ok20
cmp [eax+24],ecx; je @ok24
cmp [eax+28],ecx; je @ok28
add eax,32
cmp edx,8
@s2:
jae @s1
test edx,edx; jz @z // we need to find first -> no jmp [edx*4] trick :(
cmp [eax],ecx; je @ok0; dec edx; jz @z
cmp [eax+4],ecx; je @ok4; dec edx; jz @z
cmp [eax+8],ecx; je @ok8; dec edx; jz @z
cmp [eax+12],ecx; je @ok12; dec edx; jz @z
cmp [eax+16],ecx; je @ok16; dec edx; jz @z
cmp [eax+20],ecx; je @ok20; dec edx; jz @z
cmp [eax+24],ecx; je @ok24
@z:
pop ecx
or eax,-1
end;
function AddInteger(var Res: TIntegerDynArray; var Count: integer; aValue: integer;
DontDuplicate: boolean = false): integer;
// ex: AddInteger(IntArray,IntArrayCount,256);
begin
if DontDuplicate then begin
result := FastFindIntegerIndex(@Res[0],Count,aValue);
if result>=0 then
exit;
end;
if Count>=length(Res) then
SetLength(Res,Count+200);
Res[Count] := aValue;
result := Count;
inc(Count);
end;
function PCharToHex32(p: PAnsiChar; d: ppChar): cardinal;
var v0,v1: cardinal;
begin
result := 0;
if p<>nil then begin
while p^=' ' do inc(p);
repeat
v0 := Hex2Dec[p[0]];
if v0=255 then break; // not in '0'..'9','a'..'f'
v1 := Hex2Dec[p[1]];
inc(p);
if v1=255 then begin
result := (result shl 4)+v0; // only one char left
break;
end;
v0 := v0 shl 4;
result := result shl 8;
inc(v0,v1);
inc(p);
inc(result,v0);
until false;
end;
if d<>nil then
d^ := p;
end;
function HexToInt(const s: AnsiString): cardinal;
begin
result := PCharToHex32(pointer(s),nil);
end;
{ TIntegers }
function TIntegers.Add(aValue: integer; DontDuplicate: boolean): integer;
begin
result := AddInteger(Value,Count,aValue,DontDuplicate);
end;
function TIntegers.Find(aValue: integer): boolean;
begin
result := FastFindInteger(@Value[0],Count,aValue);
end;
function TIntegers.FindIndex(aValue: integer): integer;
begin
result := FastFindIntegerIndex(@Value[0],Count,aValue);
end;
procedure TIntegers.Init(InitialCapacity: integer);
begin
Count := 0;
system.SetLength(Value,InitialCapacity);
end;
procedure TIntegers.SetLength;
begin
system.SetLength(Value,Count);
end;
function AddString(var Res: TStringDynArray; var Count: integer; aValue: AnsiString): integer;
begin
if Count>=length(Res) then
SetLength(Res,Count+200);
Res[Count] := aValue;
result := Count;
inc(Count);
end;
{ TAnsiStrings }
function TAnsiStrings.Add(const aValue: AnsiString; DontDuplicate: boolean = false;
CaseSensitive: boolean=false): integer;
begin
if DontDuplicate then begin
result := FindIndex(aValue,CaseSensitive);
if result>=0 then
exit;
end;
result := AddString(Value,Count,aValue);
end;
procedure TAnsiStrings.Delete(aIndex: integer);
var i: integer;
begin
if cardinal(aIndex)>=cardinal(Count) then exit;
dec(Count);
for i := aIndex to Count do // fast (no move, only lockinc), and avoid GPF
Value[i] := Value[i+1];
end;
function TAnsiStrings.Find(const aValue: AnsiString; CaseSensitive: boolean=false): boolean;
begin
result := FindIndex(aValue,CaseSensitive)>=0;
end;
function TAnsiStrings.FindIndex(const aValue: AnsiString; CaseSensitive: boolean=false): integer;
begin
if CaseSensitive then begin
for result := 0 to Count-1 do
if Value[result]=aValue then
exit;
end else
for result := 0 to Count-1 do
if SameText(Value[result],aValue) then
exit;
result := -1;
end;
function UpperCase(const S: string): string;
var L: Integer;
begin
L := length(S);
result := '';
if L=0 then
exit;
SetLength(result,L);
TabledPCharCpy(pointer(S),Pointer(result),L,NormToUpper);
end;
function TAnsiStrings.FindValue(const aName: AnsiString; const aSep: AnsiString='='): AnsiString;
var i, L: integer;
tmp: AnsiString;
begin
{$ifdef LVCL}
tmp := UpperCase(aName)+aSep;
L := length(tmp)+1;
{$else}
i := length(aName);
L := length(aSep);
if (i=0) or (L=0) then begin
result := '';
exit;
end;
system.SetLength(tmp,i+L);
TabledPCharCpy(pointer(aName),pointer(tmp),i,NormToUpper);
move(pointer(aSep)^,pointer(integer(tmp)+i)^,L);
inc(L,i+1);
{$endif}
for i := 0 to Count-1 do
if IdemPChar(pointer(Value[i]),pointer(tmp)) then begin
while Value[i][L]=' ' do inc(L); // trim left
result := copy(Value[i],L,maxInt);
exit;
end;
result := '';
end;
procedure TAnsiStrings.Init(InitialCapacity: integer);
begin
Count := 0;
system.SetLength(Value,InitialCapacity);
end;
procedure TAnsiStrings.SetLength;
begin
system.SetLength(Value,Count);
end;
function StringReplaceAll(const S, OldPattern, NewPattern: AnsiString): AnsiString;
// fast replacement of StringReplace(S, OldPattern, NewPattern,[rfReplaceAll]);
procedure Process(j: integer);
var i: integer;
begin
Result := '';
i := 1;
repeat
Result := Result+Copy(S,i,j-i)+NewPattern;
i := j+length(OldPattern);
j := PosEx(OldPattern, S, i);
if j=0 then begin
Result := Result+Copy(S, i, maxInt);
break;
end;
until false;
end;
var j: integer;
begin
j := Pos(OldPattern, S);
if j=0 then
result := S else
Process(j);
end;
function StringFromFile(const FN: TFileName): AnsiString;
var
F: THandle;
r, l: integer;
tmp: array[0..4095] of AnsiChar; // 4KB temp buffer
begin
result := '';
F := FileOpen(FN, fmOpenRead);
if F <= 0 then
exit;
l := 0;
repeat
r := FileRead(F, tmp, SizeOf(tmp));
if r <= 0 then
break;
SetLength(result, l + r);
Move(tmp, PByteArray(result)[l], r);
inc(l, r);
until r < SizeOf(tmp);
FileClose(F);
end;
procedure ValAtPChar(pc: pAnsiChar; index: integer; charLimit: AnsiChar; var result: AnsiString);
var pdeb: pAnsiChar; // optimized asm
label s;
begin
if pc=nil then
exit;
if index=0 then
goto s; // goto is deprecated, but fast & easy in this code
dec(index);
while pc^<>#0 do begin
if pc^=charLimit then begin
if index=0 then begin
inc(pc);
if pc^=#10 then
inc(pc); // ignore #10
s: pdeb := pc;
while (pc^<>#0) and (pc^<>charLimit) do
inc(pc);
SetString(result,pdeb,pc-pdeb);
exit;
end;
dec(index);
end;
inc(pc);
end;
end;
function ValAt(const value: AnsiString; index: integer; charLimit: AnsiChar = ','): AnsiString;
// ValAt('un,deux',1)='deux'
begin
result := '';
if index>=0 then
ValAtPChar(pointer(value),index,charLimit,result);
end;
function WithoutExt(const s: AnsiString): AnsiString;
var
i, max: integer;
begin
i := length(s);
max := i - 16;
while (i > 0) and
not (s[i] in ['\', '/', '.']) and
(i >= max) do
dec(i);
if (i = 0) or
(s[i] <> '.') then
result := s
else
result := copy(s, 1, i - 1);
end;
function IdemPChar(p, up: pAnsiChar): boolean;
// if the beginning of p^ is same as up^ (ignore case - up^ must be already Upper)
// eax=p edx=up
asm
test eax,eax
jz @e // P=nil -> false
test edx,edx
push ebx
push esi
jz @z // up=nil -> true
mov esi,offset NormToUpper
xor ebx,ebx
xor ecx,ecx
@1:
mov cl,[edx] // cl=up^
mov bl,[eax] // bl=p^
mov bl,[ebx+esi] // bl=NormToUpper[p^]
test cl,cl
jz @z // up^=#0 -> OK
inc edx
inc eax
cmp bl,cl
je @1
pop esi
pop ebx
xor eax,eax
@e:
ret
@z:
mov al,1 // up^=#0 -> OK
pop esi
pop ebx
end;
function GetStringIndex(const Values: array of AnsiString; const Value: AnsiString): integer;
// (['un','deux','trois'],'deux') -> 1
begin
if Value<>'' then
for result := 0 to length(Values)-1 do
if SameText(Values[result],Value) then
exit;
result := -1;
end;
function GetNextItem(var P: PAnsiChar; Sep: AnsiChar = ','): AnsiString;
// return next CSV string in P, nil if no more
var S: PAnsiChar;
begin
if P=nil then
result := '' else begin
S := P;
while (S^<>#0) and (S^<>Sep) do
inc(S);
SetString(result,P,S-P);
if S^<>#0 then
P := S+1 else
P := nil;
end;
end;
function GetNextItemCardinal(var P: PAnsiChar): cardinal;
/// return the next value as an integer, 0 if not a positive integer
var c: cardinal;
begin
if P=nil then begin
result := 0;
exit;
end;
if P^=' ' then repeat inc(P) until P^<>' ';
c := byte(P^)-48;
if c>9 then
result := 0 else begin
result := c;
inc(P);
repeat
c := byte(P^)-48;
if c>9 then
break else
result := result*10+c;
inc(P);
until false;
end;
end;
function SectionNumbersCompare(List: TStringList; Index1, Index2: Integer): Integer;
var P1,P2: PAnsiChar;
V1,V2: integer;
begin
P1 := pointer(List.Strings[Index1]);
P2 := pointer(List.Strings[Index2]);
repeat
V1 := GetNextItemCardinal(P1);
V2 := GetNextItemCardinal(P2);
result := V1-V2;
if result<>0 then
exit;
if (P1^<>'.') and (P2^<>'.') then
break;
if P1^='.' then
inc(P1);
if P2^='.' then
inc(P2);
until false;
result := StrIComp(P1,P2);
end;
function GetNextItemTrimed(var P: PAnsiChar; Sep: AnsiChar = ','): AnsiString;
// return trim(next CSV string) from P, nil if no more
var S: PAnsiChar;
L: cardinal;
begin
if P=nil then
result := '' else begin
S := P;
while (S^<>#0) and (S^<>Sep) do
inc(S);
while P^=' ' do inc(P); // trim left
L := S-P;
while P[L-1]=' ' do dec(L); // trim right
SetString(result,P,L);