Grav v1.14.0

Path

import "std/io" (included in import "std") · namespace std.io

String functions for file paths separated by /. They only work on the text: nothing here touches the file system.

grav
import "std"

fn main() {
    let p = "/home/ada/notes/today.txt"
    print(Path.basename(p))
    print(Path.dirname(p))
    print(Path.extension(p))
    print(Path.stem(p))
    print(Path.withExtension(p, ".md"))     // the dot is part of the extension
    print(Path.isAbsolute(p))
    print(Path.combine("logs", "2024", "app.log"))
    print(Path.join(["a", "b", "c.txt"]))
    print(Path.normalize("/a/./b/../c//d"))
}
Output
today.txt
/home/ada/notes
.txt
today
/home/ada/notes/today.md
true
logs/2024/app.log
a/b/c.txt
/a/c/d

API

class Path

Static members

  • static fn basename(path: string) -> string — "dir/file.txt" -> "file.txt"
  • static fn dirname(path: string) -> string — "dir/file.txt" -> "dir" ("." for a bare name, "/" for a root entry)
  • static fn extension(path: string) -> string — "file.tar.gz" -> ".gz" ("" when there is none; dot-files like ".bashrc" have none)
  • static fn stem(path: string) -> string — "dir/report.pdf" -> "report"
  • static fn withExtension(path: string, ext: string) -> string — "notes.txt" with ".md" -> "notes.md"
  • static fn isAbsolute(path: string) -> bool — Whether the path starts at the root ("/").
  • static fn join(parts: Array<string>) -> string — The parts joined with "/" (an absolute part starts over; empty parts are skipped).
  • static fn combine(a: string, b: string) -> string — a and b joined with "/".
  • static fn combine(a: string, b: string, c: string) -> string — a, b and c joined with "/".
  • static fn normalize(path: string) -> string — Resolves "." and ".." and repeated slashes: "a/./b/../c//d" -> "a/c/d".
  • static fn realPath(path: string) -> string — The absolute path with every symbolic link, "." and ".." resolved ("/etc/localtime" -> "/usr/share/zoneinfo/Africa/Nairobi"); "" when it does not exist.
  • static fn matches(name: string, pattern: string) -> bool — Whether name matches a glob pattern: * any text, ? one character, [abc] one of the listed characters: Path.matches("app.grav", "*.grav").

Edit this page on GitHub