Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function MSSL_ClosestPos(trgt_pos: Integer; s, str: string): Integer;
- var
- strL, o, p: Integer;
- begin
- strL := Length(str);
- if ((Length(s) > strL) or (trgt_pos < 1) or (trgt_pos > strL)) then
- Exit;
- repeat
- p := PosEx(s, str, (o + 1));
- if (p > 0) then
- begin
- if (Result = 0) then
- Result := p
- else
- if (IAbs(trgt_pos - p) < IAbs(trgt_pos - Result)) then
- Result := p
- else
- Exit;
- o := p;
- end;
- until (p <= 0);
- end;
- function MSSL_FurthestPos(trgt_pos: Integer; s, str: string): Integer;
- var
- strL, o, p: Integer;
- begin
- strL := Length(str);
- if ((Length(s) > strL) or (trgt_pos < 1) or (trgt_pos > strL)) then
- Exit;
- repeat
- p := PosEx(s, str, (o + 1));
- if (p > 0) then
- begin
- if (Result = 0) then
- Result := p
- else
- if (IAbs(trgt_pos - p) > IAbs(trgt_pos - Result)) then
- Result := p;
- o := p;
- end;
- until (p <= 0);
- end;
- var
- str: string;
- begin
- str := 'TEST is only a TEST... TEST!';
- WriteLn(MSSL_ClosestPos(Pos('...', str), 'TEST', str));
- WriteLn(MSSL_FurthestPos(Pos('...', str), 'TEST', str));
- end.
Advertisement
Add Comment
Please, Sign In to add comment