Changeset 186


Ignore:
Timestamp:
Jul 11, 2018, 3:34:11 PM (6 years ago)
Author:
chronos
Message:
  • Modified: Faster substring searching with PosFromIndex function without unnecessary string copying.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormCheck.pas

    r185 r186  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    9   ExtCtrls, ComCtrls, UAcronym, URegistry, Registry;
     9  ExtCtrls, ComCtrls, UAcronym, URegistry, Registry, UCommon;
    1010
    1111type
     
    4949    function SearchLine(Lines: TStrings; Text: string; Start: Integer = 0): Integer;
    5050    function SearchLineReverse(Lines: TStrings; Text: string; Start: Integer = -1): Integer;
    51     function PosFromIndex(SubStr: string; Text: string; StartIndex: Integer): Integer;
    52     function PosFromIndexReverse(SubStr: string; Text: string; StartIndex: Integer): Integer;
    5351    procedure FindInSummary;
    5452    procedure FindInContent;
     
    173171    else Result := Start;
    174172  while (Result >= 0) and (Pos(Text, Lines[Result]) = 0) do Dec(Result);
    175 end;
    176 
    177 function TFormCheck.PosFromIndex(SubStr: string; Text: string;
    178   StartIndex: Integer): Integer;
    179 begin
    180   Delete(Text, 1, StartIndex);
    181   Result := Pos(SubStr, Text);
    182   if Result <> 0 then Result := Result + StartIndex;
    183 end;
    184 
    185 function TFormCheck.PosFromIndexReverse(SubStr: string; Text: string;
    186   StartIndex: Integer): Integer;
    187 begin
    188   Delete(Text, StartIndex + 1, Length(Text));
    189   Result := LastDelimiter(SubStr, Text);
    190173end;
    191174
  • trunk/Packages/Common/UCommon.pas

    r181 r186  
    7575  FilterMethod: TFilterMethodMethod);
    7676function GetStringPart(var Text: string; Separator: string): string;
     77function PosFromIndex(SubStr: string; Text: string;
     78  StartIndex: Integer): Integer;
     79function PosFromIndexReverse(SubStr: string; Text: string;
     80  StartIndex: Integer): Integer;
    7781
    7882
     
    559563end;
    560564
    561 
     565function PosFromIndex(SubStr: string; Text: string;
     566  StartIndex: Integer): Integer;
     567var
     568  I, MaxLen: SizeInt;
     569  Ptr: PAnsiChar;
     570begin
     571  Result := 0;
     572  if (StartIndex < 1) or (StartIndex > Length(Text) - Length(SubStr)) then Exit;
     573  if Length(SubStr) > 0 then begin
     574    MaxLen := Length(Text) - Length(SubStr) + 1;
     575    I := StartIndex;
     576    Ptr := @Text[StartIndex];
     577    while (I <= MaxLen) do begin
     578      if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
     579        Result := I;
     580        Exit;
     581      end;
     582      Inc(I);
     583      Inc(Ptr);
     584    end;
     585  end;
     586end;
     587
     588function PosFromIndexReverse(SubStr: string; Text: string;
     589  StartIndex: Integer): Integer;
     590var
     591  I: SizeInt;
     592  Ptr: PAnsiChar;
     593begin
     594  Result := 0;
     595  if (StartIndex < 1) or (StartIndex > Length(Text)) then Exit;
     596  if Length(SubStr) > 0 then begin
     597    I := StartIndex;
     598    Ptr := @Text[StartIndex];
     599    while (I > 0) do begin
     600      if (SubStr[1] = Ptr^) and (CompareByte(Substr[1], Ptr^, Length(SubStr)) = 0) then begin
     601        Result := I;
     602        Exit;
     603      end;
     604      Dec(I);
     605      Dec(Ptr);
     606    end;
     607  end;
     608end;
    562609
    563610initialization
Note: See TracChangeset for help on using the changeset viewer.