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

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

本文简介:选择自 luckyjan 的 blog

    // that is, the source consists only of
    // afindstring, and we're going to replace
    // every one of them!
    actualresultlen :=
      sourcelen +
      (sourcelen * replacelen div findlen) +
      replacelen;
  // set the length of result; this
  // will assign the memory, etc.
  setlength(result,actualresultlen);
  currentpos := 1;
  resultlen := 0;
  lastpos := 1;
//again, i’m eliminating an if statement in a loop by repeating code–this ap
//proach results in very slightly larger code, but if ever you can trade some
//memory in exchange for speed, go for it!
  if replacelen > 0 then begin
    repeat
      // get the position of the first (or next)
      // afindstring in asourcestring.
      // note that there's no if casesensitive,
      // i just call fastposproc, which is pointing
      // to the correct pre-determined routine.
      currentpos :=
        fastposproc(asourcestring, afindstring,
          sourcelen, findlen, currentpos);
      // if 0, then we're finished.
      if currentpos = 0 then break;
      // number of bytes to copy from the
      // source string is currentpos - lastpos,
      // i.e. " cat " in "the cat the".
      bytestocopy := currentpos-lastpos;
      // copy chars from asourcestring
      // to the end of result.
      mymove(asourcestring[lastpos],
        result[resultlen+1], bytestocopy);
      // copy chars from areplacestring to
      // the end of result.
      mymove(areplacestring[1],
        result[resultlen+1+bytestocopy], replacelen);
      // remember, using copy would copy all of
      // the data over and over again.
      // never fall into this trap (like a certain
      // software company did).
      // set the running length to
      resultlen := resultlen +
        bytestocopy + replacelen;
      // set the position in asourcestring to where
      // we want to continue searching from.
      currentpos := currentpos + findlen;
      lastpos := currentpos;
    until false;
  end else begin
    // you might have noticed if replacelen > 0.
    // well, if replacelen = 0, then we're deleting the
    // substrings, rather than replacing them, so we
    // don't need the extra mymove from areplacestring.
    repeat
      currentpos := fastpos(asourcestring,
        afindstring, sourcelen, findlen, currentpos);
      if currentpos = 0 then break;
      bytestocopy := currentpos-lastpos;
      mymove(asourcestring[lastpos],
        result[resultlen+1], bytestocopy);
      resultlen := resultlen +
        bytestocopy + replacelen;
      currentpos := currentpos + findlen;
      lastpos := currentpos;
    until false;
  end;
//now that we’ve finished doing all of the replaces, i just need to adjust th
//e length of the final result:
  dec(lastpos);
//now i set the length to the length plus the bit of string left. that is, " m
//at" when replacing "the" in "sat on the mat".
  setlength(result, resultlen + (sourcelen-lastpos));
  // if there's a bit of string dangling, then
  // add it to the end of our string.
  if lastpos+1 <= sourcelen then
    mymove(asourcestring[lastpos+1],
      result[resultlen+1],sourcelen-lastpos);

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

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

go top