diff --git a/src/path.ml b/src/path.ml index 996fd267..81d56ed6 100644 --- a/src/path.ml +++ b/src/path.ml @@ -188,8 +188,9 @@ module Local = struct | _ -> let of_len = String.length of_ in let t_len = String.length t in - if (t_len = of_len && t = of_) || - (t_len >= of_len && t.[of_len] = '/' && String.is_prefix t ~prefix:of_) then + if t_len = of_len then + Option.some_if (t = of_) t + else if (t_len >= of_len && t.[of_len] = '/' && String.is_prefix t ~prefix:of_) then Some (String.sub t ~pos:(of_len + 1) ~len:(t_len - of_len - 1)) else None diff --git a/test/unit-tests/path.mlt b/test/unit-tests/path.mlt index f690221c..eac4c413 100644 --- a/test/unit-tests/path.mlt +++ b/test/unit-tests/path.mlt @@ -1,5 +1,4 @@ (* -*- tuareg -*- *) - open Jbuilder;; open Import;; @@ -10,37 +9,23 @@ let r = Path.(relative root);; Path.(let p = relative root "foo" in descendant p ~of_:p) [%%expect{| val r : string -> Jbuilder.Path.t = -Exception: Invalid_argument "String.sub / Bytes.sub". -Raised at file "pervasives.ml", line 33, characters 25-45 -Called from file "string.ml", line 47, characters 2-23 -Called from file "src/path.ml", line 193, characters 13-71 -Called from file "toplevel/toploop.ml", line 180, characters 17-56 +- : Jbuilder.Path.t option = Some foo |}] (* different strings but same length *) Path.(descendant (relative root "foo") ~of_:(relative root "bar")) [%%expect{| -Exception: Invalid_argument "index out of bounds". -Raised by primitive operation at file "src/path.ml", line 192, characters 29-39 -Called from file "toplevel/toploop.ml", line 180, characters 17-56 +- : Jbuilder.Path.t option = None |}] Path.(descendant (r "foo") ~of_:(r "foo/")) [%%expect{| -Exception: Invalid_argument "String.sub / Bytes.sub". -Raised at file "pervasives.ml", line 33, characters 25-45 -Called from file "string.ml", line 47, characters 2-23 -Called from file "src/path.ml", line 193, characters 13-71 -Called from file "toplevel/toploop.ml", line 180, characters 17-56 +- : Jbuilder.Path.t option = Some foo |}] Path.(descendant (r "foo/") ~of_:(r "foo")) [%%expect{| -Exception: Invalid_argument "String.sub / Bytes.sub". -Raised at file "pervasives.ml", line 33, characters 25-45 -Called from file "string.ml", line 47, characters 2-23 -Called from file "src/path.ml", line 193, characters 13-71 -Called from file "toplevel/toploop.ml", line 180, characters 17-56 +- : Jbuilder.Path.t option = Some foo |}] Path.(descendant (r "foo/bar") ~of_:(r "foo"))