raz / site / content / advanced / 01-fmt.txt

164 lines · 4.5 KiB · raw · history · last change a5145f64

1title: Output and formatting
2part: The library
3summary: Out's buffer, Fmt's arguments, formatting into a Buf, and laying out columns.
4---
5The first tour used `Out` for everything it printed. Two modules do the work: `Out`, which owns standard output and its buffer, and `Fmt`, which turns values into text.
6
7## What `Out` does with its buffer
8
9`Out` keeps a buffer of 4 KB. It writes it out when it is full, at each line feed when standard output is a terminal, at `Out.flush`, and when the program ends by `Rt.exit` (which a normal end and a failed check both are). A lock keeps what each call writes together when threads write at once.
10
11You can see the effect with `strace`, which lists the system calls a program makes. A program that prints three lines to a file makes one `write`:
12
13```raz Three
14module Three;
15import Out;
16
17proc main*;
18begin
19 Out.str("one\n");
20 Out.str("two\n");
21 Out.str("three\n");
22end;
23
24end Three.
25```
26
27```sh
28$ raz Three.raz
29$ strace -e trace=write ./Three > /dev/null
30```
31
32Three lines, one system call. That is why `Out` is cheap, and why a program that mixes it with C's `printf`, which keeps a buffer of its own, must flush one before the other.
33
34## `Fmt.print`
35
36`Fmt.print(format, args)` writes to standard output. Each `{}` in the format takes the next argument, and `{{` and `}}` stand for the braces. The arguments are a slice of `Fmt.Arg`, a variant, so each is built with the constructor for its kind: `int`, `uint`, `hex`, `str`, `char`, `bool` and `real`, which takes the number of digits after the point.
37
38```raz Kinds run
39module Kinds;
40import Fmt;
41
42proc main*;
43var big: U64; small: I8;
44begin
45 big := 0FFFFFFFFFFFFFFFFH;
46 small := -5;
47 Fmt.print("int {}, uint {}, hex {}\n",
48 [Fmt.int(-42), Fmt.uint(big), Fmt.hex(255)]);
49 Fmt.print("str {}, char {}, bool {}\n",
50 [Fmt.str("hi"), Fmt.char('x'), Fmt.bool(1 < 2)]);
51 Fmt.print("real {} and {} and {}\n",
52 [Fmt.real(3.14159, 2), Fmt.real(2.5, 0), Fmt.real(1.0 / 3.0, 9)]);
53 Fmt.print("an I8: {}; braces: {{ and }}\n", [Fmt.int(I64(small))]);
54end;
55
56end Kinds.
57```
58
59An argument whose type is wrong doesn't compile: there is no `%d` that disagrees with its argument. A `{}` with no argument left prints nothing, and arguments left over are not printed, so a mismatch shows up as missing text, never as a crash or as memory read by mistake.
60
61## Into a buffer
62
63`Fmt.write(b, format, args)` does the same into a `Buf` instead of standard output. That is how a message is built to send somewhere else: a file, a socket, a window, a string kept for later.
64
65```raz Message run
66module Message;
67import Out, Buf, Fmt, Heap;
68
69(* "name (age n)", built in a Buf the caller owns *)
70proc describe(var b: Buf.Buf; name: []const U8; age: INT);
71begin
72 Fmt.write(b, "{} (age {})", [Fmt.str(name), Fmt.int(age)]);
73end;
74
75proc main*;
76var b: Buf.Buf;
77begin
78 Buf.init(b, Heap.heap);
79 defer Buf.done(b);
80 describe(b, "Ada", 36);
81 Buf.str(b, "; ");
82 describe(b, "Grace", 45);
83 Out.str(Buf.bytes(b)); Out.ln;
84 Out.int(b.len); Out.str(" bytes\n");
85end;
86
87end Message.
88```
89
90## Columns
91
92`Fmt` writes values as they are, with no widths. A program that wants columns pads in one small procedure and uses it everywhere, which is easier to read than a format language.
93
94```raz Table run
95module Table;
96import Out;
97
98(* s, then spaces up to width *)
99proc left(s: []const U8; width: INT);
100var i: INT;
101begin
102 Out.str(s);
103 for i := LEN(s) to width - 1 do Out.char(' '); end;
104end;
105
106(* n, right aligned in width *)
107proc right(n: INT; width: INT);
108var digits: INT; v: INT; i: INT;
109begin
110 digits := 1;
111 v := n;
112 if v < 0 then INC(digits); v := -v; end;
113 while v >= 10 do INC(digits); v := v div 10; end;
114 for i := digits to width - 1 do Out.char(' '); end;
115 Out.int(n);
116end;
117
118proc row(name: []const U8; qty, price: INT);
119begin
120 left(name, 10);
121 right(qty, 5);
122 right(price, 8);
123 right(qty * price, 9);
124 Out.ln;
125end;
126
127proc main*;
128begin
129 Out.str("item qty price total\n");
130 row("bolts", 120, 3);
131 row("washers", 1000, 1);
132 row("gears", 8, 250);
133 row("shims", 12, -4);
134end;
135
136end Table.
137```
138
139## Writing numbers in other bases
140
141`Out.hex(n, digits)` writes `n` in hexadecimal, padded with zeros to `digits`. A binary dump is a `for` loop over a slice, one `Out.hex(U64(b), 2)` to a byte.
142
143```raz Dump run
144module Dump;
145import Out;
146
147proc dump(data: []const U8);
148var i: INT;
149begin
150 for i := 0 to LEN(data) - 1 do
151 if i > 0 then Out.char(' '); end;
152 Out.hex(U64(data[i]), 2);
153 end;
154 Out.ln;
155end;
156
157proc main*;
158begin
159 dump("raz\n");
160 dump("\u{3BB}");
161end;
162
163end Dump.
164```