Explorar o código

FIX: Using fast search algorithm if ASCII or case insensitive

Alexander Koblov %!s(int64=11) %!d(string=hai) anos
pai
achega
9671d0fe0a
Modificáronse 2 ficheiros con 36 adicións e 9 borrados
  1. 23 9
      src/fviewer.pas
  2. 13 0
      src/uconvencoding.pas

+ 23 - 9
src/fviewer.pas

@@ -1991,9 +1991,11 @@ end;
 procedure TfrmViewer.DoSearch(bQuickSearch: Boolean; bSearchBackwards: Boolean);
 var
   PAdr: PtrInt;
-  iSizeData, charLen: Integer;
+  PAnsiAddr: PByte;
+  bTextFound: Boolean;
   sSearchTextU: UTF8String;
   sSearchTextA: AnsiString;
+  iSearchParameter: Integer;
 begin
   // in first use create dialog
   if not Assigned(FFindDialog) then
@@ -2030,10 +2032,9 @@ begin
 
   if bPlugin then
     begin
-      iSizeData:= 0;
-      if FFindDialog.cbCaseSens.Checked then
-        iSizeData:= lcs_matchcase;
-      WlxPlugins.GetWLxModule(ActivePlugin).CallListSearchText(sSearchTextU, iSizeData);
+      iSearchParameter:= 0;
+      if FFindDialog.cbCaseSens.Checked then iSearchParameter:= lcs_matchcase;
+      WlxPlugins.GetWLxModule(ActivePlugin).CallListSearchText(sSearchTextU, iSearchParameter);
     end
   else
     begin
@@ -2054,12 +2055,25 @@ begin
       end;
 
       sSearchTextA:= ViewerControl.ConvertFromUTF8(sSearchTextU);
-      PAdr := ViewerControl.FindUtf8Text(FLastSearchPos, sSearchTextU,
-                          FFindDialog.cbCaseSens.Checked, bSearchBackwards);
+      // Using fast search algorithm if ASCII or case insensitive
+      if FFindDialog.cbCaseSens.Checked or TextIsASCII(sSearchTextA) then
+      begin
+        PAnsiAddr := PosMem(ViewerControl.GetDataAdr, ViewerControl.FileSize,
+                            FLastSearchPos, sSearchTextA,
+                            FFindDialog.cbCaseSens.Checked, bSearchBackwards);
+        bTextFound := (PAnsiAddr <> Pointer(-1));
+        if bTextFound then FLastSearchPos := PAnsiAddr - ViewerControl.GetDataAdr;
+      end
+      else begin
+        PAdr := ViewerControl.FindUtf8Text(FLastSearchPos, sSearchTextU,
+                                           FFindDialog.cbCaseSens.Checked,
+                                           bSearchBackwards);
+        bTextFound := (PAdr <> PtrInt(-1));
+        if bTextFound then FLastSearchPos := PAdr;
+      end;
 
-      if (PAdr <> PtrInt(-1)) then
+      if bTextFound then
         begin
-          FLastSearchPos := PAdr;
           // Text found, show it in ViewerControl if not visible
           ViewerControl.MakeVisible(FLastSearchPos);
           // Select found text.

+ 13 - 0
src/uconvencoding.pas

@@ -26,6 +26,7 @@ unit uConvEncoding;
 
 interface
 
+function TextIsASCII(const S: String): Boolean;
 function DetectEncoding(const s: string): string;
 
 implementation
@@ -427,6 +428,18 @@ begin
   Result:= MyDetectCodePageType(s);
 end;
 
+function TextIsASCII(const S: String): Boolean; inline;
+var
+  I: Integer;
+begin
+  for I:= 1 to Length(S) do
+  begin
+    if Ord(S[I]) > 127 then
+      Exit(False);
+  end;
+  Result:= True;
+end;
+
 initialization
   InitStatistic;
   GetLanguageIDs(Lang, FallbackLang);