| | @@ -1,7 +1,12 @@ |
| 1 | 1 | (* Formatted output. Each {} in the format takes the next argument: |
| 2 | 2 | Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)]); |
| 3 | 3 | {{ 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. *) |
| 5 | 10 | module Fmt; |
| 6 | 11 | import Out, Buf, Arena, Rt, Sys; |
| 7 | 12 | |
| | @@ -9,46 +14,75 @@ |
| 9 | 14 | (* An argument, and how it is printed. A real has places digits |
| 10 | 15 | after the point, at most 9, rounded to the nearest, half away |
| 11 | 16 | 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. *) |
| 13 | 18 | Arg* = variant |
| 14 | 19 | int(i: I64); (* in decimal *) |
| 15 | 20 | uint(u: U64); (* in decimal *) |
| 16 | 21 | hex(h: U64); (* in hexadecimal, capitals, no 0x *) |
| 22 | + lowHex(h: U64); (* in hexadecimal, lower case, no 0x *) |
| 17 | 23 | str(s: []U8); |
| 18 | 24 | char(c: U8); (* the one byte *) |
| 19 | 25 | bool(b: BOOL); (* true or false *) |
| 20 | 26 | real(r: F64; places: INT); |
| 21 | 27 | end; |
| 22 | 28 | |
| 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); |
| 25 | 48 | var d: [24]U8; i: INT; v: U8; |
| 26 | 49 | begin |
| 27 | 50 | i := 24; |
| 28 | 51 | repeat |
| 29 | 52 | DEC(i); |
| 30 | 53 | 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; |
| 32 | 55 | u := u div base; |
| 33 | 56 | until u = 0; |
| 34 | 57 | return d, i; |
| 35 | 58 | end; |
| 36 | 59 | |
| 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; |
| 39 | 71 | 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; |
| 41 | 74 | end; |
| 42 | 75 | |
| 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); |
| 46 | 80 | const zeros = "00000000"; |
| 47 | 81 | var d: [24]U8; at, i, e: INT; scale, whole, frac: I64; |
| 48 | 82 | 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; |
| 52 | 86 | if places < 0 then places := 0; elsif places > 9 then places := 9; end; |
| 53 | 87 | scale := 1; |
| 54 | 88 | for i := 1 to places do scale := scale * 10; end; |
| | @@ -59,62 +93,124 @@ |
| 59 | 93 | frac := I64((x - F64(whole)) * F64(scale) + 0.5); |
| 60 | 94 | if frac = scale then INC(whole); frac := 0; end; |
| 61 | 95 | 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]); |
| 64 | 98 | 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]); |
| 69 | 103 | end; |
| 70 | 104 | 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; |
| 74 | 119 | end; |
| 120 | + return neg; |
| 75 | 121 | end; |
| 76 | 122 | |
| 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); |
| 78 | 125 | var d: [24]U8; at: INT; one: [1]U8; |
| 79 | 126 | begin |
| 80 | 127 | match a of |
| 81 | 128 | | int(i): |
| 82 | 129 | if i < 0 then |
| 83 | | - put(b, "-"); |
| 84 | | - d, at := digits(0 - U64(i), 10); |
| 130 | + d, at := digits(0 - U64(i), 10, 'A'); |
| 85 | 131 | else |
| 86 | | - d, at := digits(U64(i), 10); |
| 132 | + d, at := digits(U64(i), 10, 'A'); |
| 87 | 133 | 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); |
| 95 | 142 | end; |
| 96 | 143 | end; |
| 97 | 144 | |
| 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 | + |
| 98 | 187 | 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; |
| 100 | 189 | begin |
| 190 | + k.b := b; k.dry := false; k.n := 0; |
| 101 | 191 | i := 0; start := 0; n := 0; |
| 102 | 192 | while i < LEN(f) do |
| 103 | 193 | 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; |
| 105 | 197 | 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]); |
| 107 | 199 | INC(i); |
| 108 | 200 | 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; |
| 110 | 202 | INC(n); |
| 111 | 203 | 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; |
| 112 | 208 | end; |
| 113 | 209 | start := i + 1; |
| 114 | 210 | end; |
| 115 | 211 | INC(i); |
| 116 | 212 | end; |
| 117 | | - put(b, f[start..LEN(f)]); |
| 213 | + put(k, f[start..LEN(f)]); |
| 118 | 214 | end; |
| 119 | 215 | |
| 120 | 216 | (* Writes f to standard output, each {} replaced by the next of args. *) |
| | @@ -172,7 +268,16 @@ |
| 172 | 268 | Buf.reset(b); |
| 173 | 269 | write(b, "{} {} {} {} {} {}", [real(1.5E17, 2), real(-9.999E20, 2), real(1.0E15, 0), real(1.7976931348623157E308, 3), |
| 174 | 270 | 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; |
| 176 | 281 | Arena.done(ar); |
| 177 | 282 | end; |
| 178 | 283 | |