Skip to content

Commit

Permalink
refactor: use [Parse_buffer] in glob parser
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <[email protected]>

<!-- ps-id: c1f899c5-66ff-4bd2-b2d3-01a565061ccf -->
  • Loading branch information
rgrinberg committed Apr 22, 2024
1 parent 244e9fa commit 59b90dd
Showing 1 changed file with 5 additions and 32 deletions.
37 changes: 5 additions & 32 deletions lib/glob.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,13 @@ type piece =
type t = piece list

let of_string ~double_asterisk s : t =
let i = ref 0 in
let l = String.length s in
let eos () = !i = l in
let read c =
let r = (not (eos ())) && s.[!i] = c in
if r then incr i;
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_notrace Exit
done;
i := !i + pattern_len;
true
with
| Exit -> false)
in
let buf = Parse_buffer.create s in
let eos () = Parse_buffer.eos buf in
let read c = Parse_buffer.accept buf c in
let char () =
ignore (read '\\' : bool);
if eos () then raise Parse_error;
let r = s.[!i] in
incr i;
r
Parse_buffer.get buf
in
let enclosed () : enclosed list =
let rec loop s =
Expand All @@ -93,7 +66,7 @@ let of_string ~double_asterisk s : t =
loop []
in
let piece () =
if double_asterisk && read_ahead "/**" && not (eos ())
if double_asterisk && Parse_buffer.accept_s buf "/**" && not (eos ())
then ManyMany
else if read '*'
then if double_asterisk && read '*' then ManyMany else Many
Expand Down

0 comments on commit 59b90dd

Please sign in to comment.