如何改进多个StringReplace调用?

时间:2016-01-13 15:01:07

标签: string delphi delphi-xe7

我从客户那里读取文件,我需要处理读取数据并删除一些不需要的字符。我的功能有效,但我正在尝试改进FixData功能,以提高速度/性能和可维护性。

是否可以用一些只会循环数据并替换为需要的内容来替换多个StringReplace调用?

我无法找到MultipleStringReplace或类似功能。

MCVE:

function FixData(const vStr:string):string;
var i:integer;
begin
  Result:=vStr;

  // empty string
  if Result = #0 then Result := '';

  // fix just New line indicator
  if Result = #13#10 then  Result := #8;

  // remove 'end'/#0  characters
    if Pos(#0, Result) > 0 then
      for i := 1 to Length(Result) do
        if Result[i] = #0 then
          Result[i] := ' ';

    //  #$D#$A  -> #8
    if Pos(#$D#$A, Result) > 0 then
      Result := StringReplace(Result, #$D#$A, #8, [rfReplaceAll]);

    // remove &#xD
    if Pos('
', Result) > 0 then
      Result := StringReplace(Result, '
', '', [rfReplaceAll]);

    // #$A -> #8
    if Pos(#$A, Result) > 0 then
      Result := StringReplace(Result, #$A, #8, [rfReplaceAll]);

    // replace " with temp_replacement value
    if Pos(chr(34), Result) > 0 then
      Result := StringReplace(Result, chr(34), '\_/', [rfReplaceAll]);
end;

procedure TForm1.Button1Click(Sender: TObject);
var vStr,vFixedStr:string;
begin
  vStr:='testingmystr:"quotest" - '+#0+' substr 
 new line '#$A' 2nd line '#$D#$A' end of data';
  vFixedStr:=FixData(vStr);
end;

1 个答案:

答案 0 :(得分:2)

我想,你必须将你的字符串拆分成一组字符串(非分隔符和分隔符(模式)),然后替换数组中的项目,然后再将它们组合起来。你可以从较长的模式开始,然后选择更短的模式(对模式内部模式进行安全检查),然后额外的运行将是一个char-to-one-char替换(因为它们可以就地完成并且不需要记忆复制。)

双重复制,搜索缩放为O(长度(输入)*计数(分隔符))。

像这样的伪代码草案(没有实现到最后一个点,只是为了让你有这个想法):

由于您的模式很短,我认为线性搜索会没问题,否则会更优化,但需要复杂的算法: https://en.wikipedia.org/wiki/String_searching_algorithm#Algorithms_using_a_finite_set_of_patterns

将其分类为您认为合适的较小功能,以便于理解/维护。

Type TReplaceItem = record (match, subst: string; position: integer);
var matches: array of TReplaceItem;

SetLength(matches, 3);
matches[0].match := '
'; // most long first;
  matches[0].subst := ''; 
matches[1].match := #$D#$A; // most long first;
  matches[1].subst := #8; 
matches[2].match := #34; // most long first;
  matches[2].subst := '\_/'; 

sb := TStringBuilder.Create( 2*Length(InputString) ); 
// or TList<String>, or iJclStringList of Jedi CodeLib, or TStringList... depending on performance and preferences
// Capacity parameter is for - warming up, pre-allocating memory that is "usually enough" 
try    

  NextLetterToParse := 1;
  for I := Low(matches) to high(matches) do
    matches[I].position := PosEx(matches[I].match, InputString, NextLetterToParse ); 

  While True do begin

     ClosestMatchIdx := -1;

     ClosestMatchPos := { minimal match[???].Position that is >= NextLetterToParse };
     ClosestMatchIdx := {index - that very [???] above - of the minimum, IF ANY, or remains -1}

     if ClosestMatchIdx < 0 {we have no more matches} then begin

      //dump ALL the remaining not-yet-parsed rest
        SB.Append( Copy( InputString, NextLetterToParse , Length(InputString));

      // exit stage1: splitting loop
        break;
     end;

     // dumping the before-any-next-delimiter part of not-parsed-yet tail of the input
     // there may be none - delimiters could go one after another
     if ClosestMatchPos > NextLetterToParse then
         SB.Append( Copy( InputString, NextLetterToParse, ClosestMatchPos-NextLetterToParse);

     // dumping the instead-of-delimiter pattern
     SB.Append( matches[ ClosestMatchIdx ].Subst );

     ShiftLength := (ClosestMatchPos - NextLetterToParse) + Length(matches[ ClosestMatchIdx ].Match); 
     // that extra part got already dumped now

     Inc( NextLetterToParse, ShiftLength);

     for I := Low(matches) to high(matches) do
       if matches[I].position < NextLetterToParse then
          matches[I].position := PosEx(matches[I].match, InputString, NextLetterToParse ); 
     // updating next closest positions for every affected delimiter,
     // those that were a bit too far to be affected ( usually all 
     // but the one being dumped) need not to be re-scanned 

  end; // next stage 1 loop iteration

现在我们有一个容器/数组/列表/由非匹配块和替换模式组成的任何东西。除了就地替换一个字符。合并时间并进行最后一次扫描。

Stage2String := SB.ToString();

finally 
  SB.Destroy; 
end;

for I := 1 to Length( Stage2String ) do
  case Stage2String[I] of
    #0: Stage2String[I] := #32;

    #10, #13: Stage2String[I] := #8;
    // BTW - ^M=#13=#$D sometimes can be met without trailing ^J=#10=#$A
    // that was the end-of-line char used in old Macintosh text files

    else ; // do nothing, let it stay as is
  end;

Result := Stage2String;