Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 1.52 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can i use PChars correctly to improve this optimization?
  2. function StripString(mask: string; var modifiedstring: string): string;
  3. var
  4.   index,len: integer;
  5.   s: string;
  6. begin
  7.     Index := pos(Mask, ModifiedString);
  8.     len := Length(ModifiedString);
  9.     if index <> 0 then
  10.     begin
  11.         if Index <> 1 then
  12.         begin
  13.             s := LeftStr(ModifiedString, index - 1);
  14.             ModifiedString := RightStr(ModifiedString, len-index);
  15.         end else begin
  16.             if Length(ModifiedString)>1 then
  17.               ModifiedString := Copy(ModifiedString,2,len)
  18.             else
  19.               ModifiedString := '';
  20.             s := '';
  21.         end;
  22.     end else begin
  23.         s := ModifiedString;
  24.         ModifiedString := '';
  25.     end;
  26.     result := s
  27. end;
  28.        
  29. //faster method - uses PChars
  30. function StripStringEx(mask: char; var modifiedstring: string): string;
  31. var
  32.   pSt,pCur,pEnd : Pchar;
  33. begin
  34.     pEnd := @modifiedString[Length(modifiedString)];
  35.     pSt := @modifiedString[1];
  36.     pCur := pSt;
  37.     while pCur <= pEnd do
  38.     begin
  39.          if pCur^ = mask then break;
  40.          inc(pCur);
  41.     end;
  42.     SetString(Result,pSt,pCur-pSt);
  43.     SetString(ModifiedString,pCur+1,pEnd-pCur);
  44. end;
  45.        
  46. function StripString(const Mask: string; var ModifiedString: string): string;
  47. var
  48.   Index: Integer;
  49. begin
  50.   Index := Pos(Mask, ModifiedString);
  51.   if Index <> 0 then
  52.   begin
  53.     Result := LeftStr(ModifiedString, Index - 1);
  54.     Delete(ModifiedString, Index);
  55.   end
  56.   else
  57.   begin
  58.     Result := ModifiedString;
  59.     ModifiedString := '';  
  60.   end;
  61. end;