提高FastReplace速度 (fStrRep.pas)[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 luckyjan 的 blog

    mov  al, [esi+ebx]
    // get asourcestring character (current
    // position + afindstringlength).
    mov  ah, [edi+ebx]
    // compare them.
    cmp  al, ah
    jz   @matches
    // if they don't match, we put the first char
    // of afindstring into al again to continue
    // looking for the first character.
    mov  al, [esi]
    jmp  @nextchar
    @matches:
    // if they match, we dec ebx (point to
    // previous character to compare).
    dec  ebx
    // if ebx <> 0 ("j"ump "n"ot "z"ero), we
    // continue comparing strings.
    jnz  @comparenext

    //add by shengquanhu
    @endofmatch:
    //add end

    // if ebx is zero, then we've successfully
    // compared each character; i.e. it's a match!
    // move the address of the *current*
    // character in edi.
    // note, we haven't altered edi since
    // the first char was found.
    mov  eax, edi
    // this is an address, so subtract the
    // address of asourcestring[1] to get
    // an actual character position.
    sub  eax, asourcestring
    // inc eax to make it 1-based,
    // rather than 0-based.
    inc  eax
    // put it into result.
    mov  result, eax
    // finish this routine!
    jmp  @theend
    @nextchar:
//this is where i jump to when i want to continue searching for the first char
//acter of afindstring in asearchstring:
    // point edi (afindstring[x]) to
    // the next character.
    inc  edi
    // dec ecx tells us that we've checked
    // another character, and that we're
    // fast running out of string to check!
    dec  ecx
    // if ebx <> 0, then continue scanning
    // for the first character.

    //add by shengquanhu
    //if ah is chinese char,jump again
    jz   @result0
    cmp  ah, $80
    jb   @scasb
    inc  edi
    dec  ecx
    //add by shengquanhu end

    jnz  @scasb

    //add by shengquanhu
    @result0:
    //add by shengquanhu end

    // if ebx = 0, then move 0 into result.
    mov  result,0
    // restore ebx, edi, esi for delphi
    // to work correctly.
    // note that they're popped in the
    // opposite order they were pushed.
    @theend:
    pop  ebx
    pop  edi
    pop  esi
  end;
end;

//this routine is an identical copy of fastpos except where commented! the ide
//a is that when grabbing bytes, it ands them with $df, effectively making the
//m lowercase before comparing. maybe this would be quicker if afindstring was
// made lowercase in one fell swoop at the beginning of the function, saving a
//n and instruction each time.
function fastposnocase(
  const asourcestring, afindstring : string;
  const asourcelen, afindlen, startpos : integer
  ) : integer;
var
  sourcelen : integer;
begin
  sourcelen := asourcelen;
  sourcelen := sourcelen - afindlen;
  if (startpos-1) > sourcelen then begin
    result := 0;
    exit;
  end;
  sourcelen := sourcelen - startpos;
  sourcelen := sourcelen +2;
  asm
    push esi
    push edi
    push ebx

    mov edi, asourcestring
    add edi, startpos
    dec edi

本文关键:提高FastReplace速度 (fStrRep.pas)
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top