raz / sbcl / lib / Fmt.raz

146 lines · 3.9 KiB · raw · history · last change 55bc6c0d

1(* Formatted output: Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)]),
2 or into a Buf with Fmt.write. Each {} takes the next argument; {{ and
3 }} stand for { and }. *)
4module Fmt;
5import Out, Buf;
6
7type Arg* = variant
8 int(i: I64);
9 uint(u: U64);
10 hex(h: U64);
11 str(s: []U8);
12 char(c: U8);
13 bool(b: BOOL);
14 real(r: F64; places: INT);
15end;
16
17const digits = "0123456789ABCDEF";
18
19(* Text is made in a Piece before it goes out, a piece at a time. *)
20type Piece = record bytes: [64]U8; len: INT; end;
21
22proc add(p: ^Piece; c: U8);
23begin
24 p^.bytes[p^.len] := c;
25 INC(p^.len);
26end;
27
28(* u in base, at least min digits *)
29proc unsigned(p: ^Piece; u: U64; base: U64; min: INT);
30var d: [20]U8; n: INT;
31begin
32 repeat
33 d[n] := digits[INT(u mod base)];
34 u := u div base;
35 INC(n);
36 until u = 0;
37 while n < min do d[n] := '0'; INC(n); end;
38 while n > 0 do DEC(n); add(p, d[n]); end;
39end;
40
41proc signed(p: ^Piece; i: I64);
42begin
43 if i < 0 then add(p, '-'); unsigned(p, 0 - U64(i), 10, 1);
44 else unsigned(p, U64(i), 10, 1);
45 end;
46end;
47
48(* x, at least 0 and below 1e15, with places digits after the point *)
49proc fixed(p: ^Piece; x: F64; places: INT);
50var whole, scale: F64; ip, frac, one: U64; i: INT;
51begin
52 one := 1;
53 for i := 1 to places do one := one * 10; end;
54 scale := F64(one);
55 ip := U64(x);
56 whole := F64(ip);
57 frac := U64((x - whole) * scale + 0.5);
58 if frac >= one then INC(ip); frac := frac - one; end;
59 unsigned(p, ip, 10, 1);
60 if places > 0 then
61 add(p, '.');
62 unsigned(p, frac, 10, places);
63 end;
64end;
65
66proc writeReal(p: ^Piece; x: F64; places: INT);
67var e, i: INT; m: F64; q: Piece;
68begin
69 if places < 0 then places := 0; end;
70 if places > 9 then places := 9; end;
71 if x # x then add(p, 'n'); add(p, 'a'); add(p, 'n'); return; end;
72 if x < 0.0 or (x = 0.0 and 1.0 / x < 0.0) then add(p, '-'); x := -x; end;
73 if x > 1.7976931348623157E308 then add(p, 'i'); add(p, 'n'); add(p, 'f'); return; end;
74 if x < 1.0E15 then fixed(p, x, places); return; end;
75 (* m times ten to the e, m from 1 to 10 *)
76 m := x;
77 while m >= 10.0 do m := m / 10.0; INC(e); end;
78 fixed(ADR(q), m, places);
79 if q.bytes[0] = '1' and q.bytes[1] = '0' then
80 (* m rounded up to 10 *)
81 q.len := 0;
82 fixed(ADR(q), m / 10.0, places);
83 INC(e);
84 end;
85 for i := 0 to q.len - 1 do add(p, q.bytes[i]); end;
86 add(p, 'e');
87 signed(p, I64(e));
88end;
89
90(* The text of each piece of f, and of each argument, given to put. *)
91proc format(f: []U8; args: []Arg; out: proc(s: []U8; b: ^Buf.Buf); b: ^Buf.Buf);
92var i, start, next: INT; p: Piece;
93begin
94 while i < LEN(f) do
95 if (f[i] = '{' or f[i] = '}') and i + 1 < LEN(f) and f[i + 1] = f[i] then
96 out(f[start..i + 1], b);
97 i := i + 2;
98 start := i;
99 elsif f[i] = '{' and i + 1 < LEN(f) and f[i + 1] = '}' then
100 out(f[start..i], b);
101 if next < LEN(args) then
102 p.len := 0;
103 match args[next] of
104 | int(n): signed(ADR(p), n);
105 | uint(u): unsigned(ADR(p), u, 10, 1);
106 | hex(h): unsigned(ADR(p), h, 16, 1);
107 | str(s): out(s, b);
108 | char(c): add(ADR(p), c);
109 | bool(v): if v then out("true", b); else out("false", b); end;
110 | real(x, places): writeReal(ADR(p), x, places);
111 end;
112 out(p.bytes[0..p.len], b);
113 INC(next);
114 end;
115 i := i + 2;
116 start := i;
117 else
118 INC(i);
119 end;
120 end;
121 out(f[start..LEN(f)], b);
122end;
123
124proc toOut(s: []U8; b: ^Buf.Buf);
125begin
126 Out.str(s);
127end;
128
129proc toBuf(s: []U8; b: ^Buf.Buf);
130begin
131 Buf.str(b, s);
132end;
133
134(* to standard output *)
135proc print*(f: []U8; args: []Arg);
136begin
137 format(f, args, toOut, nil);
138end;
139
140(* added to b *)
141proc write*(b: ^Buf.Buf; f: []U8; args: []Arg);
142begin
143 format(f, args, toBuf, b);
144end;
145
146end Fmt.