Janilabo

Janilabo | MSSL_ClosestPos() / MSSL_FurthestPos()

Aug 7th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.18 KB | None | 0 0
  1. function MSSL_ClosestPos(trgt_pos: Integer; s, str: string): Integer;
  2. var
  3.   strL, o, p: Integer;
  4. begin
  5.   strL := Length(str);
  6.   if ((Length(s) > strL) or (trgt_pos < 1) or (trgt_pos > strL)) then
  7.     Exit;
  8.   repeat
  9.     p := PosEx(s, str, (o + 1));
  10.     if (p > 0) then
  11.     begin
  12.       if (Result = 0) then
  13.         Result := p
  14.       else
  15.         if (IAbs(trgt_pos - p) < IAbs(trgt_pos - Result)) then
  16.           Result := p
  17.         else
  18.           Exit;
  19.       o := p;
  20.     end;
  21.   until (p <= 0);
  22. end;
  23.  
  24. function MSSL_FurthestPos(trgt_pos: Integer; s, str: string): Integer;
  25. var
  26.   strL, o, p: Integer;
  27. begin
  28.   strL := Length(str);
  29.   if ((Length(s) > strL) or (trgt_pos < 1) or (trgt_pos > strL)) then
  30.     Exit;
  31.   repeat
  32.     p := PosEx(s, str, (o + 1));
  33.     if (p > 0) then
  34.     begin    
  35.       if (Result = 0) then
  36.         Result := p
  37.       else
  38.         if (IAbs(trgt_pos - p) > IAbs(trgt_pos - Result)) then
  39.           Result := p;
  40.       o := p;
  41.     end;
  42.   until (p <= 0);
  43. end;
  44.  
  45. var
  46.   str: string;
  47.  
  48. begin
  49.   str := 'TEST is only a TEST... TEST!';
  50.   WriteLn(MSSL_ClosestPos(Pos('...', str), 'TEST', str));
  51.   WriteLn(MSSL_FurthestPos(Pos('...', str), 'TEST', str));
  52. end.
Advertisement
Add Comment
Please, Sign In to add comment