raz / lib / Fmt.raz

629 lines · 22.0 KiB · raw · history · last change e0d008cd

1(* Formatted output. Each {} in the format takes the next argument:
2 Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)]);
3 {{ and }} stand for { and }. A {} with no argument left prints
4 nothing, and arguments left over are not printed.
5 A width between the braces pads the argument to that many bytes:
6 {3} on the left with spaces, {03} on the left with zeros (after a
7 minus sign), {-3} on the right with spaces. A longer argument is
8 written whole. A { followed by anything else is no width.
9 Left out: precision, other fills, argument numbers and names. *)
10module Fmt;
11import Out, Buf, Arena, Rt, Sys;
12import SYSTEM;
13
14const
15 Limb = 1000000000; (* a Big's base, 10^9 *)
16 LimbDigits = 9; (* the decimal digits of a limb *)
17 LimbsMax = 90; (* limbs of the largest Big: 769 digits, for the least F64's lo *)
18 MostDigits = 17; (* digits that always read back as the F64 they came from *)
19 MantBits = 52; (* an F64's fraction bits *)
20 ExpBias = 1075; (* its exponent's bias, with the fraction a whole number *)
21 ExpMask = 7FFH; (* its exponent's bits, shifted down *)
22 TwoStep = 29; (* doublings at once: 10^9 * 2^29 < 2^63 *)
23 FiveStep = 13; (* fives multiplied at once: 10^9 * 5^13 < 2^63 *)
24
25const ShortMost* = 24; (* the longest short: -2.2250738585072014E-308 *)
26
27type
28 (* An argument, and how it is printed. A real has places digits
29 after the point, at most 9, rounded to the nearest, half away
30 from zero; from 1E15 on, it has the exponent that makes it less
31 than 10, as 1.50E17; nan and inf are spelled so. *)
32 Arg* = variant
33 int(i: I64); (* in decimal *)
34 uint(u: U64); (* in decimal *)
35 hex(h: U64); (* in hexadecimal, capitals, no 0x *)
36 lowHex(h: U64); (* in hexadecimal, lower case, no 0x *)
37 str(s: []const U8);
38 char(c: U8); (* the one byte *)
39 bool(b: BOOL); (* true or false *)
40 real(r: F64; places: INT);
41 end;
42
43 (* Where the pieces go: to out if slice is set, else to b, or to
44 standard output if b is nil. n counts the bytes given, so a
45 slice holds the first LEN(out) of them and the rest are cut.
46 With dry set nothing is written and n only counts the bytes
47 that would be, to know how much a width must pad. *)
48 Sink = record
49 b: ^Buf.Buf;
50 out: []U8;
51 slice, dry: BOOL;
52 n: INT;
53 end;
54
55 (* What a {} says: its width, whether it pads with zeros, and
56 whether on the right. *)
57 Spec = record
58 width: INT;
59 zero, left: BOOL;
60 end;
61
62 (* A whole number, not negative, in base 10^9, the lowest limb
63 first. *)
64 Big = record
65 v: [LimbsMax]U64;
66 n: INT; (* the limbs in use *)
67 end;
68
69 (* A number above 0, exactly in decimal: 0.d times 10^e, its first
70 digit not 0. *)
71 Exact = record
72 d: [LimbsMax * LimbDigits]U8; (* '0' to '9' *)
73 n, e: INT; (* how many digits, and the exponent *)
74 end;
75
76 (* A number above 0 as short writes it, in the same way. *)
77 Short = record
78 d: [MostDigits]U8;
79 n, e: INT;
80 end;
81
82(* The digits of u in base, at the end of the array: it and where they
83 start. Digits past 9 are letters from letter on, 'A' or 'a'. *)
84proc digits(u: U64; base: U64; letter: U8): ([24]U8, INT);
85var d: [24]U8; i: INT; v: U8;
86begin
87 i := 24;
88 repeat
89 DEC(i);
90 v := U8(u mod base);
91 if v < 10 then d[i] := v + '0'; else d[i] := v - 10 + letter; end;
92 u := u div base;
93 until u = 0;
94 return d, i;
95end;
96
97(* A sink that writes to b, or to standard output if b is nil. *)
98proc sink(b: ^Buf.Buf): Sink;
99var k: Sink;
100begin
101 k.b := b; k.out := []; k.slice := false; k.dry := false; k.n := 0;
102 return k;
103end;
104
105(* s to k. *)
106proc put(var k: Sink; s: []const U8);
107var at, room: INT;
108begin
109 at := k.n;
110 INC(k.n, LEN(s));
111 if k.dry then
112 elsif k.slice then
113 room := LEN(k.out) - at; (* what is past the end is cut *)
114 if room > LEN(s) then room := LEN(s); end;
115 if room > 0 then COPY(k.out[at..at + room], s[0..room]); end;
116 elsif k.b = nil then
117 Out.str(s);
118 else
119 Buf.str(k.b^, s);
120 end;
121end;
122
123(* n copies of c to k. *)
124proc fill(var k: Sink; c: U8; n: INT);
125var one: [1]U8; i: INT;
126begin
127 one[0] := c;
128 for i := 1 to n do put(k, one); end;
129end;
130
131(* x as Arg's real says, but for the minus sign, which arg writes. The
132 whole part and the fraction are taken apart, so that each fits an
133 I64: hence the exponent from 1E15. *)
134proc putReal(var k: Sink; x: F64; places: INT);
135const zeros = "00000000";
136var d: [24]U8; at, i, e: INT; scale, whole, frac: I64;
137begin
138 if x # x then put(k, "nan"); return; end;
139 if x < 0.0 then x := -x; end;
140 if x > 1.7976931348623157E308 then put(k, "inf"); return; end;
141 if places < 0 then places := 0; elsif places > 9 then places := 9; end;
142 scale := 1;
143 for i := 1 to places do scale := scale * 10; end;
144 if x >= 1.0E15 then
145 while x >= 10.0 do x := x / 10.0; INC(e); end;
146 end;
147 whole := I64(x);
148 frac := I64((x - F64(whole)) * F64(scale) + 0.5);
149 if frac = scale then INC(whole); frac := 0; end;
150 if (e > 0) and (whole = 10) then whole := 1; INC(e); end;
151 d, at := digits(U64(whole), 10, 'A');
152 put(k, d[at..24]);
153 if places > 0 then
154 put(k, ".");
155 d, at := digits(U64(frac), 10, 'A');
156 put(k, zeros[0..places - (24 - at)]);
157 put(k, d[at..24]);
158 end;
159 if e > 0 then
160 d, at := digits(U64(e), 10, 'A');
161 put(k, "E");
162 put(k, d[at..24]);
163 end;
164end;
165
166(* ---- short: the fewest digits that read back ----
167
168 An F64 x is m times 2^k, m and k whole numbers. Every number between
169 the halves to the F64s on either side of x reads back as x, so short
170 looks for the fewest digits that land between those halves, lo and
171 hi:
172
173 below lo x hi above the F64s around x
174 |--------|------*------|--------|
175
176 x, lo and hi are written out exactly in decimal, each an Exact, and
177 compared digit by digit with each try, so nothing is rounded on the
178 way. For p digits the tries are x cut to p digits and that one more
179 in its last place: the two numbers of p digits nearest x. The first
180 p for which one of them lies between lo and hi gives the answer; if
181 both do, the nearer x is taken. 17 digits are always enough.
182
183 An exact value is made in a Big, a whole number in base 10^9: for k
184 at least 0, m times 2^k is m doubled k times; for k below 0, it is m
185 times 5^-k with the point -k places from the right, since 2^-k is
186 5^-k / 10^-k. *)
187
188(* The bits of x. *)
189proc bits(x: F64): U64;
190var p: ^U64;
191begin
192 p := ^U64(SYSTEM.ADR(x));
193 return p^;
194end;
195
196(* b times f, which is at most 2^31. *)
197proc bigMul(var b: Big; f: U64);
198var i: INT; t, carry: U64;
199begin
200 carry := 0;
201 for i := 0 to b.n - 1 do
202 t := b.v[i] * f + carry;
203 carry := t div Limb;
204 b.v[i] := t - carry * Limb;
205 end;
206 while carry > 0 do
207 b.v[b.n] := carry mod Limb;
208 carry := carry div Limb;
209 INC(b.n);
210 end;
211end;
212
213(* b times 2^k, or 5^k if five, a few steps at once. *)
214proc bigPow(var b: Big; k: INT; five: BOOL);
215var step, i: INT; f: U64;
216begin
217 while k > 0 do
218 if five then step := FiveStep; else step := TwoStep; end;
219 if k < step then step := k; end;
220 f := 1;
221 for i := 1 to step do
222 if five then f := f * 5; else f := f * 2; end;
223 end;
224 bigMul(b, f);
225 DEC(k, step);
226 end;
227end;
228
229(* m times 2^k, m not 0, exactly in decimal, into w. *)
230proc expand(m: U64; k: INT; var w: Exact);
231var b: Big; d: [24]U8; at, i, j, point: INT; v: U64;
232begin
233 b.n := 0;
234 while m > 0 do
235 b.v[b.n] := m mod Limb;
236 m := m div Limb;
237 INC(b.n);
238 end;
239 point := 0;
240 if k < 0 then point := -k; bigPow(b, -k, true); else bigPow(b, k, false); end;
241 (* the top limb without leading zeros, then nine digits a limb *)
242 d, at := digits(b.v[b.n - 1], 10, 'A');
243 w.n := 24 - at;
244 COPY(w.d[0..w.n], d[at..24]);
245 for i := b.n - 2 to 0 by -1 do
246 v := b.v[i];
247 for j := LimbDigits - 1 to 0 by -1 do
248 w.d[w.n + j] := U8(v mod 10) + '0';
249 v := v div 10;
250 end;
251 INC(w.n, LimbDigits);
252 end;
253 w.e := w.n - point;
254end;
255
256(* -1, 0 or 1 as 0.d times 10^e, d's first digit not 0, is below, at or
257 above w. *)
258proc compare(d: []const U8; e: INT; const w: Exact): INT;
259var i, n: INT; a, c: U8;
260begin
261 if e # w.e then
262 if e < w.e then return -1; end;
263 return 1;
264 end;
265 n := w.n;
266 if LEN(d) > n then n := LEN(d); end;
267 for i := 0 to n - 1 do
268 a := '0'; c := '0'; (* past its end each has zeros *)
269 if i < LEN(d) then a := d[i]; end;
270 if i < w.n then c := w.d[i]; end;
271 if a # c then
272 if a < c then return -1; end;
273 return 1;
274 end;
275 end;
276 return 0;
277end;
278
279(* Whether s reads back as the F64 that lo and hi are the halves around:
280 it lies between them, or on one of them if ends, as the F64 whose m
281 is even takes a tie. *)
282proc between(const s: Short; const lo, hi: Exact; ends: BOOL): BOOL;
283var below, above: INT;
284begin
285 below := compare(s.d[0..s.n], s.e, lo);
286 above := compare(s.d[0..s.n], s.e, hi);
287 return ((below > 0) or (below = 0) and ends) and ((above < 0) or (above = 0) and ends);
288end;
289
290(* s one more in its last place, carrying, without the zeros that leaves
291 at the end: 129 becomes 13, and 99 becomes 1 with the point moved. *)
292proc up(var s: Short);
293var i: INT;
294begin
295 i := s.n - 1;
296 while (i >= 0) and (s.d[i] = '9') do DEC(i); end;
297 if i >= 0 then
298 INC(s.d[i]);
299 s.n := i + 1;
300 else
301 s.d[0] := '1';
302 s.n := 1;
303 INC(s.e);
304 end;
305end;
306
307(* Whether x, cut where rest begins, is nearer the number one more in
308 the last place than to the cut one: rest is above a half, or a half
309 and last, the cut one's last digit, odd. *)
310proc nearerUp(rest: []const U8; last: U8): BOOL;
311var i: INT;
312begin
313 if rest[0] # '5' then return rest[0] > '5'; end;
314 for i := 1 to LEN(rest) - 1 do
315 if rest[i] # '0' then return true; end;
316 end;
317 return (last - '0') & 1 = 1;
318end;
319
320(* The fewest digits that read back as x, finite and above 0, into s. *)
321proc shortest(x: F64; var s: Short);
322var u, m: U64; k, p: INT; mid, lo, hi: Exact; ends, downOk, upOk: BOOL; next: Short;
323begin
324 (* x as m times 2^k: the fraction with its hidden 1 but for the
325 smallest exponent, the F64s below 2^-1022 *)
326 u := bits(x);
327 m := u & (U64(1) shl MantBits - 1);
328 k := INT(u shr MantBits & ExpMask);
329 if k = 0 then k := 1; else m := m | U64(1) shl MantBits; end;
330 DEC(k, ExpBias);
331 (* the halves to the F64s around x; the one below a power of two is
332 half as far as the one above, but at the smallest exponent *)
333 expand(m, k, mid);
334 expand(2 * m + 1, k - 1, hi);
335 if (m = U64(1) shl MantBits) and (k > 1 - ExpBias) then
336 expand(4 * m - 1, k - 2, lo);
337 else
338 expand(2 * m - 1, k - 1, lo);
339 end;
340 ends := m & 1 = 0;
341 for p := 1 to MostDigits do
342 s.n := p;
343 if p >= mid.n then s.n := mid.n; end; (* x itself has no more digits *)
344 s.e := mid.e;
345 COPY(s.d[0..s.n], mid.d[0..s.n]);
346 if s.n = mid.n then return; end;
347 next := s;
348 up(next);
349 downOk := between(s, lo, hi, ends);
350 upOk := between(next, lo, hi, ends);
351 if upOk and (not downOk or nearerUp(mid.d[p..mid.n], s.d[p - 1])) then
352 s := next;
353 return;
354 elsif downOk then
355 return;
356 end;
357 end;
358end;
359
360(* s added to out at n, which moves past it. *)
361proc emit(var out: [ShortMost]U8; var n: INT; s: []const U8);
362begin
363 COPY(out[n..n + LEN(s)], s);
364 INC(n, LEN(s));
365end;
366
367(* x in the fewest digits Math.toReal reads back as x, the nearer of
368 two such, and of two as near the one whose last digit is even,
369 written into out from 0: those bytes. From 1E-4 to below 1E15 it is
370 plain, as 0.0001, 0.1, 42 or 123456789012345; else a digit, a point
371 and the rest, an E and the exponent, as 1.5E17 or 5E-324. Zero is 0
372 or -0, keeping its sign so that it too reads back; nan, inf and
373 -inf are spelled so. The longest is ShortMost, 24 bytes, as
374 -2.2250738585072014E-308: a sign, 17 digits, a point and E-308; the
375 longest plain one, -0.00012345678901234567, has 22. *)
376proc short*(x: F64; var out: [ShortMost]U8): []U8;
377var s: Short; d: [24]U8; n, at, e, z: INT;
378begin
379 n := 0;
380 if x # x then emit(out, n, "nan"); return out[0..n]; end;
381 if bits(x) shr 63 = 1 then emit(out, n, "-"); x := -x; end; (* -0.0 too *)
382 if x > 1.7976931348623157E308 then emit(out, n, "inf"); return out[0..n]; end;
383 if x = 0.0 then emit(out, n, "0"); return out[0..n]; end;
384 shortest(x, s);
385 if (s.e >= -3) and (s.e <= 15) then (* from 1E-4 to below 1E15 *)
386 if s.e <= 0 then
387 emit(out, n, "0.");
388 for z := 1 to -s.e do emit(out, n, "0"); end;
389 emit(out, n, s.d[0..s.n]);
390 elsif s.e >= s.n then
391 emit(out, n, s.d[0..s.n]);
392 for z := 1 to s.e - s.n do emit(out, n, "0"); end;
393 else
394 emit(out, n, s.d[0..s.e]); emit(out, n, "."); emit(out, n, s.d[s.e..s.n]);
395 end;
396 else
397 emit(out, n, s.d[0..1]);
398 if s.n > 1 then emit(out, n, "."); emit(out, n, s.d[1..s.n]); end;
399 emit(out, n, "E");
400 e := s.e - 1;
401 if e < 0 then emit(out, n, "-"); e := -e; end;
402 d, at := digits(U64(e), 10, 'A');
403 emit(out, n, d[at..24]);
404 end;
405 return out[0..n];
406end;
407
408(* Whether a is printed with a minus sign. *)
409proc negative(a: Arg): BOOL;
410var neg: BOOL;
411begin
412 match a of
413 | int(i): neg := i < 0;
414 | real(r, _places): neg := r < 0.0;
415 | else: neg := false;
416 end;
417 return neg;
418end;
419
420(* a as Arg says, but for the minus sign. *)
421proc magnitude(var k: Sink; a: Arg);
422var d: [24]U8; at: INT; one: [1]U8;
423begin
424 match a of
425 | int(i):
426 if i < 0 then
427 d, at := digits(0 - U64(i), 10, 'A');
428 else
429 d, at := digits(U64(i), 10, 'A');
430 end;
431 put(k, d[at..24]);
432 | uint(u): d, at := digits(u, 10, 'A'); put(k, d[at..24]);
433 | hex(h): d, at := digits(h, 16, 'A'); put(k, d[at..24]);
434 | lowHex(h): d, at := digits(h, 16, 'a'); put(k, d[at..24]);
435 | str(s): put(k, s);
436 | char(c): one[0] := c; put(k, one);
437 | bool(t): if t then put(k, "true"); else put(k, "false"); end;
438 | real(r, places): putReal(k, r, places);
439 end;
440end;
441
442(* a as Arg says. *)
443proc arg(var k: Sink; a: Arg);
444begin
445 if negative(a) then put(k, "-"); end;
446 magnitude(k, a);
447end;
448
449(* a in a field of s.width bytes: on the left spaces, or zeros after
450 the minus sign; or spaces on the right. A longer a is written whole. *)
451proc field(var k: Sink; a: Arg; const s: Spec);
452var dry: Sink; pad: INT;
453begin
454 dry := sink(nil); dry.dry := true;
455 arg(dry, a);
456 pad := s.width - dry.n;
457 if s.left then
458 arg(k, a); fill(k, ' ', pad);
459 elsif s.zero then
460 if negative(a) then put(k, "-"); end;
461 fill(k, '0', pad); magnitude(k, a);
462 else
463 fill(k, ' ', pad); arg(k, a);
464 end;
465end;
466
467(* If f[i], a {, starts a width, {2}, {02} or {-10}: it is read into s
468 and the place of its } is the result; else the result is -1. *)
469proc spec(f: []const U8; i: INT; var s: Spec): INT;
470var count: INT;
471begin
472 s.width := 0; s.zero := false; s.left := false;
473 INC(i);
474 if (i < LEN(f)) and (f[i] = '-') then s.left := true; INC(i); end;
475 if (i < LEN(f)) and (f[i] = '0') then s.zero := true; end;
476 while (i < LEN(f)) and (f[i] >= '0') and (f[i] <= '9') and (count < 4) do
477 s.width := s.width * 10 + INT(f[i] - '0');
478 INC(count); INC(i);
479 end;
480 if (count > 0) and (i < LEN(f)) and (f[i] = '}') then return i; end;
481 return -1;
482end;
483
484(* f to k, each {} replaced by the next of args. *)
485proc format(var k: Sink; f: []const U8; args: []const Arg);
486var i, start, n, close: INT; s: Spec;
487begin
488 i := 0; start := 0; n := 0;
489 while i < LEN(f) do
490 if (f[i] = '{') or (f[i] = '}') then
491 put(k, f[start..i]);
492 close := -1;
493 if f[i] = '{' then close := spec(f, i, s); end;
494 if (i + 1 < LEN(f)) and (f[i + 1] = f[i]) then
495 put(k, f[i..i + 1]);
496 INC(i);
497 elsif (f[i] = '{') and (i + 1 < LEN(f)) and (f[i + 1] = '}') then
498 if n < LEN(args) then arg(k, args[n]); end;
499 INC(n);
500 INC(i);
501 elsif close >= 0 then
502 if n < LEN(args) then field(k, args[n], s); end;
503 INC(n);
504 i := close;
505 end;
506 start := i + 1;
507 end;
508 INC(i);
509 end;
510 put(k, f[start..LEN(f)]);
511end;
512
513(* Writes f to standard output, each {} replaced by the next of args. *)
514proc print*(f: []const U8; args: []const Arg);
515var k: Sink;
516begin
517 k := sink(nil);
518 format(k, f, args);
519end;
520
521(* Writes f to standard error as print writes it, after flushing Out,
522 so that what was printed before comes first. The text is made whole
523 in a buffer and written at once, to stay together when threads
524 write at once. The buffer is an arena's, not the heap's, so that a
525 program that doesn't call eprint carries no heap for it; its pages
526 are mapped as they are written, and a text of more than a megabyte
527 stops the program, as a Buf does when it can't grow. *)
528proc eprint*(f: []const U8; args: []const Arg);
529var ar: Arena.Arena; b: Buf.Buf; k: Sink; s: []U8; n: INT;
530begin
531 Out.flush;
532 if not Arena.init(ar, 1 shl 20) then Rt.stop("Fmt.eprint: no arena"); end;
533 Buf.init(b, SYSTEM.ADR(ar.base));
534 k := sink(SYSTEM.ADR(b));
535 format(k, f, args);
536 s := Buf.bytes(b);
537 while LEN(s) > 0 do
538 n := Sys.write(Sys.stderr, s);
539 if n > 0 then s := s[n..LEN(s)];
540 elsif n # Sys.errIntr then break; (* a signal broke in: try again *)
541 end;
542 end;
543 Arena.done(ar);
544end;
545
546(* Appends f to b, each {} replaced by the next of args. *)
547proc write*(var b: Buf.Buf; f: []const U8; args: []const Arg);
548var k: Sink;
549begin
550 k := sink(SYSTEM.ADR(b)); (* a sink's nil is standard output *)
551 format(k, f, args);
552end;
553
554(* Formats f into out as write formats it: the bytes written, and false
555 if out was too short and the text is cut there. It needs no memory
556 of its own. *)
557proc into*(out: []U8; f: []const U8; args: []const Arg): (INT, BOOL);
558var k: Sink;
559begin
560 k := sink(nil);
561 k.out := out; k.slice := true;
562 format(k, f, args);
563 if k.n > LEN(out) then return LEN(out), false; end;
564 return k.n, true;
565end;
566
567(* Each of xs, shorts, with a space between, added to b. *)
568proc shorts(var b: Buf.Buf; xs: []const F64);
569var d: [ShortMost]U8; i: INT;
570begin
571 for i := 0 to LEN(xs) - 1 do
572 if i > 0 then Buf.add(b, ' '); end;
573 Buf.str(b, short(xs[i], d));
574 end;
575end;
576
577(* The module's test: formats into a buffer over an arena. *)
578proc main*;
579var ar: Arena.Arena; b: Buf.Buf; zero: F64; small: [8]U8; d: [ShortMost]U8; n: INT; whole: BOOL;
580begin
581 if not Arena.init(ar, 1 shl 16) then Rt.stop("Fmt: no arena"); end;
582 Buf.init(b, SYSTEM.ADR(ar.base));
583 write(b, "{} {} {} {}|{}{}{} {{}}", [int(-42), uint(0FFFFFFFFFFFFFFFFH), hex(48879),
584 str("s"), char('c'), bool(true), int(0)]);
585 if Buf.bytes(b) # "-42 18446744073709551615 BEEF s|ctrue0 {}" then Rt.stop("Fmt.write"); end;
586 Buf.reset(b);
587 write(b, "{} and {}", [int(1)]);
588 if Buf.bytes(b) # "1 and " then Rt.stop("Fmt: too few arguments"); end;
589 Buf.reset(b);
590 write(b, "{} {} {} {} {} {} {} {}", [real(3.14159, 2), real(-2.5, 0), real(0.999, 2), real(0.05, 1),
591 real(1.0, 12), real(-0.001, 2), real(123.0, 0), real(999999999999999.9, 0)]);
592 if Buf.bytes(b) # "3.14 -3 1.00 0.1 1.000000000 -0.00 123 1000000000000000" then Rt.stop("Fmt.real"); end;
593 Buf.reset(b);
594 write(b, "{} {} {} {} {} {}", [real(1.5E17, 2), real(-9.999E20, 2), real(1.0E15, 0), real(1.7976931348623157E308, 3),
595 real(1.0 / zero, 2), real(zero / zero, 2)]);
596 if Buf.bytes(b) # "1.50E17 -1.00E21 1E15 1.798E308 inf nan" then Rt.stop("Fmt.real: an exponent"); end;
597 Buf.reset(b);
598 write(b, "[{3}][{03}][{-3}][{04}][{4}][{-4}|][{2}]", [int(7), int(7), int(7), int(-7), int(-7), int(-7), int(12345)]);
599 if Buf.bytes(b) # "[ 7][007][7 ][-007][ -7][-7 |][12345]" then Rt.stop("Fmt: a width"); end;
600 Buf.reset(b);
601 write(b, "{6}|{-6}|{06}|{2}|{05}|{3}", [str("ab"), str("ab"), real(-1.5, 1), hex(255), lowHex(0ABCH), char('c')]);
602 if Buf.bytes(b) # " ab|ab |-001.5|FF|00abc| c" then Rt.stop("Fmt: a width on other arguments"); end;
603 Buf.reset(b);
604 write(b, "{x} {2x} {0} {-} {a} {1", [int(1), int(2)]);
605 if Buf.bytes(b) # "x 2x 1 - a 1" then Rt.stop("Fmt: a { that is no width"); end;
606 Buf.reset(b);
607 shorts(b, [0.1, 1.0 / 3.0, -2.5, 100.0, 42.0E3, 0.0001, 0.00012, 123456789012345.0]);
608 if Buf.bytes(b) # "0.1 0.3333333333333333 -2.5 100 42000 0.0001 0.00012 123456789012345" then Rt.stop("Fmt.short"); end;
609 Buf.reset(b);
610 shorts(b, [1.0E15, 1.5E17, 9.9E-5, 5.0E-324, 1.7976931348623157E308, 2.2250738585072014E-308,
611 9007199254740992.0]);
612 if Buf.bytes(b) # "1E15 1.5E17 9.9E-5 5E-324 1.7976931348623157E308 2.2250738585072014E-308 9.007199254740992E15" then
613 Rt.stop("Fmt.short: an exponent");
614 end;
615 Buf.reset(b);
616 shorts(b, [zero, -zero, 1.0 / zero, -1.0 / zero, zero / zero]);
617 write(b, " {6}|{-6}", [str(short(0.5, d)), str(short(0.5, d))]);
618 if Buf.bytes(b) # "0 -0 inf -inf nan 0.5|0.5 " then Rt.stop("Fmt.short: zero, inf, nan and a width"); end;
619 if LEN(short(-2.2250738585072014E-308, d)) # ShortMost then Rt.stop("Fmt.short: the longest"); end;
620 n, whole := into(small, "{}:{02}", [int(-3), int(7)]);
621 if (small[0..n] # "-3:07") or not whole then Rt.stop("Fmt.into"); end;
622 n, whole := into(small, "line {}, {}", [int(12), str("x")]);
623 if (small[0..n] # "line 12,") or whole then Rt.stop("Fmt.into: cut short"); end;
624 n, whole := into(small[0..0], "{}", [int(1)]);
625 if (n # 0) or whole then Rt.stop("Fmt.into: no room"); end;
626 Arena.done(ar);
627end;
628
629end Fmt.