hare

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

commit 3186faa225d926d17b369afc7e648d94ed8c2d77
parent 6e56868c2a71bd7aece81349f3fe5ec4271dc61c
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 27 Feb 2021 10:04:04 -0500

path: fix non-rooted cases in names

Diffstat:
Mpath/names.ha | 21+++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/path/names.ha b/path/names.ha @@ -8,7 +8,10 @@ use io; // from the input, use [dup] to extend its lifetime. export fn dirname(path: path::path) path::path = { let b = pathbytes(normalize(path)); - let i = bytes::rindex(b, PATHSEP) as size; + let i = match (bytes::rindex(b, PATHSEP)) { + void => return path, + z: size => z, + }; if (i == 0) { b = [PATHSEP]; i += 1; @@ -25,6 +28,10 @@ export fn dirname(path: path::path) path::path = { assert(dirname(p) as str == "/"); let p: path = "/"; assert(dirname(p) as str == "/"); + let p: path = "foo/bar"; + assert(dirname(p) as str == "foo"); + let p: path = "foo"; + assert(dirname(p) as str == "foo"); }; // Returns the final component of a given path. For a path to a file name, this @@ -36,7 +43,10 @@ export fn basename(path: path::path) path::path = { let i = if (len(b) == 1) { b = [PATHSEP]; 0z; - } else bytes::rindex(b, PATHSEP) as size + 1; + } else match (bytes::rindex(b, PATHSEP)) { + void => return path, + z: size => z + 1, + }; return if (path is str) strings::from_utf8_unsafe(b[i..]) else b[i..]; @@ -49,4 +59,11 @@ export fn basename(path: path::path) path::path = { assert(basename(p) as str == "foo"); let p: path = "/"; assert(basename(p) as str == "/"); + let p: path = "foo/bar"; + assert(basename(p) as str == "bar"); + let p: path = "foo"; + assert(basename(p) as str == "foo"); }; + +// Returns true if the given path is a root directory. +export fn is_root(path: path::path) bool = len(pathbytes(path)) == 1;