hare

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

commit 045f309618a09c836b54662ee1bcf8239b365f61
parent f4e6b6ac950ec5055bf37b883ef36faf513633a2
Author: Sebastian LaVine <mail@smlavine.com>
Date:   Wed, 28 Sep 2022 16:01:20 -0400

Rename strings::try_fromutf8 to strings::fromutf8

According to ecs on IRC, these functions were written before the error
assertion operator was added. This design is more in line with the rest
of the standard library.

Signed-off-by: Sebastian LaVine <mail@smlavine.com>

Diffstat:
Mcmd/hare/release.ha | 4++--
Mcmd/haredoc/hare.ha | 2+-
Mcmd/haredoc/html.ha | 2+-
Mcmd/haredoc/tty.ha | 2+-
Mcrypto/bcrypt/+test.ha | 2+-
Mcrypto/bcrypt/bcrypt.ha | 2+-
Mencoding/base32/base32.ha | 2+-
Mencoding/base64/base64.ha | 2+-
Mencoding/json/lex.ha | 2+-
Mencoding/pem/pem.ha | 4++--
Mfmt/fmt.ha | 2+-
Mfnmatch/fnmatch.ha | 2+-
Mformat/ini/scan.ha | 2+-
Mfs/util.ha | 2+-
Mgetopt/getopts.ha | 2+-
Mhare/lex/lex.ha | 10+++++-----
Mhare/module/manifest.ha | 2+-
Mmime/system.ha | 2+-
Mnet/dns/decode.ha | 2+-
Mos/+freebsd/dirfdfs.ha | 2+-
Mos/+freebsd/environ.ha | 12++++++------
Mos/+linux/dirfdfs.ha | 2+-
Mos/+linux/environ.ha | 2+-
Mstrings/iter.ha | 2+-
Mstrings/replace.ha | 2+-
Mstrings/runes.ha | 2+-
Mstrings/tokenize.ha | 6+++---
Mstrings/trim.ha | 2+-
Mstrings/utf8.ha | 20++++++--------------
Mstrio/stream.ha | 2+-
Mtime/chrono/leapsec.ha | 2+-
Mtime/chrono/tzdb.ha | 4++--
Munix/hosts/lookup.ha | 4++--
Munix/passwd/group.ha | 2+-
Munix/passwd/passwd.ha | 2+-
Munix/resolvconf/load.ha | 4++--
36 files changed, 57 insertions(+), 65 deletions(-)

diff --git a/cmd/hare/release.ha b/cmd/hare/release.ha @@ -251,7 +251,7 @@ fn choosekey() (str | release_error) = { case io::EOF => fmt::fatal("No suitable key available. Terminating."); case let line: []u8 => - yield strings::fromutf8(line); + yield strings::fromutf8(line)!; }; defer free(line); if (line != "" && line != "y" && line != "Y") { @@ -345,7 +345,7 @@ fn git_readcmd(args: str...) (str | release_error) = { const result = io::drain(pipe.0)?; const status = exec::wait(&proc)?; exec::check(&status)?; - return strings::fromutf8(result); + return strings::fromutf8(result)!; }; fn get_defaultbranch() (str | release_error) = { diff --git a/cmd/haredoc/hare.ha b/cmd/haredoc/hare.ha @@ -26,7 +26,7 @@ fn emit_hare(ctx: *context) (void | error) = { case io::EOF => break; case let b: []u8 => fmt::fprintfln(ctx.out, - "// {}", strings::fromutf8(b))?; + "// {}", strings::fromutf8(b)!)?; free(b); }; }; diff --git a/cmd/haredoc/html.ha b/cmd/haredoc/html.ha @@ -44,7 +44,7 @@ fn html_escape(out: io::handle, in: str) (size | io::error) = { case '\'' => yield "&apos;"; case => - yield strings::fromutf8(utf8::encoderune(rn)); + yield strings::fromutf8(utf8::encoderune(rn))!; })?; }; }; diff --git a/cmd/haredoc/tty.ha b/cmd/haredoc/tty.ha @@ -29,7 +29,7 @@ fn emit_tty(ctx: *context) (void | error) = { defer free(b); firstline = false; insert(b[0], ' '); - comment_tty(ctx.out, strings::fromutf8(b))?; + comment_tty(ctx.out, strings::fromutf8(b)!)?; }; case void => void; }; diff --git a/crypto/bcrypt/+test.ha b/crypto/bcrypt/+test.ha @@ -14,6 +14,6 @@ use fmt; const salt = strings::toutf8("XajjQvNhvvRt5GSeFk1xFe"); const expect = "$2a$10$XajjQvNhvvRt5GSeFk1xFeyqRrsxkhBkUiQeg0dt.wU1qD4aFDcga"; - const hash = strings::fromutf8(bcrypt(pass, salt, 10)!); + const hash = strings::fromutf8(bcrypt(pass, salt, 10)!)!; assert(strings::hassuffix(expect, hash)); }; diff --git a/crypto/bcrypt/bcrypt.ha b/crypto/bcrypt/bcrypt.ha @@ -146,7 +146,7 @@ fn load(input: []u8) (hash | errors::invalid) = { fn loadtok(tok: *bytes::tokenizer) (str | errors::invalid) = { match (bytes::next_token(tok)) { case let b: []u8 => - match (strings::try_fromutf8(b)) { + match (strings::fromutf8(b)) { case let s: str => return s; case => diff --git a/encoding/base32/base32.ha b/encoding/base32/base32.ha @@ -176,7 +176,7 @@ export fn encodeslice(enc: *encoding, in: []u8) []u8 = { // Encodes a byte slice in base-32, using the given encoding, returning a // string. The caller must free the return value. export fn encodestr(enc: *encoding, in: []u8) str = { - return strings::fromutf8(encodeslice(enc, in)); + return strings::fromutf8(encodeslice(enc, in))!; }; @test fn encode() void = { diff --git a/encoding/base64/base64.ha b/encoding/base64/base64.ha @@ -191,7 +191,7 @@ export fn encode( // Encodes a byte slice in base 64, using the given encoding, returning a // string. The caller must free the return value. export fn encodestr(enc: *encoding, in: []u8) str = { - return strings::fromutf8(encodeslice(enc, in)); + return strings::fromutf8(encodeslice(enc, in))!; }; @test fn encode() void = { diff --git a/encoding/json/lex.ha b/encoding/json/lex.ha @@ -307,7 +307,7 @@ fn scan_escape(lex: *lexer) (rune | error) = { case size => yield; }; - const s = match (strings::try_fromutf8(buf)) { + const s = match (strings::fromutf8(buf)) { case let s: str => yield s; case => diff --git a/encoding/pem/pem.ha b/encoding/pem/pem.ha @@ -89,7 +89,7 @@ export fn next(dec: *decoder) ((str, pemdecoder) | io::EOF | io::error) = { case io::EOF => return io::EOF; case let line: []u8 => - yield match (strings::try_fromutf8(line)) { + yield match (strings::fromutf8(line)) { case let s: str => yield s; case => @@ -147,7 +147,7 @@ fn pem_read(st: *io::stream, buf: []u8) (size | io::EOF | io::error) = { case io::EOF => return io::EOF; case let line: []u8 => - yield match (strings::try_fromutf8(line)) { + yield match (strings::fromutf8(line)) { case let s: str => yield s; case => diff --git a/fmt/fmt.ha b/fmt/fmt.ha @@ -420,7 +420,7 @@ fn scan_uint(iter: *strings::iterator) uint = { append(num, r: u32: u8); } else { strings::prev(iter); - match (strconv::stou(strings::fromutf8(num))) { + match (strconv::stou(strings::fromutf8(num)!)) { case (strconv::invalid | strconv::overflow) => abort("Invalid format string (invalid index)"); case let u: uint => diff --git a/fnmatch/fnmatch.ha b/fnmatch/fnmatch.ha @@ -389,6 +389,6 @@ fn advance_or_err(it: *strings::iterator) (rune | errors::invalid) = { fn cut_tail(s: str, it: *strings::iterator) str = { let s_len = len(s), t_len = len(strings::iterstr(it)); let b = strings::toutf8(s); - return strings::fromutf8(b[..s_len - t_len]); + return strings::fromutf8(b[..s_len - t_len])!; }; diff --git a/format/ini/scan.ha b/format/ini/scan.ha @@ -54,7 +54,7 @@ export fn next(sc: *scanner) (entry | io::EOF | error) = { for (true) { const line = match (bufio::scanline(sc.in)?) { case let b: []u8 => - yield strings::try_fromutf8(b)?; + yield strings::fromutf8(b)?; case io::EOF => return io::EOF; }; diff --git a/fs/util.ha b/fs/util.ha @@ -37,7 +37,7 @@ export fn mode_str(m: mode) const str = { else if (m & mode::OTHER_X == mode::OTHER_X) 'x' else '-'): u32: u8, ]; - return strings::fromutf8(buf); + return strings::fromutf8(buf)!; }; @test fn mode_str() void = { diff --git a/getopt/getopts.ha b/getopt/getopts.ha @@ -110,7 +110,7 @@ export fn parse(args: []str, help: help...) command = { i += 1; append(opts, (r, args[i])); } else { - let s = strings::fromutf8(d.src[d.offs..]); + let s = strings::fromutf8(d.src[d.offs..])!; append(opts, (r, s)); }; continue :arg; diff --git a/hare/lex/lex.ha b/hare/lex/lex.ha @@ -386,7 +386,7 @@ fn lex_comment(lexr: *lexer) (void | error) = { }; let bytes = strings::toutf8(lexr.comment); append(bytes, strings::toutf8(strio::string(&buf))...); - lexr.comment = strings::fromutf8(bytes); + lexr.comment = strings::fromutf8(bytes)!; }; fn lex_literal(lex: *lexer) (token | error) = { @@ -536,7 +536,7 @@ fn lex_literal(lex: *lexer) (token | error) = { case let suff: size => yield suff; }; - yield strings::fromutf8(chars[exp..end]); + yield strings::fromutf8(chars[exp..end])!; }; let exp = match (strconv::stoi(exp)) { case let exp: int => @@ -555,7 +555,7 @@ fn lex_literal(lex: *lexer) (token | error) = { }; let suff = match (suff) { case let suff: size => - yield strings::fromutf8(chars[suff..]); + yield strings::fromutf8(chars[suff..])!; case void => yield ""; }; @@ -583,10 +583,10 @@ fn lex_literal(lex: *lexer) (token | error) = { "invalid negative exponent of integer"); } else exp: size; - let val = strings::fromutf8(chars[..end]); + let val = strings::fromutf8(chars[..end])!; let val = switch (suff) { case ltok::LIT_F32, ltok::LIT_F64, ltok::LIT_FCONST => - val = strings::fromutf8(chars[..floatend]); + val = strings::fromutf8(chars[..floatend])!; yield strconv::stof64(val); case => yield strconv::stou64b(val, base); diff --git a/hare/module/manifest.ha b/hare/module/manifest.ha @@ -84,7 +84,7 @@ export fn manifest_load(ctx: *context, ident: ast::ident) (manifest | error) = { }; defer free(line); - let line = match (strings::try_fromutf8(line)) { + let line = match (strings::fromutf8(line)) { case utf8::invalid => // Treat an invalid manifest as empty return manifest; diff --git a/mime/system.ha b/mime/system.ha @@ -26,7 +26,7 @@ fn load_systemdb() (void | fs::error | io::error) = { for (true) { const line = match (bufio::scanline(&strm)) { case let bytes: []u8 => - yield match (strings::try_fromutf8(bytes)) { + yield match (strings::fromutf8(bytes)) { case utf8::invalid => fmt::errorln("Warning: /etc/mime.types contains invalid UTF-8")!; io::close(&strm)?; diff --git a/net/dns/decode.ha b/net/dns/decode.ha @@ -109,7 +109,7 @@ fn decode_name(dec: *decoder) ([]str | format) = { break; }; - const name = strings::fromutf8(dec.cur[..z]); + const name = strings::fromutf8(dec.cur[..z])!; dec.cur = dec.cur[z..]; if (!ascii::validstr(name)) { return format; diff --git a/os/+freebsd/dirfdfs.ha b/os/+freebsd/dirfdfs.ha @@ -274,7 +274,7 @@ fn fs_readlink(fs: *fs::fs, path: str) (str | fs::error) = { case let z: size => yield z; }; - return strings::fromutf8(buf[..z]); + return strings::fromutf8(buf[..z])!; }; fn fs_rmdir(fs: *fs::fs, path: str) (void | fs::error) = { diff --git a/os/+freebsd/environ.ha b/os/+freebsd/environ.ha @@ -49,7 +49,7 @@ export fn getenv(name: const str) (str | void) = { }; if (bytes::equal(name_b, item[..eq])) { const ln = strings::cstrlen(item: *const char); - return strings::fromutf8(item[eq+1..ln]); + return strings::fromutf8(item[eq+1..ln])!; }; }; }; @@ -81,7 +81,7 @@ export fn sysname() const str = { static let buf: [512]u8 = [0...]; let sz: size = len(buf); rt::sysctlbyname("kern.ostype", &buf, &sz, null, 0)!; - return strings::fromutf8(buf[..(sz - 1)]); + return strings::fromutf8(buf[..(sz - 1)])!; }; // Returns the host system hostname @@ -89,7 +89,7 @@ export fn hostname() const str = { static let buf: [512]u8 = [0...]; let sz: size = len(buf); rt::sysctlbyname("kern.hostname", &buf, &sz, null, 0)!; - return strings::fromutf8(buf[..(sz - 1)]); + return strings::fromutf8(buf[..(sz - 1)])!; }; // Returns the host kernel version @@ -97,7 +97,7 @@ export fn release() const str = { static let buf: [512]u8 = [0...]; let sz: size = len(buf); rt::sysctlbyname("kern.osrelease", &buf, &sz, null, 0)!; - return strings::fromutf8(buf[..(sz - 1)]); + return strings::fromutf8(buf[..(sz - 1)])!; }; // Returns the host operating system version @@ -105,7 +105,7 @@ export fn version() const str = { static let buf: [512]u8 = [0...]; let sz: size = len(buf); rt::sysctlbyname("kern.version", &buf, &sz, null, 0)!; - return strings::fromutf8(buf[..(sz - 1)]); + return strings::fromutf8(buf[..(sz - 1)])!; }; // Returns the host CPU architecture @@ -113,7 +113,7 @@ export fn machine() const str = { static let buf: [512]u8 = [0...]; let sz: size = len(buf); rt::sysctlbyname("hw.machine", &buf, &sz, null, 0)!; - const mach = strings::fromutf8(buf[..(sz - 1)]); + const mach = strings::fromutf8(buf[..(sz - 1)])!; // Translate to Hare names switch (mach) { case "amd64" => diff --git a/os/+linux/dirfdfs.ha b/os/+linux/dirfdfs.ha @@ -304,7 +304,7 @@ fn fs_readlink(fs: *fs::fs, path: str) (str | fs::error) = { case let z: size => yield z; }; - return strings::fromutf8(buf[..z]); + return strings::fromutf8(buf[..z])!; }; fn fs_rmdir(fs: *fs::fs, path: str) (void | fs::error) = { diff --git a/os/+linux/environ.ha b/os/+linux/environ.ha @@ -49,7 +49,7 @@ export fn getenv(name: const str) (str | void) = { }; if (bytes::equal(name_b, item[..eq])) { const ln = strings::cstrlen(item: *const char); - return strings::fromutf8(item[eq+1..ln]); + return strings::fromutf8(item[eq+1..ln])!; }; }; }; diff --git a/strings/iter.ha b/strings/iter.ha @@ -88,7 +88,7 @@ fn next_backward(iter: *iterator) (rune | void) = { // Return a substring from the next rune to the end of the string. export fn iterstr(iter: *iterator) str = { - return fromutf8(iter.dec.src[iter.dec.offs..]); + return fromutf8(iter.dec.src[iter.dec.offs..])!; }; @test fn iter() void = { diff --git a/strings/replace.ha b/strings/replace.ha @@ -32,7 +32,7 @@ export fn multireplace(s: str, repls: (str, str)...) str = { idx += len(target); }; }; - return fromutf8(res); + return fromutf8(res)!; }; @test fn replace() void = { diff --git a/strings/runes.ha b/strings/runes.ha @@ -24,7 +24,7 @@ export fn fromrunes(rs: []rune) str = { const bs = encoding::utf8::encoderune(rs[i]); append(bytes, bs...); }; - return fromutf8(bytes); + return fromutf8(bytes)!; }; @test fn fromrunes() void = { diff --git a/strings/tokenize.ha b/strings/tokenize.ha @@ -26,7 +26,7 @@ export fn tokenize(s: str, delim: str) tokenizer = export fn next_token(s: *tokenizer) (str | void) = { return match (bytes::next_token(s)) { case let b: []u8 => - yield fromutf8(b); + yield fromutf8(b)!; case void => void; }; }; @@ -35,7 +35,7 @@ export fn next_token(s: *tokenizer) (str | void) = { export fn peek_token(s: *tokenizer) (str | void) = { return match (bytes::peek_token(s)) { case let b: []u8 => - yield fromutf8(b); + yield fromutf8(b)!; case void => void; }; }; @@ -43,7 +43,7 @@ export fn peek_token(s: *tokenizer) (str | void) = { // Returns the remainder of the string associated with a tokenizer, without doing // any further tokenization. export fn remaining_tokens(s: *tokenizer) str = { - return fromutf8(bytes::remaining_tokens(s)); + return fromutf8(bytes::remaining_tokens(s))!; }; @test fn tokenize() void = { diff --git a/strings/trim.ha b/strings/trim.ha @@ -36,7 +36,7 @@ export fn ltrim(input: str, trim: rune...) str = { break; }; }; - return fromutf8(it.dec.src[it.dec.offs..]); + return fromutf8(it.dec.src[it.dec.offs..])!; }; // Returns a string (borrowed from given input string) after trimming off of diff --git a/strings/utf8.ha b/strings/utf8.ha @@ -15,18 +15,10 @@ export fn fromutf8_unsafe(in: []u8) str = { return *(&s: *const str); }; -// Converts a byte slice into a string, aborting the program if the bytes -// contain invalid UTF-8. The return value is borrowed from the input. To handle -// errors without aborting, see [[try_fromutf8]] or [[encoding::utf8]]. -export fn fromutf8(in: []u8) str = { - let s = fromutf8_unsafe(in); - assert(utf8::valid(s), "attempted to load invalid UTF-8 string"); - return s; -}; - -// Converts a byte slice into a string. If the slice contains invalid UTF-8 -// sequences, [[encoding::utf8::invalid]] is returned instead. -export fn try_fromutf8(in: []u8) (str | utf8::invalid) = { +// Converts a byte slice into a string. The return value is borrowed from the +// input. If the slice contains invalid UTF-8 sequences, +// [[encoding::utf8::invalid]] is returned instead. +export fn fromutf8(in: []u8) (str | utf8::invalid) = { let s = fromutf8_unsafe(in); if (!utf8::valid(s)) { return utf8::invalid; @@ -41,6 +33,6 @@ export fn toutf8(in: str) []u8 = *(&in: *[]u8); @test fn utf8() void = { assert(fromutf8([ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, - ]) == "hello world"); - assert(fromutf8([]) == ""); + ])! == "hello world"); + assert(fromutf8([])! == ""); }; diff --git a/strio/stream.ha b/strio/stream.ha @@ -20,7 +20,7 @@ export type stream = struct { // from the stream, and will be freed when the stream is closed. Use // [[strings::dup]] to extend its lifetime. export fn string(in: *stream) str = { - return strings::fromutf8(in.buf); + return strings::fromutf8(in.buf)!; }; // Resets the buffer's length to zero, but does not attempt to deallocate its diff --git a/time/chrono/leapsec.ha b/time/chrono/leapsec.ha @@ -75,7 +75,7 @@ fn parse_utc_leapsecs( case io::EOF => return; case let line: []u8 => - yield strings::try_fromutf8(line)?; + yield strings::fromutf8(line)?; }; defer free(line); if (strings::hasprefix(line, '#')) { diff --git a/time/chrono/tzdb.ha b/time/chrono/tzdb.ha @@ -177,7 +177,7 @@ fn parse_tzif(h: io::handle, tz: *timezone) (void | invalidtzif | io::error) = { }; append(footerdata, buf1...); }; - const posix_extend = match (strings::try_fromutf8(footerdata)) { + const posix_extend = match (strings::fromutf8(footerdata)) { case let s: str => yield s; case encoding::utf8::invalid => @@ -224,7 +224,7 @@ fn parse_tzif(h: io::handle, tz: *timezone) (void | invalidtzif | io::error) = { if (len(bytes) == 0) { // no NUL encountered return invalidtzif; }; - const abbr = match (strings::try_fromutf8(bytes)) { + const abbr = match (strings::fromutf8(bytes)) { case let s: str => yield s; case encoding::utf8::invalid => diff --git a/unix/hosts/lookup.ha b/unix/hosts/lookup.ha @@ -43,7 +43,7 @@ fn lookupio(name: str, src: io::handle) []ip::addr = { yield tok; }; defer free(tok); - const addr = ip::parse(strings::fromutf8(tok))!; + const addr = ip::parse(strings::fromutf8(tok)!)!; for (true) { const tok = match (bufio::scantok(&scanner, ' ', '\t')!) { @@ -54,7 +54,7 @@ fn lookupio(name: str, src: io::handle) []ip::addr = { }; defer free(tok); - if (strings::fromutf8(tok) == name) { + if (strings::fromutf8(tok)! == name) { append(addrs, addr); }; }; diff --git a/unix/passwd/group.ha b/unix/passwd/group.ha @@ -29,7 +29,7 @@ export fn nextgr(in: io::handle) (grent | io::EOF | io::error | invalid) = { case io::EOF => return io::EOF; }; - let line = match (strings::try_fromutf8(line)) { + let line = match (strings::fromutf8(line)) { case let s: str => yield s; case => diff --git a/unix/passwd/passwd.ha b/unix/passwd/passwd.ha @@ -35,7 +35,7 @@ export fn nextpw(file: io::handle) (pwent | io::EOF | io::error | invalid) = { case let ln: []u8 => yield ln; }; - let line = match (strings::try_fromutf8(line)) { + let line = match (strings::fromutf8(line)) { case let s: str => yield s; case => diff --git a/unix/resolvconf/load.ha b/unix/resolvconf/load.ha @@ -52,7 +52,7 @@ fn loadio(src: io::handle) []ip::addr = { yield tok; }; defer free(tok); - if (strings::fromutf8(tok) != "nameserver") { + if (strings::fromutf8(tok)! != "nameserver") { continue; }; @@ -63,7 +63,7 @@ fn loadio(src: io::handle) []ip::addr = { yield tok; }; defer free(tok); - append(cache, ip::parse(strings::fromutf8(tok))!); + append(cache, ip::parse(strings::fromutf8(tok)!)!); }; return cache;