zoukankan      html  css  js  c++  java
  • Delphi极速字符串替换函数

    //此极速字符串替换函数为[盒子论坛hq200306兄]所作,在此感谢!亲测原本48秒的长文本替换操作,现在只要几十毫秒不到!
    
    function PosX(const SubStr, Str: string; Offset: Integer): Integer;
    var
      I, LIterCnt, L, J: Integer;
      PSubStr, PS: PChar;
    begin
      L := Length(SubStr);
      { Calculate the number of possible iterations. Not valid if Offset < 1. }
      LIterCnt := Length(Str) - Offset - L + 1;
    
      { Only continue if the number of iterations is positive or zero (there is space to check) }
      if (Offset > 0) and (LIterCnt >= 0) and (L > 0) then
      begin
        PSubStr := PChar(SubStr);
        PS := PChar(Str);
        Inc(PS, Offset - 1);
    
        for I := 0 to LIterCnt do
        begin
          J := 0;
          while (J >= 0) and (J < L) do
          begin
            if UpCase(PS[I + J]) = UpCase(PSubStr[J]) then
              Inc(J)
            else
              J := -1;
          end;
          if J >= L then
            Exit(I + Offset);
        end;
      end;
    
      Result := 0;
    end;
    
    function StringReplaceEx(const st, oldSubstr, newSubStr: string): string;
    var
      idx, len: Integer;
      iStart: Integer;
      sb: TStringBuilder;
    begin
      len := Length(oldSubstr);
      iStart := 1;
      sb := TStringBuilder.Create;
      try
        repeat
          idx := posX(oldSubstr, st, iStart);
          if idx > 0 then
          begin
            sb.Append(Copy(st, iStart, idx - iStart));
            sb.Append(newSubStr);
            iStart := idx + len;
          end;
        until idx <= 0;
        sb.Append(Copy(st, iStart, length(st)));
        Result := sb.ToString;
      finally
        sb.Free;
      end;
    end;
    

      

  • 相关阅读:
    wustctf2020_name_your_dog
    wdb2018_guess(stack smash,__environ获取栈变量)
    ciscn_2019_en_3
    植物大战僵尸逆向(秒杀僵尸)
    pwnable_hacknote
    pwn-hitcontraining_magicheap(unsorted bin attack)
    pwn-[XMAN]level5(通用gadget,mprotect)
    pwn-pwnable_orw(seccomp&orw)
    《心流》有感
    LC112 路径总和
  • 原文地址:https://www.cnblogs.com/xenli/p/13812750.html
Copyright © 2011-2022 走看看