File: wrappers.ha

package info (click to toggle)
hare 0.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,352 kB
  • sloc: asm: 1,374; makefile: 123; sh: 117; lisp: 101
file content (125 lines) | stat: -rw-r--r-- 4,619 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use io;
use memio;
use os;
use strings;

// Formats text for printing and writes it to [[os::stdout]].
export fn printf(fmt: str, args: field...) (size | io::error) =
	fprintf(os::stdout, fmt, args...);

// Formats text for printing and writes it to [[os::stdout]], followed by a line
// feed.
export fn printfln(fmt: str, args: field...) (size | io::error) =
	fprintfln(os::stdout, fmt, args...);

// Formats text for printing and writes it to [[os::stderr]].
export fn errorf(fmt: str, args: field...) (size | io::error) =
	fprintf(os::stderr, fmt, args...);

// Formats text for printing and writes it to [[os::stderr]], followed by a line
// feed.
export fn errorfln(fmt: str, args: field...) (size | io::error) =
	fprintfln(os::stderr, fmt, args...);

// 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: field...) (str | nomem) = {
	let buf = memio::dynamic();
	match (fprintf(&buf, fmt, args...)) {
	case size => void;
	case let e: io::error =>
		return e as nomem;
	};
	return strings::fromutf8_unsafe(memio::buffer(&buf));
};

// Formats text for printing and writes it into a caller supplied buffer. The
// returned string is borrowed from this buffer. Returns nomem, if the buffer
// isn't large enough to hold the formatted text.
export fn bsprintf(buf: []u8, fmt: str, args: field...) (str | nomem) = {
	let sink = memio::fixed(buf);
	match (fprintf(&sink, fmt, args...)) {
	case let l: size =>
		return strings::fromutf8_unsafe(buf[..l]);
	case let e: io::error =>
		return e as nomem;
	};
};

// Formats text for printing and writes it to [[os::stderr]], followed by a line
// feed, then exits the program with an error status.
export fn fatalf(fmt: str, args: field...) never = {
	_ = fprintfln(os::stderr, fmt, args...);
	os::exit(255);
};

// Formats values for printing using the default format modifiers and writes
// them to [[os::stderr]] separated by spaces and followed by a line feed, then
// exits the program with an error status.
export fn fatal(args: formattable...) never = {
	_ = fprintln(os::stderr, args...);
	os::exit(255);
};

// Formats text for printing and writes it to an [[io::handle]], followed by a
// line feed.
export fn fprintfln(
	h: io::handle,
	fmt: str,
	args: field...
) (size | io::error) = fprintf(h, fmt, args...)? + io::write(h, ['\n'])?;

// Formats values for printing using the default format modifiers and writes
// them to [[os::stdout]] separated by spaces.
export fn print(args: formattable...) (size | io::error) =
	fprint(os::stdout, args...);

// Formats values for printing using the default format modifiers and writes
// them to [[os::stdout]] separated by spaces and followed by a line feed.
export fn println(args: formattable...) (size | io::error) =
	fprintln(os::stdout, args...);

// Formats values for printing using the default format modifiers and writes
// them to [[os::stderr]] separated by spaces.
export fn error(args: formattable...) (size | io::error) =
	fprint(os::stderr, args...);

// Formats values for printing using the default format modifiers and writes
// them to [[os::stderr]] separated by spaces and followed by a line feed.
export fn errorln(args: formattable...) (size | io::error) =
	fprintln(os::stderr, args...);

// Formats values for printing using the default format modifiers and writes
// them into a heap-allocated string separated by spaces. The caller must free
// the return value.
export fn asprint(args: formattable...) (str | nomem) = {
	let buf = memio::dynamic();
	match (fprint(&buf, args...)) {
	case size => void;
	case let e: io::error =>
		return e as nomem;
	};
	return strings::fromutf8_unsafe(memio::buffer(&buf));
};

// 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. Returns nomem, if the buffer isn't large enough
// to hold the formatted text.
export fn bsprint(buf: []u8, args: formattable...) (str | nomem) = {
	let sink = memio::fixed(buf);
	match (fprint(&sink, args...)) {
	case let l: size =>
		return strings::fromutf8_unsafe(buf[..l]);
	case let e: io::error =>
		return e as nomem;
	};
};

// Formats values for printing using the default format modifiers and writes
// them to an [[io::handle]] separated by spaces and followed by a line feed.
export fn fprintln(h: io::handle, args: formattable...) (size | io::error) =
	fprint(h, args...)? + io::write(h, ['\n'])?;