hare

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

commit 2476403d74e1cfa2e0a38c1a1496fcb673a94a9d
parent d84d5b58c24313580d541b5858691d38f1c788cf
Author: Sebastian <sebastian@sebsite.pw>
Date:   Sat,  4 Feb 2023 00:59:01 -0500

strings: return utf8::invalid from fromc on invalid UTF-8

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

Diffstat:
Mformat/tar/reader.ha | 2+-
Mlinux/vdso/vdso.ha | 4++--
Mos/+freebsd/dirfdfs.ha | 2+-
Mos/+freebsd/environ.ha | 6+++---
Mos/+freebsd/fs.ha | 2+-
Mos/+linux/dirfdfs.ha | 2+-
Mos/+linux/environ+libc.ha | 16++++++++--------
Mos/+linux/environ.ha | 16++++++++--------
Mos/+linux/fs.ha | 2+-
Mstrings/cstrings.ha | 5++---
Munix/tty/+freebsd/pty.ha | 2+-
11 files changed, 29 insertions(+), 30 deletions(-)

diff --git a/format/tar/reader.ha b/format/tar/reader.ha @@ -207,7 +207,7 @@ fn readstr(rd: *bufio::memstream, ln: size) str = { case io::EOF => abort(); }; - return strings::fromc(buf: *[*]u8: *const char); + return strings::fromc(buf: *[*]u8: *const char)!; }; fn readoct(rd: *bufio::memstream, ln: size) (uint | invalid) = { diff --git a/linux/vdso/vdso.ha b/linux/vdso/vdso.ha @@ -141,7 +141,7 @@ fn vdso_checkver(ctx: *vdso_ctx, version: str, num: u32) bool = { cur.vd_aux: uintptr): *elf::verdaux64; const name = ctx.stringtab: uintptr + aux.vda_name: uintptr; - return version == strings::fromc(name: *char); + return version == strings::fromc(name: *char)!; }; prev = cur; cur += cur.vd_next: uintptr; @@ -174,7 +174,7 @@ export fn getsym(symname: str, symver: str) nullable *void = { continue; }; const name = ctx.stringtab: uintptr + sym.st_name: uintptr; - const s: str = strings::fromc(name: *char); + const s: str = strings::fromc(name: *const char)!; if (s != symname) continue; if (!vdso_checkver(ctx, symver, i)) diff --git a/os/+freebsd/dirfdfs.ha b/os/+freebsd/dirfdfs.ha @@ -383,7 +383,7 @@ fn iter_next(iter: *fs::iterator) (fs::dirent | void) = { }; let de = &iter.buf[iter.buf_pos]: *rt::freebsd11_dirent; iter.buf_pos += de.d_reclen; - let name = strings::fromc(&de.d_name: *const char); + let name = strings::fromc(&de.d_name: *const char)!; let ftype: fs::mode = switch (de.d_type) { case rt::DT_UNKNOWN => diff --git a/os/+freebsd/environ.ha b/os/+freebsd/environ.ha @@ -18,12 +18,12 @@ let args_static: [32]str = [""...]; if (rt::argc < len(args_static)) { args = args_static[..rt::argc]; for (let i = 0z; i < rt::argc; i += 1) { - args[i] = strings::fromc(rt::argv[i]); + args[i] = strings::fromc(rt::argv[i])!; }; } else { args = alloc([], rt::argc); for (let i = 0z; i < rt::argc; i += 1) { - append(args, strings::fromc(rt::argv[i])); + append(args, strings::fromc(rt::argv[i])!); }; }; @@ -72,7 +72,7 @@ export fn getenvs() []str = { return envp; }; for (let i = 0z; rt::envp[i] != null; i += 1) { - append(envp, strings::fromc(rt::envp[i]: *const char)); + append(envp, strings::fromc(rt::envp[i]: *const char)!); }; return envp; }; diff --git a/os/+freebsd/fs.ha b/os/+freebsd/fs.ha @@ -22,7 +22,7 @@ export fn init_cwd() void = { // Returns the current working directory. The return value is statically // allocated and must be duplicated (see [[strings::dup]]) before calling getcwd // again. -export fn getcwd() str = strings::fromc(rt::getcwd() as *const char); +export fn getcwd() str = strings::fromc(rt::getcwd() as *const char)!; // Change the current working directory. export fn chdir(target: (*fs::fs | str)) (void | fs::error) = { diff --git a/os/+linux/dirfdfs.ha b/os/+linux/dirfdfs.ha @@ -431,7 +431,7 @@ fn iter_next(iter: *fs::iterator) (fs::dirent | void) = { }; let de = &iter.buf[iter.buf_pos]: *rt::dirent64; iter.buf_pos += de.d_reclen; - let name = strings::fromc(&de.d_name: *const char); + let name = strings::fromc(&de.d_name: *const char)!; let ftype: fs::mode = switch (de.d_type) { case rt::DT_UNKNOWN => diff --git a/os/+linux/environ+libc.ha b/os/+linux/environ+libc.ha @@ -18,12 +18,12 @@ let args_static: [32]str = [""...]; if (rt::argc < len(args_static)) { args = args_static[..rt::argc]; for (let i = 0z; i < rt::argc; i += 1) { - args[i] = strings::fromc(rt::argv[i]); + args[i] = strings::fromc(rt::argv[i])!; }; } else { args = alloc([], rt::argc); for (let i = 0z; i < rt::argc; i += 1) { - append(args, strings::fromc(rt::argv[i])); + append(args, strings::fromc(rt::argv[i])!); }; }; @@ -72,7 +72,7 @@ export fn getenvs() []str = { return envp; }; for (let i = 0z; rt::envp[i] != null; i += 1) { - append(envp, strings::fromc(rt::envp[i]: *const char)); + append(envp, strings::fromc(rt::envp[i]: *const char)!); }; return envp; }; @@ -85,7 +85,7 @@ export fn sysname() const str = { if (!uts_valid) { rt::uname(&uts) as void; }; - return strings::fromc(&uts.sysname: *const char); + return strings::fromc(&uts.sysname: *const char)!; }; // Returns the host system hostname @@ -93,7 +93,7 @@ export fn hostname() const str = { if (!uts_valid) { rt::uname(&uts) as void; }; - return strings::fromc(&uts.nodename: *const char); + return strings::fromc(&uts.nodename: *const char)!; }; // Returns the host kernel version @@ -101,7 +101,7 @@ export fn release() const str = { if (!uts_valid) { rt::uname(&uts) as void; }; - return strings::fromc(&uts.release: *const char); + return strings::fromc(&uts.release: *const char)!; }; // Returns the host operating system version @@ -109,7 +109,7 @@ export fn version() const str = { if (!uts_valid) { rt::uname(&uts) as void; }; - return strings::fromc(&uts.version: *const char); + return strings::fromc(&uts.version: *const char)!; }; // Returns the host CPU architecture @@ -117,5 +117,5 @@ export fn machine() const str = { if (!uts_valid) { rt::uname(&uts) as void; }; - return strings::fromc(&uts.machine: *const char); + return strings::fromc(&uts.machine: *const char)!; }; diff --git a/os/+linux/environ.ha b/os/+linux/environ.ha @@ -18,12 +18,12 @@ let args_static: [32]str = [""...]; if (rt::argc < len(args_static)) { args = args_static[..rt::argc]; for (let i = 0z; i < rt::argc; i += 1) { - args[i] = strings::fromc(rt::argv[i]); + args[i] = strings::fromc(rt::argv[i])!; }; } else { args = alloc([], rt::argc); for (let i = 0z; i < rt::argc; i += 1) { - append(args, strings::fromc(rt::argv[i])); + append(args, strings::fromc(rt::argv[i])!); }; }; @@ -72,7 +72,7 @@ export fn getenvs() []str = { return envp; }; for (let i = 0z; rt::envp[i] != null; i += 1) { - append(envp, strings::fromc(rt::envp[i]: *const char)); + append(envp, strings::fromc(rt::envp[i]: *const char)!); }; return envp; }; @@ -85,7 +85,7 @@ export fn sysname() const str = { if (!uts_valid) { rt::uname(&uts) as void; }; - return strings::fromc(&uts.sysname: *const char); + return strings::fromc(&uts.sysname: *const char)!; }; // Returns the host system hostname @@ -93,7 +93,7 @@ export fn hostname() const str = { if (!uts_valid) { rt::uname(&uts) as void; }; - return strings::fromc(&uts.nodename: *const char); + return strings::fromc(&uts.nodename: *const char)!; }; // Returns the host kernel version @@ -101,7 +101,7 @@ export fn release() const str = { if (!uts_valid) { rt::uname(&uts) as void; }; - return strings::fromc(&uts.release: *const char); + return strings::fromc(&uts.release: *const char)!; }; // Returns the host operating system version @@ -109,7 +109,7 @@ export fn version() const str = { if (!uts_valid) { rt::uname(&uts) as void; }; - return strings::fromc(&uts.version: *const char); + return strings::fromc(&uts.version: *const char)!; }; // Returns the host CPU architecture @@ -117,5 +117,5 @@ export fn machine() const str = { if (!uts_valid) { rt::uname(&uts) as void; }; - return strings::fromc(&uts.machine: *const char); + return strings::fromc(&uts.machine: *const char)!; }; diff --git a/os/+linux/fs.ha b/os/+linux/fs.ha @@ -22,7 +22,7 @@ export fn init_cwd() void = { // Returns the current working directory. The return value is statically // allocated and must be duplicated (see [[strings::dup]]) before calling getcwd // again. -export fn getcwd() str = strings::fromc(rt::getcwd() as *const char); +export fn getcwd() str = strings::fromc(rt::getcwd() as *const char)!; // Change the current working directory. export fn chdir(target: (*fs::fs | str)) (void | fs::error) = { diff --git a/strings/cstrings.ha b/strings/cstrings.ha @@ -28,10 +28,9 @@ export fn fromc_unsafe(cstr: *const char) const str = { // Converts a C string to a Hare string in O(n). If the string is not valid // UTF-8, abort. -export fn fromc(cstr: *const char) const str = { +export fn fromc(cstr: *const char) (const str | utf8::invalid) = { let s = fromc_unsafe(cstr); - assert(utf8::valid(s)); - return s; + return if (utf8::valid(s)) s else utf8::invalid; }; // Converts a Hare string to a C string. The result is allocated; the caller diff --git a/unix/tty/+freebsd/pty.ha b/unix/tty/+freebsd/pty.ha @@ -48,7 +48,7 @@ export fn ptsname(master: io::file) (str | error) = { case => static let path_buf: [rt::PATH_MAX]u8 = [0...]; return fmt::bsprintf(path_buf, - "/dev/{}", strings::fromc(&name: *const char)); + "/dev/{}", strings::fromc(&name: *const char)!); }; };