1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185:
| program test; {$MODE DELPHI} uses sysutils, strutils;
const gesLen = 10*1000*1000; rowLen = 60; var s : AnsiString; p : longInt;
function StringReplaceMultiple(const Source: AnsiString; const OldPatterns, NewPatterns: array of AnsiString; CaseSensitive: Boolean = True): AnsiString;
type TFoundPos = record Position: Integer; PatternNum: Integer; end;
var C: Integer; FoundCount: Integer; SourcePosition: Integer; PatternCount: Integer; Positions: array of TFoundPos; PositionLength: Integer;
PatternNum: Integer; SourceLength: Integer; OldLengths, NewLengths: array of Integer; DeltaOld: Integer;
Delta: Integer;
PSource, PDest, PNew: PAnsiChar;
SearchSource: AnsiString; CasePatterns: array of AnsiString;
begin if (Source = '') or (Length(OldPatterns) <> Length(NewPatterns)) then begin Result := Source; Exit; end;
try PatternCount := Length(OldPatterns); SourceLength := Length(Source); SetLength(OldLengths, PatternCount); SetLength(NewLengths, PatternCount); Delta := 0; DeltaOld := 0; for C := 0 to PatternCount - 1 do begin OldLengths[C] := Length(OldPatterns[C]); NewLengths[C] := Length(NewPatterns[C]); Inc(DeltaOld, OldLengths[C]); end; DeltaOld := Round(DeltaOld / PatternCount);
SetLength(CasePatterns, PatternCount); if CaseSensitive then begin SearchSource := Source; for C := 0 to PatternCount - 1 do CasePatterns[C] := OldPatterns[C]; end else begin SearchSource := AnsiLowerCase(Source); for C := 0 to PatternCount - 1 do CasePatterns[C] := AnsiLowerCase(OldPatterns[C]); end;
FoundCount := 0;
PositionLength := SourceLength div DeltaOld + 1; SetLength(Positions, PositionLength);
C := 1; while C <= SourceLength do begin for PatternNum := 0 to PatternCount - 1 do begin if (SearchSource[C]) = (CasePatterns[PatternNum][1]) then begin if CompareMem(@SearchSource[C], @CasePatterns[PatternNum][1], OldLengths[PatternNum]) then begin if FoundCount >= PositionLength then begin Inc(PositionLength, 4); SetLength(Positions, PositionLength); end;
Positions[FoundCount].Position := C; Positions[FoundCount].PatternNum := PatternNum; Inc(FoundCount); Inc(C, OldLengths[PatternNum] - 1); Inc(Delta, NewLengths[PatternNum] - OldLengths[PatternNum]); Break; end; end; end; Inc(C); end;
SetLength(CasePatterns, 0);
if FoundCount > 0 then begin SetLength(Result, SourceLength + Delta);
SourcePosition := 1; PSource := PAnsiChar(Source); PDest := PAnsiChar(Result);
for C := 0 to FoundCount - 1 do begin PNew := PAnsiChar(NewPatterns[Positions[C].PatternNum]);
Move(PSource^, PDest^, Positions[C].Position - SourcePosition); Inc(PDest, Positions[C].Position - SourcePosition);
Move(PNew^, PDest^, NewLengths[Positions[C].PatternNum]); Inc(PDest, NewLengths[Positions[C].PatternNum]);
Inc(PSource, Positions[C].Position - SourcePosition + OldLengths[Positions[C].PatternNum]); SourcePosition := Positions[C].Position + OldLengths[Positions[C].PatternNum]; end;
Move(PSource^, PDest^, SourceLength - SourcePosition + 1); end else Result := Source; Finalize(Positions); except end; end; var Pat,Rpl : array of AnsiString; Begin setlength(s,gesLen); fillchar(s[1],length(s),'A'); p := (length(s) DIV rowLen) *rowLen-1; while p > 2 do begin s[p]:= #13; s[p+1]:= #10; dec(p,60); end; setlength(Pat,1); setlength(Rpl,1); Pat[0] := #13#10; Rpl[0] := ' ';
writeln(length(s)); s:=StringReplaceMultiple(s,Pat,Rpl); writeln(length(s)); end. |