Skip to content

Commit

Permalink
Eio.Path: always use "/" as separator
Browse files Browse the repository at this point in the history
path.mli says:

> In Eio, the directory separator is always "/", even on Windows.

However, `Path.(/)` used `Filename.concat` to create paths, which uses
the native separator ("\" on Windows).
  • Loading branch information
talex5 committed Jun 19, 2024
1 parent a21b507 commit 32e501a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib_eio/path.ml
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
type 'a t = 'a Fs.dir * Fs.path

(* Like [Filename.is_relative] but always using "/" as the separator. *)
let is_relative = function
| "" -> true
| x -> x.[0] <> '/'

(* Like [Filename.concat] but always using "/" as the separator. *)
let concat a b =
let l = String.length a in
if l = 0 || a.[l - 1] = '/' then a ^ b
else a ^ "/" ^ b

let ( / ) (dir, p1) p2 =
match p1, p2 with
| p1, "" -> (dir, Filename.concat p1 p2)
| _, p2 when not (Filename.is_relative p2) -> (dir, p2)
| p1, "" -> (dir, concat p1 p2)
| _, p2 when not (is_relative p2) -> (dir, p2)
| ".", p2 -> (dir, p2)
| p1, p2 -> (dir, Filename.concat p1 p2)
| p1, p2 -> (dir, concat p1 p2)

let pp f (Resource.T (t, ops), p) =
let module X = (val (Resource.get ops Fs.Pi.Dir)) in
Expand Down
26 changes: 26 additions & 0 deletions tests/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -985,3 +985,29 @@ Exception: Failure "Simulated error".
+seek from end: 9
- : unit = ()
```

# Extending paths

```ocaml
# run @@ fun env ->
let base = fst env#cwd in
List.iter (fun (a, b) -> traceln "%S / %S = %S" a b (snd ((base, a) / b))) [
"foo", "bar";
"foo/", "bar";
"foo", "/bar";
"foo", "";
"foo/", "";
"", "";
"", "bar";
"/", "";
]
+"foo" / "bar" = "foo/bar"
+"foo/" / "bar" = "foo/bar"
+"foo" / "/bar" = "/bar"
+"foo" / "" = "foo/"
+"foo/" / "" = "foo/"
+"" / "" = ""
+"" / "bar" = "bar"
+"/" / "" = "/"
- : unit = ()
```

0 comments on commit 32e501a

Please sign in to comment.