Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Path.{basename,dirname} #648

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib_eio/path.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ let split (dir, p) =
in
Some ((dir, dirname), basename)

let basename t = Option.map snd (split t)

let dirname t = Option.map fst (split t)

let open_in ~sw t =
let (Resource.T (dir, ops), path) = t in
let module X = (val (Resource.get ops Fs.Pi.Dir)) in
Expand Down
36 changes: 36 additions & 0 deletions lib_eio/path.mli
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,42 @@ val split : 'a t -> ('a t * string) option
- [split (root, "/") = None]
*)

val basename : _ t -> string option
(** [basename t] returns the last path component in [t].

This is a convenience wrapper around {!split}. [basename t = None] if there
is nothing to split.

For example:

- [basename (root, "foo/bar") = Some "bar"]
- [basename (root, "/foo/bar") = Some "bar"]
- [basename (root, "/foo/bar/baz") = Some "baz"]
- [basename (root, "/foo/bar//baz/") = Some "baz"]
- [basename (root, "bar") = Some "bar"]
- [basename (root, ".") = Some "."]
- [basename (root, "") = None]
- [basename (root, "/") = None]
*)

val dirname : 'a t -> 'a t option
(** [dirname t] returns [Some dir], where [dir] is [t] without its last path component.

This is a convenience wrapper around {!split}. [dirname t = None] if there
is nothing to split.

For example:

- [dirname (root, "foo/bar") = Some (root, "foo")]
- [dirname (root, "/foo/bar") = Some (root, "/foo")]
- [dirname (root, "/foo/bar/baz") = Some (root, "/foo/bar")]
- [dirname (root, "/foo/bar//baz/") = Some (root, "/foo/bar")]
- [dirname (root, "bar") = Some (root, "")]
- [dirname (root, ".") = Some (root, "")]
- [dirname (root, "") = None]
- [dirname (root, "/") = None]
*)

(** {1 Reading files} *)

val load : _ t -> string
Expand Down
82 changes: 82 additions & 0 deletions tests/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,88 @@ let split path = Eio.Path.split (fake_dir, path) |> Option.map (fun ((_, dirname
- : (string * string) option = None
```

# Basename

```ocaml
let basename path = Eio.Path.basename (fake_dir, path)
```

```ocaml
# basename "foo/bar";
- : string option = Some "bar"

# basename "/foo/bar";
- : string option = Some "bar"

# basename "/foo/bar/baz";
- : string option = Some "baz"

# basename "/foo/bar//baz/";
- : string option = Some "baz"

# basename "bar";
- : string option = Some "bar"

# basename "/bar";
- : string option = Some "bar"

# basename ".";
- : string option = Some "."

# basename "./";
- : string option = Some "."

# basename "";
- : string option = None

# basename "/";
- : string option = None

# basename "///";
- : string option = None
```

# Dirname

```ocaml
let dirname path = Eio.Path.dirname (fake_dir, path) |> Option.map (fun (_, dirname) -> dirname)
```

```ocaml
# dirname "foo/bar";
- : string option = Some "foo"

# dirname "/foo/bar";
- : string option = Some "/foo"

# dirname "/foo/bar/baz";
- : string option = Some "/foo/bar"

# dirname "/foo/bar//baz/";
- : string option = Some "/foo/bar"

# dirname "bar";
- : string option = Some ""

# dirname "/bar";
- : string option = Some "/"

# dirname ".";
- : string option = Some ""

# dirname "./";
- : string option = Some ""

# dirname "";
- : string option = None

# dirname "/";
- : string option = None

# dirname "///";
- : string option = None
```

# Mkdirs

Recursively creating directories with `mkdirs`.
Expand Down
Loading