hare

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

commit 530920a08c0914862c528203d382dd086c552707
parent 4a1cf04c14cc9b6fce49f034de422aa23220eaae
Author: Drew DeVault <sir@cmpwn.com>
Date:   Tue, 23 Feb 2021 09:32:10 -0500

fmt: add asprintf

Diffstat:
Mfmt/fmt.ha | 23+++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/fmt/fmt.ha b/fmt/fmt.ha @@ -37,6 +37,7 @@ // // TODO: Expand this with more format modifiers use ascii; +use bufio; use encoding::utf8; use io; use os; @@ -48,24 +49,34 @@ use types; export type formattable = (...types::numeric | uintptr | str | rune | nullable *void); -// Formats text for printing writes it to [os::stdout]. +// Formats text for printing and writes it to [os::stdout]. export fn printf(fmt: str, args: formattable...) (io::error | size) = fprintf(os::stdout, fmt, args...); -// Formats text for printing writes it to [os::stdout], followed by a line feed. +// Formats text for printing and writes it to [os::stdout], followed by a line +// feed. export fn println(fmt: str, args: formattable...) (io::error | size) = fprintln(os::stdout, fmt, args...); -// Formats text for printing writes it to [os::stderr]. +// Formats text for printing and writes it to [os::stderr]. export fn errorf(fmt: str, args: formattable...) (io::error | size) = fprintf(os::stderr, fmt, args...); -// Formats text for printing writes it to [os::stderr], followed by a line feed. +// Formats text for printing and writes it to [os::stderr], followed by a line +// feed. export fn errorln(fmt: str, args: formattable...) (io::error | size) = fprintln(os::stderr, fmt, args...); -// Formats text for printing writes it to [os::stderr], followed by a line feed, -// then exits the program with an error status. +// Formats text for printing and writes it into a heap-allocated string. The +// caller must free the return value. +export fn asprintf(fmt: str, args: formattable...) str = { + let buf = bufio::dynamic(); + assert(fprintf(buf, fmt, args...) is size); + return strings::from_utf8_unsafe(bufio::buffer(buf) as []u8); +}; + +// Formats text for printing and writes it to [os::stderr], followed by a line +// feed, then exits the program with an error status. export @noreturn fn fatal(fmt: str, args: formattable...) void = { fprintln(os::stderr, fmt, args...); os::exit(1);