Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Glob: Make double asterisk match zero or more directories #192

Merged
merged 4 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Unreleased
* Add the `[:alpha:]` character class in `Re.Perl` (#169)
* Double asterisk (`**`) in `Re.Glob` (#172)
Like `*` but also match `/` characters when `pathname` is set.
* Double asterisk should match 0 or more directories unless in trailing
position. (#192, fixes #185)

1.9.0 (05-Apr-2019)
-------------------
Expand Down
24 changes: 23 additions & 1 deletion lib/glob.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ let of_string ~double_asterisk s : t =
r
in

(**
[read_ahead pattern] will attempt to read [pattern] and will return [true] if it was successful.
If it fails, it will return [false] and not increment the read index.
*)
let read_ahead pattern =
let pattern_len = String.length pattern in
(* if the pattern we are looking for exeeds the remaining length of s, return false immediately *)
if !i + pattern_len >= l then
false
else
try
for j = 0 to pattern_len - 1 do
let found = not (eos ()) && s.[!i + j] = pattern.[j] in
if not found then raise_no_trace Exit;
done;
i := !i + pattern_len;
true
with | Exit -> false
in

let char () =
ignore (read '\\' : bool);
if eos () then raise Parse_error;
Expand Down Expand Up @@ -75,7 +95,9 @@ let of_string ~double_asterisk s : t =
in

let piece () =
if read '*'
if double_asterisk && read_ahead "/**" && not (eos ())
then ManyMany
else if read '*'
then if double_asterisk && read '*'
then ManyMany
else Many
Expand Down
11 changes: 11 additions & 0 deletions lib_test/test_glob.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,20 @@ let _ =
assert (re_match (glob ~anchored "foo/**/bar") "foo/far/oof/bar");
assert (re_match (glob ~anchored "foo/**bar") "foo/far/oofbar");
assert (re_match (glob ~anchored "foo/**bar") "foo/bar");
assert (re_match (glob ~anchored "foo/**bar") "foo/foobar");
assert (re_match (glob ~anchored "/**") "//foo");
assert (re_match (glob ~anchored "**") "foo//bar");

assert (re_match (glob ~anchored "foo/bar/**/*.ml") "foo/bar/baz/foobar.ml");
assert (re_match (glob ~anchored "foo/bar/**/*.ml") "foo/bar/foobar.ml");

assert (re_match (glob ~anchored "foo/**/bar/**/baz") "foo/bar/baz");
assert (re_match (glob ~anchored "foo/**/bar/**/baz") "foo/bar/x/y/z/baz");
assert (re_match (glob ~anchored "foo/**/bar/**/baz") "foo/x/y/z/bar/baz");
assert (re_match (glob ~anchored "foo/**/bar/**/baz") "foo/bar/x/bar/x/baz");
assert (re_mismatch (glob ~anchored "foo/**/bar/**/baz") "foo/bar/../x/baz");
assert (re_mismatch (glob ~anchored "foo/**/bar/**/baz") "foo/bar/./x/baz");

(* Interaction with [~period] *)
let period = true in
assert (re_mismatch (glob ~anchored ~period "**") ".foobar");
Expand Down