raz / site / public / advanced / fmt.html

198 lines · 13.1 KiB · raw · history · last change de634aad

1<!doctype html>
2<html lang="en">
3<head>
4<meta charset="utf-8">
5<meta name="viewport" content="width=device-width, initial-scale=1">
6<title>Output and formatting - More Raz</title>
7<meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux.">
8<meta name="theme-color" content="#E07A5F">
9<meta property="og:title" content="Output and formatting - More Raz">
10<meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report.">
11<meta property="og:image" content="https://raz.lucela.org/social.png">
12<meta name="twitter:card" content="summary_large_image">
13<link rel="icon" href="../favicon.svg" type="image/svg+xml">
14<link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32">
15<link rel="apple-touch-icon" href="../apple-touch-icon.png">
16<link rel="stylesheet" href="../style.css">
17</head>
18<body>
19<header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header>
20<main>
21<div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'>
22<li><a href="fmt.html" aria-current="page">Output and formatting</a></li>
23<li><a href="bytes.html">Strings and bytes</a></li>
24<li><a href="collections.html">Lists, arrays, maps and sorting</a></li>
25<li><a href="files.html">Files and the command line</a></li>
26<li><a href="processes.html">Time, randomness and other programs</a></li>
27<li><a href="allocators.html">Writing an allocator</a></li>
28</ol>
29<h3>Standing alone</h3><ol start='7'>
30<li><a href="static.html">A program with no run time</a></li>
31<li><a href="linkstatic.html">Threads without C, and keeping the two apart</a></li>
32<li><a href="lifecycle.html">Starting, ending, and signals</a></li>
33<li><a href="targets.html">One module, one file per target</a></li>
34</ol>
35<h3>The machine</h3><ol start='11'>
36<li><a href="asm.html">Procedures in assembly</a></li>
37<li><a href="syscalls.html">System calls</a></li>
38<li><a href="listing.html">What the compiler makes</a></li>
39</ol>
40<h3>Shared memory</h3><ol start='14'>
41<li><a href="volatile.html">Volatile</a></li>
42<li><a href="atomics.html">Atomics</a></li>
43<li><a href="ordering.html">Ordering and publication</a></li>
44<li><a href="lockfree.html">A lock-free stack</a></li>
45<li><a href="ring.html">A ring buffer between threads</a></li>
46</ol></aside><article><p class="kicker">The library &middot; lesson 1 of 18</p><h1>Output and formatting</h1>
47<p>The first tour used <code>Out</code> for everything it printed. Two modules do the work: <code>Out</code>, which owns standard output and its buffer, and <code>Fmt</code>, which turns values into text.</p>
48<h2>What <code>Out</code> does with its buffer</h2>
49<p><code>Out</code> 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 <code>Out.flush</code>, and when the program ends by <code>Rt.exit</code> (which a normal end and a failed check both are). A lock keeps what each call writes together when threads write at once.</p>
50<p>You can see the effect with <code>strace</code>, which lists the system calls a program makes. A program that prints three lines to a file makes one <code>write</code>:</p>
51<div class="file"><span>Three.raz</span></div><pre class="code"><code><span class="kw">module</span> Three;
52<span class="kw">import</span> Out;
53
54<span class="kw">proc</span> main*;
55<span class="kw">begin</span>
56 Out.str(<span class="string">&quot;one\n&quot;</span>);
57 Out.str(<span class="string">&quot;two\n&quot;</span>);
58 Out.str(<span class="string">&quot;three\n&quot;</span>);
59<span class="kw">end</span>;
60
61<span class="kw">end</span> Three.</code></pre>
62<pre class="output term"><code><span class="cmd">$ raz Three.raz</span>
63<span class="cmd">$ strace -e trace=write ./Three &gt; /dev/null</span>
64write(1, &quot;one\ntwo\nthree\n&quot;, 14) = 14
65+++ exited with 0 +++</code></pre>
66<p>Three lines, one system call. That is why <code>Out</code> is cheap, and why a program that mixes it with C's <code>printf</code>, which keeps a buffer of its own, must flush one before the other.</p>
67<h2><code>Fmt.print</code></h2>
68<p><code>Fmt.print(format, args)</code> writes to standard output. Each <code>{}</code> in the format takes the next argument, and <code>{{</code> and <code>}}</code> stand for the braces. The arguments are a slice of <code>Fmt.Arg</code>, a variant, so each is built with the constructor for its kind: <code>int</code>, <code>uint</code>, <code>hex</code>, <code>str</code>, <code>char</code>, <code>bool</code> and <code>real</code>, which takes the number of digits after the point.</p>
69<div class="file"><span>Kinds.raz</span></div><pre class="code"><code><span class="kw">module</span> Kinds;
70<span class="kw">import</span> Fmt;
71
72<span class="kw">proc</span> main*;
73<span class="kw">var</span> big: <span class="ty">U64</span>; small: <span class="ty">I8</span>;
74<span class="kw">begin</span>
75 big := <span class="number">0FFFFFFFFFFFFFFFFH</span>;
76 small := -<span class="number">5</span>;
77 Fmt.print(<span class="string">&quot;int {}, uint {}, hex {}\n&quot;</span>,
78 [Fmt.int(-<span class="number">42</span>), Fmt.uint(big), Fmt.hex(<span class="number">255</span>)]);
79 Fmt.print(<span class="string">&quot;str {}, char {}, bool {}\n&quot;</span>,
80 [Fmt.str(<span class="string">&quot;hi&quot;</span>), Fmt.char(<span class="string">&#x27;x&#x27;</span>), Fmt.bool(<span class="number">1</span> &lt; <span class="number">2</span>)]);
81 Fmt.print(<span class="string">&quot;real {} and {} and {}\n&quot;</span>,
82 [Fmt.real(<span class="number">3.14159</span>, <span class="number">2</span>), Fmt.real(<span class="number">2.5</span>, <span class="number">0</span>), Fmt.real(<span class="number">1.0</span> / <span class="number">3.0</span>, <span class="number">9</span>)]);
83 Fmt.print(<span class="string">&quot;an I8: {}; braces: {{ and }}\n&quot;</span>, [Fmt.int(<span class="ty">I64</span>(small))]);
84<span class="kw">end</span>;
85
86<span class="kw">end</span> Kinds.</code></pre>
87<pre class="output"><code>int -42, uint 18446744073709551615, hex FF
88str hi, char x, bool true
89real 3.14 and 3 and 0.333333333
90an I8: -5; braces: { and }
91</code></pre>
92<p>An argument whose type is wrong doesn't compile: there is no <code>%d</code> that disagrees with its argument. A <code>{}</code> 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.</p>
93<h2>Into a buffer</h2>
94<p><code>Fmt.write(b, format, args)</code> does the same into a <code>Buf</code> 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.</p>
95<div class="file"><span>Message.raz</span></div><pre class="code"><code><span class="kw">module</span> Message;
96<span class="kw">import</span> Out, Buf, Fmt, Heap;
97
98<span class="comment">(* &quot;name (age n)&quot;, built in a Buf the caller owns *)</span>
99<span class="kw">proc</span> describe(<span class="kw">var</span> b: Buf.Buf; name: []<span class="kw">const</span> <span class="ty">U8</span>; age: <span class="ty">INT</span>);
100<span class="kw">begin</span>
101 Fmt.write(b, <span class="string">&quot;{} (age {})&quot;</span>, [Fmt.str(name), Fmt.int(age)]);
102<span class="kw">end</span>;
103
104<span class="kw">proc</span> main*;
105<span class="kw">var</span> b: Buf.Buf;
106<span class="kw">begin</span>
107 Buf.init(b, Heap.heap);
108 <span class="kw">defer</span> Buf.done(b);
109 describe(b, <span class="string">&quot;Ada&quot;</span>, <span class="number">36</span>);
110 Buf.str(b, <span class="string">&quot;; &quot;</span>);
111 describe(b, <span class="string">&quot;Grace&quot;</span>, <span class="number">45</span>);
112 Out.str(Buf.bytes(b)); Out.ln;
113 Out.int(b.len); Out.str(<span class="string">&quot; bytes\n&quot;</span>);
114<span class="kw">end</span>;
115
116<span class="kw">end</span> Message.</code></pre>
117<pre class="output"><code>Ada (age 36); Grace (age 45)
11828 bytes
119</code></pre>
120<h2>Columns</h2>
121<p><code>Fmt</code> 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.</p>
122<div class="file"><span>Table.raz</span></div><pre class="code"><code><span class="kw">module</span> Table;
123<span class="kw">import</span> Out;
124
125<span class="comment">(* s, then spaces up to width *)</span>
126<span class="kw">proc</span> left(s: []<span class="kw">const</span> <span class="ty">U8</span>; width: <span class="ty">INT</span>);
127<span class="kw">var</span> i: <span class="ty">INT</span>;
128<span class="kw">begin</span>
129 Out.str(s);
130 <span class="kw">for</span> i := <span class="ty">LEN</span>(s) <span class="kw">to</span> width - <span class="number">1</span> <span class="kw">do</span> Out.char(<span class="string">&#x27; &#x27;</span>); <span class="kw">end</span>;
131<span class="kw">end</span>;
132
133<span class="comment">(* n, right aligned in width *)</span>
134<span class="kw">proc</span> right(n: <span class="ty">INT</span>; width: <span class="ty">INT</span>);
135<span class="kw">var</span> digits: <span class="ty">INT</span>; v: <span class="ty">INT</span>; i: <span class="ty">INT</span>;
136<span class="kw">begin</span>
137 digits := <span class="number">1</span>;
138 v := n;
139 <span class="kw">if</span> v &lt; <span class="number">0</span> <span class="kw">then</span> <span class="ty">INC</span>(digits); v := -v; <span class="kw">end</span>;
140 <span class="kw">while</span> v &gt;= <span class="number">10</span> <span class="kw">do</span> <span class="ty">INC</span>(digits); v := v <span class="kw">div</span> <span class="number">10</span>; <span class="kw">end</span>;
141 <span class="kw">for</span> i := digits <span class="kw">to</span> width - <span class="number">1</span> <span class="kw">do</span> Out.char(<span class="string">&#x27; &#x27;</span>); <span class="kw">end</span>;
142 Out.int(n);
143<span class="kw">end</span>;
144
145<span class="kw">proc</span> row(name: []<span class="kw">const</span> <span class="ty">U8</span>; qty, price: <span class="ty">INT</span>);
146<span class="kw">begin</span>
147 left(name, <span class="number">10</span>);
148 right(qty, <span class="number">5</span>);
149 right(price, <span class="number">8</span>);
150 right(qty * price, <span class="number">9</span>);
151 Out.ln;
152<span class="kw">end</span>;
153
154<span class="kw">proc</span> main*;
155<span class="kw">begin</span>
156 Out.str(<span class="string">&quot;item qty price total\n&quot;</span>);
157 row(<span class="string">&quot;bolts&quot;</span>, <span class="number">120</span>, <span class="number">3</span>);
158 row(<span class="string">&quot;washers&quot;</span>, <span class="number">1000</span>, <span class="number">1</span>);
159 row(<span class="string">&quot;gears&quot;</span>, <span class="number">8</span>, <span class="number">250</span>);
160 row(<span class="string">&quot;shims&quot;</span>, <span class="number">12</span>, -<span class="number">4</span>);
161<span class="kw">end</span>;
162
163<span class="kw">end</span> Table.</code></pre>
164<pre class="output"><code>item qty price total
165bolts 120 3 360
166washers 1000 1 1000
167gears 8 250 2000
168shims 12 -4 -48
169</code></pre>
170<h2>Writing numbers in other bases</h2>
171<p><code>Out.hex(n, digits)</code> writes <code>n</code> in hexadecimal, padded with zeros to <code>digits</code>. A binary dump is a <code>for</code> loop over a slice, one <code>Out.hex(U64(b), 2)</code> to a byte.</p>
172<div class="file"><span>Dump.raz</span></div><pre class="code"><code><span class="kw">module</span> Dump;
173<span class="kw">import</span> Out;
174
175<span class="kw">proc</span> dump(data: []<span class="kw">const</span> <span class="ty">U8</span>);
176<span class="kw">var</span> i: <span class="ty">INT</span>;
177<span class="kw">begin</span>
178 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(data) - <span class="number">1</span> <span class="kw">do</span>
179 <span class="kw">if</span> i &gt; <span class="number">0</span> <span class="kw">then</span> Out.char(<span class="string">&#x27; &#x27;</span>); <span class="kw">end</span>;
180 Out.hex(<span class="ty">U64</span>(data[i]), <span class="number">2</span>);
181 <span class="kw">end</span>;
182 Out.ln;
183<span class="kw">end</span>;
184
185<span class="kw">proc</span> main*;
186<span class="kw">begin</span>
187 dump(<span class="string">&quot;raz\n&quot;</span>);
188 dump(<span class="string">&quot;\u{3BB}&quot;</span>);
189<span class="kw">end</span>;
190
191<span class="kw">end</span> Dump.</code></pre>
192<pre class="output"><code>72 61 7A 0A
193CE BB
194</code></pre><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="index.html">Contents</a><a class="next" href="bytes.html">Strings and bytes &rarr;</a></div></article></div>
195</main>
196<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
197</body>
198</html>