Skip to content

path

Build and take apart file paths. These functions only look at the text: they never touch the disk, so a path does not need to exist.

Paths use / between parts. To check whether a path exists or is a folder, use fs.exists and fs.is_dir.

import path
print(path.join("src", "main.brass"))
print(path.join("src/", "lib/util.brass"))
print(path.join("src", "/etc/hosts"))
src/main.brass
src/lib/util.brass
/etc/hosts
import path
let p = "docs/guide/intro.tar.gz"
print(path.parent(&p))
print(path.file_name(&p))
print(path.stem(&p))
print(path.extension(&p))
docs/guide
intro.tar.gz
intro.tar
gz
import path
print(path.normalize("a/./b/../c//d"))
print(path.normalize("/../etc"))
print(path.normalize("../x/.."))
a/c/d
/etc
..
import path
pub fn join(a: &Str, b: &Str) -> Str

Paths are text with / between parts; these functions only look at the text, not the disk.

When b starts with /, the result is b alone, as in most languages. Joining text from outside the program (a request path, an archive entry) can therefore point anywhere; use Untrusted.path_inside for that instead.

pub fn file_name(p: &Str) -> Str

The part after the last /: file_name("a/b.txt") is b.txt.

pub fn parent(p: &Str) -> Str

Everything before the last part: parent("a/b.txt") is a, parent("b.txt") is ``.

pub fn extension(p: &Str) -> Str

The file name’s ending after its last ., without the dot; `` if there is none.

A name that starts with a dot and has no other dot, like .bashrc, has no extension.

import path
print(path.extension(".bashrc").len())
print(path.extension("archive.tar.gz"))
0
gz
pub fn stem(p: &Str) -> Str

The file name without its extension: stem("a/b.tar.gz") is b.tar.

pub fn is_absolute(p: &Str) -> bool
pub fn normalize(p: &Str) -> Str

. parts are dropped and .. removes the part before it, without touching the disk.

Works on the text only, so a/link/.. becomes a even if link is a symbolic link to another folder. Leading .. parts of a relative path are kept; on an absolute path they are dropped, since /.. is /.