wrappers.ha (4283B)
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...) (io::error | size) = 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...) (io::error | size) = 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...) (io::error | size) = 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...) (io::error | size) = 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 = { 30 let buf = memio::dynamic(); 31 assert(fprintf(&buf, fmt, args...) is size); 32 return strings::fromutf8_unsafe(memio::buffer(&buf)); 33 }; 34 35 // Formats text for printing and writes it into a caller supplied buffer. The 36 // returned string is borrowed from this buffer. Aborts if the buffer isn't 37 // large enough to hold the formatted text. 38 export fn bsprintf(buf: []u8, fmt: str, args: field...) str = { 39 let sink = memio::fixed(buf); 40 let l = fprintf(&sink, fmt, args...)!; 41 return strings::fromutf8_unsafe(buf[..l]); 42 }; 43 44 // Formats text for printing and writes it to [[os::stderr]], followed by a line 45 // feed, then exits the program with an error status. 46 export fn fatalf(fmt: str, args: field...) never = { 47 fprintfln(os::stderr, fmt, args...)!; 48 os::exit(255); 49 }; 50 51 // Formats values for printing using the default format modifiers and writes 52 // them to [[os::stderr]] separated by spaces and followed by a line feed, then 53 // exits the program with an error status. 54 export fn fatal(args: formattable...) never = { 55 fprintln(os::stderr, args...)!; 56 os::exit(255); 57 }; 58 59 // Formats text for printing and writes it to an [[io::handle]], followed by a 60 // line feed. 61 export fn fprintfln( 62 h: io::handle, 63 fmt: str, 64 args: field... 65 ) (io::error | size) = fprintf(h, fmt, args...)? + io::write(h, ['\n'])?; 66 67 // Formats values for printing using the default format modifiers and writes 68 // them to [[os::stdout]] separated by spaces. 69 export fn print(args: formattable...) (io::error | size) = 70 fprint(os::stdout, args...); 71 72 // Formats values for printing using the default format modifiers and writes 73 // them to [[os::stdout]] separated by spaces and followed by a line feed. 74 export fn println(args: formattable...) (io::error | size) = 75 fprintln(os::stdout, args...); 76 77 // Formats values for printing using the default format modifiers and writes 78 // them to [[os::stderr]] separated by spaces. 79 export fn error(args: formattable...) (io::error | size) = 80 fprint(os::stderr, args...); 81 82 // Formats values for printing using the default format modifiers and writes 83 // them to [[os::stderr]] separated by spaces and followed by a line feed. 84 export fn errorln(args: formattable...) (io::error | size) = 85 fprintln(os::stderr, args...); 86 87 // Formats values for printing using the default format modifiers and writes 88 // them into a heap-allocated string separated by spaces. The caller must free 89 // the return value. 90 export fn asprint(args: formattable...) str = { 91 let buf = memio::dynamic(); 92 assert(fprint(&buf, args...) is size); 93 return strings::fromutf8_unsafe(memio::buffer(&buf)); 94 }; 95 96 // Formats values for printing using the default format modifiers and writes 97 // them into a caller supplied buffer separated by spaces. The returned string 98 // is borrowed from this buffer. Aborts if the buffer isn't large enough to hold 99 // the formatted text. 100 export fn bsprint(buf: []u8, args: formattable...) str = { 101 let sink = memio::fixed(buf); 102 let l = fprint(&sink, args...)!; 103 return strings::fromutf8_unsafe(buf[..l]); 104 }; 105 106 // Formats values for printing using the default format modifiers and writes 107 // them to an [[io::handle]] separated by spaces and followed by a line feed. 108 export fn fprintln(h: io::handle, args: formattable...) (io::error | size) = 109 fprint(h, args...)? + io::write(h, ['\n'])?;