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.
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.
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)returnsb.join(a, "")returnsa.join("a", "b")returnsa/b.join("a/", "/b")returnsa/b.
No other normalization is performed. Repeated separators away from the join
boundary, . components, and .. components are preserved as text.
| Symbol | Signature | Notes |
|---|---|---|
basename | basename(path: str) -> str | Returns the final lexical component. |
dirname | dirname(path: str) -> str | Returns the lexical parent prefix. |
extension | extension(path: str) -> str? | Returns the final extension without . when present. |
join | join(a: str, b: str) throws(AllocError) -> String | Allocates a joined UTF-8 path string. |