如何改善多个StringReplace调用? [英] How to improve multiple StringReplace calls?

查看:70
本文介绍了如何改善多个StringReplace调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



是否可以用某些东西替换多个StringReplace调用



我找不到MultipleStringReplace或类似的函数。



MCVE:

  function FixData(const vStr:string):string; 
var i:integer;
开始
结果:= vStr;

//空字符串
如果Result =#0,则结果:=’’;

//仅修复换行指示器
如果Result =#13#10然后Result:=#8;

//如果Pos(#0,Result)>删除'end'/#0字符
0,然后i的
:= 1到Length(Result)如果Result [i] =#0,则
执行
,然后
Result [i]:=’′

//#$ D#$ A-> #8
如果Pos(#$ D#$ A,Result)> 0然后
结果:= StringReplace(Result,#$ D#$ A,#8,[rfReplaceAll]);

//如果Pos(’& #xD;’,结果)>删除& #xD
0,然后
结果:= StringReplace(结果,& #xD;,,[rfReplaceAll]);

//#$ A-> #8
如果Pos(#$ A,Result)> 0然后
结果:= StringReplace(Result,#$ A,#8,[rfReplaceAll]);

//如果Pos(chr(34),Result)> 0则$ temp_replacement值
,然后
Result:= StringReplace(Result,chr(34),' \_ /',[rfReplaceAll]);
结尾;

过程TForm1.Button1Click(Sender:TObject);
var vStr,vFixedStr:string;
开始
vStr:='testingmystr: quotest-'+#0 +'substr& #xD;新行'#$ A'第二行'#$ D#$ A'数据结尾';
vFixedStr:= FixData(vStr);
结尾;


解决方案

我想,您必须将字符串拆分为一组字符串(非定界符和定界符(模式)),然后替换数组中的项,然后再次将其组合回去。并转到较短的代码(针对pattern-inside-pattern进行安全检查),那么另一轮操作将是进行一个字符到一个字符的替换(因为它们可以就地完成,并且不需要内存复制)



双重复制,搜索比例为O(Length(input)* Cou nt(Delimiters))。



类似于此伪代码草稿(未实现到最后一个点,只是为了让您有个主意):



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



  Type TReplaceItem = record(match,subst:string;位置:整数); 
var匹配项:TReplaceItem数组;

SetLength(matches,3);
match [0] .match:=‘& #xD;’; //最长的第一;
matchs [0] .subst:=’’;
match [1] .match:=#$ D#$ A; //最长的第一;
matchs [1] .subst:=#8;
matchs [2] .match:=#34; //最长的第一;
匹配[2] .subst:=‘\_ /’;

sb:= TStringBuilder.Create(2 * Length(InputString));
//或TList< String>或Jedi CodeLib的iJclStringList或TStringList ...取决于性能和首选项
//容量参数用于-预热,预分配内存,通常足够
试试

NextLetterToParse:= 1;
for I:=低(匹配)到高(匹配)执行
match [I] .position:= PosEx(matches [I] .match,InputString,NextLetterToParse);

True时开始

ClosestMatchIdx:= -1;

ClosestMatchPos:= {最小匹配[???]。位置> = NextLetterToParse};
ClosestMatchIdx:= {如果最小,则等于[索引]-高于最小值[???]或保持为-1}

如果ClosestMatchIdx< 0 {我们没有更多的匹配项},然后开始

//转储所有剩余的尚未解析的其余
SB.Append(Copy(InputString,NextLetterToParse,Length(InputString));;

//退出stage1:拆分循环
中断;
结束;

//转储not-的next-next-delimiter部分输入
的解析后尾// //可能没有-如果ClosestMatchPos> NextLetterToParse然后
SB.Append(Copy(InputString,NextLetterToParse,ClosestMatchPos- NextLetterToParse);

//转储分隔符模式
SB.Append(match [ClosestMatchIdx] .Subst);

ShiftLength:=(ClosestMatchPos -NextLetterToParse)+ Length(matches [ClosestMatchIdx] .Match);
//现在已经丢弃了多余的部分

Inc(NextLetterToParse,ShiftLength);

对于我:=低(匹配)到高(匹配)做
如果匹配[I] .position<然后NextLetterToParse
match [I] .position:= PosEx(matches [I] .match,InputString,NextLetterToParse);
//为每个受影响的定界符更新下一个最近的位置,
//那些影响太远而不会受到影响的位置(通常是所有
//但正在被丢弃的那个)重新扫描

结束; //下一阶段的循环迭代

现在我们有一个容器/数组/列表/包含非匹配的块和替换的模式。除了就地一字符替换。是时候合并并进行最后一次扫描了。

  Stage2String:= SB.ToString(); 

最终
SB.Destroy;
结尾; I的

:= 1到Length(Stage2String)执行
情况
的Stage2String [I]#0:Stage2String [I]:=#32;

#10,#13:Stage2String [I]:=#8;
// BTW-有时无需尾随即可满足^ M =#13 =#$ D ^ J =#10 =#$ A
// //这是以前使用的行结束符Macintosh文本文件

else; //不执行任何操作,使其保持原样,即
结尾;

结果:= Stage2String;


I read files from customers and I need to process the read data and remove some unneeded characters. My function works, but I'm trying to improve the FixData function to improve speed/performance and maintainability.

Is it possible to replace multiple StringReplace calls with something that will only loop through data once and replace with whatever it needs to?

I can't find MultipleStringReplace or similar function.

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('&#xD;', Result) > 0 then
      Result := StringReplace(Result, '&#xD;', '', [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 &#xD; new line '#$A' 2nd line '#$D#$A' end of data';
  vFixedStr:=FixData(vStr);
end;

解决方案

I guess, you have to split your string into a set of strings ( non-delimiters and delimiters(patterns) ) and then replace items in the array and then combine them back yet again. You would start with longer patterns and go to shorter ones (safety check against pattern-inside-pattern), then an extra run would be to make one-char-to-one-char substitutions (as they can be done in-place and would not require memory copying).

Double copy, and search scaling as O(Length(input)*Count(Delimiters)).

Something like this pseudocode draft (not implemented to the last dot, just for you to have the idea):

Since your patterns are short I think linear search would be okay, otherwise more optimized but complex algorithms would be needed: https://en.wikipedia.org/wiki/String_searching_algorithm#Algorithms_using_a_finite_set_of_patterns

Hash it to smaller functions as you see fit for ease of understanding/maintenance.

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

SetLength(matches, 3);
matches[0].match := '&#xD;'; // 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

Now we have a container/array/list/anything comprised of non-matched chunks and replaced patterns. Except for in-place one-char replacement. Time to merge and do one last scan.

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;

这篇关于如何改善多个StringReplace调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆