hare

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

commit daa85419c44df08f6c4c3f9ffdce9c632b3d275a
parent 6f1d95ee4b17d3d74dbb85936f5a9ddb3b43f4f0
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat,  8 Jan 2022 13:55:22 +0100

path: accept buffer for most path utilities

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

Diffstat:
Mpath/iter.ha | 7+------
Mpath/names.ha | 6++++--
Mpath/util.ha | 24+++++++++++++++++++++---
3 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/path/iter.ha b/path/iter.ha @@ -16,12 +16,7 @@ let pathsep: []u8 = [PATHSEP]; // Returns an iterator which yields each component of a path. If the path is // absolute, the first component will be the root path (e.g. "/"). export fn iter(path: (str | *buffer)) iterator = { - let path = match (path) { - case let s: str => - yield strings::toutf8(s); - case let b: *buffer => - yield b.cur; - }; + let path = getbytes(path); let flags = iflags::NONE; if (len(path) > 0 && path[0] == PATHSEP) { flags |= iflags::ABSOLUTE; diff --git a/path/names.ha b/path/names.ha @@ -7,7 +7,8 @@ use strings; // this returns the path to its parent directory. The return value is either // borrowed from the input or statically allocated), use [[dup]] to extend its // lifetime or modify it. -export fn dirname(path: str) const str = { +export fn dirname(path: (str | *buffer)) const str = { + let path = getstring(path); if (path == "") { return "."; }; @@ -53,7 +54,8 @@ export fn dirname(path: str) const str = { // returns the file name. For a path to a directory, this returns the directory // name. The return value is either borrowed from the input or statically // allocated, use [[dup]] to extend its lifetime or modify it. -export fn basename(path: str) const str = { +export fn basename(path: (str | *buffer)) const str = { + let path = getstring(path); if (path == "") { return "."; }; diff --git a/path/util.ha b/path/util.ha @@ -1,10 +1,28 @@ use strings; +fn getbytes(in: (str | *buffer)) []u8 = { + match (in) { + case let st: str => + return strings::toutf8(st); + case let buf: *buffer => + return buf.cur; + }; +}; + +fn getstring(in: (str | *buffer)) str = { + match (in) { + case let st: str => + return st; + case let buf: *buffer => + return string(buf); + }; +}; + // Returns true if a path is an absolute path. export fn abs(path: str) bool = { - let b = strings::toutf8(path); - if (len(b) == 0) { + let path = getbytes(path); + if (len(path) == 0) { return false; }; - return b[0] == PATHSEP; + return path[0] == PATHSEP; };