Fix Path.descendent

It was broken for local paths with equal length
This commit is contained in:
Rudi Grinberg 2017-12-20 14:05:17 +07:00 committed by Jérémie Dimino
parent 9e4390ddc4
commit bbb6108924
2 changed files with 7 additions and 21 deletions

View File

@ -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

View File

@ -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 = <fun>
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"))