commit 5893c49443b70fb649836b1da6a0caf4fe274249
parent 505fdcd2925c71a6b101ca8f5129f0f51b7858e0
Author: Sebastian <sebastian@sebsite.pw>
Date: Tue, 5 Sep 2023 22:06:51 -0400
path: statically allocate result
Instead of modifying the original string. Necessary since the const
overhaul will make strings immutable.
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
2 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/path/buffer.ha b/path/buffer.ha
@@ -43,12 +43,22 @@ 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
-// original string, but is idempotent. The result is borrowed from the input.
+// Replaces all instances of '/' in a string with [[SEP]]. The result is
+// statically-allocated.
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] = SEP;
+ static let buf: [MAX]u8 = [0...];
+ return _local(path, &buf);
+};
+
+fn _local(path: str, buf: *[MAX]u8) str = {
+ let buf = buf[..0];
+ const path = strings::toutf8(path);
+ for (let i = 0z; i < len(path); i += 1) {
+ if (path[i] == '/') {
+ static append(buf, SEP);
+ } else {
+ static append(buf, path[i]);
+ };
};
- return path;
+ return strings::fromutf8(buf)!;
};
diff --git a/path/posix.ha b/path/posix.ha
@@ -65,7 +65,9 @@ export fn basename(path: const str) const str = {
["/home//dwc//test", "/home//dwc", "test"],
];
for (let i = 0z; i < len(table); i += 1) {
- assert(dirname(local(table[i][0])) == local(table[i][1]));
- assert(basename(local(table[i][0])) == local(table[i][2]));
+ let input: [MAX]u8 = [0...];
+ const input = _local(table[i][0], &input);
+ assert(dirname(input) == local(table[i][1]));
+ assert(basename(input) == local(table[i][2]));
};
};