| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use io; |
| 5 |
use memio; |
| 6 |
use os; |
| 7 |
use strings; |
| 8 |
|
| 9 |
// Formats text for printing and writes it to [[os::stdout]]. |
| 10 |
export fn printf(fmt: str, args: field...) (size | io::error) = |
| 11 |
fprintf(os::stdout, fmt, args...); |
| 12 |
|
| 13 |
// Formats text for printing and writes it to [[os::stdout]], followed by a line |
| 14 |
// feed. |
| 15 |
export fn printfln(fmt: str, args: field...) (size | io::error) = |
| 16 |
fprintfln(os::stdout, fmt, args...); |
| 17 |
|
| 18 |
// Formats text for printing and writes it to [[os::stderr]]. |
| 19 |
export fn errorf(fmt: str, args: field...) (size | io::error) = |
| 20 |
fprintf(os::stderr, fmt, args...); |
| 21 |
|
| 22 |
// Formats text for printing and writes it to [[os::stderr]], followed by a line |
| 23 |
// feed. |
| 24 |
export fn errorfln(fmt: str, args: field...) (size | io::error) = |
| 25 |
fprintfln(os::stderr, fmt, args...); |
| 26 |
|
| 27 |
// Formats text for printing and writes it into a heap-allocated string. The |
| 28 |
// caller must free the return value. |
| 29 |
export fn asprintf(fmt: str, args: field...) (str | nomem) = { |
| 30 |
let buf = memio::dynamic(); |
| 31 |
match (fprintf(&buf, fmt, args...)) { |
| 32 |
case size => void; |
| 33 |
case let e: io::error => |
| 34 |
return e as nomem; |
| 35 |
}; |
| 36 |
return strings::fromutf8_unsafe(memio::buffer(&buf)); |
| 37 |
}; |
| 38 |
|
| 39 |
// Formats text for printing and writes it into a caller supplied buffer. The |
| 40 |
// returned string is borrowed from this buffer. Returns nomem, if the buffer |
| 41 |
// isn't large enough to hold the formatted text. |
| 42 |
export fn bsprintf(buf: []u8, fmt: str, args: field...) (str | nomem) = { |
| 43 |
let sink = memio::fixed(buf); |
| 44 |
match (fprintf(&sink, fmt, args...)) { |
| 45 |
case let l: size => |
| 46 |
return strings::fromutf8_unsafe(buf[..l]); |
| 47 |
case let e: io::error => |
| 48 |
return e as nomem; |
| 49 |
}; |
| 50 |
}; |
| 51 |
|
| 52 |
// Formats text for printing and writes it to [[os::stderr]], followed by a line |
| 53 |
// feed, then exits the program with an error status. |
| 54 |
export fn fatalf(fmt: str, args: field...) never = { |
| 55 |
_ = fprintfln(os::stderr, fmt, args...); |
| 56 |
os::exit(255); |
| 57 |
}; |
| 58 |
|
| 59 |
// Formats values for printing using the default format modifiers and writes |
| 60 |
// them to [[os::stderr]] separated by spaces and followed by a line feed, then |
| 61 |
// exits the program with an error status. |
| 62 |
export fn fatal(args: formattable...) never = { |
| 63 |
_ = fprintln(os::stderr, args...); |
| 64 |
os::exit(255); |
| 65 |
}; |
| 66 |
|
| 67 |
// Formats text for printing and writes it to an [[io::handle]], followed by a |
| 68 |
// line feed. |
| 69 |
export fn fprintfln( |
| 70 |
h: io::handle, |
| 71 |
fmt: str, |
| 72 |
args: field... |
| 73 |
) (size | io::error) = fprintf(h, fmt, args...)? + io::write(h, ['\n'])?; |
| 74 |
|
| 75 |
// Formats values for printing using the default format modifiers and writes |
| 76 |
// them to [[os::stdout]] separated by spaces. |
| 77 |
export fn print(args: formattable...) (size | io::error) = |
| 78 |
fprint(os::stdout, args...); |
| 79 |
|
| 80 |
// Formats values for printing using the default format modifiers and writes |
| 81 |
// them to [[os::stdout]] separated by spaces and followed by a line feed. |
| 82 |
export fn println(args: formattable...) (size | io::error) = |
| 83 |
fprintln(os::stdout, args...); |
| 84 |
|
| 85 |
// Formats values for printing using the default format modifiers and writes |
| 86 |
// them to [[os::stderr]] separated by spaces. |
| 87 |
export fn error(args: formattable...) (size | io::error) = |
| 88 |
fprint(os::stderr, args...); |
| 89 |
|
| 90 |
// Formats values for printing using the default format modifiers and writes |
| 91 |
// them to [[os::stderr]] separated by spaces and followed by a line feed. |
| 92 |
export fn errorln(args: formattable...) (size | io::error) = |
| 93 |
fprintln(os::stderr, args...); |
| 94 |
|
| 95 |
// Formats values for printing using the default format modifiers and writes |
| 96 |
// them into a heap-allocated string separated by spaces. The caller must free |
| 97 |
// the return value. |
| 98 |
export fn asprint(args: formattable...) (str | nomem) = { |
| 99 |
let buf = memio::dynamic(); |
| 100 |
match (fprint(&buf, args...)) { |
| 101 |
case size => void; |
| 102 |
case let e: io::error => |
| 103 |
return e as nomem; |
| 104 |
}; |
| 105 |
return strings::fromutf8_unsafe(memio::buffer(&buf)); |
| 106 |
}; |
| 107 |
|
| 108 |
// Formats values for printing using the default format modifiers and writes |
| 109 |
// them into a caller supplied buffer separated by spaces. The returned string |
| 110 |
// is borrowed from this buffer. Returns nomem, if the buffer isn't large enough |
| 111 |
// to hold the formatted text. |
| 112 |
export fn bsprint(buf: []u8, args: formattable...) (str | nomem) = { |
| 113 |
let sink = memio::fixed(buf); |
| 114 |
match (fprint(&sink, args...)) { |
| 115 |
case let l: size => |
| 116 |
return strings::fromutf8_unsafe(buf[..l]); |
| 117 |
case let e: io::error => |
| 118 |
return e as nomem; |
| 119 |
}; |
| 120 |
}; |
| 121 |
|
| 122 |
// Formats values for printing using the default format modifiers and writes |
| 123 |
// them to an [[io::handle]] separated by spaces and followed by a line feed. |
| 124 |
export fn fprintln(h: io::handle, args: formattable...) (size | io::error) = |
| 125 |
fprint(h, args...)? + io::write(h, ['\n'])?; |