raz / commit 4fe15c82

commit
4fe15c8228e21c191fbd2d48df696bb3f01c6da4
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
0079c65c
Fmt: a width in the braces, lowHex, and E for an exponent

patch · log

2 files changed, +155 -42

doc/raz-lib.md (history) modified +10 -2

@@ -672,6 +672,7 @@
672672 int(i: I64);
673673 uint(u: U64);
674674 hex(h: U64);
675+ lowHex(h: U64);
675676 str(s: []U8);
676677 char(c: U8);
677678 bool(b: BOOL);
@@ -862,8 +863,15 @@
862863 `less` may change places. `Math`'s procedures are as C's `math.h`
863864 has them, to within the last bit or so. `Fmt.real(x, places)` writes
864865 `x` with `places` digits after the point, at most 9, rounded to the
865-nearest; from 1e15 on, with an exponent, as `1.50e17`; and `nan`,
866-`inf` and `-inf` as themselves.
866+nearest; from 1E15 on, with an exponent, as `1.50E17`; and `nan`,
867+`inf` and `-inf` as themselves. `hex` writes capitals and `lowHex`
868+lower case, neither with a `0x`. A width between the braces pads an
869+argument to that many bytes: `{2}` with spaces on the left, `{02}`
870+with zeros on the left, after a sign (`{04}` of -7 is `-007`), `{-10}`
871+with spaces on the right. A longer argument is written whole. A `{`
872+followed by anything but `}` or a width is dropped, as before.
873+`Fmt.print("{2}:{02} U+{04}", [Fmt.int(3), Fmt.int(7), Fmt.hex(0E9H)])`
874+writes ` 3:07 U+00E9`.
867875
868876 **Files and Opt** are what most programs need of their command line.
869877

lib/Fmt.raz (history) modified +145 -40

@@ -1,7 +1,12 @@
11 (* Formatted output. Each {} in the format takes the next argument:
22 Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)]);
33 {{ and }} stand for { and }. A {} with no argument left prints
4- nothing, and arguments left over are not printed. *)
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. *)
510 module Fmt;
611 import Out, Buf, Arena, Rt, Sys;
712
@@ -9,46 +14,75 @@
914 (* An argument, and how it is printed. A real has places digits
1015 after the point, at most 9, rounded to the nearest, half away
1116 from zero; from 1E15 on, it has the exponent that makes it less
12- than 10, as 1.50e17; nan and inf are spelled so. *)
17+ than 10, as 1.50E17; nan and inf are spelled so. *)
1318 Arg* = variant
1419 int(i: I64); (* in decimal *)
1520 uint(u: U64); (* in decimal *)
1621 hex(h: U64); (* in hexadecimal, capitals, no 0x *)
22+ lowHex(h: U64); (* in hexadecimal, lower case, no 0x *)
1723 str(s: []U8);
1824 char(c: U8); (* the one byte *)
1925 bool(b: BOOL); (* true or false *)
2026 real(r: F64; places: INT);
2127 end;
2228
23-(* The digits of u in base, at the end of the array: it and where they start. *)
24-proc digits(u: U64; base: U64): ([24]U8, INT);
29+ (* Where the pieces go: to b, or to standard output if b is nil.
30+ With dry set nothing is written and n only counts the bytes
31+ that would be, to know how much a width must pad. *)
32+ Sink = record
33+ b: ^Buf.Buf;
34+ dry: BOOL;
35+ n: INT;
36+ end;
37+
38+ (* What a {} says: its width, whether it pads with zeros, and
39+ whether on the right. *)
40+ Spec = record
41+ width: INT;
42+ zero, left: BOOL;
43+ end;
44+
45+(* The digits of u in base, at the end of the array: it and where they
46+ start. Digits past 9 are letters from letter on, 'A' or 'a'. *)
47+proc digits(u: U64; base: U64; letter: U8): ([24]U8, INT);
2548 var d: [24]U8; i: INT; v: U8;
2649 begin
2750 i := 24;
2851 repeat
2952 DEC(i);
3053 v := U8(u mod base);
31- if v < 10 then d[i] := v + '0'; else d[i] := v - 10 + 'A'; end;
54+ if v < 10 then d[i] := v + '0'; else d[i] := v - 10 + letter; end;
3255 u := u div base;
3356 until u = 0;
3457 return d, i;
3558 end;
3659
37-(* Where the pieces go: to b, or to standard output if b is nil. *)
38-proc put(b: ^Buf.Buf; s: []U8);
60+(* s to k. *)
61+proc put(var k: Sink; s: []U8);
62+begin
63+ INC(k.n, LEN(s));
64+ if k.dry then return; end;
65+ if k.b = nil then Out.str(s); else Buf.str(k.b^, s); end;
66+end;
67+
68+(* n copies of c to k. *)
69+proc fill(var k: Sink; c: U8; n: INT);
70+var one: [1]U8; i: INT;
3971 begin
40- if b = nil then Out.str(s); else Buf.str(b^, s); end;
72+ one[0] := c;
73+ for i := 1 to n do put(k, one); end;
4174 end;
4275
43-(* x as Arg's real says. The whole part and the fraction are taken
44- apart, so that each fits an I64: hence the exponent from 1E15. *)
45-proc putReal(b: ^Buf.Buf; x: F64; places: INT);
76+(* x as Arg's real says, but for the minus sign, which arg writes. The
77+ whole part and the fraction are taken apart, so that each fits an
78+ I64: hence the exponent from 1E15. *)
79+proc putReal(var k: Sink; x: F64; places: INT);
4680 const zeros = "00000000";
4781 var d: [24]U8; at, i, e: INT; scale, whole, frac: I64;
4882 begin
49- if x # x then put(b, "nan"); return; end;
50- if x < 0.0 then put(b, "-"); x := -x; end;
51- if x > 1.7976931348623157E308 then put(b, "inf"); return; end;
83+ if x # x then put(k, "nan"); return; end;
84+ if x < 0.0 then x := -x; end;
85+ if x > 1.7976931348623157E308 then put(k, "inf"); return; end;
5286 if places < 0 then places := 0; elsif places > 9 then places := 9; end;
5387 scale := 1;
5488 for i := 1 to places do scale := scale * 10; end;
@@ -59,62 +93,124 @@
5993 frac := I64((x - F64(whole)) * F64(scale) + 0.5);
6094 if frac = scale then INC(whole); frac := 0; end;
6195 if (e > 0) and (whole = 10) then whole := 1; INC(e); end;
62- d, at := digits(U64(whole), 10);
63- put(b, d[at..24]);
96+ d, at := digits(U64(whole), 10, 'A');
97+ put(k, d[at..24]);
6498 if places > 0 then
65- put(b, ".");
66- d, at := digits(U64(frac), 10);
67- put(b, zeros[0..places - (24 - at)]);
68- put(b, d[at..24]);
99+ put(k, ".");
100+ d, at := digits(U64(frac), 10, 'A');
101+ put(k, zeros[0..places - (24 - at)]);
102+ put(k, d[at..24]);
69103 end;
70104 if e > 0 then
71- d, at := digits(U64(e), 10);
72- put(b, "e");
73- put(b, d[at..24]);
105+ d, at := digits(U64(e), 10, 'A');
106+ put(k, "E");
107+ put(k, d[at..24]);
108+ end;
109+end;
110+
111+(* Whether a is printed with a minus sign. *)
112+proc negative(a: Arg): BOOL;
113+var neg: BOOL;
114+begin
115+ match a of
116+ | int(i): neg := i < 0;
117+ | real(r, _places): neg := r < 0.0;
118+ | else: neg := false;
74119 end;
120+ return neg;
75121 end;
76122
77-proc arg(b: ^Buf.Buf; a: Arg);
123+(* a as Arg says, but for the minus sign. *)
124+proc magnitude(var k: Sink; a: Arg);
78125 var d: [24]U8; at: INT; one: [1]U8;
79126 begin
80127 match a of
81128 | int(i):
82129 if i < 0 then
83- put(b, "-");
84- d, at := digits(0 - U64(i), 10);
130+ d, at := digits(0 - U64(i), 10, 'A');
85131 else
86- d, at := digits(U64(i), 10);
132+ d, at := digits(U64(i), 10, 'A');
87133 end;
88- put(b, d[at..24]);
89- | uint(u): d, at := digits(u, 10); put(b, d[at..24]);
90- | hex(h): d, at := digits(h, 16); put(b, d[at..24]);
91- | str(s): put(b, s);
92- | char(c): one[0] := c; put(b, one);
93- | bool(t): if t then put(b, "true"); else put(b, "false"); end;
94- | real(r, places): putReal(b, r, places);
134+ put(k, d[at..24]);
135+ | uint(u): d, at := digits(u, 10, 'A'); put(k, d[at..24]);
136+ | hex(h): d, at := digits(h, 16, 'A'); put(k, d[at..24]);
137+ | lowHex(h): d, at := digits(h, 16, 'a'); put(k, d[at..24]);
138+ | str(s): put(k, s);
139+ | char(c): one[0] := c; put(k, one);
140+ | bool(t): if t then put(k, "true"); else put(k, "false"); end;
141+ | real(r, places): putReal(k, r, places);
95142 end;
96143 end;
97144
145+(* a as Arg says. *)
146+proc arg(var k: Sink; a: Arg);
147+begin
148+ if negative(a) then put(k, "-"); end;
149+ magnitude(k, a);
150+end;
151+
152+(* a in a field of s.width bytes: on the left spaces, or zeros after
153+ the minus sign; or spaces on the right. A longer a is written whole. *)
154+proc field(var k: Sink; a: Arg; const s: Spec);
155+var dry: Sink; pad: INT;
156+begin
157+ dry.b := nil; dry.dry := true; dry.n := 0;
158+ arg(dry, a);
159+ pad := s.width - dry.n;
160+ if s.left then
161+ arg(k, a); fill(k, ' ', pad);
162+ elsif s.zero then
163+ if negative(a) then put(k, "-"); end;
164+ fill(k, '0', pad); magnitude(k, a);
165+ else
166+ fill(k, ' ', pad); arg(k, a);
167+ end;
168+end;
169+
170+(* If f[i], a {, starts a width, {2}, {02} or {-10}: it is read into s
171+ and the place of its } is the result; else the result is -1. *)
172+proc spec(f: []U8; i: INT; var s: Spec): INT;
173+var count: INT;
174+begin
175+ s.width := 0; s.zero := false; s.left := false;
176+ INC(i);
177+ if (i < LEN(f)) and (f[i] = '-') then s.left := true; INC(i); end;
178+ if (i < LEN(f)) and (f[i] = '0') then s.zero := true; end;
179+ while (i < LEN(f)) and (f[i] >= '0') and (f[i] <= '9') and (count < 4) do
180+ s.width := s.width * 10 + INT(f[i] - '0');
181+ INC(count); INC(i);
182+ end;
183+ if (count > 0) and (i < LEN(f)) and (f[i] = '}') then return i; end;
184+ return -1;
185+end;
186+
98187 proc format(b: ^Buf.Buf; f: []U8; args: []Arg);
99-var i, start, n: INT;
188+var i, start, n, close: INT; k: Sink; s: Spec;
100189 begin
190+ k.b := b; k.dry := false; k.n := 0;
101191 i := 0; start := 0; n := 0;
102192 while i < LEN(f) do
103193 if (f[i] = '{') or (f[i] = '}') then
104- put(b, f[start..i]);
194+ put(k, f[start..i]);
195+ close := -1;
196+ if f[i] = '{' then close := spec(f, i, s); end;
105197 if (i + 1 < LEN(f)) and (f[i + 1] = f[i]) then
106- put(b, f[i..i + 1]);
198+ put(k, f[i..i + 1]);
107199 INC(i);
108200 elsif (f[i] = '{') and (i + 1 < LEN(f)) and (f[i + 1] = '}') then
109- if n < LEN(args) then arg(b, args[n]); end;
201+ if n < LEN(args) then arg(k, args[n]); end;
110202 INC(n);
111203 INC(i);
204+ elsif close >= 0 then
205+ if n < LEN(args) then field(k, args[n], s); end;
206+ INC(n);
207+ i := close;
112208 end;
113209 start := i + 1;
114210 end;
115211 INC(i);
116212 end;
117- put(b, f[start..LEN(f)]);
213+ put(k, f[start..LEN(f)]);
118214 end;
119215
120216 (* Writes f to standard output, each {} replaced by the next of args. *)
@@ -172,7 +268,16 @@
172268 Buf.reset(b);
173269 write(b, "{} {} {} {} {} {}", [real(1.5E17, 2), real(-9.999E20, 2), real(1.0E15, 0), real(1.7976931348623157E308, 3),
174270 real(1.0 / zero, 2), real(zero / zero, 2)]);
175- if Buf.bytes(b) # "1.50e17 -1.00e21 1e15 1.798e308 inf nan" then Rt.stop("Fmt.real: an exponent"); end;
271+ if Buf.bytes(b) # "1.50E17 -1.00E21 1E15 1.798E308 inf nan" then Rt.stop("Fmt.real: an exponent"); end;
272+ Buf.reset(b);
273+ write(b, "[{3}][{03}][{-3}][{04}][{4}][{-4}|][{2}]", [int(7), int(7), int(7), int(-7), int(-7), int(-7), int(12345)]);
274+ if Buf.bytes(b) # "[ 7][007][7 ][-007][ -7][-7 |][12345]" then Rt.stop("Fmt: a width"); end;
275+ Buf.reset(b);
276+ write(b, "{6}|{-6}|{06}|{2}|{05}|{3}", [str("ab"), str("ab"), real(-1.5, 1), hex(255), lowHex(0ABCH), char('c')]);
277+ if Buf.bytes(b) # " ab|ab |-001.5|FF|00abc| c" then Rt.stop("Fmt: a width on other arguments"); end;
278+ Buf.reset(b);
279+ write(b, "{x} {2x} {0} {-} {a} {1", [int(1), int(2)]);
280+ if Buf.bytes(b) # "x 2x 1 - a 1" then Rt.stop("Fmt: a { that is no width"); end;
176281 Arena.done(ar);
177282 end;
178283