cheatah
Module

os::path

os.path — the path-manipulation submodule.

Functions

fn bool exists(const std::string &p) source#

Path existence test.

Follows symlinks and is true for any existing entry — file, directory, or other; returns false for a missing path.

Parameters
p

the path.

Returns

true iff p exists.

Complexity

O(n) + a syscall.

Allocation

none.

Warning

The answer is a snapshot: the entry can be created or removed between this check and any subsequent use (TOCTOU) — do not rely on it as a security check.

Compile-run testOsCompileRun.PathExists
Performance361 ns/call in cheatah · 770 ns/call in CPython 3.12.3 · ≈2.1× faster
fn bool isfile(const std::string &p) source#

Regular-file test.

Returns false (rather than throwing) when p is missing or is a non-regular entry such as a directory; symlinks are followed to their target.

Parameters
p

the path.

Returns

true iff p is a regular file.

Complexity

O(n) + a syscall.

Allocation

none.

Compile-run testOsCompileRun.PathIsfile
Performance369 ns/call in cheatah · 826 ns/call in CPython 3.12.3 · ≈2.2× faster
fn bool isdir(const std::string &p) source#

Directory test.

Returns false (rather than throwing) when p is missing or is not a directory; symlinks are followed to their target.

Parameters
p

the path.

Returns

true iff p is a directory.

Complexity

O(n) + a syscall.

Allocation

none.

Compile-run testOsCompileRun.PathIsdir
System testStdlibE2E.Os
Performance368 ns/call in cheatah · 819 ns/call in CPython 3.12.3 · ≈2.2× faster
fn std::string basename(const std::string &p) source#

Final path component.

Returns the trailing filename component lexically, without touching the filesystem; a path ending in a separator (e.g. a/b/) yields an empty string, matching std::filesystem::path::filename.

Parameters
p

the path.

Returns

the basename (filename).

Complexity

O(n).

Allocation

allocates a path temporary and the result string.

Performance96.70 ns/call in cheatah · 224 ns/call in CPython 3.12.3 · ≈2.3× faster
fn std::string dirname(const std::string &p) source#

Parent path.

Returns everything before the final component lexically, without touching the filesystem; a bare filename with no separator (e.g. file.txt) yields an empty string, matching std::filesystem::path::parent_path.

Parameters
p

the path.

Returns

the directory portion of p.

Complexity

O(n).

Allocation

allocates a path temporary and the result string.

Compile-run testOsCompileRun.PathDirname
System testStdlibE2E.Os
Performance169 ns/call in cheatah · 316 ns/call in CPython 3.12.3 · ≈1.9× faster
fn std::string abspath(const std::string &p) source#

Absolute path.

Prepends the current working directory to a relative p; it does not collapse ./.. segments or resolve symlinks (combine with normpath for that), and p need not exist.

Parameters
p

the path.

Returns

p resolved against the cwd.

Complexity

O(n) + a syscall (reads the cwd).

Allocation

allocates the result string.

Compile-run testOsCompileRun.PathAbspath
System testStdlibE2E.Os
Performance167 ns/call in cheatah · 325 ns/call in CPython 3.12.3 · ≈1.9× faster
fn std::string normpath(const std::string &p) source#

Lexically normalized path (collapses current-dir and parent-dir segments).

Parameters
p

the path.

Returns

the normalized path.

Complexity

O(n) (purely lexical, no syscall).

Allocation

allocates a path temporary and the result string.

Performance332 ns/call in cheatah · 170 ns/call in CPython 3.12.3 · ≈2× slower
fn std::uintmax_t getsize(const std::string &p) source#

File size in bytes.

Defined only for regular files; querying a missing path, or a directory or other non-regular entry, throws rather than returning a sentinel.

Parameters
p

the file path.

Returns

p's size.

Complexity

O(1) + a syscall.

Allocation

none.

Compile-run testOsCompileRun.PathGetsize
Performance373 ns/call in cheatah · 796 ns/call in CPython 3.12.3 · ≈2.1× faster
fn std::pair< std::string, std::string > splitext(const std::string &p) source#

Split a path into root and extension.

Splits at the last dot of the final component so that concatenating the two results reproduces p; when there is no extension the whole path is the root and the extension is empty. The extension includes its leading dot, and a leading-dot name (e.g. .bashrc) is treated as having no extension.

Parameters
p

the path.

Returns

e.g. splitext("dir/file.purr") -> {"dir/file", ".purr"} (empty extension when none).

Complexity

O(n).

Allocation

allocates the two result strings and a path temporary.

System testStdlibE2E.Os
Performancereturns a (root, ext) pair — not reduced to one scalar here
fn std::string join(const std::string &first, const Parts &... rest) source#

Join path components with the platform separator.

Appends each component with path::operator/=, inserting a separator as needed; following std::filesystem rules, an absolute component discards everything joined before it. Purely lexical — the filesystem is not touched.

Parameters
first

the first component.

rest

any further string-constructible components.

Returns

e.g. join("a","b","c") -> "a/b/c".

Complexity

O(total length).

Allocation

allocates the result string and per-part path temporaries.

Compile-run testOsCompileRun.PathJoin
Performance137 ns/call in cheatah · 394 ns/call in CPython 3.12.3 · ≈2.9× faster