std.path

Lexical path joining and inspection.

Importuse std.path

Functions

fn join(parts: [string]) -> string#

Naively joins path fragments, inserting at most one native separator at each boundary and ignoring empty fragments. . and .. are not resolved.

  • parts: Fragments to join, in order; empty fragments are ignored.
println(std.path.join(["tour", "examples", "lesson.mn"])) // tour/examples/lesson.mn
println(std.path.join(["a/", "/b", ""]))                  // a/b
println(std.path.join2("src", "main.mn"))                 // src/main.mn
fn join2(a: string, b: string) -> string#

Two-fragment convenience form of join.

  • a: Leading fragment.
  • b: Trailing fragment.
fn basename(path: string) -> string#

Last path component, excluding trailing separators.

  • path: Path to take the last component of.
println(std.path.basename("tour/examples/lesson.mn")) // lesson.mn
println(std.path.basename("tour/examples/"))          // examples
fn dirname(path: string) -> string#

Path with its final component removed, or "" when there is no directory component (and for a root path).

  • path: Path to strip the last component from.
println(std.path.dirname("tour/examples/lesson.mn")) // tour/examples
println(std.path.dirname("/lesson.mn"))              // /
println(std.path.dirname("lesson.mn") == "")         // true
fn extension(path: string) -> string#

Final filename suffix beginning at its last non-leading dot, or "".

  • path: Path whose filename suffix is returned.
println(std.path.extension("archive.tar.gz")) // .gz
println(std.path.extension(".bashrc") == "")  // true — a leading dot is no suffix
fn stem(path: string) -> string#

Last component without its final extension. A leading-dot name is its own stem.

  • path: Path whose filename without extension is returned.
println(std.path.stem("archive.tar.gz")) // archive.tar
println(std.path.stem(".bashrc"))        // .bashrc
fn is_absolute(path: string) -> bool#

Whether path has an absolute native root.

  • path: Path to test.
println(std.path.is_absolute("/etc/hosts")) // true
println(std.path.is_absolute("etc/hosts"))  // false
fn normalize(path: string) -> string#

Lexically resolves duplicate separators plus . and .. in one path. Relative paths stay relative; traversal above an absolute root is ignored.

  • path: Path to resolve lexically, without touching the filesystem.
println(std.path.normalize("a//b/./c/../d")) // a/b/d
println(std.path.normalize("../x"))          // ../x
println(std.path.normalize("/a/../../b"))    // /b
fn separator() -> string#

The native path separator (/ on POSIX, \\ on Windows).

println(std.path.separator()) // / on POSIX, \ on Windows
fn to_posix(path: string) -> string#

Replaces every backslash with /.

  • path: Path whose backslashes become forward slashes.
println(std.path.to_posix("dir\\file.txt")) // dir/file.txt
fn to_native(path: string) -> string#

Converts / to the native separator on Windows; POSIX strings are already native and are returned unchanged.

  • path: Path whose forward slashes become the native separator.
let native = std.path.to_native("assets/img/logo.png")
// assets\img\logo.png on Windows; unchanged on POSIX