2011-02-10 4 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の出来上がりの配列とそれを養う、バイナリセーフであるあなたはまた、に興味があるかもしれない –

+0

に動作し、ブラウザで書かれ

バイトのためのStringReplace関数:http://stackoverflow.com/questions/3106139/binary-version-of-stringreplace –

答えて

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

私はpbyteの代わりに仮引数を使います。同じ意味ですが、より多くの種類と互換性があります。 –

+0

@Andreas、私はそれをしないと思う:もしパターンが見つからなければ、あなたのルーチンは最初のバイトへのポインタを返すでしょう...パターンが最初のバイトで見つかったらどうでしょうか?パターンが見つからない場合は、より良い戻り値が返されます。 パターンの最初と2番目のバイトが一致する場合、ルーチンはその2番目のバイトのアドレスを返して終了します(パターンの残りの部分をさらに比較せずに) – jachguate

+0

@jachguate:私はそれを見ました。 –

0

として任意の機能があります。 1バイトだけを探すときは最適化を行うことができないので、基本的には同じことが実現されます。

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は –

+0

はい、あなたは1バイトのパターンを見つけると、バイト。 –

+1

ダウン投票は少し意味があった!それはすでに底にあった! –

関連する問題