gpt4 book ai didi

string - 如何改进多次 StringReplace 调用?

转载 作者:行者123 更新时间:2023-12-03 15:56:10 26 4
gpt4 key购买 nike

我从客户那里读取文件,我需要处理读取的数据并删除一些不需要的字符。我的函数有效,但我正在尝试改进 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;

最佳答案

我想,您必须将字符串拆分为一组字符串(非分隔符和分隔符(模式)),然后替换数组中的项目,然后再次将它们组合起来。您将从较长的模式开始,然后转向较短的模式(针对模式内模式的安全检查),然后额外的运行将进行一个字符到一个字符的替换(因为它们可以就地完成并且不需要内存复制)。

双重复制,搜索缩放为 O(Length(input)*Count(Delimiters))。

类似于这个伪代码草案(未实现到最后一个点,只是为了让您有想法):

由于您的模式很短,我认为线性搜索就可以了,否则需要更优化但复杂的算法: 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

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

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;

关于string - 如何改进多次 StringReplace 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34769953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com