
Untitled
By: a guest on
Jun 21st, 2012 | syntax:
None | size: 1.52 KB | hits: 12 | expires: Never
How can i use PChars correctly to improve this optimization?
function StripString(mask: string; var modifiedstring: string): string;
var
index,len: integer;
s: string;
begin
Index := pos(Mask, ModifiedString);
len := Length(ModifiedString);
if index <> 0 then
begin
if Index <> 1 then
begin
s := LeftStr(ModifiedString, index - 1);
ModifiedString := RightStr(ModifiedString, len-index);
end else begin
if Length(ModifiedString)>1 then
ModifiedString := Copy(ModifiedString,2,len)
else
ModifiedString := '';
s := '';
end;
end else begin
s := ModifiedString;
ModifiedString := '';
end;
result := s
end;
//faster method - uses PChars
function StripStringEx(mask: char; var modifiedstring: string): string;
var
pSt,pCur,pEnd : Pchar;
begin
pEnd := @modifiedString[Length(modifiedString)];
pSt := @modifiedString[1];
pCur := pSt;
while pCur <= pEnd do
begin
if pCur^ = mask then break;
inc(pCur);
end;
SetString(Result,pSt,pCur-pSt);
SetString(ModifiedString,pCur+1,pEnd-pCur);
end;
function StripString(const Mask: string; var ModifiedString: string): string;
var
Index: Integer;
begin
Index := Pos(Mask, ModifiedString);
if Index <> 0 then
begin
Result := LeftStr(ModifiedString, Index - 1);
Delete(ModifiedString, Index);
end
else
begin
Result := ModifiedString;
ModifiedString := '';
end;
end;