-
Notifications
You must be signed in to change notification settings - Fork 58
/
StrUtil.pas
2568 lines (2320 loc) · 65.3 KB
/
StrUtil.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
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:gdeatz@hlmdd.com }
{ }
{ Copyright (c) 1998-2011 Devrace Ltd. }
{ Written by Serge Buzadzhy (buzz@devrace.com) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
{*******************************************************}
{ }
{ This unit based on StrUtils.pas from RX Library }
{ and SoWldStr.pas from SOHOLIB }
{ }
{*******************************************************}
unit StrUtil;
interface
{$I FIBPlus.inc}
uses
SysUtils, Classes {$IFDEF WINDOWS}, Windows {$ENDIF};
type
TPosition = record
X: Longint;
Y: Longint;
end;
{$IFDEF D_XE5}
TFastStringStream = TStringStream;
{$ELSE}
TFastStringStream = class(TStream)
private
FDataString: string;
FPosition: Integer;
FSize: Integer;
FActualLength: Integer;
FElementSize: Integer;
function GetDataString: string;
protected
procedure SetSize(aNewSize: Longint); override;
procedure Grow(Min: Integer = 0);
function CurMemory: Pointer;
public
constructor Create(const AString: string);
destructor Destroy; override;
function Read(var Buffer; Count: Longint): Longint; override;
function ReadString(Count: Longint): string;
function Seek(Offset: Longint; Origin: Word): Longint; override;
procedure PackData;
function Write(const Buffer; Count: Longint): Longint; override;
procedure WriteString(const AString: string);
property DataString: string read GetDataString;
end;
{$ENDIF}
type
TCharSet = set of AnsiChar;
function Position(AX, AY: Integer): TPosition;
// from RX
function MakeStr(C: Char; N: Integer): string;
function StrAsFloat(S: string): double;
function ToClientDateFmt(D: string; caseFmt: Byte): string;
function ExtractWord(Num: Integer; const Str: string; const WordDelims: TCharSet): string;
// function ExtractLastWord(const Str: String;const WordDelims:TCharSet):String;
function WordCount(const S: string; const WordDelims: TCharSet): Integer;
// from SOHOLIB
function AnsiUpperCaseA(const S: AnsiString): AnsiString;
function CompareStrWA(const S1: string; const S2: AnsiString): Integer;
// function AnsiCompareTextWA(const S1,S2: Pointer): Integer;
function AnsiCompareTextAA(const S1, S2: AnsiString): Integer;
function WildStringCompare(FirstString, SecondString: string): Boolean;
function MaskCompare(const AString, Mask: String): Boolean;
function SQLMaskCompare(const AString, Mask: String): Boolean; {$IFDEF D6+} overload; {$ENDIF}
function SQLMaskCompareAW(const AString: AnsiString; const Mask: string): Boolean;
function SQLMaskCompareAA(const AString, Mask: AnsiString): Boolean; {$IFDEF D6+}
function SQLMaskCompare(const AString, Mask: Widestring): Boolean; overload; {$ENDIF}
// function WldIndexOf(ts:TStrings;const Value:AnsiString;CaseSensitive:Boolean):Integer;
// from Me
function TrimCLRF(const S: String): String;
{ procedure CopyChars(const Src:Ansistring; var Dest:Ansistring;
StartInSrc,StartInDest,Count:Integer
); }
function ReplaceStr(const S, Srch, Replace: string): string;
function ReplaceStrInSubstr(const Source: string; const Srch, Replace: string; BeginPos, EndPos: Integer): string;
function ReplaceCIStr(const S, Srch, Replace: string; Ansi: Boolean = True): string;
function ReplaceStrCIInSubstr(const Source: string; const Srch, Replace: string; BeginPos, EndPos: Integer): string;
function FastUpperCase(const S: string): string; {$IFDEF D2007+} inline; {$ENDIF}
procedure DoUpperCase(var S: string; FirstPos: Integer = 1; LastPos: Integer = 0);
{$IFDEF D2009+}
procedure DoAnsiUpperCase(var S: AnsiString); overload;
{$ENDIF}
procedure DoAnsiUpperCase(var S: string); overload;
procedure DoWideUpperCase(var S: Widestring);
procedure DoUtf8Decode(const S: PAnsiChar; StrLen: Integer; var ws: Widestring); overload;
procedure DoUtf8Decode(const S: AnsiString; var ws: Widestring); overload;
procedure DoUtf8Decode(const S: variant; var ws: Widestring); overload;
function EquelStrings(const S, S1: string; CaseSensitive: Boolean): Boolean;
function iifStr(Condition: Boolean; const Str1, Str2: string): string;
function iifVariant(Condition: Boolean; const Var1, Var2: variant): Variant;
function StrOnMask(const StrIn, MaskIn, MaskOut: string): string;
function StrIsInteger(const Str: string): Boolean;
function FormatIdentifierValue(Dialect: Integer; const Value: string): string; {$IFDEF D2007+} inline; {$ENDIF}
function FormatIdentifier(Dialect: Integer; const Value: string): string; {$IFDEF D2007+} inline; {$ENDIF}
function NormalizeName(Dialect: Integer; const Name: string): string; {$IFDEF D2007+} inline; {$ENDIF}
function EasyFormatIdentifier(Dialect: Integer; const Value: string; DoEasy: Boolean): string; {$IFDEF D2007+} inline;
{$ENDIF}
function EquelNames(CI: Boolean; const Value, Value1: String): Boolean; {$IFDEF D2007+} inline; {$ENDIF}
function NeedQuote(const Name: string): Boolean;
function EasyNeedQuote(const Name: string): Boolean;
function PosCh(aCh: Char; const S: string): Integer;
function PosCh1(aCh: Char; const S: string; StartPos: Integer): Integer; overload;
{$IFDEF D2009+}
function PosCh1(aCh: AnsiChar; const S: AnsiString; StartPos: Integer): Integer; overload;
{$ENDIF}
function PosCI(const Substr, Str: string): Integer; {$IFDEF D2007+} inline;
{$ENDIF}
function PosExt(const Substr, Str: string; BegSub, EndSub: TCharSet): Integer; {$IFDEF D2009+} inline; {$ENDIF}
function PosExtCI(const Substr, Str: string; BegSub, EndSub: TCharSet; AnsiUpper: Boolean = True): Integer;
function PosInSubstr(const Substr, Str: string; BeginPos, EndPos: Integer): Integer;
function PosInRight(const Substr, Str: string; BeginPos: Integer): Integer;
function PosInSubstrCI(const SearchStr, Str: string; BeginPos, EndPos: Integer): Integer;
function PosInSubstrExt(const SearchStr: string; Str: string; BeginPos, EndPos: Integer;
BegSub, EndSub: TCharSet): Integer;
function PosInSubstrCIExt(const SearchStr, Str: string; BeginPos, EndPos: Integer; BegSub, EndSub: TCharSet): Integer;
function FirstPos(Search: array of Char; const InString: string): Integer; overload;
{$IFDEF D2009+}
function FirstPos(Search: array of AnsiChar; const InString: AnsiString): Integer; overload;
{$ENDIF}
function LastChar(const Str: string): Char;
function QuotedStr(const S: string): string;
function CutQuote(const S: String): string;
function StringInArray(const S: string; const a: array of string): Boolean;
function IsBlank(const Str: string): Boolean;
function IsBeginPartStr(const PartStr, TargetStr: AnsiString): Boolean;
{$IFDEF D6+} overload; {$ENDIF}
{$IFDEF D2009+}
function IsBeginPartStrWA(const PartStr: string; const TargetStr: AnsiString): Boolean;
{$ENDIF}
function IsBeginPartStrVarA(const PartStr: variant; const TargetStr: AnsiString): Boolean;
{$IFDEF D6+}
function IsBeginPartStr(const PartStr, TargetStr: Widestring): Boolean; overload;
{$ENDIF}
// Ansistring procedures
procedure DoLowerCase(var Str: string); overload;
{$IFDEF D2009+}
procedure DoLowerCase(var Str: AnsiString); overload;
{$ENDIF}
procedure DoTrim(var Str: string);
// procedure DoTrimTo(const Source:Ansistring ;var Dest:Ansistring);
function VarTrimRight(const Str: variant): variant;
procedure DoTrimRight(var Str: string); overload;
procedure DoTrimRight(var Str: Widestring); overload;
{$IFDEF D2009+}
procedure DoTrimRight(var Str: AnsiString); overload;
{$ENDIF}
procedure DoCopy(const Source: string; var Dest: string; Index, Count: Integer); overload;
{$IFDEF D2009+}
procedure DoCopy(const Source: AnsiString; var Dest: AnsiString; Index, Count: Integer); overload;
{$ENDIF}
// function StrTrimmedLen(Str:PChar):Integer;
function FastTrim(const S: string): string;
function FastCopy(const S: String; Index: Integer; Count: Integer): String; overload;
{$IFDEF D2009+}
function FastCopy(const S: AnsiString; Index: Integer; Count: Integer): AnsiString; overload;
{$ENDIF}
// TStringList functions
procedure SLDifference(ASL, BSL: TStringList; ResultSL: TStrings);
function EmptyStrings(SL: TStrings): Boolean;
procedure DeleteEmptyStr(Src: TStrings);
function NonAnsiSortCompareStrings(SL: TStringList; Index1, Index2: Integer): Integer;
function FindInDiapazon(SL: TStringList; const S: String; const StartIndex, EndIndex: Integer; AnsiCompare: Boolean;
var Index: Integer): Boolean;
function NonAnsiIndexOf(SL: TStringList; const S: string): Integer; // Non Ansi
procedure GetNameAndValue(const S: string; var Name, Value: AnsiString);
function StrToDateFmt(const ADate, Fmt: string): TDateTime;
function DateToSQLStr(const ADate: TDateTime): string;
function ValueFromStr(const Str: string): string;
{$IFNDEF D6+}
function WideUpperCase(const S: Widestring): Widestring;
{$ENDIF}
function Q_StrLen(P: PAnsiChar): Cardinal;
function IsOldParamName(const ParamName: string): Boolean;
function IsNewParamName(const ParamName: string): Boolean;
function IsMasParamName(const ParamName: string): Boolean;
function GUIDAsString(const AGUID: TGUID): AnsiString; // fast GUIDToString
procedure GUIDAsStringToPChar(const AGUID: PGUID; Dest: PAnsiChar);
function StringAsGUID(const AStr: AnsiString): TGUID;
function CompareStrAndGuid(GUID: PGUID; const Str: AnsiString): Integer;
function CompareStrAndGuidP(GUID: PGUID; const Str: PAnsiString): Integer;
function IsEqualGUIDs(const guid1, guid2: TGUID): Boolean;
{ Thanks to Anton Tril }
function IsNumericStr(const Str: string): Boolean;
function IsEmptyStr(const Str: string): Boolean;
function CompareStrTrimmed(const Str1, Str2: PChar; Len: Integer): Integer;
{$IFNDEF D2009+}
function CharInSet(C: AnsiChar; const CharSet: TSysCharSet): Boolean;
{$ENDIF}
implementation
uses SysConst
{$IFNDEF D6+}
, StdFuncs
{$ELSE}
, Variants
{$ENDIF}
;
{$IFNDEF D2009+}
function CharInSet(C: AnsiChar; const CharSet: TSysCharSet): Boolean;
begin
Result := C in CharSet;
end;
{$ENDIF}
{$IFNDEF D6+}
type
IntegerArray = array [0 .. $EFFFFFF] of Integer;
PIntegerArray = ^IntegerArray;
{$ENDIF}
function IsNumericStr(const Str: string): Boolean;
var
pStr, pStrEnd: PChar;
begin
if Length(Str) = 0 then
begin
Result := False;
Exit;
end;
pStr := Pointer(Str);
pStrEnd := Pointer(Str);
Inc(pStrEnd, Length(Str));
while (pStr < pStrEnd) and CharInSet(pStr^, ['+', '-']) do
Inc(pStr);
if pStr = pStrEnd then
begin
Result := False;
Exit;
end;
while pStr < pStrEnd do
begin
if CharInSet(pStr^, ['0' .. '9', '.']) then
Inc(pStr)
else
begin
Result := False;
Exit;
end;
end;
Result := True;
end;
function IsEmptyStr(const Str: string): Boolean;
var
pStr, pStrEnd: PChar;
begin
if Length(Str) = 0 then
begin
Result := True;
Exit;
end;
pStr := Pointer(Str);
pStrEnd := Pointer(Str);
Inc(pStrEnd, Length(Str));
while (pStr < pStrEnd) do
begin
if pStr^ > ' ' then
begin
Result := False;
Exit;
end;
Inc(pStr);
end;
Result := True;
end;
function CompareStrTrimmed(const Str1, Str2: PChar; Len: Integer): Integer;
var
i: Integer;
begin
// Str2 must be trimmed
while Str2[Len - 1] < #33 do
Dec(Len);
i := 0;
while i < Len do
begin
if Str1[i] > Str2[i] then
begin
Result := 1;
Exit;
end
else if Str1[i] < Str2[i] then
begin
Result := -1;
Exit;
end
else
Inc(i)
end;
Result := 0;
end;
function CompareStrAndGuid(GUID: PGUID; const Str: AnsiString): Integer;
begin
Result := CompareStr(Str, GUIDAsString(GUID^))
end;
function CompareStrAndGuidP(GUID: PGUID; const Str: PAnsiString): Integer;
begin
Result := CompareStr(Str^, GUIDAsString(GUID^))
end;
function CompareStrWA(const S1: string; const S2: AnsiString): Integer;
begin
// Result :=CompareStr(S1,S2)
if S1 <> S2 then
Result := 1
else
Result := 0
end;
function AnsiCompareTextAA(const S1, S2: AnsiString): Integer;
begin
Result := AnsiCompareText(S1, S2)
end;
function AnsiUpperCaseA(const S: AnsiString): AnsiString;
begin
Result := AnsiUpperCase(S)
end;
function IsEqualGUIDs(const guid1, guid2: TGUID): Boolean;
var
a, b: PIntegerArray;
begin
a := PIntegerArray(@guid1);
b := PIntegerArray(@guid2);
Result := (a^[0] = b^[0]) and (a^[1] = b^[1]) and (a^[2] = b^[2]) and (a^[3] = b^[3]);
end;
type
TGUIDBytes = record
F0, F1, F2, F3, F4, F5, F6, F7, F8, F9, FA, FB, FC, FD, FE, FF: byte;
end;
{$R+}
{$IFNDEF D6+}
resourcestring
SInvalidGUID = '''%s'' is not a valid GUID value';
{$ENDIF}
const
Int2HexHash: Array [0 .. 255] of Word = ($3030, $3130, $3230, $3330, $3430, $3530, $3630, $3730, $3830, $3930, $4130,
$4230, $4330, $4430, $4530, $4630, $3031, $3131, $3231, $3331, $3431, $3531, $3631, $3731, $3831, $3931, $4131,
$4231, $4331, $4431, $4531, $4631, $3032, $3132, $3232, $3332, $3432, $3532, $3632, $3732, $3832, $3932, $4132,
$4232, $4332, $4432, $4532, $4632, $3033, $3133, $3233, $3333, $3433, $3533, $3633, $3733, $3833, $3933, $4133,
$4233, $4333, $4433, $4533, $4633, $3034, $3134, $3234, $3334, $3434, $3534, $3634, $3734, $3834, $3934, $4134,
$4234, $4334, $4434, $4534, $4634, $3035, $3135, $3235, $3335, $3435, $3535, $3635, $3735, $3835, $3935, $4135,
$4235, $4335, $4435, $4535, $4635, $3036, $3136, $3236, $3336, $3436, $3536, $3636, $3736, $3836, $3936, $4136,
$4236, $4336, $4436, $4536, $4636, $3037, $3137, $3237, $3337, $3437, $3537, $3637, $3737, $3837, $3937, $4137,
$4237, $4337, $4437, $4537, $4637, $3038, $3138, $3238, $3338, $3438, $3538, $3638, $3738, $3838, $3938, $4138,
$4238, $4338, $4438, $4538, $4638, $3039, $3139, $3239, $3339, $3439, $3539, $3639, $3739, $3839, $3939, $4139,
$4239, $4339, $4439, $4539, $4639, $3041, $3141, $3241, $3341, $3441, $3541, $3641, $3741, $3841, $3941, $4141,
$4241, $4341, $4441, $4541, $4641, $3042, $3142, $3242, $3342, $3442, $3542, $3642, $3742, $3842, $3942, $4142,
$4242, $4342, $4442, $4542, $4642, $3043, $3143, $3243, $3343, $3443, $3543, $3643, $3743, $3843, $3943, $4143,
$4243, $4343, $4443, $4543, $4643, $3044, $3144, $3244, $3344, $3444, $3544, $3644, $3744, $3844, $3944, $4144,
$4244, $4344, $4444, $4544, $4644, $3045, $3145, $3245, $3345, $3445, $3545, $3645, $3745, $3845, $3945, $4145,
$4245, $4345, $4445, $4545, $4645, $3046, $3146, $3246, $3346, $3446, $3546, $3646, $3746, $3846, $3946, $4146,
$4246, $4346, $4446, $4546, $4646);
function StringAsGUID(const AStr: AnsiString): TGUID;
const
Hex2IntHash: Array [0 .. 70] of byte = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0,
10, 11, 12, 13, 14, 15);
begin
{ Thanks to Anton Tril }
if (Length(AStr) <> 38) or (AStr[1] <> '{') or (AStr[38] <> '}') or (AStr[10] <> '-') or (AStr[15] <> '-') or
(AStr[20] <> '-') or (AStr[25] <> '-') then
begin
raise EConvertError.CreateResFmt(PResStringRec(@SInvalidGUID), [AStr]);
end;
try
TGUIDBytes(Result).F3 := Hex2IntHash[byte(AStr[2])] shl 4 or Hex2IntHash[byte(AStr[3])];
TGUIDBytes(Result).F2 := Hex2IntHash[byte(AStr[4])] shl 4 or Hex2IntHash[byte(AStr[5])];
TGUIDBytes(Result).F1 := Hex2IntHash[byte(AStr[6])] shl 4 or Hex2IntHash[byte(AStr[7])];
TGUIDBytes(Result).F0 := Hex2IntHash[byte(AStr[8])] shl 4 or Hex2IntHash[byte(AStr[9])];
// -
TGUIDBytes(Result).F5 := Hex2IntHash[byte(AStr[11])] shl 4 or Hex2IntHash[byte(AStr[12])];
TGUIDBytes(Result).F4 := Hex2IntHash[byte(AStr[13])] shl 4 or Hex2IntHash[byte(AStr[14])];
// -
TGUIDBytes(Result).F7 := Hex2IntHash[byte(AStr[16])] shl 4 or Hex2IntHash[byte(AStr[17])];
TGUIDBytes(Result).F6 := Hex2IntHash[byte(AStr[18])] shl 4 or Hex2IntHash[byte(AStr[19])];
// -
TGUIDBytes(Result).F8 := Hex2IntHash[byte(AStr[21])] shl 4 or Hex2IntHash[byte(AStr[22])];
TGUIDBytes(Result).F9 := Hex2IntHash[byte(AStr[23])] shl 4 or Hex2IntHash[byte(AStr[24])];
// -
TGUIDBytes(Result).FA := Hex2IntHash[byte(AStr[26])] shl 4 or Hex2IntHash[byte(AStr[27])];
TGUIDBytes(Result).FB := Hex2IntHash[byte(AStr[28])] shl 4 or Hex2IntHash[byte(AStr[29])];
TGUIDBytes(Result).FC := Hex2IntHash[byte(AStr[30])] shl 4 or Hex2IntHash[byte(AStr[31])];
TGUIDBytes(Result).FD := Hex2IntHash[byte(AStr[32])] shl 4 or Hex2IntHash[byte(AStr[33])];
TGUIDBytes(Result).FE := Hex2IntHash[byte(AStr[34])] shl 4 or Hex2IntHash[byte(AStr[35])];
TGUIDBytes(Result).FF := Hex2IntHash[byte(AStr[36])] shl 4 or Hex2IntHash[byte(AStr[37])];
except
raise EConvertError.CreateResFmt(PResStringRec(@SInvalidGUID), [AStr]);
end;
end;
{$R-}
procedure GUIDAsStringToPChar(const AGUID: PGUID; Dest: PAnsiChar);
var
P: Cardinal;
begin
P := Cardinal(Dest);
PByte(P)^ := Ord('{');
Inc(P);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).F3];
Inc(P, 2);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).F2];
Inc(P, 2);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).F1];
Inc(P, 2);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).F0];
Inc(P, 2);
PByte(P)^ := Ord('-');
Inc(P);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).F5];
Inc(P, 2);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).F4];
Inc(P, 2);
PByte(P)^ := Ord('-');
Inc(P);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).F7];
Inc(P, 2);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).F6];
Inc(P, 2);
PByte(P)^ := Ord('-');
Inc(P);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).F8];
Inc(P, 2);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).F9];
Inc(P, 2);
PByte(P)^ := Ord('-');
Inc(P);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).FA];
Inc(P, 2);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).FB];
Inc(P, 2);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).FC];
Inc(P, 2);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).FD];
Inc(P, 2);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).FE];
Inc(P, 2);
PWord(P)^ := Int2HexHash[TGUIDBytes(AGUID^).FF];
Inc(P, 2);
PByte(P)^ := Ord('}');
end;
function GUIDAsString(const AGUID: TGUID): AnsiString;
begin
SetLength(Result, 38);
GUIDAsStringToPChar(@AGUID, PAnsiChar(Result))
end;
function IsOldParamName(const ParamName: string): Boolean;
begin
Result := (Length(ParamName) > 4) and CharInSet(ParamName[1], ['O', 'o']) and (ParamName[4] = '_') and
CharInSet(ParamName[2], ['L', 'l']) and CharInSet(ParamName[3], ['D', 'd'])
end;
function IsNewParamName(const ParamName: string): Boolean;
begin
Result := (Length(ParamName) > 4) and CharInSet(ParamName[1], ['N', 'n']) and (ParamName[4] = '_') and
CharInSet(ParamName[2], ['E', 'e']) and CharInSet(ParamName[3], ['W', 'w'])
end;
function IsMasParamName(const ParamName: string): Boolean;
begin
Result := (Length(ParamName) > 4) and CharInSet(ParamName[1], ['M', 'm']) and (ParamName[4] = '_') and
CharInSet(ParamName[2], ['A', 'a']) and CharInSet(ParamName[3], ['S', 's'])
end;
function StrToDateFmt(const ADate, Fmt: string): TDateTime;
var
OldShortDateFormat: string;
begin
{$IFDEF D_XE3}with FormatSettings do {$ENDIF}
begin
OldShortDateFormat := {$IFDEF D_XE3}FormatSettings.{$ENDIF}ShortDateFormat;
try
ShortDateFormat := Fmt;
Result := StrToDateTime(ADate);
finally
ShortDateFormat := OldShortDateFormat;
end
end;
end;
function DateToSQLStr(const ADate: TDateTime): string;
var
Year, Month, Day: Word;
begin
DecodeDate(ADate, Year, Month, Day);
Result := IntToStr(Day) + '.' + IntToStr(Month) + '.' + IntToStr(Year)
end;
function ValueFromStr(const Str: string): string;
var
P: Integer;
begin
P := PosCh('=', Str);
if P = 0 then
Result := ''
else
Result := Copy(Str, P + 1, MaxInt)
end;
{$IFDEF D6+}
function IsBeginPartStr(const PartStr, TargetStr: Widestring): Boolean;
var
i, L: Integer;
begin
L := Length(PartStr);
if L = 0 then
Result := True
else if L > Length(TargetStr) then
Result := False
else
begin
for i := 1 to L do
if PartStr[i] <> TargetStr[i] then
begin
Result := False;
Exit;
end;
Result := True;
end;
end;
{$ENDIF}
{$IFDEF D2009+}
function IsBeginPartStrWA(const PartStr: string; const TargetStr: AnsiString): Boolean;
var
i, L: Integer;
wTargStr: Widestring;
begin
L := Length(PartStr);
if L = 0 then
Result := True
else if L > Length(TargetStr) then
Result := False
else
begin
wTargStr := TargetStr;
for i := 1 to L do
if PartStr[i] <> wTargStr[i] then
begin
Result := False;
Exit;
end;
Result := True;
end;
end;
{$ENDIF}
function IsBeginPartStrVarA(const PartStr: variant; const TargetStr: AnsiString): Boolean;
begin
{$IFDEF D2009+}
Result := IsBeginPartStrWA(PartStr, TargetStr)
{$ELSE}
Result := IsBeginPartStr(PartStr, TargetStr)
{$ENDIF}
end;
function IsBeginPartStr(const PartStr, TargetStr: AnsiString): Boolean;
var
i, L: Integer;
begin
L := Length(PartStr);
if L = 0 then
Result := True
else if L > Length(TargetStr) then
Result := False
else
begin
for i := 1 to L do
if PartStr[i] <> TargetStr[i] then
begin
Result := False;
Exit;
end;
Result := True;
end;
end;
function IsBlank(const Str: string): Boolean;
var
L: Integer;
begin
L := Length(Str);
while (L > 0) and (Str[L] <= ' ') do
Dec(L);
Result := L = 0;
end;
{$IFDEF D2009+}
procedure DoLowerCase(var Str: AnsiString);
var
i: Integer;
begin
for i := Length(Str) downto 1 do
if (Str[i] >= 'A') and (Str[i] <= 'Z') then
Inc(Str[i], 32)
end;
{$ENDIF}
procedure DoLowerCase(var Str: string);
var
i: Integer;
begin
for i := Length(Str) downto 1 do
if (Str[i] >= 'A') and (Str[i] <= 'Z') then
Inc(Str[i], 32)
end;
procedure TrimPositions(const Str: string; var Left, Right: Integer);
begin
Right := Length(Str);
if Right = 0 then
begin
Left := 0;
Exit;
end;
Left := 1;
while (Right > 0) and (Str[Right] <= ' ') do
Dec(Right);
while (Left <= Right) and (Str[Left] <= ' ') do
Inc(Left);
end;
procedure DoTrim(var Str: string);
var
Left, Right: Integer;
begin
TrimPositions(Str, Left, Right);
if Right < Length(Str) then
SetLength(Str, Right);
if Left > 1 then
Delete(Str, 1, Left - 1);
end;
function VarTrimRight(const Str: variant): Variant;
var
i: Integer;
ws: Widestring;
S: string;
begin
case VarType(Str) of
varOleStr:
begin
ws := Str;
i := Length(ws);
if (i > 0) and (ws[i] <> ' ') then
begin
Result := ws;
Exit;
end;
while (i > 0) and (ws[i] = ' ') do
Dec(i);
Result := Copy(ws, 1, i);
end;
else
begin
S := Str;
i := Length(S);
if (i > 0) and (S[i] <> ' ') then
begin
Result := S;
Exit;
end;
while (i > 0) and (S[i] = ' ') do
Dec(i);
Result := Copy(S, 1, i);
end
end;
end;
procedure DoTrimRight(var Str: string);
var
i: Integer;
begin
i := Length(Str);
while (i > 0) and (Str[i] = ' ') do
Dec(i);
if i < Length(Str) then
SetLength(Str, i)
end;
procedure DoTrimRight(var Str: Widestring);
var
i: Integer;
begin
i := Length(Str);
while (i > 0) and (Str[i] = ' ') do
Dec(i);
if i < Length(Str) then
SetLength(Str, i)
end;
{$IFDEF D2009+}
procedure DoTrimRight(var Str: AnsiString);
var
i: Integer;
P, p1: PAnsiChar;
begin
{ I := Length(Str);
while (I > 0) and (Str[I] <= ' ') do Dec(I);
if I< Length(Str) then SetLength(Str,I) }
i := Length(Str);
if i > 0 then
begin
P := PAnsiChar(Pointer(Str));
p1 := P;
Inc(p1, i - 1);
if p1^ = ' ' then
begin
repeat
Dec(p1);
if p1^ <> ' ' then
begin
SetLength(Str, p1 - P + 1);
Exit
end;
until (p1 = P);
Str := '';
end
end
end;
{$ENDIF}
{$IFDEF D2009+}
procedure DoCopy(const Source: AnsiString; var Dest: AnsiString; Index, Count: Integer);
var
L: Integer;
begin
L := Length(Source) - Index + 1;
if L > Count then
L := Count;
if L <= 0 then
Dest := ''
else
begin
if Length(Dest) <> L then
SetLength(Dest, L);
Move(Source[Index], Dest[1], L * SizeOf(AnsiChar));
end;
end;
{$ENDIF}
procedure DoCopy(const Source: string; var Dest: string; Index, Count: Integer);
var
L: Integer;
begin
L := Length(Source) - Index + 1;
if L > Count then
L := Count;
if L <= 0 then
Dest := ''
else
begin
if Length(Dest) <> L then
SetLength(Dest, L);
Move(Source[Index], Dest[1], L * SizeOf(Char));
end;
end;
function FastTrim(const S: string): string;
var
i, L: Integer;
begin
L := Length(S);
i := 1;
while (i <= L) and (S[i] <= ' ') do
Inc(i);
if i > L then
Result := ''
else
begin
while S[L] <= ' ' do
Dec(L);
if (i > 1) or (L < Length(S)) then
begin
SetLength(Result, L - i + 1);
Move(S[i], Result[1], (L - i + 1) * SizeOf(Char));
end
else
Result := S
end;
end;
function FastCopy(const S: string; Index: Integer; Count: Integer): string;
var
L: Integer;
begin
L := Length(S);
if (Index > L) or (Count <= 0) then
Result := ''
else
begin
if (Count - L + Index) > 0 then
Count := L - Index + 1;
SetLength(Result, Count);
Move(S[Index], Result[1], Count * SizeOf(Char));
end;
end;
{$IFDEF D2009+}
function FastCopy(const S: AnsiString; Index: Integer; Count: Integer): AnsiString;
var
L: Integer;
begin
L := Length(S);
if (Index > L) or (Count <= 0) then
Result := ''
else
begin
if (Count - L + Index) > 0 then
Count := L - Index + 1;
SetLength(Result, Count);
Move(S[Index], Result[1], Count * SizeOf(AnsiChar));
end;
end;
{$ENDIF}
function StringInArray(const S: string; const a: array of String): Boolean;
var
i: Integer;
begin
Result := False;
for i := Low(a) to High(a) do
begin
Result := EquelNames(False, S, a[i]);
if Result then
Exit;
end;
end;
function FormatIdentifierValue(Dialect: Integer; const Value: string): string;
begin
if Dialect = 1 then
Result := FastUpperCase(FastTrim(Value))
else
Result := Value;
end;
function EquelNames(CI: Boolean; const Value, Value1: string): Boolean;
begin
if Length(Value) <> Length(Value1) then
Result := False
else
begin
if CI then
Result := AnsiCompareText(Value, Value1) = 0
else
Result := Value = Value1
end;
end;
function EasyFormatIdentifier(Dialect: Integer; const Value: string; DoEasy: Boolean): string;
var
b: Boolean;
begin
if Dialect = 1 then
Result := UpperCase(FastTrim(Value))
else
begin
if DoEasy then
b := EasyNeedQuote(Value)
else
b := NeedQuote(Value);
if b then
if (Length(Value) > 0) and (Value[1] <> '"') then
Result := '"' + Value + '"'
else
Result := Value
else if (Length(Value) > 0) and (Value[1] <> '"') then
Result := UpperCase(Value)
else
Result := Value
end;
end;
function FormatIdentifier(Dialect: Integer; const Value: string): string;
begin
if Dialect = 1 then
Result := UpperCase(Trim(Value))
else if NeedQuote(Value) then
Result := '"' + Value + '"'
else
Result := Value
end;
function NormalizeName(Dialect: Integer; const Name: string): string;
begin
if Dialect < 3 then
Result := UpperCase(Name)
else if (Length(Name) > 0) and (Name[1] = '"') then
Result := Name
else
Result := UpperCase(Name)
end;
{$IFDEF CPUX86}
function Q_StrScan(const S: AnsiString; Ch: AnsiChar; StartPos: Integer): Integer;
asm
TEST EAX,EAX
JE @@qt
PUSH EDI
MOV EDI,EAX
LEA EAX,[ECX-1]
MOV ECX,[EDI-4]
SUB ECX,EAX
JLE @@m1
PUSH EDI
ADD EDI,EAX
MOV EAX,EDX
POP EDX
REPNE SCASB
JNE @@m1
MOV EAX,EDI
SUB EAX,EDX
POP EDI