Path
Path module: pure path manipulation (no I/O). All operations are pure string functions.
Functions
Panic if path contains any .. component (path traversal attempt). Call this before joining a user-supplied path segment into a filesystem path.
Examples
march> Path.assert_no_traversal("foo/bar")
()
march> Path.assert_no_traversal("../etc/passwd")
** panic: path traversal detected: ../etc/passwdReturn the last component of a path, or the path itself when there are no components (empty string, single segment with no /).
Examples
march> Path.basename("/usr/local/bin/march")
"march"
march> Path.basename("file.txt")
"file.txt"
march> Path.basename("/")
"/"Split a path into its non-empty /-separated components. Repeated separators and a leading / collapse to no extra entries.
Examples
march> Path.components("/usr/local/bin")
["usr", "local", "bin"]
march> Path.components("a//b/")
["a", "b"]
march> Path.components("")
[]Return the directory portion of a path: everything except the last component. Returns "/" for an absolute path with one component and "." for a relative path with one component, mirroring the POSIX convention.
Examples
march> Path.dirname("/usr/local/bin/march")
"/usr/local/bin"
march> Path.dirname("/etc")
"/"
march> Path.dirname("file.txt")
"."Return the file extension (without the leading dot), or the empty string if there is none. A leading . does not count as an extension (so .bashrc has no extension).
Examples
march> Path.extension("file.txt")
"txt"
march> Path.extension("archive.tar.gz")
"gz"
march> Path.extension(".bashrc")
""
march> Path.extension("README")
""Returns true if path starts with /.
Examples
march> Path.is_absolute("/etc")
true
march> Path.is_absolute("etc")
false
march> Path.is_absolute("")
falseJoin two path segments with a single separator, avoiding double slashes if a already ends with /.
Examples
march> Path.join("/usr", "local")
"/usr/local"
march> Path.join("/usr/", "local")
"/usr/local"
march> Path.join("a", "b")
"a/b"Join a list of path segments into a single path, inserting / separators. Respects a trailing / on any segment (avoids doubling). An empty list returns the empty string.
Examples
march> Path.join_all(["usr", "local", "bin"])
"usr/local/bin"
march> Path.join_all(["/var/store", "packages", "ab", "cd"])
"/var/store/packages/ab/cd"
march> Path.join_all([])
""Normalize a path by collapsing . (current dir) and .. (parent) segments. Absolute paths cannot ascend past root; relative paths preserve leading .. components. An empty input or a path that cancels out to nothing returns ".".
Examples
march> Path.normalize("/usr/local/../bin")
"/usr/bin"
march> Path.normalize("a/./b/../c")
"a/c"
march> Path.normalize("../foo/../bar")
"../bar"
march> Path.normalize("")
"."
march> Path.normalize("/..")
"/"Remove the final extension from a path, preserving the directory. Files with no extension (or only a leading dot) are returned unchanged.
Examples
march> Path.strip_extension("file.txt")
"file"
march> Path.strip_extension("/tmp/archive.tar.gz")
"/tmp/archive.tar"
march> Path.strip_extension("README")
"README"
march> Path.strip_extension(".bashrc")
".bashrc"Remove a single leading / from a path, if present. A path that does not start with / is returned unchanged. An empty path is returned unchanged.
Examples
march> Path.strip_leading_slash("/foo/bar")
"foo/bar"
march> Path.strip_leading_slash("foo/bar")
"foo/bar"
march> Path.strip_leading_slash("/")
""