hare

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

commit a79fc48efa90b70394959db471c4082481568aac
parent a564b68f20ad6888610d403760ea33e2431412d3
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri,  7 Jan 2022 12:56:11 +0100

path::join: fix join("/") case

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mpath/join.ha | 17+++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/path/join.ha b/path/join.ha @@ -11,12 +11,21 @@ export fn join(paths: str...) str = { let sink = bufio::dynamic(io::mode::WRITE); let utf8 = true; for (let i = 0z; i < len(paths); i += 1) { - let buf = strings::toutf8(paths[i]); + const buf = strings::toutf8(paths[i]); + if (len(buf) == 0) { + continue; // Empty path component, discard + } else if (len(buf) == 1 && buf[0] == PATHSEP && i == 0) { + // "/" as the first component + io::write(&sink, [PATHSEP])!; + continue; + }; + + // Trim away trailing PATHSEPs, if present let l = len(buf); - if (l == 0) continue; for (l > 0 && buf[l - 1] == PATHSEP) { l -= 1; }; + for (let q = 0z; q < l) { let w = io::write(&sink, buf[q..l]) as size; q += w; @@ -62,4 +71,8 @@ export fn join(paths: str...) str = { let p = join("/", "foo", "bar", "baz"); defer free(p); assert(p == "/foo/bar/baz"); + + let i = join("/"); + defer free(i); + assert(i == "/"); };