StringReplace alternatives to improve performance

前端 未结 8 756
温柔的废话
温柔的废话 2021-02-04 12:46

I am using StringReplace to replace > and < by the char itself in a generated XML like this:

StringReplace(xml.Text,\'>\',\'>\',[rfReplaceA         


        
8条回答
  •  遥遥无期
    2021-02-04 13:12

    Untested conversion of the C# code written by Jorge Ferreira.

    function ReplaceLtGt(const s: string): string;
    var
      inPtr, outPtr: integer;
    begin
      SetLength(Result, Length(s));
      inPtr := 1;
      outPtr := 1;
      while inPtr <= Length(s) do begin
        if (s[inPtr] = '&') and ((inPtr + 3) <= Length(s)) and
           (s[inPtr+1] in ['l', 'g']) and (s[inPtr+2] = 't') and
           (s[inPtr+3] = ';') then
        begin
          if s[inPtr+1] = 'l' then
            Result[outPtr] :=  '<'
          else
            Result[outPtr] := '>';
          Inc(inPtr, 3);
        end
        else begin
          Result[outPtr] := Result[inPtr];
          Inc(inPtr);
        end;
        Inc(outPtr);
      end;
      SetLength(Result, outPtr - 1);
    end;
    

提交回复
热议问题