Skip to content

Commit

Permalink
Rem_empty_seg
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbin committed Oct 15, 2024
1 parent 0b1e3f5 commit 655fcf4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
25 changes: 25 additions & 0 deletions lib/fpath_base/test/test__absolute_path.ml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,31 @@ let%expect_test "to_dir_path" =
()
;;

let%expect_test "rem_empty_seg" =
let test path =
let is_dir_path = Absolute_path.is_dir_path path in
let path2 = Absolute_path.rem_empty_seg path in
let is_dir_path2 = Absolute_path.is_dir_path path2 in
print_s
[%sexp
{ path : Absolute_path.t; is_dir_path : bool }
, { path2 : Absolute_path.t; is_dir_path2 : bool }]
in
test (Absolute_path.v "/tmp/my-dir/");
[%expect
{|
(((path /tmp/my-dir/) (is_dir_path true))
((path2 /tmp/my-dir) (is_dir_path2 false)))
|}];
test (Absolute_path.v "/tmp/my-file");
[%expect
{|
(((path /tmp/my-file) (is_dir_path false))
((path2 /tmp/my-file) (is_dir_path2 false)))
|}];
()
;;

let%expect_test "relativize" =
let v str = str |> Fpath.v in
let abs = Absolute_path.v in
Expand Down
25 changes: 25 additions & 0 deletions lib/fpath_base/test/test__relative_path.ml
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,31 @@ let%expect_test "to_dir_path" =
()
;;

let%expect_test "rem_empty_seg" =
let test path =
let is_dir_path = Relative_path.is_dir_path path in
let path2 = Relative_path.rem_empty_seg path in
let is_dir_path2 = Relative_path.is_dir_path path2 in
print_s
[%sexp
{ path : Relative_path.t; is_dir_path : bool }
, { path2 : Relative_path.t; is_dir_path2 : bool }]
in
test (Relative_path.v "tmp/my-dir/");
[%expect
{|
(((path tmp/my-dir/) (is_dir_path true))
((path2 tmp/my-dir) (is_dir_path2 false)))
|}];
test (Relative_path.v "tmp/my-file");
[%expect
{|
(((path tmp/my-file) (is_dir_path false))
((path2 tmp/my-file) (is_dir_path2 false)))
|}];
()
;;

let%expect_test "hashtbl" =
let t = Hashtbl.create (module Relative_path) in
Hashtbl.set t ~key:(Relative_path.v "path/to/my-file") ~data:42;
Expand Down
11 changes: 7 additions & 4 deletions lib/fpath_sexp0/src/fsegment.mli
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
(** Part of a file path.
A [Fpart.t] represents a segment of a file path, i.e., the parts of the
A [Fsegment.t] represents a segment of a file path, i.e., the parts of the
path that are separated by the directory separator character.
For example, in the file path ["/home/user/documents/file.txt"], the
segments are [["home" ; "user" ; "documents" ; "file.txt"]].
A valid file part cannot contain ['/'] or null characters.
A valid file segment cannot contain ['/'], the directory separator char or
null characters.
This module provides functions to convert between strings and file parts,
validate file parts, and some common file parts. *)
By contrast to Fpath's [seg], a [Fsegment.t] may not be empty.
This module provides functions to convert between strings and file segments,
validate segments, and some common file segments. *)

type t

Expand Down
8 changes: 6 additions & 2 deletions lib/fpath_sexp0/src/path.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ let parent t =
if Fpath.equal t t' then None else Some t'
;;

let empty_rel_path = Fpath.v ("." ^ Fpath.dir_sep)

let chop_prefix t ~prefix =
match Fpath.rem_prefix prefix t with
| Some t -> Some t
| None -> if Fpath.equal prefix t then Some Fpath.(v "./") else None
| None -> if Fpath.equal prefix t then Some empty_rel_path else None
;;

let chop_suffix ~empty t ~suffix =
Expand Down Expand Up @@ -65,6 +67,7 @@ module Absolute_path = struct
let chop_suffix t ~suffix = chop_suffix ~empty:root t ~suffix
let is_dir_path = Fpath.is_dir_path
let to_dir_path = Fpath.to_dir_path
let rem_empty_seg = Fpath.rem_empty_seg

let relativize ~root f =
let f = Fpath.normalize f in
Expand Down Expand Up @@ -99,7 +102,7 @@ module Relative_path = struct
| Error (`Msg m) -> invalid_arg m
;;

let empty = Fpath.v "./"
let empty = empty_rel_path
let append = append
let extend = extend
let parent = parent
Expand All @@ -108,6 +111,7 @@ module Relative_path = struct
let chop_suffix t ~suffix = chop_suffix ~empty t ~suffix
let is_dir_path = Fpath.is_dir_path
let to_dir_path = Fpath.to_dir_path
let rem_empty_seg = Fpath.rem_empty_seg
end

module Export = struct
Expand Down
2 changes: 2 additions & 0 deletions lib/fpath_sexp0/src/path.mli
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Absolute_path : sig
val chop_suffix : t -> suffix:relative_path -> t option
val is_dir_path : t -> bool
val to_dir_path : t -> t
val rem_empty_seg : t -> t

(** Converts a Path.t to an Absolute_path.t:
- If the path is already absolute, that's the answer.
Expand Down Expand Up @@ -77,6 +78,7 @@ module Relative_path : sig
val chop_suffix : t -> suffix:t -> t option
val is_dir_path : t -> bool
val to_dir_path : t -> t
val rem_empty_seg : t -> t
end

(** This module is re-exported as part of the [Fpath] module. For example:
Expand Down

0 comments on commit 655fcf4

Please sign in to comment.