| 1 | title: Output and formatting |
| 2 | part: The library |
| 3 | summary: Out's buffer, Fmt's arguments, formatting into a Buf, and laying out columns. |
| 4 | --- |
| 5 | The 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 | |
| 11 | You 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 |
| 14 | module Three; |
| 15 | import Out; |
| 16 | |
| 17 | proc main*; |
| 18 | begin |
| 19 | Out.str("one\n"); |
| 20 | Out.str("two\n"); |
| 21 | Out.str("three\n"); |
| 22 | end; |
| 23 | |
| 24 | end Three. |
| 25 | ``` |
| 26 | |
| 27 | ```sh |
| 28 | $ raz Three.raz |
| 29 | $ strace -e trace=write ./Three > /dev/null |
| 30 | ``` |
| 31 | |
| 32 | Three 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 |
| 39 | module Kinds; |
| 40 | import Fmt; |
| 41 | |
| 42 | proc main*; |
| 43 | var big: U64; small: I8; |
| 44 | begin |
| 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))]); |
| 54 | end; |
| 55 | |
| 56 | end Kinds. |
| 57 | ``` |
| 58 | |
| 59 | An 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 |
| 66 | module Message; |
| 67 | import Out, Buf, Fmt, Heap; |
| 68 | |
| 69 | (* "name (age n)", built in a Buf the caller owns *) |
| 70 | proc describe(var b: Buf.Buf; name: []const U8; age: INT); |
| 71 | begin |
| 72 | Fmt.write(b, "{} (age {})", [Fmt.str(name), Fmt.int(age)]); |
| 73 | end; |
| 74 | |
| 75 | proc main*; |
| 76 | var b: Buf.Buf; |
| 77 | begin |
| 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"); |
| 85 | end; |
| 86 | |
| 87 | end 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 |
| 95 | module Table; |
| 96 | import Out; |
| 97 | |
| 98 | (* s, then spaces up to width *) |
| 99 | proc left(s: []const U8; width: INT); |
| 100 | var i: INT; |
| 101 | begin |
| 102 | Out.str(s); |
| 103 | for i := LEN(s) to width - 1 do Out.char(' '); end; |
| 104 | end; |
| 105 | |
| 106 | (* n, right aligned in width *) |
| 107 | proc right(n: INT; width: INT); |
| 108 | var digits: INT; v: INT; i: INT; |
| 109 | begin |
| 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); |
| 116 | end; |
| 117 | |
| 118 | proc row(name: []const U8; qty, price: INT); |
| 119 | begin |
| 120 | left(name, 10); |
| 121 | right(qty, 5); |
| 122 | right(price, 8); |
| 123 | right(qty * price, 9); |
| 124 | Out.ln; |
| 125 | end; |
| 126 | |
| 127 | proc main*; |
| 128 | begin |
| 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); |
| 134 | end; |
| 135 | |
| 136 | end 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 |
| 144 | module Dump; |
| 145 | import Out; |
| 146 | |
| 147 | proc dump(data: []const U8); |
| 148 | var i: INT; |
| 149 | begin |
| 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; |
| 155 | end; |
| 156 | |
| 157 | proc main*; |
| 158 | begin |
| 159 | dump("raz\n"); |
| 160 | dump("\u{3BB}"); |
| 161 | end; |
| 162 | |
| 163 | end Dump. |
| 164 | ``` |