hare

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

commit 1a2844156211254ef1c0e069c90e3e524db3d46b
parent c19cf06d6549e89b522229f8fc34df9a9e0a6671
Author: Sebastian <sebastian@sebsite.pw>
Date:   Fri,  8 Apr 2022 15:13:40 -0400

fmt: abort bsprint if buffer is too small

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

Diffstat:
Mfmt/fmt.ha | 12+++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/fmt/fmt.ha b/fmt/fmt.ha @@ -49,10 +49,11 @@ export fn asprintf(fmt: str, args: field...) str = { }; // Formats text for printing and writes it into a caller supplied buffer. The -// returned string is borrowed from this buffer. +// returned string is borrowed from this buffer. Aborts if the buffer isn't +// large enough to hold the formatted text. export fn bsprintf(buf: []u8, fmt: str, args: field...) str = { let sink = bufio::fixed(buf, io::mode::WRITE); - let l = fprintf(&sink, fmt, args...) as size; + let l = fprintf(&sink, fmt, args...)!; return strings::fromutf8_unsafe(buf[..l]); }; @@ -104,11 +105,12 @@ export fn asprint(args: formattable...) str = { // Formats values for printing using the default format modifiers and writes // them into a caller supplied buffer separated by spaces. The returned string -// is borrowed from this buffer. +// is borrowed from this buffer. Aborts if the buffer isn't large enough to hold +// the formatted text. export fn bsprint(buf: []u8, args: formattable...) str = { let sink = bufio::fixed(buf, io::mode::WRITE); - assert(fprint(&sink, args...) is size); - return strings::fromutf8_unsafe(buf); + let l = fprint(&sink, args...)!; + return strings::fromutf8_unsafe(buf[..l]); }; // Formats values for printing using the default format modifiers and writes