-
Notifications
You must be signed in to change notification settings - Fork 3
/
uTLH.Strings.pas
5972 lines (5485 loc) · 201 KB
/
uTLH.Strings.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
{
TetzkatLipHoka 2015-2019
uTLH: Strings
Last updated: 07/04/2019
}
{
IsLower - Returns true if string is lowercase
IsUpper - Returns true if string is uppercase
IsAlpha - Returns true if string contains only chars ( A-Z,a-z )
IsAlNum - Returns true if string contains only chars ( A-Z, a-z, 0-9 )
IsNum - Check if string is a number
IsFloat - Check if string is a float
IsNumIn - Check if string is a number and within min/max
IsIn - Check if string contains 'text'
IsInNum - Similiar to IsIn but returns amount of appearances of 'text' in string
IsInNumPos - Child of IsInNum, returns start-position of apperance X in string
IsWm - Check if string contains 'wildcard'
Remove - Remove all appearances of 'Text' in 'string', result is new string
Replace - Replace all appearances of 'Text' in 'string' with 'rText', result is new string
ConvertSpacesToTabs - Convert TABs to given value of spaces
ConvertTabsToSpaces - Convert given value of spaces to TABs
RemoveTabsAndSpaces - Strip Tabs and ( dublicate ) spaces
TrimLineAfter - Remove any chars after amount of chars from start
TrimLineFromLeft - Remove amount of chars from start of the string( s )
TrimLineFromRight - Remove amount of chars from end of the string( s )
RemoveSpacesFromLineStart - Remove any blanks/spaces from line start( s )
RemoveSpacesFromLineEnd - Remove any blanks/spaces from line end( s )
ReturnWildCardPositionInString - Returns start to end char of a string matching a wildcard
ReplaceStringAreal - Replace Text in a String from 'start' to 'end' with 'rText'
WildcardToRegEx - Convert Wildcard to RegExp
GetCharFromVirtualKey - Convert VirtualKey (VK_) to Char
CompareVersion - Compares 2 Versions (1.0.0.0 format), can check for: =, <>, >, <, >=, <=
Extends missing Tokens and less-Unit-Values (1.22 >= 1.2.1.1 --> 1.22.0.0 >= 1.02.1.1)
IsIPv4 - Returns true if given value is a possible IP
IPToNum - IP address to decimal coded address
NumToIP - Coded IP address to IP address in the format %d.%d.%d.%d where %d is a byte
GetCurrencyValue - Converts a currency string to currency, even with '£', ',' etc. in the string
CountCharInString - Returns count of given char in given string
CountSubStringInString - Returns count of given SubStrings in given string
Roman_To_Integer - Roman( V ) to Integer( 5 )
Integer_To_Roman - Integer( 10 ) to Roman( X )
StrToFloat_Int - ( Try to ) Convert string to FloatNumber, trying '.' & ',' DecimalSeperators
StrToDate_Int - ( Try to ) Convert string to TDateTime, trying '/' & '.' DateSeperators
possible to exchange Day and Month
ScanFile - Search in binary file for string, returns position in file
CompareStrings - Compare two Strings - True if content is equal
StrNatCompare - Compare two strings (Internaly used (StringList.CustomSort))
StringToStringList - Converts a string into a TStringList
RandomString - Returns RandomString of given length
NumberToWord - Converts Float to words (123.5 -> 'One Hundred and Twenty Three point Five')
TextStatistics - Returns Char-, Word-, Sentence-, Paragraph-Count for given Text
GetLineSeperator - Returns Line-Seperator-Char(s) for given Text (#13, #10, #1310)
GetLineSeperatorAsText - Returns Line-Seperator-Char(s) for given Text as Text (CR, LF, CR-LF)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Bin/Hex Conversion~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ByteToHex - Convert Byte to HexChars
HexToByte - Convert HexChars to Byte
StrToHexA/W - Convert String to HexString
HexToStrA/W - Convert HexString to String
HexToInt - Convert HexString to Integer (Int64)
HexToUInt - Convert HexString to UInt (UInt64)
HexToTypedStr - Convert HextString to Typed-String (Integer would be same as 'IntToStr( HexToInt( INPUT ) )')
TypedStrToHex - Convert Typed-String to HextString
HexToByteString - Convert HexString to ByteString
IncDecNumberChar - Increase or Decrease Hex- or Number-Char
StrToBin - Convert String to BinaryString
BinToStr - Convert BinaryString to String
IntToBin - Convert Integer to Binary
StrToUInt64Def - Convert String to UInt64
UIntToStr - Convert Cardinal/UInt64 to String
IsHex - Returns true if given string is hex
IsHexChar - Returns true if given char is hex
FormatHex - Returns Group-Formated Hex-String (FF00FAFE -> FF-00-FA-FE)
ProperHex - Returns proper HexString (Removes Space, Leading '$' or '0x')
TrimHex - Trim unneeded 0's from Hex-String
HexToBinaryString - Hex to 0111...
PointerToHex - Returns PointerBytes as Hex (Max 10 Bytes)
HexToPointer - Write Hex to PointerBytes (Max 10 Bytes)
FloatToHex - Returns HexString for given Float
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~String/File Checksums~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HashDigestAsString - Convert Hash-Digest to String
Hash - Returns -see below-Checksum for String or File
HMAC - Returns HMAC-see below-Hashes for String or File (No CRC32)
CRC32 - Returns CRC32 for String or File
MD5 - Returns MD5 for String or File (D10S+ or Indy9)
SHA1 - Returns SHA1 for String or File (D10S+ or Indy10)
SHA224 - Returns SHA224 for String or File (D10S+ or Indy10)
SHA256 - Returns SHA256 for String or File (D10S+ or Indy10)
SHA384 - Returns SHA384 for String or File (D10S+ or Indy10)
SHA512 - Returns SHA512 for String or File (D10S+ or Indy10)
SHA512_256 - Returns SHA512-256 for String or File (D10S+ or Indy10)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Base64~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EncodeBase64 - Converts Data to a base64 string using Alphabet. if Pad is True, the result will be padded with PadChar to be a multiple of PadMultiple.
DecodeBase64 - Converts a base64 string using Alphabet (64 characters for values 0-63) to Data.
EncodeBase64A/W - Converts a String to a base64 string using Alphabet. if Pad is True, the result will be padded with PadChar to be a multiple of PadMultiple.
DecodeBase64A/W - Converts a base64 string using Alphabet (64 characters for values 0-63) to a String.
Base64MIMEDecode - Encode using Base64 with MIME-Alphabet
Base64MIMEEncode - Decode using Base64 with MIME-Alphabet
Base64UUEncode - Encode using Base64 with UU-Alphabet
Base64UUDecode - Decode using Base64 with UU-Alphabet
Base64XXEncode - Encode using Base64 with XX-Alphabet
Base64XXDecode - Decode using Base64 with XX-Alphabet
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Tokens~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tokenize( text, C ) : TStringDynArray Deconstructs the string in 'text' to its tokens seperated by 'C'
Tokenize( TStringDynArray, C ): string; The output is a 'TStringDynArray' of Type 'Array of String'
Also reconstructs a TStringDynArray to a string seperated by 'C'
The C parameter is the ascii value of the character separating the Tokens.
AddTok( text,Token,C ) Adds a Token to the end of text but only if it's not already in text.
AddTok( a.b.c,d,46 ) returns a.b.c.d
AddTok( a.b.c.d,c,46 ) returns a.b.c.d
DelTok( text,N,C ) Deletes the Nth Token from text.
DelTok( a.b.c.d,3,46 ) returns a.b.d
DelTok( a.b.c.d,2,3,46 ) returns a.d
FindTok( text,Token,N,C ) Returns the position of the Nth matching Token in text.
FindTok( a.b.c.d,c,1,46 ) returns 3
FindTok( a.b.c.d,e,1,46 ) returns $null
GetTok( text,N[ ,N2 ],C, IgnoreEmpty ) Returns the Nth Token in text.
GetTok( a.b.c.d.e,3,46 ) returns c
GetTok( a.b.c.d.e,9,46 ) returns $null
GetTok( a.b.c.d.e,2,4,46 ) returns Tokens 2 through 4 b.c.d
If N2 is specified a range of Tokens is returned
IgnoreEmpty is TRUE by default, means 'abc;;def' will split as 2 Tokens: 'abc;' and 'def'
IgnoreEmpty on FALSE would act like this: 'abc;;def' will split as 3 Tokens: 'abc', '' and 'def'
InsTok( text,Token,N,C ) Inserts Token into the Nth position in text, even if it already exists in text.
InsTok( a.b.d,c,3,46 ) returns a.b.c.d
InsTok( a.b.d,c,9,46 ) returns a.b.d.c
IsTok( text,Token,C ) Returns $true if Token exists, otherwise returns $false.
MatchTok( Tokens,string,N,C ) Returns Tokens that contain the specified string.
MatchTok( one two three, e, 0, 32 ) returns 2
MatchTok( one two three, e, 2, 32 ) returns three
NumTok( text,C,IgnoreEmpty ) Returns number of Tokens in text.
IgnoreEmpty is TRUE by default, means 'abc;;def' will return 2 Tokens: 'abc;' and 'def'
IgnoreEmpty on FALSE would act like this: 'abc;;def' will return 3 Tokens: 'abc', '' and 'def'
PutTok( text,Token,N,C ) Overwrites the Nth Token in text with a new Token.
PutTok( a.b.c.d,e,2,46 ) returns a.e.c.d
RemTok( text,Token,N,C ) Removes the Nth matching Token from text. If N = 0, applies to all matching items.
RemTok( a.b.c.d,b,1,46 ) returns a.c.d
RemTok( a.b.c.d,e,1,46 ) returns a.b.c.d
RemTok( a.c.c.d,c,1,46 ) returns a.c.d
RepTok( text,Token,new,N,C ) Replaces the Nth matching Token in text with a new Token. If N = 0, applies to all matching items.
RepTok( a.b.c.d,b,e,1,46 ) returns a.e.c.d
RepTok( a.b.c.d,f,e,1,46 ) returns a.b.c.d
RepTok( a.b.a.c,a,e,2,46 ) returns a.b.e.c
WildTok( Tokens,wildstring,N,C ) Returns the Nth Token that matches the wildcard string.
WildTok( one two three, t*, 0, 32 ) returns 2
WildTok( one two three, t*e, 1, 32 ) returns three
SortTok( text,C,( rnadc ) ) Sorts the Tokens in text.
SortTok( e.d.c.b.a,46 ) returns a.b.c.d.e
Options: SortTok( 1.3.5.2.4,46,nd ) returns 5.4.3.2.1
R = Regular sorting -|
N = Natural sorting -| Can't be combined
A = Ascending -|
D = Descending -| Can't be combined
C = CaseSensitive - Can't be used with Regular-sorting
Default/no options = Natural, Descending case insensitive
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~TStringList.CustomSort~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NaturalSortAsc - Natural/Human sorting, case inSensitive
NaturalSortDesc - Natural/Human sorting, case inSensitive
NaturalSortAscCS - Natural/Human sorting, case Sensitive
NaturalSortDescCS - Natural/Human sorting, case Sensitive
}
{$IFNDEF TLH_OMIT_SECTIONS}
unit uTLH.Strings;
{$WARN UNSAFE_CODE OFF}
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CAST OFF}
{$IF CompilerVersion >= 23}
{$WARN IMPLICIT_STRING_CAST OFF}
{$WARN SYMBOL_PLATFORM OFF}
{$LEGACYIFEND ON}
{$IFEND}
{$WARN GARBAGE OFF}
{$I uTLH.inc}
interface
uses
Windows, Classes, Types{TStringDynArray},
SysUtils,
Controls{StrToDate_Int}
{$IFNDEF UNICODE}
{$IFDEF USE_UNICODE}
,Unicode
{$ENDIF}
{$IFDEF USE_SYNUNICODE}
,SynUnicode
{$ENDIF}
{$ENDIF}
;
{$ENDIF TLH_OMIT_SECTIONS}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$IF NOT Defined( TLH_IMPLEMENTATIONSECTION ) AND NOT Defined( TLH_INITIALIZATIONSECTION ) AND NOT Defined( TLH_FINALIZATIONSECTION )}
function CompareStrings( Str, Str2: String; caseSensitive : boolean = false ):boolean; overload; // True if similiar
function IsLower( value: string ): Boolean;
function IsUpper( value: string ): Boolean;
function IsAlpha( value: string ): Boolean;
function IsAlNum( value: string ): Boolean;
function IsNum( Number: string; Signed : boolean = True ): Boolean;
function IsFloat( Number: string; Signed : boolean = True ): Boolean;
function IsNumIn( Number: string; Min, Max : integer ): Boolean; overload;
function IsIn( Text, Wild: string; caseSensitive : boolean = false ): boolean;
function IsInNum( Text, Wild: string; caseSensitive : boolean = false ): integer;
function IsinNumPos( Text, Wild: string; N : integer; caseSensitive : boolean = false ): integer;
function IsWm( Text, MatchText: String; caseSensitive : boolean = false ): Boolean; //Text Matches wildcard
function Remove( Text, RText: string; WildCard : Boolean = false; caseSensitive : boolean = false ):string; overload; //Removes RText from Text
function Replace( Text, mText, rText: string; WildCard : Boolean = false; caseSensitive : boolean = false ): string; overload //Replace any occurence of Item on Text by NewItem
function StrNatCompare( const Str1, Str2: string; const CaseSensitive: boolean = false ): integer;
function ConvertSpacesToTabs( text : string; value : Byte ): string; overload;
function ConvertTabsToSpaces( text : string; value : Byte ): string; overload;
function RemoveTabsAndSpaces( text : string ): string; overload;
function StringToStringList( text : string ): TStringList;
function StringToStrings( text : string ): TStrings; overload;
{$IFNDEF UNICODE}
{$IFDEF USE_UNICODE}
function StringToStrings( text : WideString ): TWideStrings;
{$ENDIF}
{$IFDEF USE_SYNUNICODE}
function StringToStrings( text : UnicodeString ): TUnicodeStrings; overload;
{$ENDIF}
{$ENDIF}
function TrimLineAfter( str : string; Value : Byte ): string; overload;
function TrimLineFromLeft( str : string; Value : Byte ): string; overload;
function TrimLineFromRight( str : string; Value : Byte ): string; overload;
function RemoveSpacesFromLineStart( str : string ): string; overload;
function RemoveSpacesFromLineEnd( str : string ): string; overload;
function ReturnWildCardPositionInString( Text, WildCard : string; var start, endN : integer; caseSensitive : boolean = false ): boolean;
function WildcardToRegEx( Wildcard : string ): string;
function IsIPv4( IP : string ): boolean;
function IPToNum( IP: string ): string;
function NumToIP( Num: string ): string;
function Roman_To_Integer( Roman: String ): Integer;
function Integer_To_Roman( number: integer ): String;
function StrToFloat_Int( pText : string; default : Extended = 0 ): Extended;
function StrToDate_Int( value : string; changeMonthAndDay : boolean = false ): TDate;
function GetCurrencyValue( S : string ) : extended;
function CountCharInString( c: char; s: string ):integer;
function CountSubStringInString( const Text: string; const SubText: string ) : Cardinal;
function ScanFile( const FileName: string; const forString: string; caseSensitive: Boolean = false ): Longint;
function GetCharFromVirtualKey( Key: Word ): String;
function RandomString( const Len: Byte = 16 ): string;
function NumberToWord( ChkER: boolean; ER: string; D: double ): string;
procedure TextStatistics( Text : string; Chars : PCardinal; Words : PCardinal = nil; Sentences : PCardinal = nil; Paragraphs : PCardinal = nil; Lines : PCardinal = nil );
function ReplaceStringAreal( InString : string; start, endN : integer; rText : string ): string; overload;
procedure ReplaceStringAreal_( var InString : string; start, endN : integer; rText : string ); overload;
{$IFNDEF UNICODE}
function ReplaceStringAreal( InString : WideString; start, endN : integer; rText : string ): WideString; overload;
procedure ReplaceStringAreal_( var InString : WideString; start, endN : integer; rText : WideString ); overload;
{$ENDIF}
function GetLineSeperator( Text : string ) : string;
function GetLineSeperatorAsText( Text : string ) : string;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type
tCompareVersionMode = ( cvmEqual, cvmUnequal, cvmAbove, cvmBelow, cvmAboveOrEqual, cvmBelowOrEqual );
function CompareVersion( v1, v2 : string; vMode : tCompareVersionMode = cvmAboveOrEqual ) : boolean;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function HexToByte( sHex : string ) : Byte;
function ByteToHex( B : Byte ) : ShortString;
function StrToHex( sHex: string ): string;
function StrToHexA( sHex: AnsiString ): string;
function StrToHexW( sHex: WideString ): string;
function HexToStrA( sHex: String ): AnsiString;
function HexToStrW( sHex: String ): WideString;
function HexToInt( sHex : string ): Int64;
function HexToUInt( sHex : string; BigEndian : boolean = false ): UInt64;
type
tHexToTypedStrTypes = ( httsByte, httsShortInt, httsWord, httsSmallInt, httsCardinal, httsInteger, httsUINT64, httsInt64, httsReal48, httsSingle,
httsDouble, httsExtended, httsComp, httsCurrency, httsDateTime, httsCTime, httsAnsiString, httsWideString );
function HexToTypedStr( sHex : string; vType : tHexToTypedStrTypes; BigEndian : boolean = false ) : String;
function TypedStrToHex( s : string; vType : tHexToTypedStrTypes; BigEndian : boolean = false ) : String;
function HexToByteString( sHex : string; SeperatorChar : Char = #32 ) : string;
function IncDecNumberChar( C : Char; Increase : Boolean = True; Hex : boolean = false ) : Char;
function StrToBin( aStr: string ): string;
function BinToStr( aStr: string ): string;
function IntToBin( Value: Cardinal ): string;
{$IF CompilerVersion <= 23}
function StrToUInt64Def( const s: string; Default : UInt64 ): UInt64;
{$IFEND}
{$IF CompilerVersion <= 23}
function UIntToStr( UInt : Cardinal ): string; overload;
function UIntToStr( UInt : UInt64 ): string; overload; // D7 is still unable to Handle it properly as unsigned
{$IFEND}
function IsHexChar( c : char ): boolean;
function IsHex( sHex : String ):Boolean;
function ProperHex( sHex : String ) : String;
function FormatHex( sHex : String; GroupLen : Byte = 2 ) : String;
function TrimHex( sHex : string; BigEndian : boolean = false ) : string;
function HexToBinaryString( sHex : string; ByteCount : Cardinal = 0; BigEndian : Boolean = False ) : string;
function PointerToHex( P : Pointer; TypeSize : Byte; BigEndian : boolean = false ): string;
function HexToPointer( sHex : string; P : Pointer; TypeSize : Byte; BigEndian : boolean = false ) : Boolean;
function FloatToHex( f : real48; BigEndian : boolean = false ): string; overload;
function FloatToHex( f : single; BigEndian : boolean = false ): string; overload;
function FloatToHex( f : double; BigEndian : boolean = false ): string; overload;
function FloatToHex( f : extended; BigEndian : boolean = false ): string; overload;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Checksum
function CRC32( const Bytes : {$IF CompilerVersion >= 30}TBytes{$ELSE}TByteDynArray{$IFEND} ): string; overload;
function CRC32( const TextOrFileName: string ): string; overload;
{$IF ( CompilerVersion >= 30 ) OR Defined( INDY90 ) OR Defined( INDY100 )}
type
tHashType = ( htCRC32, htMD5, htSHA1, htSHA224, htSHA256, htSHA384, htSHA512, htSHA512_224, htSHA512_256 );
function HMAC( const TextOrFileName, Key : string; HashType : tHashType = htMD5 ) : string; overload;
function HMAC( const Bytes : {$IF CompilerVersion >= 30}TBytes{$ELSE}TByteDynArray{$IFEND}; Key : string; HashType : tHashType = htMD5 ) : string; overload;
function Hash( const Bytes : {$IF CompilerVersion >= 30}TBytes{$ELSE}TByteDynArray{$IFEND}; HashType : tHashType = htMD5 ) : string; overload;
function Hash( const TextOrFileName : string; HashType : tHashType = htMD5 ) : string; overload;
function HashDigestAsString(const ADigest: {$IF CompilerVersion >= 30}TBytes{$ELSE}TByteDynArray{$IFEND}): string;
function MD5( const TextOrFileName : string ) : string;
{$IFEND}
{$IF ( CompilerVersion >= 30 ) OR Defined( INDY100 )}
function SHA1( const TextOrFileName : string ) : string;
function SHA224( const TextOrFileName : string ) : string;
function SHA256( const TextOrFileName : string ) : string;
function SHA384( const TextOrFileName : string ) : string;
function SHA512( const TextOrFileName : string ) : string;
{$IFEND}
{$IF CompilerVersion >= 30}
function SHA512_256( const TextOrFileName : string ) : string;
{$IFEND}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const
// ValidURLChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$-_@.&+-!*"''(),;/#?:';
ValidURLChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$-_@.&+-!*"''(),;#?';
function URLEncode( const Value: AnsiString; ValidChars : AnsiString = ValidURLChars ): AnsiString;
function URLDecode( const Value: AnsiString ): AnsiString;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type
ByteCharSet = set of AnsiChar;
PByteChar = ^AnsiChar;
function EncodeBase64( const Data : Pointer; const Len : Int64; const Alphabet: AnsiString; const Pad: Boolean = False; const PadMultiple: Integer = 4; const PadChar: AnsiChar = AnsiChar( Ord( '=' ) ) ): AnsiString; overload;
function EncodeBase64( const S : string; const Alphabet: AnsiString; const Pad: Boolean = False; const PadMultiple: Integer = 4; const PadChar: AnsiChar = AnsiChar( Ord( '=' ) ) ): AnsiString; overload;
function EncodeBase64A( const S, Alphabet: AnsiString; const Pad: Boolean = False; const PadMultiple: Integer = 4; const PadChar: AnsiChar = AnsiChar( Ord( '=' ) ) ): AnsiString;
function EncodeBase64W( const S : WideString; const Alphabet: AnsiString; const Pad: Boolean = False; const PadMultiple: Integer = 4; const PadChar: AnsiChar = AnsiChar( Ord( '=' ) ) ): AnsiString;
procedure DecodeBase64( const S, Alphabet: AnsiString; var OutByteArray : TByteDynArray; const PadSet: ByteCharSet ); overload;
function DecodeBase64( const S, Alphabet: AnsiString; const PadSet: ByteCharSet ) : string; overload;
function DecodeBase64A( const S, Alphabet: AnsiString; const PadSet: ByteCharSet ) : AnsiString;
function DecodeBase64W( const S, Alphabet: AnsiString; const PadSet: ByteCharSet ) : WideString;
const
b64_MIMEBase64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
b64_UUEncode = ' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_';
b64_XXEncode = '+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
// MIME
function Base64MIMEEncode( const P: Pointer; const Len : Int64 ): AnsiString; overload;
function Base64MIMEEncode( const S: String ): String; overload;
function Base64MIMEEncodeA( const S: AnsiString ): AnsiString;
function Base64MIMEEncodeW( const S: WideString ): WideString;
procedure Base64MIMEDecode( const S: AnsiString; var OutByteArray : TByteDynArray ); overload;
function Base64MIMEDecode( const S: AnsiString ): String; overload;
function Base64MIMEDecodeA( const S: AnsiString ): AnsiString;
function Base64MIMEDecodeW( const S: AnsiString ): WideString;
// UU
function Base64UUEncode( const P: Pointer; const Len : Int64 ): AnsiString; overload;
function Base64UUEncode( const S: String ): String; overload;
function Base64UUEncodeA( const S: AnsiString ): AnsiString;
function Base64UUEncodeW( const S: WideString ): WideString;
procedure Base64UUDecode( const S: AnsiString; var OutByteArray : TByteDynArray ); overload;
function Base64UUDecode( const S: AnsiString ): String; overload;
function Base64UUDecodeA( const S: AnsiString ): AnsiString;
function Base64UUDecodeW( const S: AnsiString ): WideString;
// XX
function Base64XXEncode( const P: Pointer; const Len : Int64 ): AnsiString; overload;
function Base64XXEncode( const S: String ): String; overload;
function Base64XXEncodeA( const S: AnsiString ): AnsiString;
function Base64XXEncodeW( const S: WideString ): WideString;
procedure Base64XXDecode( const S: AnsiString; var OutByteArray : TByteDynArray ); overload;
function Base64XXDecode( const S: AnsiString ): String; overload;
function Base64XXDecodeA( const S: AnsiString ): AnsiString;
function Base64XXDecodeW( const S: AnsiString ): WideString;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Tokenize
type
TWStringDynArray = Array of WideString;
function AddTok( const S, T: string; const C: Char; caseSensitive : boolean = False; AllowDuplicates : boolean = false ): string; overload;
function AddTok( const S, T: string; const B: Byte; caseSensitive : boolean = False; AllowDuplicates : boolean = false ): string; overload;
function DelTok( const S: string; const N: Integer; const B: Byte ): string; overload;
function DelTok( const S: string; const N: Integer; const C: Char ): string; overload;
function DelTok( const S: string; const N1, N2: Integer; const B: Byte ): string; overload;
function DelTok( const S: string; const N1, N2: Integer; const C: Char ): string; overload;
function FindTok( const S, T: string; N: Integer; const B: Byte; caseSensitive : boolean = False ): Integer; overload;
function FindTok( const S, T: string; N: Integer; const C: Char; caseSensitive : boolean = False ): Integer; overload;
function GetTok( const S: string; const N1, N2: Integer; const B: Byte; IgnoreEmpty : boolean = true ): string; overload;
function GetTok( const S: string; const N1, N2: Integer; const C: Char; IgnoreEmpty : boolean = true ): string; overload;
function GetTok( const S: string; N: Integer; const B: Byte; IgnoreEmpty : boolean = true ): string; overload;
function GetTok( const S: string; N: Integer; const C: Char; IgnoreEmpty : boolean = true ): string; overload;
function InsTok( const S, T: string; N: Integer; const B: Byte ): string; overload;
function InsTok( const S, T: string; N: Integer; const C: Char ): string; overload;
function PutTok( const S, T: string; N: Integer; const B: Byte ): string; overload;
function PutTok( const S, T: string; N: Integer; const C: Char ): string; overload;
function IsTok( const S, T: string; const C: Char; caseSensitive : boolean = False ): Boolean; overload;
function IsTok( const S, T: string; const B: Byte; caseSensitive : boolean = False ): Boolean; overload;
function MatchTok( const S, T: string; N: Integer; const B: Byte; caseSensitive : boolean = False ): Variant; overload;
function MatchTok( const S, T: string; N: Integer; const C: Char; caseSensitive : boolean = False ): Variant; overload;
function NumTok( const S: string; const C: Char; IgnoreEmpty : boolean = true ): Integer; overload;
function NumTok( const S: string; const B: Byte; IgnoreEmpty : boolean = true ): Integer; overload;
function RemTok( const S, T: string; N: Integer; const C: Char; const caseSensitive : boolean = False ): string; overload;
function RemTok( const S, T: string; N: Integer; const B: Byte; const caseSensitive : boolean = False ): string; overload;
function ExTok( const S : string; T1, T2: Integer; const B: Byte ): string; overload;
function ExTok( const S : string; T1, T2: Integer; const C: Char ): string; overload;
function MoveTok( const S : string; T1, T2: Integer; const B: Byte ): string; overload;
function MoveTok( const S : string; T1, T2: Integer; const C: Char ): string; overload;
function CloneTok( const S : string; T1, T2: Integer; const B: Byte ): string; overload;
function CloneTok( const S : string; T1, T2: Integer; const C: Char ): string; overload;
function RepTok( const S, T, T2: string; N: Integer; const B: Byte; const caseSensitive : boolean = False ): string; overload;
function RepTok( const S, T, T2: string; N: Integer; const C: Char; const caseSensitive : boolean = False ): string; overload;
function Tokenize( const S: string; const B: Byte; IgnoreEmpty : boolean = true ): TStringDynArray; overload;
function Tokenize( const S: string; const C: Char; IgnoreEmpty : boolean = true ): TStringDynArray; overload;
function Tokenize( const S: TStringDynArray; const B: Byte; caseSensitive : boolean = False; AllowDuplicates : boolean = false ): string; overload;
function Tokenize( const S: TStringDynArray; const C: Char; caseSensitive : boolean = False; AllowDuplicates : boolean = false ): string; overload;
function WildTok( const S, T: string; N: Integer; const C: Char ): Variant; overload;
function WildTok( const S, T: string; N: Integer; const B: Byte ): Variant; overload;
function SortTok( const S : string; C : Char ): string; overload;
function SortTok( const S : string; B : Byte ): string; overload;
function SortTok( const S, Options : string; C : Char ): string; overload;
function SortTok( const S, Options : string; B : Byte ): string; overload;
// WideString Versions
{$IFNDEF UNICODE}
{$IF NOT Defined( USE_UNICODE ) AND NOT Defined( USE_SYNUNICODE )}
function WStrComp(Str1, Str2: PWideChar): Integer;
function WStrLComp(Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
{$IFEND}
function AddTok( const S, T: WideString; const C: WideChar; caseSensitive : boolean = False; AllowDuplicates : boolean = false ): WideString; overload;
function AddTok( const S, T: WideString; const B: Byte; caseSensitive : boolean = False; AllowDuplicates : boolean = false ): WideString; overload;
function DelTok( const S: WideString; const N: Integer; const B: Byte ): WideString; overload;
function DelTok( const S: WideString; const N: Integer; const C: WideChar ): WideString; overload;
function DelTok( const S: WideString; const N1, N2: Integer; const B: Byte ): WideString; overload;
function DelTok( const S: WideString; const N1, N2: Integer; const C: WideChar ): WideString; overload;
function FindTok( const S, T: WideString; N: Integer; const B: Byte; caseSensitive : boolean = False ): Integer; overload;
function FindTok( const S, T: WideString; N: Integer; const C: WideChar; caseSensitive : boolean = False ): Integer; overload;
function GetTok( const S: WideString; const N1, N2: Integer; const B: Byte; IgnoreEmpty : boolean = true ): WideString; overload;
function GetTok( const S: WideString; const N1, N2: Integer; const C: WideChar; IgnoreEmpty : boolean = true ): WideString; overload;
function GetTok( const S: WideString; N: Integer; const B: Byte; IgnoreEmpty : boolean = true ): WideString; overload;
function GetTok( const S: WideString; N: Integer; const C: WideChar; IgnoreEmpty : boolean = true ): WideString; overload;
function InsTok( const S, T: WideString; N: Integer; const B: Byte ): WideString; overload;
function InsTok( const S, T: WideString; N: Integer; const C: WideChar ): WideString; overload;
function PutTok( const S, T: WideString; N: Integer; const B: Byte ): WideString; overload;
function PutTok( const S, T: WideString; N: Integer; const C: WideChar ): WideString; overload;
function IsTok( const S, T: WideString; const C: WideChar; caseSensitive : boolean = False ): Boolean; overload;
function IsTok( const S, T: WideString; const B: Byte; caseSensitive : boolean = False ): Boolean; overload;
function MatchTok( const S, T: WideString; N: Integer; const B: Byte; caseSensitive : boolean = False ): Variant; overload;
function MatchTok( const S, T: WideString; N: Integer; const C: WideChar; caseSensitive : boolean = False ): Variant; overload;
function NumTok( const S: WideString; const C: WideChar; IgnoreEmpty : boolean = true ): Integer; overload;
function NumTok( const S: WideString; const B: Byte; IgnoreEmpty : boolean = true ): Integer; overload;
function RemTok( const S, T: WideString; N: Integer; const C: WideChar; const caseSensitive : boolean = False ): WideString; overload;
function RemTok( const S, T: WideString; N: Integer; const B: Byte; const caseSensitive : boolean = False ): WideString; overload;
function ExTok( const S : WideString; T1, T2: Integer; const B: Byte ): WideString; overload;
function ExTok( const S : WideString; T1, T2: Integer; const C: WideChar ): WideString; overload;
function MoveTok( const S : WideString; T1, T2: Integer; const B: Byte ): WideString; overload;
function MoveTok( const S : WideString; T1, T2: Integer; const C: WideChar ): WideString; overload;
function CloneTok( const S : WideString; T1, T2: Integer; const B: Byte ): WideString; overload;
function CloneTok( const S : WideString; T1, T2: Integer; const C: WideChar ): WideString; overload;
function RepTok( const S, T, T2: WideString; N: Integer; const B: Byte; const caseSensitive : boolean = False ): WideString; overload;
function RepTok( const S, T, T2: WideString; N: Integer; const C: WideChar; const caseSensitive : boolean = False ): WideString; overload;
function Tokenize( const S: WideString; const B: Byte; IgnoreEmpty : boolean = true ): TStringDynArray; overload;
function Tokenize( const S: WideString; const C: WideChar; IgnoreEmpty : boolean = true ): TStringDynArray; overload;
function Tokenize( const S: TWStringDynArray; const B: Byte; caseSensitive : boolean = False; AllowDuplicates : boolean = false ): WideString; overload;
function Tokenize( const S: TWStringDynArray; const C: WideChar; caseSensitive : boolean = False; AllowDuplicates : boolean = false ): WideString; overload;
function WildTok( const S, T: WideString; N: Integer; const C: WideChar ): Variant; overload;
function WildTok( const S, T: WideString; N: Integer; const B: Byte ): Variant; overload;
function SortTok( const S : WideString; C : WideChar ): WideString; overload;
function SortTok( const S : WideString; B : Byte ): WideString; overload;
function SortTok( const S, Options : WideString; C : WideChar ): WideString; overload;
function SortTok( const S, Options : WideString; B : Byte ): WideString; overload;
{$ENDIF UNICODE}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~TStringList.CustomSort~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function NaturalSortAsc( List: TStringList; Index1, Index2: Integer ): Integer;{$IFNDEF UNICODE} overload;{$ENDIF}
function NaturalSortDesc( List: TStringList; Index1, Index2: Integer ): Integer;{$IFNDEF UNICODE} overload;{$ENDIF}
function NaturalSortAscCS( List: TStringList; Index1, Index2: Integer ): Integer;{$IFNDEF UNICODE} overload;{$ENDIF}
function NaturalSortDescCS( List: TStringList; Index1, Index2: Integer ): Integer;{$IFNDEF UNICODE} overload;{$ENDIF}
function DateSortAsc( List: TStringList; Index1, Index2: Integer ): Integer;{$IFNDEF UNICODE} overload;{$ENDIF}
function DateSortDesc( List: TStringList; Index1, Index2: Integer ): Integer;{$IFNDEF UNICODE} overload;{$ENDIF}
{$IFNDEF UNICODE}
{$IFDEF USE_UNICODE}
function NaturalSortAsc( List: TWideStringList; Index1, Index2: Integer ): Integer; overload;
function NaturalSortDesc( List: TWideStringList; Index1, Index2: Integer ): Integer; overload;
function NaturalSortAscCS( List: TWideStringList; Index1, Index2: Integer ): Integer; overload;
function NaturalSortDescCS( List: TWideStringList; Index1, Index2: Integer ): Integer; overload;
function DateSortAsc( List: TWideStringList; Index1, Index2: Integer ): Integer; overload;
function DateSortDesc( List: TWideStringList; Index1, Index2: Integer ): Integer; overload;
{$ENDIF}
{$IFDEF USE_SYNUNICODE}
function NaturalSortAscU( String1, String2 : UnicodeString ): Integer; overload;
function NaturalSortDescU( String1, String2 : UnicodeString): Integer; overload;
function NaturalSortAscCSU( String1, String2 : UnicodeString ): Integer; overload;
function NaturalSortDescCSU( String1, String2 : UnicodeString ): Integer; overload;
function DateSortAscU( String1, String2 : UnicodeString ): Integer; overload;
function DateSortDescU( String1, String2 : UnicodeString ): Integer; overload;
{$ENDIF}
{$ENDIF}
{$IFEND TLH_IMPLEMENTATIONSECTION}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$IFNDEF TLH_OMIT_SECTIONS}
implementation
uses
Math, StrUtils, Variants
{$IF CompilerVersion >= 30}
,System.Hash
{$ELSE}
{$IFDEF USE_EXTERNAL_HASH}
,uHash
{$IFDEF USE_MD5},MD5{$ENDIF}
{$IFDEF USE_SHA1},SHA1{$ENDIF}
{$IFDEF USE_SHA224},SHA224{$ENDIF}
{$IFDEF USE_SHA256},SHA256{$ENDIF}
{$IFDEF USE_SHA384},SHA384{$ENDIF}
{$IFDEF USE_SHA512},SHA512{$ENDIF}
{$IFDEF USE_SHA5_224},SHA5_224{$ENDIF}
{$IFDEF USE_SHA5_256},SHA5_256{$ENDIF}
{$ENDIF}
{$IF Defined( INDY90 ) OR Defined( INDY100 )},IdHashMessageDigest{$IFEND} // MD5 - Indy9/10
{$IFDEF INDY100},IdHash, IdHashSHA, IdSSLOpenSSLHeaders, IdGlobal{$ENDIF} // SHA1/SHA256 - Indy10+
{$IFEND}
;
{$ENDIF TLH_OMIT_SECTIONS}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$IF NOT Defined( TLH_INTERFACESECTION ) AND NOT Defined( TLH_INITIALIZATIONSECTION ) AND NOT Defined( TLH_FINALIZATIONSECTION )}
// Regular
function CompareStrings( Str, Str2: String; caseSensitive : boolean = false ):boolean; // True if similiar
begin
if caseSensitive then
Result := CompareStr( Str, Str2 ) = 0
else
result := CompareText( Str, Str2 ) = 0;
end;
function IsLower( value: string ): Boolean;
begin
result := CompareStrings( value, LowerCase( value ), true );
end;
function IsUpper( value: string ): Boolean;
begin
result := CompareStrings( value, UpperCase( value ), true );
end;
function IsAlpha( value: string ): Boolean;
var
i: Integer;
begin
Result := True;
value := Trim( value );
for i := {$IF CompilerVersion >= 24}Low( value ){$ELSE}1{$IFEND} to {$IF CompilerVersion >= 24}High( value ){$ELSE}Length( value ){$IFEND} do
if NOT {$IF CompilerVersion >= 22}CharInSet( value[ i ],{$ELSE}( value[ i ] in{$IFEND} [ 'a'..'z','A'..'Z' ] ) then
begin
Result := false;
break;
end;
end;
function IsAlNum( value: string ): Boolean;
var
i : integer;
begin
Result := True;
value := Trim( value );
for i := {$IF CompilerVersion >= 24}Low( value ){$ELSE}1{$IFEND} to {$IF CompilerVersion >= 24}High( value ){$ELSE}Length( value ){$IFEND} do
if NOT {$IF CompilerVersion >= 22}CharInSet( value[ i ],{$ELSE}( value[ i ] in{$IFEND}[ {$IF CompilerVersion >= 30}FormatSettings.{$ELSE}SysUtils.{$IFEND}DecimalSeparator, '-', 'a'..'z','A'..'Z','0'..'9' ] ) then
begin
Result := false;
break;
end;
end;
function IsNum( Number: string; Signed : boolean = True ): Boolean;
var
i : integer;
begin
for i := {$IF CompilerVersion >= 24}Low( number ){$ELSE}1{$IFEND} to {$IF CompilerVersion >= 24}High( number ){$ELSE}Length( number ){$IFEND} do
begin
if ( ( Number[ i ] = '-' ) OR ( Number[ i ] = '+' ) ) then
begin
if ( ( NOT Signed ) AND ( ( Number[ i ] = '-' ) ) ) OR ( i > {$IF CompilerVersion >= 24}Low( number ){$ELSE}1{$IFEND} ) then
begin
result := false;
Exit;
end;
end
else if NOT {$IF CompilerVersion >= 22}CharInSet( Number[ i ],{$ELSE}( Number[ i ] in{$IFEND} [ '0'..'9' ] ) then
begin
Result := False;
Exit;
end;
end;
Result := True;
end;
function IsFloat( Number: string; Signed : boolean = True ): Boolean;
var
i : integer;
hasDS : Boolean;
hasE : Boolean;
begin
hasDS := false;
hasE := false;
for i := {$IF CompilerVersion >= 24}Low( number ){$ELSE}1{$IFEND} to {$IF CompilerVersion >= 24}High( number ){$ELSE}Length( number ){$IFEND} do
begin
if ( Number[ i ] = '-' ) OR ( Number[ i ] = '+' ) then
begin
if ( ( NOT Signed ) AND ( ( Number[ i ] = '-' ) ) ) OR ( ( i > {$IF CompilerVersion >= 24}Low( number ){$ELSE}1{$IFEND} ) AND ( NOT {$IF CompilerVersion >= 22}CharInSet( Number[ i-1 ],{$ELSE}( Number[ i-1 ] in{$IFEND} [ 'E', 'e' ] ) ) ) then
begin
result := false;
Exit;
end;
end
else if ( {$IF CompilerVersion >= 22}CharInSet( Number[ i ],{$ELSE}( Number[ i ] in{$IFEND} [ 'E', 'e' ] ) ) then
begin
if ( i > {$IF CompilerVersion >= 24}Low( number ){$ELSE}1{$IFEND} ) AND ( NOT hasE ) then
hasE := True
else
begin
result := false;
Exit;
end;
end
else if ( Number[ i ] = {$IF CompilerVersion >= 30}FormatSettings.{$ELSE}SysUtils.{$IFEND}DecimalSeparator ) then
begin
if ( i > {$IF CompilerVersion >= 24}Low( number ){$ELSE}1{$IFEND} ) AND ( NOT hasDS ) then
hasDS := True
else
begin
result := false;
Exit;
end;
end
else if NOT {$IF CompilerVersion >= 22}CharInSet( Number[ i ],{$ELSE}( Number[ i ] in{$IFEND} [ '0'..'9' ] ) then
begin
Result := False;
Exit;
end;
end;
Result := True;
end;
function IsNumIn( Number: string; Min, Max : integer ): Boolean;
begin
result := false;
if IsNum( Number ) then
result := ( StrToInt( Number ) >= min ) and ( StrToInt( Number ) <= Max );
end;
function Isin( Text, Wild: string; caseSensitive : boolean = false ): Boolean;
begin
if ( caseSensitive ) then
Result := StrPos( PChar( Text ), PChar( Wild ) ) <> Nil
else
Result := StrPos( PChar( UpperCase( Text ) ), PChar( UpperCase( Wild ) ) ) <> Nil;
end;
function IsinNum( Text, Wild: string; caseSensitive : boolean = false ): integer;
var
i : integer;
begin
result := 0;
i := 0;
if ( caseSensitive ) then
begin
while ( i <= Length( Text ) ) AND ( PosEx( PChar( Wild ), PChar( Text ), i ) <> 0 ) do
begin
i := PosEx( PChar( Wild ), PChar( Text ), i )+1;
inc( result );
end;
end
else
begin
while ( i <= Length( Text ) ) AND ( PosEx( LowerCase( PChar( Wild ) ), LowerCase( PChar( Text ) ), i ) <> 0 ) do
begin
i := PosEx( LowerCase( PChar( Wild ) ), LowerCase( PChar( Text ) ), i )+1;
inc( result );
end;
end;
end;
function IsinNumPos( Text, Wild: string; N : integer; caseSensitive : boolean = false ): integer;
var
i : integer;
begin
result := 0;
if ( Text = '' ) OR ( Wild = '' ) OR ( N <= 0 ) OR ( N > IsInNum( Text, Wild, caseSensitive ) ) then
Exit;
i := 0;
if ( caseSensitive ) then
begin
while ( result <= Length( Text ) ) AND ( i <> N ) do
begin
result := PosEx( PChar( Wild ), PChar( Text ), result )+1;
inc( i );
end;
end
else
begin
while ( result <= Length( Text ) ) AND ( i <> N ) do
begin
result := PosEx( LowerCase( PChar( Wild ) ), LowerCase( PChar( Text ) ), result )+1;
inc( i );
end;
end;
end;
function IsWm( Text, MatchText: String; caseSensitive : boolean = false ): Boolean;
var
StringPtr: PChar;
PatternPtr: PChar;
StringRes: PChar;
PatternRes: PChar;
tText,
tMatchText : String;
begin
Result := False;
if ( Text = '' ) or ( MatchText = '' ) then
Exit;
if ( caseSensitive ) then
begin
tText := Text;
tMatchText := MatchText;
end
else
begin
tText := UpperCase( Text );
tMatchText := UpperCase( MatchText );
end;
StringPtr := PChar( tText );
PatternPtr := PChar( tMatchText );
StringRes := nil;
PatternRes := nil;
repeat
repeat
case PatternPtr^ of
#0:
begin
Result := StringPtr^ = #0;
if Result or ( StringRes = nil ) or ( PatternRes = nil ) then
Exit;
StringPtr := StringRes;
PatternPtr := PatternRes;
Break;
end;
'*':
begin
Inc( PatternPtr );
PatternRes := PatternPtr;
Break;
end;
'?':
begin
if StringPtr^ = #0 then
Exit;
Inc( StringPtr );
Inc( PatternPtr );
end;
else
begin
if StringPtr^ = #0 then
Exit;
if StringPtr^ <> PatternPtr^ then
begin
if ( StringRes = nil ) or ( PatternRes = nil ) then
Exit;
StringPtr := StringRes;
PatternPtr := PatternRes;
Break;
end
else
begin
Inc( StringPtr );
Inc( PatternPtr );
end;
end;
end;
until False;
repeat
case PatternPtr^ of
#0:
begin
Result := True;
Exit;
end;
'*':
begin
Inc( PatternPtr );
PatternRes := PatternPtr;
end;
'?':
begin
if StringPtr^ = #0 then
Exit;
Inc( StringPtr );
Inc( PatternPtr );
end;
else
begin
repeat
if StringPtr^ = #0 then
Exit;
if StringPtr^ = PatternPtr^ then
Break;
Inc( StringPtr );
until False;
Inc( StringPtr );
StringRes := StringPtr;
Inc( PatternPtr );
Break;
end;
end;
until False;
until False;
end;
{
uses Masks
function IsWM( const S,Wildcard: String ): Boolean;
var
Mask: TMask;
begin
Mask := TMask.Create( Wildcard );
Result := Mask.Matches( S );
Mask.Free;
end;
}
function Remove( Text, RText: string; WildCard : Boolean = false; caseSensitive : boolean = false ):string;
begin
Result := Replace( Text, RText, '', WildCard, caseSensitive );
end;
function Replace( text, mText, rText : string; WildCard : Boolean = false; caseSensitive : boolean = false ): string;
var
start,
endN : integer;
begin
result := text;
if ( text = '' ) or ( mText = '' ) then
Exit;
if WildCard then
begin
if NOT IsWm( Text, '*'+mText+'*', caseSensitive ) then
Exit;
if ( ReturnWildCardPositionInString( text, mText, start, endN, caseSensitive ) ) then
result := copy( text, 0, Max( start-1,0 ) ) + rText + copy( text, endN, length( text )-endN+1 );
end
else
begin
if ( caseSensitive ) then
Result := StringReplace( Text, mText, rText, [ rfReplaceAll ] )
else
Result := StringReplace( Text, mText, rText, [ rfReplaceAll, rfIgnoreCase ] );
end;
end;
function StrNatCompare( const Str1, Str2: string; const CaseSensitive: boolean = false ): integer;
// helper function, check char for digit
function nat_isDigit( const AChar: char ): boolean;
var
charCode: integer;
begin
charCode := ord( AChar );
Result := ( charCode >= 48 ) and ( charCode <= 57 );
end;
// helper function, check for whitespace char ( space, CR, LF,... )
function nat_isSpace( const AChar: char ): boolean;
begin
Result := ( aChar <> #0 ) and ( ord( AChar ) <= 32 );
end;
// helper function, returns uppercase'd char
function nat_ToUpper( const AChar: char ): char;
var
str: string;
begin
str := AChar;
Result := AnsiUpperCase( AChar )[ 1 ];
end;
// helper function to imitate zero-terminated strings
function GetCharAt( const AStr: string; const APos: integer ): char;
begin
if ( APos > 0 ) and ( APos <= length( AStr ) ) then begin
Result := AStr[ APos ];
end else begin
Result := #0;
end;
end;
// number comparison, longest run
function compare_right( const a,b: string ): integer;
var bias: integer;
ia, ib: integer; // counter for string-positions
ca, cb: char; // current char
begin
bias := 0;
ia := 0;
ib := 0;
{
The longest run of digits wins. That aside, the greatest
value wins, but we can't know that it will until we've scanned
both numbers to know that they have the same magnitude, so we
remember it in BIAS
}
{$IF CompilerVersion < 30}result := 0;{$IFEND}
while true do
begin
inc( ia );
inc( ib );
ca := GetCharAt( a,ia );
cb := GetCharAt( b, ib );
if ( not nat_isDigit( ca ) ) and ( not nat_isDigit( cb ) ) then
begin
Result := bias;
break;
end
else if ( not nat_isDigit( ca ) ) then
begin
Result := -1;
break;
end
else if ( not nat_isDigit( cb ) ) then
begin
Result := +1;
break;
end
else if ( ca < cb ) then