hare

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

commit 98d0f573a8cf5d63546d03114167d1f21660a90b
parent 04da54aa69972d48e898bae3d4c48b38641bd44e
Author: Autumn! <autumnull@posteo.net>
Date:   Sun,  7 May 2023 01:43:23 +0000

path: rename add() to push()

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

Diffstat:
Mcmd/hare/schedule.ha | 2+-
Mdirs/xdg.ha | 2+-
Mfs/util.ha | 8++++----
Mhare/module/context.ha | 2+-
Mpath/README | 2+-
Mpath/buffer.ha | 8++++----
Dpath/join.ha | 69---------------------------------------------------------------------
Apath/stack.ha | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpath/util.ha | 7++-----
Mscripts/gen-stdlib | 2+-
Mstdlib.mk | 4++--
11 files changed, 86 insertions(+), 89 deletions(-)

diff --git a/cmd/hare/schedule.ha b/cmd/hare/schedule.ha @@ -303,7 +303,7 @@ fn sched_hare_object( defer free(td); let buf = path::init(plan.context.cache)!; - path::add(&buf, namespace...)!; + path::push(&buf, namespace...)!; const path = path::string(&buf); match (os::mkdirs(path, 0o755)) { case void => void; diff --git a/dirs/xdg.ha b/dirs/xdg.ha @@ -17,7 +17,7 @@ fn lookup(prog: str, var: str, default: str) str = { path::set(&buf)!; match (os::getenv(var)) { case let s: str => - const path = path::add(&buf, s, prog)!; + const path = path::push(&buf, s, prog)!; if (!path::abs(path)) { yield; }; diff --git a/fs/util.ha b/fs/util.ha @@ -124,7 +124,7 @@ fn rmdirall_path(fs: *fs, buf: *path::buffer) (void | error) = { if (ent.name == "." || ent.name == "..") { continue; }; - path = path::add(buf, ent.name)!; + path = path::push(buf, ent.name)!; switch (ent.ftype & mode::DIR) { case mode::DIR => @@ -132,7 +132,7 @@ fn rmdirall_path(fs: *fs, buf: *path::buffer) (void | error) = { case => remove(fs, path)?; }; - path = path::add(buf, "..")!; + path = path::push(buf, "..")!; case void => break; }; @@ -157,7 +157,7 @@ export fn realpath(fs: *fs, path: str) (str | error) = { break; }; - const item = path::add(&buf, item)!; + const item = path::push(&buf, item)!; const link = match (readlink(fs, item)) { case let link: str => yield link; @@ -168,7 +168,7 @@ export fn realpath(fs: *fs, path: str) (str | error) = { }; if (!path::abs(link)) { - path::add(&buf, "..", link)!; + path::push(&buf, "..", link)!; } else { path::set(&buf, link)!; }; diff --git a/hare/module/context.ha b/hare/module/context.ha @@ -95,7 +95,7 @@ export fn identpath(name: ast::ident) str = { }; let buf = path::init()!; for (let i = 0z; i < len(name); i += 1) { - path::add(&buf, name[i])!; + path::push(&buf, name[i])!; }; return strings::dup(path::string(&buf)); }; diff --git a/path/README b/path/README @@ -14,7 +14,7 @@ normalized, which is to say that it will not include any of the following: Assuming that PATHSEP is '/', "/usr//bin/../bin/./hare" becomes "/usr/bin/hare" and "../../foo/bar" is unchanged. The path will only end in -a slash if the last item which was added ended in a slash, like so: +a slash if the last item which was pushed ended in a slash, like so: let buf = path::init("foo", "bar")!; assert(path::string(&buf) == "foo/bar"); diff --git a/path/buffer.ha b/path/buffer.ha @@ -13,7 +13,7 @@ export type buffer = struct { // Initializes a new path buffer. export fn init(items: str...) (buffer | errors::overflow) = { let buf = buffer { ... }; - add(&buf, items...)?; + push(&buf, items...)?; return buf; }; @@ -21,7 +21,7 @@ export fn init(items: str...) (buffer | errors::overflow) = { // previous value. Returns the new string value of the path. export fn set(buf: *buffer, items: str...) (str | errors::overflow) = { buf.end = 0; - return add(buf, items...); + return push(buf, items...); }; // Returns the current path stored in this buffer. @@ -89,7 +89,7 @@ fn parent(buf: *buffer) (void | errors::overflow) = { // XXX: This is not super efficient const name = dirname(string(buf)); buf.end = 0; - add(buf, name)?; + push(buf, name)?; }; fn doappend(buf: *buffer, elem: []u8) (void | errors::overflow) = { @@ -189,7 +189,7 @@ fn doappend(buf: *buffer, elem: []u8) (void | errors::overflow) = { assert(string(&buf) == s); free(s); s = strings::concat(pathsepstr, "baz"); - add(&buf, s)!; + push(&buf, s)!; free(s); s = strings::join(pathsepstr, "foo", "bar", "baz"); assert(string(&buf) == s); diff --git a/path/join.ha b/path/join.ha @@ -1,69 +0,0 @@ -// License: MPL-2.0 -// (c) 2021-2022 Drew DeVault <sir@cmpwn.com> -use bytes; -use errors; -use strings; - -// Joins several path elements together and copies them into a path buffer. -// Returns the new string value of the path. -export fn add(buf: *buffer, items: str...) (str | errors::overflow) = { - for (let i = 0z; i < len(items); i += 1) { - const elem = strings::toutf8(items[i]); - const tok = bytes::tokenize(elem, pathsep); - for (let j = 0z; true; j += 1) { - const next = match (bytes::next_token(&tok)) { - case let tok: []u8 => - yield tok; - case void => - break; - }; - if (len(next) == 0 && j == 0) { - // Handles the add("/foo") case as - // add("/", "foo"); - appendnorm(buf, pathsepstr)?; - }; - appendnorm(buf, next)?; - }; - }; - return string(buf); -}; - -@test fn add() void = { - let buf = init()!; - add(&buf, "foo", "bar", "baz")!; - let s = strings::join(pathsepstr, "foo", "bar", "baz"); - assert(string(&buf) == s); - free(s); - - buf.end = 0; - s = strings::join(pathsepstr, "", "foo", "bar"); - add(&buf, s, "baz")!; - free(s); - s = strings::join(pathsepstr, "", "foo", "bar", "baz"); - assert(string(&buf) == s); - free(s); - - buf.end = 0; - s = strings::join(pathsepstr, "foo", "bar"); - add(&buf, pathsepstr, s, "baz")!; - free(s); - s = strings::join(pathsepstr, "", "foo", "bar", "baz"); - assert(string(&buf) == s); - free(s); - - buf.end = 0; - s = strings::join(pathsepstr, ".", "foo", "bar"); - add(&buf, s)!; - free(s); - s = strings::join(pathsepstr, "foo", "bar"); - assert(string(&buf) == s); - free(s); -}; - -// Joins a list of path components together, normalizes it, and returns the -// resulting string. The caller must free the return value. If the resulting -// path would exceed [[PATH_MAX]], the program aborts. -export fn join(items: str...) str = { - static let buf = buffer { ... }; - return strings::dup(set(&buf, items...)!); -}; diff --git a/path/stack.ha b/path/stack.ha @@ -0,0 +1,69 @@ +// License: MPL-2.0 +// (c) 2021-2022 Drew DeVault <sir@cmpwn.com> +use bytes; +use errors; +use strings; + +// Joins path elements onto the end of a path buffer. +// Returns the new string value of the path. +export fn push(buf: *buffer, items: str...) (str | errors::overflow) = { + for (let i = 0z; i < len(items); i += 1) { + const elem = strings::toutf8(items[i]); + const tok = bytes::tokenize(elem, pathsep); + for (let j = 0z; true; j += 1) { + const next = match (bytes::next_token(&tok)) { + case let tok: []u8 => + yield tok; + case void => + break; + }; + if (len(next) == 0 && j == 0) { + // Handles the push("/foo") case as + // push("/", "foo"); + appendnorm(buf, pathsepstr)?; + }; + appendnorm(buf, next)?; + }; + }; + return string(buf); +}; + +@test fn push() void = { + let buf = init()!; + push(&buf, "foo", "bar", "baz")!; + let s = strings::join(pathsepstr, "foo", "bar", "baz"); + assert(string(&buf) == s); + free(s); + + buf.end = 0; + s = strings::join(pathsepstr, "", "foo", "bar"); + push(&buf, s, "baz")!; + free(s); + s = strings::join(pathsepstr, "", "foo", "bar", "baz"); + assert(string(&buf) == s); + free(s); + + buf.end = 0; + s = strings::join(pathsepstr, "foo", "bar"); + push(&buf, pathsepstr, s, "baz")!; + free(s); + s = strings::join(pathsepstr, "", "foo", "bar", "baz"); + assert(string(&buf) == s); + free(s); + + buf.end = 0; + s = strings::join(pathsepstr, ".", "foo", "bar"); + push(&buf, s)!; + free(s); + s = strings::join(pathsepstr, "foo", "bar"); + assert(string(&buf) == s); + free(s); +}; + +// Joins a list of path components together, normalizes it, and returns the +// resulting string. The caller must free the return value. If the resulting +// path would exceed [[PATH_MAX]], the program aborts. +export fn join(items: str...) str = { + static let buf = buffer { ... }; + return strings::dup(set(&buf, items...)!); +}; diff --git a/path/util.ha b/path/util.ha @@ -21,10 +21,7 @@ fn getstring(in: (str | *buffer)) str = { }; // Returns true if a path is an absolute path. -export fn abs(path: str) bool = { +export fn abs(path: (*buffer | str)) bool = { let path = getbytes(path); - if (len(path) == 0) { - return false; - }; - return path[0] == PATHSEP; + return 0 < len(path) && path[0] == PATHSEP; }; diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -1204,7 +1204,7 @@ path() { '+$(PLATFORM).ha' \ buffer.ha \ util.ha \ - join.ha \ + stack.ha \ names.ha \ iter.ha gen_ssa path strings bytes errors diff --git a/stdlib.mk b/stdlib.mk @@ -1837,7 +1837,7 @@ stdlib_path_any_srcs = \ $(STDLIB)/path/+$(PLATFORM).ha \ $(STDLIB)/path/buffer.ha \ $(STDLIB)/path/util.ha \ - $(STDLIB)/path/join.ha \ + $(STDLIB)/path/stack.ha \ $(STDLIB)/path/names.ha \ $(STDLIB)/path/iter.ha @@ -4111,7 +4111,7 @@ testlib_path_any_srcs = \ $(STDLIB)/path/+$(PLATFORM).ha \ $(STDLIB)/path/buffer.ha \ $(STDLIB)/path/util.ha \ - $(STDLIB)/path/join.ha \ + $(STDLIB)/path/stack.ha \ $(STDLIB)/path/names.ha \ $(STDLIB)/path/iter.ha