package b0
Install
    
    dune-project
 Dependency
Authors
Maintainers
Sources
sha512=00a6868b4dfa34565d0141b335622a81a0e8d5b9e3c6dfad025dabfa3df2db2a1302b492953bbbce30c3a4406c324fcec25250a00b38f6d18a69e15605e3b07e
    
    
  doc/b0.std/B0_std/Fpath/index.html
Module B0_std.Fpath
File paths.
A file system path specifies a file or a directory in a file system hierarchy. It is made of three parts:
- An optional, platform-dependent, volume.
 - An optional root directory separator 
dir_sepwhose presence distinguishes absolute paths ("/a") from relative ones ("a") - A non-empty list of 
dir_sepseparated segments. Segments are non empty strings except for maybe the last one. The latter syntactically distinguishes directory paths ("a/b/") from file paths ("a/b"). 
The paths segments "." and ".." are relative path segments that respectively denote the current and parent directory. The basename of a path is its last non-empty segment if it is not a relative path segment or the empty string otherwise (e.g. on "/" or "..").
Separators and segments
dir_sep_char is the platform dependent natural directory separator. This is / on POSIX and \ on Windows.
dir_sep is dir_sep_char as a string.
has_dir_sep s is true iff s contains dir_sep_char (on Windows also if it contains '/').
is_seg s is true iff s does not contain a dir_sep_char (on Windows also that it does not contain '/') or a null byte.
is_rel_seg s is true iff s is a relative segment in other words either "." or "..".
Paths
val v : string -> tv s is the string s as a path.
Warning. In code only use "/" as the directory separator even on Windows platforms (don't be upset, the module gives them back to you with backslashes).
add_seg p seg if p's last segment is non-empty this is p with seg added. If p's last segment is empty, this is p with the empty segment replaced by seg.
Famous file paths
val null : tnull represents a file on the OS that discards all writes and returns end of file on reads.
val dash : tdash is "-". This value is used in cli interface to respectively denote standard input and output.
Directory paths
Note. The following functions use syntactic semantic properties of paths. Given a path, these properties can be different from the ones your file system attributes to it.
val is_dir_path : t -> boolis_dir_path p is true iff p syntactically represents a directory. This means that p is ., .. or ends with /, /. or /...
strip_dir_sep p is p without an existing last empty segment when p is not a root path, ensuring the result has no trailing dir_sep when converted to a string.
Basename and parent directory
Note. The following functions use syntactic semantic properties of paths. Given a path, these properties can be different from the ones your file system attributes to it.
val basename : ?no_ext:bool -> t -> stringbasename p is the last non-empty segment of p or the empty string otherwise. The latter occurs only on root paths and on paths whose last non-empty segment is a relative segment. If no_ext is true (default to false) the basename's multiple extension, if any, is removed from the result.
parent p is a directory path that contains p. If p is a root path this is p itself. If p is in the current directory this is ./.
Strict prefixes and roots
is_prefix prefix p is true iff prefix is a strict prefix of p that respects path segments. More formally iff the following two conditions hold:
not Fpath.(equal (to_dir_path prefix) (to_dir_path p))Fpath.(String.is_prefix (to_string (to_dir_path prefix) (to_string p)))istrue
Warning. By definition is_prefix p p is false. Note also that the prefix relation does not entail directory containement; for example is_prefix (v "..")  (v "../..") holds.
strip_prefix prefix p is:
Noneifis_prefixprefix pisfalse.Some qotherwise whereqispwithout the string prefixFpath.to_dir_path prefix. This means thatqis always relative, that it preservesp's directoryness and thatFpath.(equal (prefix // q) p)holds.
Warning. By definition strip_prefix p p is None.
drop_prefixed ps is ps without elements that have a strict prefixes in ps. The list order is preserved. Duplicates are not removed use uniquify for this.
reroot ~root ~dst p assumes root prefixes p removes the prefix and prepends dst to the result.
relative ~to_dir p is q such that to_dir // q represents the same path as p. Note that q is not necessarily relative: if to_dir is relative and p is absolute p is returned.
Warning. This function is mostly broken at the moment.
Predicates and comparison
val is_rel : t -> boolis_rel p is true iff p is a relative path, i.e. the root directory separator is missing in p.
val is_abs : t -> boolis_abs p is true iff p is an absolute path, i.e. the root directory separator is present in p.
val is_root : t -> boolis_root p is true iff p is a root directory, i.e. p has the root directory separator and a single, empty, segment.
val is_current_dir : t -> boolis_current_dir p is true iff p is either "." or "./".
val is_parent_dir : t -> boolis_parent_dir p is true iff p is either ".." or "../".
equal_basename p0 p1 is String.equal (basename p0) (basename p1).
File extensions
The file extension (resp. multiple file extension) of a path segment is the suffix that starts at the last (resp. first) occurence of a '.' that is preceeded by at least one non '.' character. If there is no such occurence in the segment, the extension is empty. With these definitions, ".", "..", "..." and dot files like ".ocamlinit" or "..ocamlinit" have no extension, but ".emacs.d" and "..emacs.d" do have one.
get_ext p is p's basename file extension or the empty string if there is no extension. If multi is true (defaults to false), returns the multiple file extension.
has_ext ext p is true iff String.equal (get_ext p) e || String.equal (get_ext ~multi:true p) e.
strip_ext ?multi p is p with the extension of p's basename removed. If multi is true (defaults to false), the multiple file extension is removed.
cut_ext ?multi p is (strip_ext ?multi p, get_ext ?multi p).
Converting
of_string s is the string s as a path. The following transformations are performed on the string:
- On Windows any / (
0x2F) occurence is converted to \ (0x5C) - Non initial empty segments are suppressed; "a//b" becomes "a/b", "//a////b//" becomes "//a/b/", etc
 
An error returned if s is "" or if it contains a null byte. The error string mentions s.
val to_string : t -> stringto_string p is the path p as a string. The result can be safely converted back with v.
val to_uri_path : ?escape_space:bool -> t -> stringto_uri_path p is the path p as an URI path. This is p with the system specific dir_sep_char directory separator replaced by '/' and with the following characters percent encoded: '%', '?', '#', ' ' (unless escape_space is false, defaults to true), and the US-ASCII control characters.
Note. In 2019, the standard definition of URIs is in a sorry state. Assuming p is UTF-8 encoded. It is believed the above function should lead to an URI path component that can be parsed by HTML5's definition of URI parsing.
Formatting
pp ppf p prints path p on ppf. The path is quoted with Filename.quote if needed. For now this means if it contains spaces (U+0020).
pp_quoted ppf p prints path p on ppf using Filename.quote.
Uniqueness
Paths map and sets
type path = tmodule Set : sig ... endPath sets.
module Map : sig ... endPath maps.
Sorts
sort_by_parent ps maps elements of ps by their Fpath.parent.
val sort_by_ext : ?multi:bool -> Set.t -> Set.t String.Map.tsort_by_ext ~multi ps maps elements of ps by their extension as determined by Fpath.get_ext ~multi.
Search paths
A search path is a list of paths separated by a designated separator. A well known search path is PATH in which executable binaries are looked up.
search_path_sep is the default platform specific separator for search paths, this is ";" if Sys.win32 is true and ":" otherwise.
list_of_search_path ~sep s parses sep separated file paths from s. sep is not allowed to appear in the file paths, it defaults to search_path_sep. The order in the list matches the order from left to right in s.