Path

Lexical Unix-style path helpers in std.path.

std.path provides lexical Unix-style path helpers for command-line tools. These functions operate on UTF-8 text only. They do not access the filesystem, resolve symlinks, check existence, normalize . or .., or inspect platform path state.

The current path surface is /-separated. Windows drive letters, backslash separators, UNC paths, and platform-specific normalization are not part of this contract yet.

Components

import std.io;
import std.path;

fn main() {
    let dir = path.dirname("src/main.zx");
    let base = path.basename("src/main.zx");

    io.println("dir={dir}");
    io.println("base={base}");

    let ext = path.extension("src/main.zx");
    if ext != null {
        io.println("ext={ext}");
    }
}

basename(path: str) -> str returns the last non-empty component after /. Trailing slashes are ignored except for the root path /, whose basename is /. The empty path has an empty basename.

dirname(path: str) -> str returns the lexical parent prefix before the final component. The empty path and paths without / return ""; root returns /.

extension(path: str) -> str? returns the suffix after the final . in the basename, without the dot. Hidden files such as .env, names without an extension, names ending in ., and paths ending in / return null.

The str results from component helpers are borrowed lexical views. When trailing separators require a trimmed copy, the implementation may use thread-local scratch storage. Consume or copy the result before the next std.path component call if it must be kept.

Joining

import std.io;
import std.path;

fn main() {
    let joined = try path.join("src", "main.zx");
    io.println("joined={joined.str()}");
}

join(a: str, b: str) throws(AllocError) -> String concatenates two lexical path fragments with one separator at the boundary:

  • join("", b) returns b.
  • join(a, "") returns a.
  • join("a", "b") returns a/b.
  • join("a/", "/b") returns a/b.

No other normalization is performed. Repeated separators away from the join boundary, . components, and .. components are preserved as text.

API Reference

SymbolSignatureNotes
basenamebasename(path: str) -> strReturns the final lexical component.
dirnamedirname(path: str) -> strReturns the lexical parent prefix.
extensionextension(path: str) -> str?Returns the final extension without . when present.
joinjoin(a: str, b: str) throws(AllocError) -> StringAllocates a joined UTF-8 path string.