2011-02-10 49 views
5
var 
    FileBuff: TBytes; 
    Pattern: TBytes; 
begin 
    FileBuff := filetobytes(filename); 
    Result := CompareMem(@Pattern[0], @FileBuff[0], Length(Pattern)); 
end; 

有什麼功能,如是否有任何「Pos」函數來查找字節?

Result := Pos(@Pattern[0], @FileBuff[0]); 
+3

pos是二進制安全的,ANSIChar類型的數組餵養它,瞧,工程 – 2011-02-10 16:25:28

+0

您可能也有興趣在一個StringReplace功能字節:http://stackoverflow.com/questions/3106139/binary-version-of-stringreplace – 2011-02-10 16:52:12

回答

7

我覺得這個做的:

function BytePos(const Pattern: TBytes; const Buffer: PByte; const BufLen: cardinal): PByte; 
var 
    PatternLength: cardinal; 
    i: cardinal; 
    j: cardinal; 
    OK: boolean; 
begin 
    result := nil; 
    PatternLength := length(Pattern); 
    if PatternLength > BufLen then Exit; 
    if PatternLength = 0 then Exit(Buffer); 
    for i := 0 to BufLen - PatternLength do 
    if PByte(Buffer + i)^ = Pattern[0] then 
    begin 
     OK := true; 
     for j := 1 to PatternLength - 1 do 
     if PByte(Buffer + i + j)^ <> Pattern[j] then 
     begin 
      OK := false; 
      break 
     end; 
     if OK then 
     Exit(Buffer + i); 
    end; 
end; 
0

寫你自己的。當只查找一個字節時,不能進行優化,所以你會發現任何實現基本上都會做同樣的事情。

寫在瀏覽器中:

function BytePos(Pattern:Byte; Buffer:PByte; BufferSize:Integer): Integer; 
var i:Integer; 
begin 
    for i:=0 to BufferSize-1 do 
    if Buffer[i] = Pattern then 
    begin 
     Result := i; 
     Exit; 
    end; 
    Result := -1; 
end; 
+1

我覺得@user想多字節模式的能力 – 2011-02-10 16:25:40

相關問題