Janilabo

Janilabo | PosAll() [SCAR Divi]

Apr 28th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.22 KB | None | 0 0
  1. {==============================================================================]  
  2.   Explanation: Returns all the s positions in str.
  3.                If overlap is set to true, strings can overlap.
  4.                ('aa', 'baaaah', False) => [2,3,4]
  5.                ('aa', 'baaaah', True) => [2,4]                    
  6. [==============================================================================}
  7. function PosAll(s, str: string; overlap: Boolean): TIntArray;
  8. var
  9.   sL, strL, o, p, r: Integer;
  10. begin
  11.   sL := Length(s);
  12.   strL := Length(str);
  13.   if (sL <= strL) then
  14.   begin
  15.     if not overlap then
  16.     begin      
  17.       o := Length(s);
  18.       p := (p - (o - 1));
  19.     end else      
  20.       o := 1;
  21.     SetLength(Result, ((strL div sL) + 1));
  22.     repeat
  23.       p := PosEx(s, str, (p + o));
  24.       if (p > 0) then
  25.       begin
  26.         Result[r] := p;
  27.         Inc(r);
  28.       end;
  29.     until (p <= 0);  
  30.   end;
  31.   SetLength(Result, r);
  32. end;
  33.  
  34. var
  35.   s, str: string;
  36.  
  37. begin
  38.   ClearDebug;
  39.   str := '|||| PosAll() Test ||| *** ||| Should work pretty well. ||||';
  40.   s := '||';
  41.   WriteLn('PosAll(s, str, True): ' + TIAToStr(PosAll('||', str, True)));
  42.   WriteLn('PosAll(s, str, False): ' + TIAToStr(PosAll('||', str, False)));
  43. end.
Advertisement
Add Comment
Please, Sign In to add comment