Skip to content

Commit 1272182

Browse files
authored
Merge pull request #743 from talex5/win-fwd-slash
Eio.Path: always use "/" as separator
2 parents a21b507 + 32e501a commit 1272182

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

lib_eio/path.ml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
type 'a t = 'a Fs.dir * Fs.path
22

3+
(* Like [Filename.is_relative] but always using "/" as the separator. *)
4+
let is_relative = function
5+
| "" -> true
6+
| x -> x.[0] <> '/'
7+
8+
(* Like [Filename.concat] but always using "/" as the separator. *)
9+
let concat a b =
10+
let l = String.length a in
11+
if l = 0 || a.[l - 1] = '/' then a ^ b
12+
else a ^ "/" ^ b
13+
314
let ( / ) (dir, p1) p2 =
415
match p1, p2 with
5-
| p1, "" -> (dir, Filename.concat p1 p2)
6-
| _, p2 when not (Filename.is_relative p2) -> (dir, p2)
16+
| p1, "" -> (dir, concat p1 p2)
17+
| _, p2 when not (is_relative p2) -> (dir, p2)
718
| ".", p2 -> (dir, p2)
8-
| p1, p2 -> (dir, Filename.concat p1 p2)
19+
| p1, p2 -> (dir, concat p1 p2)
920

1021
let pp f (Resource.T (t, ops), p) =
1122
let module X = (val (Resource.get ops Fs.Pi.Dir)) in

tests/fs.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,3 +985,29 @@ Exception: Failure "Simulated error".
985985
+seek from end: 9
986986
- : unit = ()
987987
```
988+
989+
# Extending paths
990+
991+
```ocaml
992+
# run @@ fun env ->
993+
let base = fst env#cwd in
994+
List.iter (fun (a, b) -> traceln "%S / %S = %S" a b (snd ((base, a) / b))) [
995+
"foo", "bar";
996+
"foo/", "bar";
997+
"foo", "/bar";
998+
"foo", "";
999+
"foo/", "";
1000+
"", "";
1001+
"", "bar";
1002+
"/", "";
1003+
]
1004+
+"foo" / "bar" = "foo/bar"
1005+
+"foo/" / "bar" = "foo/bar"
1006+
+"foo" / "/bar" = "/bar"
1007+
+"foo" / "" = "foo/"
1008+
+"foo/" / "" = "foo/"
1009+
+"" / "" = ""
1010+
+"" / "bar" = "bar"
1011+
+"/" / "" = "/"
1012+
- : unit = ()
1013+
```

0 commit comments

Comments
 (0)