hare

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

commit fb35c93aaeb8815bf8f821d372de741863ca3344
parent 12735a62d1180c2c611d6f294b97959a4659a604
Author: Sebastian <sebastian@sebsite.pw>
Date:   Fri, 25 Aug 2023 02:48:32 -0400

path: remove PATH prefix from constants

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mpath/+freebsd.ha | 6+++---
Mpath/+linux.ha | 6+++---
Mpath/README | 16++++++++--------
Mpath/buffer.ha | 12++++++------
Mpath/ext_stack.ha | 6+++---
Mpath/iter.ha | 6++----
Mpath/posix.ha | 16++++++++--------
Mpath/prefix.ha | 8++++----
Mpath/stack.ha | 18+++++++++---------
9 files changed, 46 insertions(+), 48 deletions(-)

diff --git a/path/+freebsd.ha b/path/+freebsd.ha @@ -2,9 +2,9 @@ // (c) 2021-2022 Drew DeVault <sir@cmpwn.com> // Platform-specific path separator byte. -export def PATHSEP: u8 = '/'; +export def SEP: u8 = '/'; -const pathsepstr: str = "/"; +const sepstr: str = "/"; // Maximum length of a file path for this platform. -export def PATH_MAX: size = 4095; +export def MAX: size = 4095; diff --git a/path/+linux.ha b/path/+linux.ha @@ -2,9 +2,9 @@ // (c) 2021-2022 Drew DeVault <sir@cmpwn.com> // Platform-specific path separator byte. -export def PATHSEP: u8 = '/'; +export def SEP: u8 = '/'; -const pathsepstr: str = "/"; +const sepstr: str = "/"; // Maximum length of a file path for this platform. -export def PATH_MAX: size = 4095; +export def MAX: size = 4095; diff --git a/path/README b/path/README @@ -12,14 +12,14 @@ normalized, which is to say that it will not include any of the following: - Redundant path separators - Any "." components, except in the case of "." -Assuming that [[PATHSEP]] is '/', "/usr//bin/../bin/./hare/" becomes -"/usr/bin/hare" and "../../foo/bar" is unchanged. - -The buffer object includes an array of length [[PATH_MAX]], which can be -somewhat large; on Linux it's 4095 bytes. You can allocate this on the stack in -most cases, but you may prefer to allocate it elsewhere depending on your -needs. Functions in this module return [[too_long]] if the buffer's capacity -would be exceeded. +Assuming that [[SEP]] is '/', "/usr//bin/../bin/./hare/" becomes "/usr/bin/hare" +and "../../foo/bar" is unchanged. + +The buffer object includes an array of length [[MAX]], which can be somewhat +large; on Linux it's 4095 bytes. You can allocate this on the stack in most +cases, but you may prefer to allocate it elsewhere depending on your needs. +Functions in this module return [[too_long]] if the buffer's capacity would be +exceeded. // Stack allocated let buf = path::init()!; diff --git a/path/buffer.ha b/path/buffer.ha @@ -5,7 +5,7 @@ use bytes; use strings; export type buffer = struct { - buf: [PATH_MAX]u8, + buf: [MAX]u8, end: size, }; @@ -33,14 +33,14 @@ export fn string(buf: *buffer) str = { // Check if a path is an absolute path. export fn abs(path: (*buffer | str)) bool = match (path) { -case let path: str => return strings::hasprefix(path, pathsepstr); -case let buf: *buffer => return 0 < buf.end && buf.buf[0] == PATHSEP; +case let path: str => return strings::hasprefix(path, sepstr); +case let buf: *buffer => return 0 < buf.end && buf.buf[0] == SEP; }; // Check if a path is the root directory. export fn isroot(path: (*buffer | str)) bool = match (path) { -case let path: str => return path == pathsepstr; -case let buf: *buffer => return buf.end == 1 && buf.buf[0] == PATHSEP; +case let path: str => return path == sepstr; +case let buf: *buffer => return buf.end == 1 && buf.buf[0] == SEP; }; // Replace '/' with system-dependent path separator in a string. Modifies the @@ -48,7 +48,7 @@ case let buf: *buffer => return buf.end == 1 && buf.buf[0] == PATHSEP; export fn local(path: str) str = { let bs = strings::toutf8(path); for (let k = 0z; k < len(bs); k += 1) { - if (bs[k] == '/') bs[k] = PATHSEP; + if (bs[k] == '/') bs[k] = SEP; }; return path; }; diff --git a/path/ext_stack.ha b/path/ext_stack.ha @@ -12,7 +12,7 @@ export fn push_ext(buf: *buffer, ext: str...) (str | error) = { }; for (let i = 0z; i < len(ext); i += 1) { const newend = buf.end + 1 + len(ext[i]); - if (PATH_MAX < newend) return too_long; + if (MAX < newend) return too_long; buf.buf[buf.end] = '.'; buf.buf[buf.end+1..newend] = strings::toutf8(ext[i]); buf.end = newend; @@ -82,7 +82,7 @@ case let s: str => // push_ext let buf = init()!; assert(push_ext(&buf, "bash") is cant_extend); - set(&buf, pathsepstr)!; + set(&buf, sepstr)!; assert(push_ext(&buf, "bash") is cant_extend); set(&buf, "....")!; assert(push_ext(&buf, "bash") is cant_extend); @@ -96,7 +96,7 @@ case let s: str => assert(pop_ext(&buf) is void); set(&buf, "..")!; assert(pop_ext(&buf) is void); - set(&buf, pathsepstr)!; + set(&buf, sepstr)!; assert(pop_ext(&buf) is void); set(&buf, "index.html.tmpl")!; diff --git a/path/iter.ha b/path/iter.ha @@ -10,8 +10,6 @@ export type iterator = struct { reverse: bool, }; -let pathsep: []u8 = [PATHSEP]; - // Returns an [[iterator]] which yields each component of a path, moving down // through child dirs. If the path is absolute, the first component will be // the root. The iterator can be copied to save its state. @@ -49,7 +47,7 @@ export fn nextiter(it: *iterator) (str | void) = { // helper function for nextiter and peekiter, returns (result, remaining) fn split_iter(it: *iterator) ([]u8, []u8) = { if (it.reverse) { - match (bytes::rindex(it.cur, PATHSEP)) { + match (bytes::rindex(it.cur, SEP)) { case let sep: size => let res = it.cur[sep+1..]; if (sep == 0) { @@ -64,7 +62,7 @@ fn split_iter(it: *iterator) ([]u8, []u8) = { return (it.cur, it.cur[..0]); }; } else { - match (bytes::index(it.cur, PATHSEP)) { + match (bytes::index(it.cur, SEP)) { case let i: size => return (it.cur[..if (i == 0) 1 else i], it.cur[i+1..]); case void => diff --git a/path/posix.ha b/path/posix.ha @@ -19,16 +19,16 @@ export fn dirname(path: const str) const str = { let path = strings::toutf8(path); if (len(path) == 0) return "."; - path = bytes::rtrim(path, PATHSEP); - if (len(path) == 0) return pathsepstr; + path = bytes::rtrim(path, SEP); + if (len(path) == 0) return sepstr; - match (bytes::rindex(path, PATHSEP)) { + match (bytes::rindex(path, SEP)) { case void => return "."; case let z: size => path = path[..z]; }; - path = bytes::rtrim(path, PATHSEP); + path = bytes::rtrim(path, SEP); - if (len(path) == 0) return pathsepstr; + if (len(path) == 0) return sepstr; return strings::fromutf8_unsafe(path); }; @@ -40,10 +40,10 @@ export fn basename(path: const str) const str = { let path = strings::toutf8(path); if (len(path) == 0) return "."; - path = bytes::rtrim(path, PATHSEP); - if (len(path) == 0) return pathsepstr; + path = bytes::rtrim(path, SEP); + if (len(path) == 0) return sepstr; - match (bytes::rindex(path, PATHSEP)) { + match (bytes::rindex(path, SEP)) { case void => void; case let z: size => path = path[z+1..]; }; diff --git a/path/prefix.ha b/path/prefix.ha @@ -13,8 +13,8 @@ export fn prepend(buf: *buffer, prefix: str...) (str | error) = { // Returns a buffer without a prefix. The prefix is normalized before // processing, and this function will return [[too_long]] if the prefix is -// longer than [[PATH_MAX]]. If the prefix is not present, returns -// [[not_prefix]]. The resulting path will always be relative. +// longer than [[MAX]]. If the prefix is not present, returns [[not_prefix]]. +// The resulting path will always be relative. // // This function does not modify the buffer. See [[popprefix]]. export fn trimprefix(buf: *buffer, prefix: str) (str | error) = { @@ -37,8 +37,8 @@ fn splitprefix(buf: *buffer, prefix: str) (size | error) = { let pref = init(prefix)?; if (pref.end == 0) { if (abs(buf)) return not_prefix; - } else if (pref.end < buf.end && pref.buf[pref.end-1] != PATHSEP) { - pref.buf[pref.end] = PATHSEP; + } else if (pref.end < buf.end && pref.buf[pref.end-1] != SEP) { + pref.buf[pref.end] = SEP; pref.end += 1; }; if (bytes::hasprefix(buf.buf[..buf.end], pref.buf[..pref.end])) { diff --git a/path/stack.ha b/path/stack.ha @@ -8,13 +8,13 @@ use strings; export fn push(buf: *buffer, items: str...) (str | error) = { for (let i = 0z; i < len(items); i += 1) { let elem = strings::toutf8(items[i]); - for (true) match (bytes::index(elem, PATHSEP)) { + for (true) match (bytes::index(elem, SEP)) { case void => buf.end = appendnorm(buf, elem)?; break; case let j: size => if (j == 0 && buf.end == 0) { - buf.buf[0] = PATHSEP; + buf.buf[0] = SEP; buf.end = 1; } else { buf.end = appendnorm(buf, elem[..j])?; @@ -29,8 +29,8 @@ const dot: []u8 = ['.']; const dotdot: []u8 = ['.', '.']; // append a path segment to a buffer, preserving normalization. -// seg must not contain any PATHSEPs. if you need to make the path -// absolute, you should do that manually. returns the new end of the buffer. +// seg must not contain any [[SEP]]s. if you need to make the path absolute, you +// should do that manually. returns the new end of the buffer. // x + => x // x + . => x // / + .. => / @@ -42,7 +42,7 @@ fn appendnorm(buf: *buffer, seg: []u8) (size | error) = { if (len(seg) == 0 || bytes::equal(dot, seg)) return buf.end; if (bytes::equal(dotdot, seg)) { if (isroot(buf)) return buf.end; - const isep = match (bytes::rindex(buf.buf[..buf.end], PATHSEP)) { + const isep = match (bytes::rindex(buf.buf[..buf.end], SEP)) { case void => yield 0z; case let i: size => yield i + 1; }; @@ -61,10 +61,10 @@ fn appendnorm(buf: *buffer, seg: []u8) (size | error) = { fn appendlit(buf: *buffer, bs: []u8) (size | error) = { let newend = buf.end; if (buf.end == 0 || isroot(buf)) { - if (PATH_MAX < buf.end + len(bs)) return too_long; + if (MAX < buf.end + len(bs)) return too_long; } else { - if (PATH_MAX < buf.end + len(bs) + 1) return too_long; - buf.buf[buf.end] = PATHSEP; + if (MAX < buf.end + len(bs) + 1) return too_long; + buf.buf[buf.end] = SEP; newend += 1; }; buf.buf[newend..newend+len(bs)] = bs; @@ -131,7 +131,7 @@ export fn pop(buf: *buffer) (str | void) = { // helper function for pop/peek, returns (new end of buffer, result) fn split(buf: *buffer) (size, (str | void)) = { if (buf.end == 0 || isroot(buf)) return (buf.end, void); - match (bytes::rindex(buf.buf[..buf.end], PATHSEP)) { + match (bytes::rindex(buf.buf[..buf.end], SEP)) { case void => return (0z, strings::fromutf8_unsafe(buf.buf[..buf.end])); case let i: size =>