Skip to content

Commit

Permalink
Glob: match zero or more directories
Browse files Browse the repository at this point in the history
  • Loading branch information
eWert-Online committed Jul 7, 2021
1 parent 0ce5bf9 commit 690a002
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/glob.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,30 @@ let of_string ~double_asterisk s : t =
in

let piece () =
if read '*'
then if double_asterisk && read '*'
then ManyMany
else Many
if read '/' then
if read '*' then
if double_asterisk && read '*' then
if eos () then [ManyMany; Exactly('/')]
else [ManyMany]
else [Many; Exactly('/')]
else [Exactly('/')]
else if read '*'
then if double_asterisk && read '*'
then [ManyMany]
else [Many]
else if read '?'
then One
then [One]
else if not (read '[')
then Exactly (char ())
then [Exactly (char ())]
else if read '^' || read '!'
then Any_but (enclosed ())
else Any_of (enclosed ())
then [Any_but (enclosed ())]
else [Any_of (enclosed ())]
in

let rec loop pieces =
if eos ()
then List.rev pieces
else loop (piece () :: pieces)
else loop (piece () @ pieces)
in

loop []
Expand Down
10 changes: 10 additions & 0 deletions lib_test/test_glob.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ let _ =
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

0 comments on commit 690a002

Please sign in to comment.