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

Split directories on platform separator and / #33

Merged
Merged
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: 2 additions & 2 deletions lib/namespaced.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type set = Set.t

let module_path_of_filename ?(nms=[]) filename =
let name = Unitname.modulize filename in
match List.rev (Support.split_on_char Filename.dir_sep.[0] filename) with
match List.rev (Support.split_on_dirs filename) with
| [] -> raise @@ Invalid_argument "Invalid name for a compilation unit"
| _ ->
{ namespace = nms ;
Expand All @@ -97,7 +97,7 @@ let module_path_of_filename ?(nms=[]) filename =

let filepath_of_filename ?(nms=[]) filename =
let name = Unitname.modulize filename in
match List.rev (Support.split_on_char Filename.dir_sep.[0] filename) with
match List.rev (Support.split_on_dirs filename) with
| [] -> raise @@ Invalid_argument "Invalid name for a compilation unit"
| _filename :: r ->
{ namespace = nms @ List.rev r ;
Expand Down
2 changes: 1 addition & 1 deletion lib/paths.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct
| a :: q -> a :: chop_extension q

let parse_filename name =
let l = Support.split_on_char (String.get (Filename.dir_sep) 0) name in
let l = Support.split_on_dirs name in
match List.rev l with
| "" :: q -> List.rev q
| l -> List.rev l
Expand Down
20 changes: 20 additions & 0 deletions lib/support.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ let split_on_char sep s =
let n = String.length s in
split [] n (n-1)

let split_on_dirs s =
let sep = Filename.dir_sep.[0] in
if sep = '/' then
split_on_char '/' s
else
let sub start stop =
String.sub s start (stop-start) in
let rec split l last pos =
if pos = 0 then
if s.[pos] = sep || s.[pos] = '/' then
"" :: sub (pos+1) last :: l
else
sub pos last :: l
else if s.[pos] = sep || s.[pos] = '/' then
split (sub (pos+1) last :: l) pos (pos-1)
else
split l last (pos-1) in
let n = String.length s in
split [] n (n-1)

let opt conv s = try Some(conv s) with Failure _ -> None

let filter_map f l =
Expand Down
9 changes: 9 additions & 0 deletions lib/support.mli
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ val remove_extension: string -> string
*)

val split_on_char: char -> string -> string list

val split_on_dirs: string -> string list
(** [split_on_dirs path] splits the file path [path] on the platform's
directory seperator and on forward slashes ([/]).

Forward slashes are always included as a split character because on
Windows the predominant means of accessing OCaml is through a Unix
emulation shell (Cygwin or MSYS2). *)

val opt: ('a -> 'b) -> 'a -> 'b option
val filter_map: ('a -> 'b option) -> 'a list -> 'b list

Expand Down
Loading