Просмотр исходного кода

UPD: Using very fast Boyer–Moore search algorithm in Viewer when possible

Alexander Koblov 11 лет назад
Родитель
Сommit
8bd1fd1b42
2 измененных файлов с 22 добавлено и 4 удалено
  1. 7 0
      components/viewer/viewercontrol.pas
  2. 15 4
      src/fviewer.pas

+ 7 - 0
components/viewer/viewercontrol.pas

@@ -113,6 +113,8 @@ type
                      veUtf32le,  // = ucs4le
                      veUtf32be); // = ucs4be
 
+  TViewerEncodings = set of TViewerEncoding;
+
 const
   ViewerEncodingsNames: array [TViewerEncoding] of string =
                    ('Auto-detect',
@@ -147,6 +149,11 @@ const
                     'UTF-32LE',
                     'UTF-32BE');
 
+const
+  ViewerEncodingMultiByte: TViewerEncodings = [
+    veUtf8, veUtf8bom, veUcs2le, veUcs2be,
+    veUtf16le, veUtf16be, veUtf32le, veUtf32be];
+
 type
 
   { TViewerControl }

+ 15 - 4
src/fviewer.pas

@@ -313,7 +313,7 @@ implementation
 uses
   FileUtil, IntfGraphics, Math, uLng, uShowMsg, uGlobs, LCLType, LConvEncoding,
   DCClassesUtf8, uFindMmap, DCStrUtils, uDCUtils, LCLIntf, uDebug, uHotkeyManager,
-  uConvEncoding, DCBasicTypes, DCOSUtils, uOSUtils;
+  uConvEncoding, DCBasicTypes, DCOSUtils, uOSUtils, uFindByrMr;
 
 const
   HotkeysCategory = 'Viewer';
@@ -1996,6 +1996,7 @@ var
   sSearchTextU: UTF8String;
   sSearchTextA: AnsiString;
   iSearchParameter: Integer;
+  RecodeTable: TRecodeTable;
 begin
   // in first use create dialog
   if not Assigned(FFindDialog) then
@@ -2055,8 +2056,8 @@ begin
       end;
 
       sSearchTextA:= ViewerControl.ConvertFromUTF8(sSearchTextU);
-      // Using fast search algorithm if ASCII or case insensitive
-      if FFindDialog.cbCaseSens.Checked or TextIsASCII(sSearchTextA) then
+      // Using standard search algorithm if case insensitive and multibyte
+      if FFindDialog.cbCaseSens.Checked and (ViewerControl.Encoding in ViewerEncodingMultiByte) then
       begin
         PAnsiAddr := PosMem(ViewerControl.GetDataAdr, ViewerControl.FileSize,
                             FLastSearchPos, sSearchTextA,
@@ -2064,12 +2065,22 @@ begin
         bTextFound := (PAnsiAddr <> Pointer(-1));
         if bTextFound then FLastSearchPos := PAnsiAddr - ViewerControl.GetDataAdr;
       end
-      else begin
+      // Using very slow search algorithm
+      else if (ViewerControl.Encoding in ViewerEncodingMultiByte) then
+      begin
         PAdr := ViewerControl.FindUtf8Text(FLastSearchPos, sSearchTextU,
                                            FFindDialog.cbCaseSens.Checked,
                                            bSearchBackwards);
         bTextFound := (PAdr <> PtrInt(-1));
         if bTextFound then FLastSearchPos := PAdr;
+      end
+      // Using very fast Boyer–Moore search algorithm
+      else begin
+        RecodeTable:= InitRecodeTable(ViewerControl.EncodingName, FFindDialog.cbCaseSens.Checked);
+        PAdr := PosMemBoyerMur(ViewerControl.GetDataAdr + FLastSearchPos,
+                               ViewerControl.FileSize, sSearchTextA, RecodeTable);
+        bTextFound := (PAdr <> PtrInt(-1));
+        if bTextFound then FLastSearchPos := PAdr;
       end;
 
       if bTextFound then