我想,您必须将字符串拆分为一组字符串(非分隔符和分隔符(模式)),然后替换数组中的项目,然后再次将它们组合回来。您将从较长的模式开始,然后转向较短的模式(针对模式内模式的安全检查),然后额外的运行将进行一个字符到一个字符的替换(因为它们可以就地完成并且不需要内存复制)。
双重复制,搜索缩放为 O(Length(input)*Count(Delimiters))。
像这样的伪代码草案(没有实现到最后一个点,只是为了让你有这个想法):
由于您的模式很短,我认为线性搜索就可以了,否则需要更优化但复杂的算法: https://en.wikipedia.org/wiki/String_searching_algorithm#Algorithms_using_a_finite_set_of_patterns 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;