Skip to content

Commit

Permalink
introduced PosExString()
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Bouchez committed Dec 2, 2019
1 parent 160c0e7 commit 0e01b30
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
3 changes: 2 additions & 1 deletion SQLite3/mORMot.pas
Expand Up @@ -59153,7 +59153,8 @@ procedure CallMethod(var Args: TCallMethodArgs); assembler;
end; end;
{$endif CPUX64} {$endif CPUX64}


{$ifdef ISDELPHI7ANDUP}{$WARN COMPARING_SIGNED_UNSIGNED OFF}{$endif} // W1023 FPC_STACKALIGNMENT {$ifdef ISDELPHI7ANDUP}{$WARN COMPARING_SIGNED_UNSIGNED OFF}{$endif}
// disable W1023 FPC_STACKALIGNMENT (not possible on Delphi 6)


{$ifdef CPUX86} {$ifdef CPUX86}


Expand Down
85 changes: 85 additions & 0 deletions SynCommons.pas
Expand Up @@ -3025,6 +3025,10 @@ function PosEx(const SubStr, S: RawUTF8; Offset: PtrUInt=1): integer;


{$endif PUREPASCAL} {$endif PUREPASCAL}


/// our own PosEx() function dedicated to VCL string process
// - Delphi XE or older don't support Pos() with an Offset
var PosExString: function(const SubStr, S: string; Offset: PtrUInt=1): PtrInt;

/// optimized version of PosEx() with search text as one AnsiChar /// optimized version of PosEx() with search text as one AnsiChar
function PosExChar(Chr: AnsiChar; const Str: RawUTF8): PtrInt; function PosExChar(Chr: AnsiChar; const Str: RawUTF8): PtrInt;
{$ifdef HASINLINE}inline;{$endif} {$ifdef HASINLINE}inline;{$endif}
Expand Down Expand Up @@ -22760,6 +22764,79 @@ function PosIU(substr: PUTF8Char; const str: RawUTF8): Integer;
result := 0; result := 0;
end; end;


// same as PosExPas() but using PChar internally for proper string process
function PosExStringPas(pSub, p: PChar; Offset: PtrUInt): PtrInt;
var len, lenSub: PtrInt;
ch: char;
pStart, pStop: PChar;
label Loop2, Loop6, TestT, Test0, Test1, Test2, Test3, Test4,
AfterTestT, AfterTest0, Ret, Exit;
begin
result := 0;
if (p=nil) or (pSub=nil) or (Offset<1) then
goto Exit;
{$ifdef FPC}
len := _LStrLenP(p);
lenSub := _LStrLenP(pSub)-1;
{$else}
len := PInteger(p-4)^;
lenSub := PInteger(pSub-4)^-1;
{$endif FPC}
if (len<lenSub+PtrInt(Offset)) or (lenSub<0) then
goto Exit;
pStop := p+len;
inc(p,lenSub);
inc(pSub,lenSub);
pStart := p;
inc(p,Offset+3);
ch := pSub[0];
lenSub := -lenSub;
if p<pStop then goto Loop6;
dec(p,4);
goto Loop2;
Loop6: // check 6 chars per loop iteration
if ch=p[-4] then goto Test4;
if ch=p[-3] then goto Test3;
if ch=p[-2] then goto Test2;
if ch=p[-1] then goto Test1;
Loop2:
if ch=p[0] then goto Test0;
AfterTest0:
if ch=p[1] then goto TestT;
AfterTestT:
inc(p,6);
if p<pStop then goto Loop6;
dec(p,4);
if p>=pStop then goto Exit;
goto Loop2;
Test4: dec(p,2);
Test2: dec(p,2);
goto Test0;
Test3: dec(p,2);
Test1: dec(p,2);
TestT: len := lenSub;
if lenSub<>0 then
repeat
if (psub[len]<>p[len+1]) or (psub[len+1]<>p[len+2]) then
goto AfterTestT;
inc(len,2);
until len>=0;
inc(p,2);
if p<=pStop then goto Ret;
goto Exit;
Test0: len := lenSub;
if lenSub<>0 then
repeat
if (psub[len]<>p[len]) or (psub[len+1]<>p[len+1]) then
goto AfterTest0;
inc(len,2);
until len>=0;
inc(p);
Ret:
result := p-pStart;
Exit:
end;

procedure AppendCharToRawUTF8(var Text: RawUTF8; Ch: AnsiChar); procedure AppendCharToRawUTF8(var Text: RawUTF8; Ch: AnsiChar);
var L: PtrInt; var L: PtrInt;
begin begin
Expand Down Expand Up @@ -63234,6 +63311,13 @@ initialization
{$ifndef HASINLINE} {$ifndef HASINLINE}
PosEx := @PosExPas; PosEx := @PosExPas;
{$endif} {$endif}
PosExString := @PosExStringPas; // fast pure pascal process
{$else}
{$ifdef UNICODE}
PosExString := @PosExStringPas; // fast PWideChar process
{$else}
PosExString := @PosEx; // use optimized PAnsiChar asm
{$endif}
{$endif} {$endif}
crc32c := @crc32cfast; // now to circumvent Internal Error C11715 for Delphi 5 crc32c := @crc32cfast; // now to circumvent Internal Error C11715 for Delphi 5
crc32cBy4 := @crc32cBy4fast; crc32cBy4 := @crc32cBy4fast;
Expand Down Expand Up @@ -63280,6 +63364,7 @@ initialization
Assert(SizeOf(TFileTime)=SizeOf(Int64)); // see e.g. FileTimeToInt64 Assert(SizeOf(TFileTime)=SizeOf(Int64)); // see e.g. FileTimeToInt64
{$endif} {$endif}
{$endif} {$endif}
PosExstring('','');


finalization finalization
GarbageCollectorFree; GarbageCollectorFree;
Expand Down
2 changes: 1 addition & 1 deletion SynopseCommit.inc
@@ -1 +1 @@
'1.18.5484' '1.18.5485'

0 comments on commit 0e01b30

Please sign in to comment.