Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {==============================================================================]
- @action: Returns true if s exists as position in str.
- @note: None.
- @contributors: Janilabo, slacky
- [==============================================================================}
- function StrPosMatch(s, str: string; position: Integer): Boolean; // callconv inline;
- var
- a, b, x, y: Integer;
- begin
- a := Length(str);
- b := Length(s);
- Result := False;
- if ((position > 0) and (b > 0) and not ((b > a) or (position > a))) then
- case (b > 1) of
- True:
- if (position <= ((a - b) + 1)) then
- begin
- y := ((position + b) - 1);
- for x := position to y do
- if not (str[x] = s[((x - position) + 1)]) then
- Exit;
- Result := True;
- end;
- False: Result := (str[position] = s[1])
- end;
- end;
- {==============================================================================]
- @action: Returns s position from str. Starts scanning from offset.
- If s doesn't exist in str, result will be set as 0.
- @note: PosEx()
- @contributors: Janilabo, slacky
- [==============================================================================}
- function StrPosEx(s, str: string; offset: Integer): Integer; // callconv inline;
- var
- a, b, o, p: Integer;
- c: Char;
- begin
- Result := 0;
- a := Length(str);
- b := Length(s);
- if ((b > 0) and not ((b > a) or (offset > a))) then
- begin
- if not (offset < 1) then
- o := offset
- else
- o := 1;
- case (b > 1) of
- True:
- if (o < ((a - b) + 2)) then
- begin
- p := ((a - b) + 1);
- for Result := o to p do
- if StrPosMatch(s, str, Result) then
- Exit;
- Result := 0;
- end;
- False:
- begin
- c := s[1];
- for Result := o to a do
- if (str[Result] = c) then
- Exit;
- Result := 0;
- end;
- end;
- end;
- end;
- {==============================================================================]
- @action: Returns the last s position in str.
- @note: None
- @contributors: Janilabo, slacky
- [==============================================================================}
- function StrLastPos(s, str: string): Integer; // callconv
- var
- l, t, p: Integer;
- m: Boolean;
- c: Char;
- begin
- l := Length(str);
- t := Length(s);
- if ((t > 0) and not (t > l)) then
- case (t > 1) of
- True:
- begin
- p := ((l - t) + 1);
- repeat
- m := StrPosMatch(s, str, p);
- if not m then
- p := (p - 1);
- until (m or (p < 1));
- Result := p; // Exit(p);
- Exit;
- end;
- False:
- begin
- c := s[1];
- for Result := l downto 1 do
- if (str[Result] = c) then
- Exit;
- end;
- end;
- Result := 0;
- end;
- {*
- Matches a pattern string and returns the match.
- Takes a ignore-char, so we skip what is between then given parts of the ignore char.
- > Developed by Janilabo!
- *}
- function StrMatches(Expr, Str: string; Ignore: Char; Sensitive: Boolean; var Match: String): Boolean;
- var
- TSA: TStrArray;
- Hi, i, _Right, P, Left: Integer;
- begin
- Result := False;
- _Right := Length(Expr);
- if ((_Right > 0) and not (_Right > Length(Str))) then
- begin
- TSA := Explode(ignore, expr);
- Hi := High(TSA);
- Left := Pos(TSA[0], Str);
- _Right := (Left + Length(TSA[0]));
- if ((Hi > 0) and (Left > 0)) then
- begin
- case Sensitive of
- True:
- for i := 1 to Hi do
- begin
- p := StrPosEx(TSA[i], Str, _Right);
- if (p > 0) then
- _Right := (P + Length(TSA[i]))
- else
- Exit;
- end;
- False:
- for i := 1 to Hi do
- begin
- p := StrLastPos(TSA[i], str);
- if (p > _Right) then
- _Right := (P + Length(TSA[i]))
- else
- Exit;
- end;
- end;
- Match := Copy(Str, Left, (_Right - Left));
- Result := True;
- end;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement