diff --git a/src/glob.ml b/src/glob.ml index 3eb6326..7bd75a8 100644 --- a/src/glob.ml +++ b/src/glob.ml @@ -159,15 +159,31 @@ let of_string ?(dir_sep = default_dir_sep) s = let cs = char_class_of_chars cs in (cs, idx) in - let components = normal [] 0 in - Ok { dir_sep; components } + match normal [] 0 with + | components -> Ok { dir_sep; components } + | exception Failure msg -> Error msg let to_string t = let buf = Buffer.create 16 in + let add_literal_string s = + String.iter + (fun c -> + match c with + | '*' | '?' | '[' -> + Buffer.add_char buf '['; + Buffer.add_char buf c; + Buffer.add_char buf ']' + | c when Char.equal c t.dir_sep -> + Buffer.add_char buf '['; + Buffer.add_char buf c; + Buffer.add_char buf ']' + | c -> Buffer.add_char buf c) + s + in List.iter (function | Dir_sep -> Buffer.add_char buf t.dir_sep - | String s -> Buffer.add_string buf s + | String s -> add_literal_string s | Question -> Buffer.add_char buf '?' | Star -> Buffer.add_char buf '*' | Range cs ->