hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 6a5dbe35b0ea90dbb6edeba87ac852267b0edb1e
parent d2e78b43d5e05701603e8635cbd091cc2450ce5c
Author: Autumn! <autumnull@posteo.net>
Date:   Sun,  7 May 2023 01:43:27 +0000

path: implement parent()

Signed-off-by: Autumn! <autumnull@posteo.net>

Diffstat:
Mpath/stack.ha | 10++++++++++
1 file changed, 10 insertions(+), 0 deletions(-)

diff --git a/path/stack.ha b/path/stack.ha @@ -182,3 +182,13 @@ export fn join(items: str...) str = { static let buf = buffer { ... }; return strings::dup(set(&buf, items...)!); }; + +// Returns the parent directory for a given path, without modifying the buffer. +// If the path is the root directory, the root directory is returned. The value +// is either borrowed from the input or statically allocated; use +// [[strings::dup]] to extend its lifetime or modify it. +export fn parent(buf: *const buffer) (str | errors::overflow) = { + const newend = appendnorm(buf, dotdot)?; + if (newend == 0) return "."; + return strings::fromutf8_unsafe(buf.buf[..newend]); +};