mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-04-08 16:38:17 +02:00
lazutils: added ReplaceSubString
git-svn-id: trunk@35499 -
This commit is contained in:
parent
3d4c144f90
commit
c80c2dfecc
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -6227,6 +6227,7 @@ test/hello.ahk svneol=native#text/plain
|
||||
test/lazutils/TestLazLogger.lpr svneol=native#text/pascal
|
||||
test/lazutils/testlazloggercase.pas svneol=native#text/pascal
|
||||
test/lazutils/testlazutf8.pas svneol=native#text/plain
|
||||
test/lazutils/testlazutils.pas svneol=native#text/plain
|
||||
test/lazutils/testlazxml.pas svneol=native#text/plain
|
||||
test/lazutils/testpaswstring.lpi svneol=native#text/plain
|
||||
test/lazutils/testpaswstring.pas svneol=native#text/plain
|
||||
|
@ -134,6 +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;
|
||||
|
||||
type
|
||||
|
||||
@ -921,6 +923,45 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
function ReplaceSubstring(const s: string; StartPos, Count: SizeInt;
|
||||
const Insertion: string): string;
|
||||
var
|
||||
MaxCount: SizeInt;
|
||||
InsertionLen: SizeInt;
|
||||
SLen: SizeInt;
|
||||
RestLen: SizeInt;
|
||||
Dest: PByte;
|
||||
begin
|
||||
SLen:=length(s);
|
||||
if StartPos>SLen then
|
||||
StartPos:=SLen;
|
||||
if StartPos<1 then StartPos:=1;
|
||||
if Count<0 then Count:=0;
|
||||
MaxCount:=SLen-StartPos+1;
|
||||
if Count>MaxCount then
|
||||
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);
|
||||
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);
|
||||
end;
|
||||
|
||||
{ TLazLoggerLogGroupList }
|
||||
|
||||
procedure TLazLoggerLogGroupList.Clear;
|
||||
|
54
test/lazutils/testlazutils.pas
Normal file
54
test/lazutils/testlazutils.pas
Normal file
@ -0,0 +1,54 @@
|
||||
{
|
||||
Test all with:
|
||||
./runtests --format=plain --suite=TTestLazUtils
|
||||
|
||||
Test specific with:
|
||||
./runtests --format=plain --suite=TestReplaceSubstring
|
||||
}
|
||||
unit TestLazUtils;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, fpcunit, testglobals, LazLogger;
|
||||
|
||||
type
|
||||
|
||||
{ TTestLazUtils }
|
||||
|
||||
TTestLazUtils = class(TTestCase)
|
||||
public
|
||||
published
|
||||
procedure TestReplaceSubstring;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TTestLazUTF8 }
|
||||
|
||||
procedure TTestLazUtils.TestReplaceSubstring;
|
||||
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'));
|
||||
end;
|
||||
|
||||
initialization
|
||||
AddToLazUtilsTestSuite(TTestLazUtils);
|
||||
|
||||
end.
|
||||
|
@ -86,9 +86,9 @@
|
||||
<UnitName Value="TestBasicCodetools"/>
|
||||
</Unit8>
|
||||
<Unit9>
|
||||
<Filename Value="lazutils\testlazutf8.pas"/>
|
||||
<Filename Value="lazutils\testlazutils.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<UnitName Value="TestLazUTF8"/>
|
||||
<UnitName Value="TestLazUtils"/>
|
||||
</Unit9>
|
||||
<Unit10>
|
||||
<Filename Value="codetoolstests\testctxmlfixfragments.pas"/>
|
||||
|
@ -30,7 +30,7 @@ uses
|
||||
TestLpi, BugTestCase,
|
||||
bug8432, testfileutil, testfileproc,
|
||||
// lazutils
|
||||
TestLazUTF8,
|
||||
TestLazUtils, TestLazUTF8,
|
||||
// codetools
|
||||
TestBasicCodetools, TestCTXMLFixFragments, TestCTRangeScan, TestCTH2Pas,
|
||||
TestCompleteBlock, TestStdCodetools,
|
||||
|
Loading…
Reference in New Issue
Block a user