raz / site / public / tour / slices.html

350 lines · 26.2 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>Arrays, slices and strings - Raz, Set, Go</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="Arrays, slices and strings - Raz, Set, Go">
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" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">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>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
22<li><a href="hello.html">Hello, world</a></li>
23<li><a href="modules.html">Modules</a></li>
24<li><a href="integers.html">Integers</a></li>
25<li><a href="values.html">Values and conversions</a></li>
26<li><a href="procedures.html">Procedures and several results</a></li>
27</ol>
28<h3>Set</h3><ol start='6'>
29<li><a href="slices.html" aria-current="page">Arrays, slices and strings</a></li>
30<li><a href="records.html">Records and pointers</a></li>
31<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
32<li><a href="memory.html">Memory and allocators</a></li>
33</ol>
34<h3>Go</h3><ol start='10'>
35<li><a href="errors.html">Errors</a></li>
36<li><a href="defer.html">Defer</a></li>
37<li><a href="checks.html">When it goes wrong</a></li>
38<li><a href="generics.html">Generic modules</a></li>
39<li><a href="c.html">Talking to C</a></li>
40<li><a href="threads.html">Threads</a></li>
41</ol></aside><article><p class="kicker">Set &middot; lesson 6 of 15</p><h1>Arrays, slices and strings</h1>
42<div class="file"><span>Slices.raz</span></div><pre class="code"><code><span class="kw">module</span> Slices;
43<span class="kw">import</span> Out, Str;
44
45<span class="kw">proc</span> sum(s: []<span class="ty">INT</span>): <span class="ty">INT</span>;
46<span class="kw">var</span> i, t: <span class="ty">INT</span>;
47<span class="kw">begin</span>
48 t := <span class="number">0</span>;
49 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(s) - <span class="number">1</span> <span class="kw">do</span> t := t + s[i]; <span class="kw">end</span>;
50 <span class="kw">return</span> t;
51<span class="kw">end</span>;
52
53<span class="kw">proc</span> main*;
54<span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; s, val: []<span class="kw">const</span> <span class="ty">U8</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>; ok: <span class="ty">BOOL</span>;
55<span class="kw">begin</span>
56 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4</span> <span class="kw">do</span> a[i] := i * i; <span class="kw">end</span>;
57 Out.int(sum(a)); Out.ln; <span class="comment">(* the array, as a slice *)</span>
58 Out.int(sum(a[<span class="number">1</span>..<span class="number">3</span>])); Out.ln; <span class="comment">(* a[1] and a[2] *)</span>
59 s := <span class="string">&quot;hello, world&quot;</span>;
60 Out.str(s[<span class="number">7</span>..<span class="ty">LEN</span>(s)]); Out.ln;
61 <span class="kw">if</span> s[<span class="number">0</span>..<span class="number">5</span>] = <span class="string">&quot;hello&quot;</span> <span class="kw">then</span> Out.str(<span class="string">&quot;it says hello\n&quot;</span>); <span class="kw">end</span>;
62 Out.int(Str.find(s, <span class="string">&quot;world&quot;</span>)); Out.ln;
63 _, val, ok := Str.cut(<span class="string">&quot; size = 42 &quot;</span>, <span class="string">&quot;=&quot;</span>);
64 <span class="kw">if</span> ok <span class="kw">then</span> Out.str(Str.trim(val)); Out.ln; <span class="kw">end</span>;
65 name := <span class="string">&quot;raz&quot;</span>; <span class="comment">(* a copy, the program&#x27;s own *)</span>
66 name[<span class="number">0</span>] := <span class="string">&#x27;R&#x27;</span>;
67 Out.str(name[<span class="number">0</span>..<span class="number">3</span>]); Out.ln;
68 Out.str(<span class="string">`
69 C:\tmp\ &quot;as it stands&quot;
70 over lines
71 `</span>);
72<span class="kw">end</span>;
73
74<span class="kw">end</span> Slices.</code></pre>
75<pre class="output"><code>30
765
77world
78it says hello
797
8042
81Raz
82C:\tmp\ &quot;as it stands&quot;
83 over lines
84</code></pre>
85<h2>Arrays</h2>
86<p><code>[5]INT</code> is an array: five <code>INT</code>s in a row, copied when it is assigned or passed. Its length is part of its type, so <code>[5]INT</code> and <code>[6]INT</code> are different types, and <code>LEN(a)</code> of an array is a constant. Array literals are written in brackets, and need a type from the place they are used.</p>
87<div class="file"><span>Arrays.raz</span></div><pre class="code"><code><span class="kw">module</span> Arrays;
88<span class="kw">import</span> Out;
89
90<span class="kw">proc</span> main*;
91<span class="kw">var</span> a, b: [<span class="number">4</span>]<span class="ty">INT</span>; grid: [<span class="number">3</span>][<span class="number">3</span>]<span class="ty">INT</span>; i, j: <span class="ty">INT</span>;
92<span class="kw">begin</span>
93 a := [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>];
94 b := a; <span class="comment">(* a copy *)</span>
95 b[<span class="number">0</span>] := <span class="number">99</span>;
96 Out.int(a[<span class="number">0</span>]); Out.char(<span class="string">&#x27; &#x27;</span>); Out.int(b[<span class="number">0</span>]); Out.ln;
97 <span class="kw">if</span> a # b <span class="kw">then</span> Out.str(<span class="string">&quot;they differ now\n&quot;</span>); <span class="kw">end</span>;
98
99 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span>
100 <span class="kw">for</span> j := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span> grid[i][j] := i * <span class="number">3</span> + j; <span class="kw">end</span>;
101 <span class="kw">end</span>;
102 Out.int(grid[<span class="number">2</span>][<span class="number">1</span>]); Out.ln; <span class="comment">(* 7 *)</span>
103 Out.int(<span class="ty">LEN</span>(grid)); Out.char(<span class="string">&#x27;x&#x27;</span>); Out.int(<span class="ty">LEN</span>(grid[<span class="number">0</span>])); Out.ln;
104<span class="kw">end</span>;
105
106<span class="kw">end</span> Arrays.</code></pre>
107<pre class="output"><code>1 99
108they differ now
1097
1103x3
111</code></pre>
112<h2>Slices</h2>
113<p><code>[]INT</code> is a slice: a pointer to elements that live elsewhere, and their number, <code>LEN(s)</code>. <code>a[lo..hi]</code> is the slice of the elements from <code>lo</code> up to, but not including, <code>hi</code>, and an array goes silently where a slice is wanted. A slice doesn't copy anything: changing an element of it changes the array it looks at.</p>
114<div class="file"><span>Windows.raz</span></div><pre class="code"><code><span class="kw">module</span> Windows;
115<span class="kw">import</span> Out;
116
117<span class="kw">proc</span> zero(s: []<span class="ty">INT</span>);
118<span class="kw">var</span> i: <span class="ty">INT</span>;
119<span class="kw">begin</span>
120 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(s) - <span class="number">1</span> <span class="kw">do</span> s[i] := <span class="number">0</span>; <span class="kw">end</span>;
121<span class="kw">end</span>;
122
123<span class="kw">proc</span> show(s: []<span class="ty">INT</span>);
124<span class="kw">var</span> i: <span class="ty">INT</span>;
125<span class="kw">begin</span>
126 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(s) - <span class="number">1</span> <span class="kw">do</span> Out.int(s[i]); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="kw">end</span>;
127 Out.ln;
128<span class="kw">end</span>;
129
130<span class="kw">proc</span> main*;
131<span class="kw">var</span> a: [<span class="number">8</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>;
132<span class="kw">begin</span>
133 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">7</span> <span class="kw">do</span> a[i] := i + <span class="number">1</span>; <span class="kw">end</span>;
134 zero(a[<span class="number">2</span>..<span class="number">5</span>]); <span class="comment">(* a window onto a *)</span>
135 show(a); <span class="comment">(* 1 2 0 0 0 6 7 8 *)</span>
136 show(a[<span class="number">6</span>..<span class="number">8</span>]);
137 show(a[<span class="number">3</span>..<span class="number">3</span>]); <span class="comment">(* empty: prints just a line feed *)</span>
138<span class="kw">end</span>;
139
140<span class="kw">end</span> Windows.</code></pre>
141<pre class="output"><code>1 2 0 0 0 6 7 8
1427 8
143
144</code></pre>
145<p>Every index and every slice bound is checked, so a slice can never reach past what it was made from; the lesson on failures shows what that looks like.</p>
146<p>A slice of a local array is good only while its procedure runs: the array is gone once it returns. So returning one is an error, <code>return a[0..n]</code> or <code>return a</code> with <code>a</code> a local, a value parameter or an array literal. Take the array as a <code>var</code> parameter instead, and the slice is of the caller's array; or fill a buffer the caller gives.</p>
147<h2><code>COPY</code></h2>
148<p><code>COPY(d, s)</code> copies the elements of <code>s</code> to the start of <code>d</code>, as one block, and is right when they overlap. <code>LEN(s)</code> must be at most <code>LEN(d)</code>.</p>
149<div class="file"><span>Moves.raz</span></div><pre class="code"><code><span class="kw">module</span> Moves;
150<span class="kw">import</span> Out;
151
152<span class="kw">proc</span> main*;
153<span class="kw">var</span> a: [<span class="number">6</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>;
154<span class="kw">begin</span>
155 a := [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">0</span>];
156 COPY(a[<span class="number">1</span>..<span class="number">6</span>], a[<span class="number">0</span>..<span class="number">5</span>]); <span class="comment">(* shift everything one place up *)</span>
157 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">5</span> <span class="kw">do</span> Out.int(a[i]); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="kw">end</span>;
158 Out.ln;
159<span class="kw">end</span>;
160
161<span class="kw">end</span> Moves.</code></pre>
162<pre class="output"><code>1 1 2 3 4 5
163</code></pre>
164<h2>Strings</h2>
165<p>A string is a slice of bytes, <code>[]U8</code>, in UTF-8, and a string constant is a <code>[]const U8</code>: a procedure that only reads a string takes that, and one that writes takes <code>[]U8</code>. A <code>[8]U8</code> made from one is a copy you can change. Arrays and slices of integers compare with <code>=</code> and <code>#</code>, so strings do too. <code>Str</code> has the rest: ordering, finding, hashing, prefixes and suffixes, trimming, taking apart, ASCII case and kinds of character, UTF-8, and numbers to and from text.</p>
166<div class="file"><span>Words.raz</span></div><pre class="code"><code><span class="kw">module</span> Words;
167<span class="kw">import</span> Out, Str;
168
169<span class="kw">proc</span> main*;
170<span class="kw">var</span> s: []<span class="kw">const</span> <span class="ty">U8</span>; n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>;
171<span class="kw">begin</span>
172 s := <span class="string">&quot;hello, world&quot;</span>;
173 Out.str(s[<span class="number">7</span>..<span class="ty">LEN</span>(s)]); Out.ln;
174 <span class="kw">if</span> s[<span class="number">0</span>..<span class="number">5</span>] = <span class="string">&quot;hello&quot;</span> <span class="kw">then</span> Out.str(<span class="string">&quot;it says hello\n&quot;</span>); <span class="kw">end</span>;
175 Out.int(Str.find(s, <span class="string">&quot;world&quot;</span>)); Out.ln; <span class="comment">(* 7 *)</span>
176 Out.int(Str.find(s, <span class="string">&quot;xyz&quot;</span>)); Out.ln; <span class="comment">(* -1 *)</span>
177 Out.int(Str.findLast(<span class="string">&quot;abcabc&quot;</span>, <span class="string">&quot;bc&quot;</span>)); Out.ln; <span class="comment">(* 4 *)</span>
178 Out.int(Str.findFrom(s, <span class="string">&quot;o&quot;</span>, <span class="number">5</span>)); Out.ln; <span class="comment">(* 8: the first o from 5 on *)</span>
179 <span class="kw">if</span> Str.isDigit(<span class="string">&#x27;7&#x27;</span>) <span class="kw">and</span> <span class="kw">not</span> Str.isLetter(<span class="string">&#x27;7&#x27;</span>) <span class="kw">then</span> Out.str(<span class="string">&quot;a digit\n&quot;</span>); <span class="kw">end</span>;
180 <span class="kw">if</span> Str.hasPrefix(s, <span class="string">&quot;hell&quot;</span>) <span class="kw">then</span> Out.str(<span class="string">&quot;prefix\n&quot;</span>); <span class="kw">end</span>;
181 <span class="kw">if</span> Str.hasSuffix(s, <span class="string">&quot;world&quot;</span>) <span class="kw">then</span> Out.str(<span class="string">&quot;suffix\n&quot;</span>); <span class="kw">end</span>;
182 Out.int(Str.cmp(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>)); Out.ln; <span class="comment">(* -1 *)</span>
183 <span class="kw">if</span> Str.eqFold(<span class="string">&quot;RAZ&quot;</span>, <span class="string">&quot;raz&quot;</span>) <span class="kw">then</span> Out.str(<span class="string">&quot;the same, but for case\n&quot;</span>); <span class="kw">end</span>;
184 Out.str(<span class="string">&quot;[&quot;</span>); Out.str(Str.trim(<span class="string">&quot; padded \t\n&quot;</span>)); Out.str(<span class="string">&quot;]\n&quot;</span>);
185
186 n, ok := Str.toInt(<span class="string">&quot;-1234&quot;</span>);
187 <span class="kw">if</span> ok <span class="kw">then</span> Out.int(n + <span class="number">1</span>); Out.ln; <span class="kw">end</span>;
188 n, ok := Str.toInt(<span class="string">&quot;12x&quot;</span>);
189 <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">then</span> Out.str(<span class="string">&quot;not a number\n&quot;</span>); <span class="kw">end</span>;
190
191 name := <span class="string">&quot;raz&quot;</span>; <span class="comment">(* a copy, the program&#x27;s own *)</span>
192 name[<span class="number">0</span>] := <span class="string">&#x27;R&#x27;</span>;
193 Out.str(name[<span class="number">0</span>..<span class="number">3</span>]); Out.ln;
194 Str.upper(name[<span class="number">0</span>..<span class="number">3</span>]); <span class="comment">(* in place: name is a variable *)</span>
195 Out.str(name[<span class="number">0</span>..<span class="number">3</span>]); Out.ln;
196<span class="kw">end</span>;
197
198<span class="kw">end</span> Words.</code></pre>
199<pre class="output"><code>world
200it says hello
2017
202-1
2034
2048
205a digit
206prefix
207suffix
208-1
209the same, but for case
210[padded]
211-1233
212not a number
213Raz
214RAZ
215</code></pre>
216<h2>Taking a string apart</h2>
217<p><code>Str.cut</code> splits a string at the first place a separator is: what comes before it, what comes after, and whether it was there at all. <code>Str.field</code> gives the first word, past any spaces, and the rest. Neither copies anything: every part is a slice of the string it came from, so taking a line or a file apart costs no memory.</p>
218<div class="file"><span>Parts.raz</span></div><pre class="code"><code><span class="kw">module</span> Parts;
219<span class="kw">import</span> Out, Str;
220
221<span class="kw">const</span> Text = <span class="string">&quot;name = raz\nkind = language\nyear = 2026\n&quot;</span>;
222
223<span class="kw">proc</span> main*;
224<span class="kw">var</span> line, rest, key, val, w: []<span class="kw">const</span> <span class="ty">U8</span>; more, ok: <span class="ty">BOOL</span>;
225<span class="kw">begin</span>
226 <span class="comment">(* line by line, then each line at its = *)</span>
227 line, rest, more := Str.cut(Text, <span class="string">&quot;\n&quot;</span>);
228 <span class="kw">while</span> more <span class="kw">do</span>
229 key, val, ok := Str.cut(line, <span class="string">&quot;=&quot;</span>);
230 <span class="kw">if</span> ok <span class="kw">then</span>
231 Out.str(Str.trim(key)); Out.str(<span class="string">&quot; is &quot;</span>); Out.str(Str.trim(val)); Out.ln;
232 <span class="kw">end</span>;
233 line, rest, more := Str.cut(rest, <span class="string">&quot;\n&quot;</span>);
234 <span class="kw">end</span>;
235
236 <span class="comment">(* word by word *)</span>
237 w, rest := Str.field(<span class="string">&quot; the quick brown fox &quot;</span>);
238 <span class="kw">while</span> <span class="ty">LEN</span>(w) &gt; <span class="number">0</span> <span class="kw">do</span>
239 Out.str(<span class="string">&quot;&lt;&quot;</span>); Out.str(w); Out.str(<span class="string">&quot;&gt;&quot;</span>);
240 w, rest := Str.field(rest);
241 <span class="kw">end</span>;
242 Out.ln;
243<span class="kw">end</span>;
244
245<span class="kw">end</span> Parts.</code></pre>
246<pre class="output"><code>name is raz
247kind is language
248year is 2026
249&lt;the&gt;&lt;quick&gt;&lt;brown&gt;&lt;fox&gt;
250</code></pre>
251<p>When <code>Str.cut</code> finds no separator it gives the whole string, an empty one and <code>false</code>, so the loop above ends after the last line that has a line feed. Building a string goes the other way, in a <code>Buf</code>: <code>Str.join</code> adds parts with a separator between them, and <code>Str.replace</code> adds a string with every match of one text replaced by another (lesson 9 shows <code>Buf</code>).</p>
252<h2>Raw strings</h2>
253<p>A string between backquotes is raw: its bytes are the text as it stands, with no escapes, so a backslash or a double quote is itself, and it may run over lines. When the opening backquote ends its line and the closing one has only spaces and tabs before it, those blanks come off the start of every line between, and the text keeps the indent of the code it sits in. Each line ends in a line feed, the last one too.</p>
254<div class="file"><span>Raw.raz</span></div><pre class="code"><code><span class="kw">module</span> Raw;
255<span class="kw">import</span> Out;
256
257<span class="kw">const</span> Path = <span class="string">`C:\Users\raz\&quot;notes&quot;.txt`</span>;
258
259<span class="kw">proc</span> usage;
260<span class="kw">begin</span>
261 Out.str(<span class="string">`
262 usage: tool [-v] file ...
263 -v say what is done
264 file read this, not standard input
265 `</span>);
266<span class="kw">end</span>;
267
268<span class="kw">proc</span> main*;
269<span class="kw">begin</span>
270 Out.str(Path); Out.ln;
271 usage;
272 Out.str(<span class="string">`a raw string on one line, \n and all`</span>); Out.ln;
273<span class="kw">end</span>;
274
275<span class="kw">end</span> Raw.</code></pre>
276<pre class="output"><code>C:\Users\raz\&quot;notes&quot;.txt
277usage: tool [-v] file ...
278 -v say what is done
279 file read this, not standard input
280a raw string on one line, \n and all
281</code></pre>
282<p>A line of an indented raw string that doesn't start with the closing line's blanks is an error, so a slip in the indent is caught where it is made, not printed. A raw string can't hold a backquote; join one in with <code>+</code> and an ordinary string that holds it.</p>
283<h2>UTF-8</h2>
284<p>A character is an <code>I32</code>, its code point. A string stays bytes; UTF-8 is how <code>Str</code> reads them. <code>Str.next</code> walks a string a character at a time, giving each one and where the next starts, -1 at the end. <code>Str.decode</code> reads the first character of a string and says how many bytes it took, <code>Str.decodeLast</code> the last, and <code>Str.encode</code> goes the other way. <code>Str.chars</code> counts characters and <code>Str.valid</code> says whether a string is UTF-8 at all; a byte that doesn't start a character rightly reads as FFFDH, the character that stands for a wrong one. Byte length and character count are different things, and the program is the one that says which it wants. On a terminal there is a third: a character takes 0, 1 or 2 columns, a combining accent none and an ideograph two, and <code>Width.columns</code> counts those.</p>
285<div class="file"><span>Runes.raz</span></div><pre class="code"><code><span class="kw">module</span> Runes;
286<span class="kw">import</span> Out, Str, Width;
287
288<span class="kw">proc</span> main*;
289<span class="kw">var</span> s: []<span class="kw">const</span> <span class="ty">U8</span>; c: <span class="ty">I32</span>; i, size: <span class="ty">INT</span>; bytes: [<span class="number">4</span>]<span class="ty">U8</span>; k: <span class="ty">INT</span>;
290<span class="kw">begin</span>
291 s := <span class="string">&quot;caf\u{E9} \u{3BB}&quot;</span>;
292 Out.int(<span class="ty">LEN</span>(s)); Out.str(<span class="string">&quot; bytes, &quot;</span>);
293 Out.int(Str.chars(s)); Out.str(<span class="string">&quot; characters, &quot;</span>);
294 Out.int(Width.columns(<span class="string">&quot;\u{4E2D}\u{6587}&quot;</span>)); Out.str(<span class="string">&quot; columns for two ideographs\n&quot;</span>);
295
296 <span class="comment">(* each character, as its code point *)</span>
297 c, i := Str.next(s, <span class="number">0</span>);
298 <span class="kw">while</span> i &gt;= <span class="number">0</span> <span class="kw">do</span>
299 Out.int(<span class="ty">I64</span>(c)); Out.str(<span class="string">&quot; &quot;</span>);
300 c, i := Str.next(s, i);
301 <span class="kw">end</span>;
302 Out.ln;
303
304 c, size := Str.decodeLast(s);
305 Out.int(<span class="ty">I64</span>(c)); Out.str(<span class="string">&quot; is last, in &quot;</span>); Out.int(size); Out.str(<span class="string">&quot; bytes\n&quot;</span>);
306 bytes, k := Str.encode(<span class="number">955</span>);
307 Out.str(bytes[<span class="number">0</span>..k]); Out.ln;
308 <span class="kw">if</span> <span class="kw">not</span> Str.valid(s[<span class="number">0</span>..<span class="number">4</span>]) <span class="kw">then</span> Out.str(<span class="string">&quot;cut in the middle of a character\n&quot;</span>); <span class="kw">end</span>;
309<span class="kw">end</span>;
310
311<span class="kw">end</span> Runes.</code></pre>
312<pre class="output"><code>8 bytes, 6 characters, 4 columns for two ideographs
31399 97 102 233 32 955
314955 is last, in 2 bytes
315λ
316cut in the middle of a character
317</code></pre>
318<p><code>Str</code> knows UTF-8 and no more of Unicode: its case is ASCII's, and it doesn't say how wide a character is on a terminal or which spellings of a text are the same. Those need tables bigger than the rest of the library.</p>
319<h2>Loops over lengths</h2>
320<p>A <code>for</code> loop takes its bounds once, and never steps past the last: with an empty slice, <code>0 to LEN(s) - 1</code> doesn't run at all, and the variable can't wrap. <code>for</code> takes a constant step with <code>by</code>, and <code>while</code> and <code>repeat ... until</code> do the rest. <code>break</code> leaves a loop, and <code>continue</code> ends its round: a <code>for</code> steps, a <code>while</code> or a <code>repeat</code> tests again. So a <code>while</code> moves on before any <code>continue</code>, or it never moves at all.</p>
321<div class="file"><span>Loops.raz</span></div><pre class="code"><code><span class="kw">module</span> Loops;
322<span class="kw">import</span> Out;
323
324<span class="kw">proc</span> main*;
325<span class="kw">var</span> i: <span class="ty">INT</span>;
326<span class="kw">begin</span>
327 <span class="kw">for</span> i := <span class="number">10</span> <span class="kw">to</span> <span class="number">0</span> <span class="kw">by</span> -<span class="number">5</span> <span class="kw">do</span> Out.int(i); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="kw">end</span>;
328 Out.ln;
329 i := <span class="number">1</span>;
330 <span class="kw">while</span> i &lt; <span class="number">100</span> <span class="kw">do</span> i := i * <span class="number">3</span>; <span class="kw">end</span>;
331 Out.int(i); Out.ln;
332 <span class="kw">repeat</span> <span class="ty">DEC</span>(i, <span class="number">30</span>); <span class="kw">until</span> i &lt; <span class="number">0</span>;
333 Out.int(i); Out.ln;
334 <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">10</span> <span class="kw">do</span>
335 <span class="kw">if</span> i <span class="kw">mod</span> <span class="number">3</span> = <span class="number">0</span> <span class="kw">then</span> continue; <span class="kw">end</span>; <span class="comment">(* 3, 6 and 9 passed over *)</span>
336 Out.int(i); Out.char(<span class="string">&#x27; &#x27;</span>);
337 <span class="kw">end</span>;
338 Out.ln;
339<span class="kw">end</span>;
340
341<span class="kw">end</span> Loops.</code></pre>
342<pre class="output"><code>10 5 0
343243
344-27
3451 2 4 5 7 8 10
346</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="procedures.html">&larr; Procedures and several results</a><a href="index.html">Contents</a><a class="next" href="records.html">Records and pointers &rarr;</a></div></article></div>
347</main>
348<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
349</body>
350</html>