lazutils: changed replacesubstring function to procedure

git-svn-id: trunk@35506 -
This commit is contained in:
mattias 2012-02-20 10:53:10 +00:00
parent 63bed888d2
commit 2e114fe055
2 changed files with 53 additions and 37 deletions

View File

@ -134,8 +134,8 @@ function DbgS(const Shift: TShiftState): string; overload;
function DbgS(const ASize: TSize): string; overload;
function ConvertLineEndings(const s: string): string;
function ReplaceSubstring(const s: string; StartPos, Count: SizeInt;
const Insertion: string): string;
procedure ReplaceSubstring(var s: string; StartPos, Count: SizeInt;
const Insertion: string);
type
@ -923,14 +923,14 @@ begin
end;
end;
function ReplaceSubstring(const s: string; StartPos, Count: SizeInt;
const Insertion: string): string;
procedure ReplaceSubstring(var s: string; StartPos, Count: SizeInt;
const Insertion: string);
var
MaxCount: SizeInt;
InsertionLen: SizeInt;
SLen: SizeInt;
RestLen: SizeInt;
Dest: PByte;
p: PByte;
begin
SLen:=length(s);
if StartPos>SLen then
@ -942,24 +942,32 @@ begin
Count:=MaxCount;
InsertionLen:=length(Insertion);
if (Count=0) and (InsertionLen=0) then
exit(s); // nothing to do
if (Count=InsertionLen)
and CompareMem(PByte(s)+StartPos-1,Pointer(Insertion),Count) then
// already the same content
exit(s);
Setlength(Result,SLen-Count+InsertionLen);
Dest:=PByte(Result);
if StartPos>1 then begin
System.Move(PByte(s)^,Dest^,StartPos-1);
inc(Dest,StartPos-1);
exit; // nothing to do
if (Count=InsertionLen) then begin
if CompareMem(PByte(s)+StartPos-1,Pointer(Insertion),Count) then
// already the same content
exit;
UniqueString(s);
end else begin
RestLen:=SLen-StartPos-Count+1;
if InsertionLen<Count then begin
// shorten
if RestLen>0 then begin
p:=PByte(s)+StartPos-1;
System.Move((p+Count)^,(p+InsertionLen)^,RestLen);
end;
Setlength(s,SLen-Count+InsertionLen);
end else begin
// longen
Setlength(s,SLen-Count+InsertionLen);
if RestLen>0 then begin
p:=PByte(s)+StartPos-1;
System.Move((p+Count)^,(p+InsertionLen)^,RestLen);
end;
end;
end;
if InsertionLen>0 then begin
System.Move(PByte(Insertion)^,Dest^,InsertionLen);
inc(Dest,InsertionLen);
end;
RestLen:=SLen-StartPos-Count+1;
if RestLen>0 then
System.Move((PByte(s)+StartPos-1+Count)^,Dest^,RestLen);
if InsertionLen>0 then
System.Move(PByte(Insertion)^,(PByte(s)+StartPos-1)^,InsertionLen);
end;
{ TLazLoggerLogGroupList }

View File

@ -29,22 +29,30 @@ implementation
{ TTestLazUTF8 }
procedure TTestLazUtils.TestReplaceSubstring;
function r(const s: string; StartPos, Count: SizeInt;
const Insertion: string): string;
begin
Result:=s;
ReplaceSubstring(Result,StartPos,Count,Insertion);
end;
begin
AssertEquals('empty string','',ReplaceSubstring('',1,1,''));
AssertEquals('empty string insert a','a',ReplaceSubstring('',1,1,'a'));
AssertEquals('empty string negative startpos','a',ReplaceSubstring('',-1,1,'a'));
AssertEquals('empty string count too big','a',ReplaceSubstring('',-1,10,'a'));
AssertEquals('empty string beyond length','a',ReplaceSubstring('',10,10,'a'));
AssertEquals('whole','a',ReplaceSubstring('a',1,1,'a'));
AssertEquals('whole','b',ReplaceSubstring('a',1,1,'b'));
AssertEquals('whole','abc',ReplaceSubstring('a',1,1,'abc'));
AssertEquals('first char','abcbc',ReplaceSubstring('abc',1,1,'abc'));
AssertEquals('last char single','aba',ReplaceSubstring('abc',3,1,'a'));
AssertEquals('last char multi','ababc',ReplaceSubstring('abc',3,1,'abc'));
AssertEquals('middle char same','abc',ReplaceSubstring('abc',2,1,'b'));
AssertEquals('middle char single','adc',ReplaceSubstring('abc',2,1,'d'));
AssertEquals('middle char multi','acdec',ReplaceSubstring('abc',2,1,'cde'));
AssertEquals('last multi','adef',ReplaceSubstring('abc',2,2,'def'));
AssertEquals('empty string','',r('',1,1,''));
AssertEquals('empty string insert a','a',r('',1,1,'a'));
AssertEquals('empty string negative startpos','a',r('',-1,1,'a'));
AssertEquals('empty string count too big','a',r('',-1,10,'a'));
AssertEquals('empty string beyond length','a',r('',10,10,'a'));
AssertEquals('whole','a',r('a',1,1,'a'));
AssertEquals('whole','b',r('a',1,1,'b'));
AssertEquals('whole','abc',r('a',1,1,'abc'));
AssertEquals('first char','abcbc',r('abc',1,1,'abc'));
AssertEquals('last char single','aba',r('abc',3,1,'a'));
AssertEquals('last char multi','ababc',r('abc',3,1,'abc'));
AssertEquals('middle char same','abc',r('abc',2,1,'b'));
AssertEquals('middle char single','adc',r('abc',2,1,'d'));
AssertEquals('middle char multi','acdec',r('abc',2,1,'cde'));
AssertEquals('last multi','adef',r('abc',2,2,'def'));
end;
initialization