hare

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

commit 7db2bed83d7233a9a95e9e272994932d618ab9b5
parent a5a086e570b3c77a83505f12fea0fbd2ece6c2ec
Author: Autumn! <autumnull@posteo.net>
Date:   Sun,  7 May 2023 01:43:25 +0000

path: implement peek()

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

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

diff --git a/path/stack.ha b/path/stack.ha @@ -60,6 +60,19 @@ export fn push(buf: *buffer, items: str...) (str | errors::overflow) = { free(s); }; +// Examine the final path segment in a buffer. +// Returns void if the path is empty or is the root dir. +export fn peek(buf: *const buffer) (str | void) = { + let trimmed = bytes::rtrim(buf.buf[..buf.end], PATHSEP); + if (len(trimmed) == 0) return void; + match (bytes::rindex(trimmed, PATHSEP)) { + case void => + return strings::fromutf8_unsafe(buf.buf[..buf.end]); + case let i: size => + return strings::fromutf8_unsafe(buf.buf[i+1..buf.end]); + }; +}; + // Remove and return the final path segment in a buffer. // Returns void if the path is empty or is the root dir. export fn pop(buf: *buffer) (str | void) = {