| 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 }. *) |
| 4 | module Fmt; |
| 5 | import Out, Buf; |
| 6 | |
| 7 | type 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); |
| 15 | end; |
| 16 | |
| 17 | const digits = "0123456789ABCDEF"; |
| 18 | |
| 19 | (* Text is made in a Piece before it goes out, a piece at a time. *) |
| 20 | type Piece = record bytes: [64]U8; len: INT; end; |
| 21 | |
| 22 | proc add(p: ^Piece; c: U8); |
| 23 | begin |
| 24 | p^.bytes[p^.len] := c; |
| 25 | INC(p^.len); |
| 26 | end; |
| 27 | |
| 28 | (* u in base, at least min digits *) |
| 29 | proc unsigned(p: ^Piece; u: U64; base: U64; min: INT); |
| 30 | var d: [20]U8; n: INT; |
| 31 | begin |
| 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; |
| 39 | end; |
| 40 | |
| 41 | proc signed(p: ^Piece; i: I64); |
| 42 | begin |
| 43 | if i < 0 then add(p, '-'); unsigned(p, 0 - U64(i), 10, 1); |
| 44 | else unsigned(p, U64(i), 10, 1); |
| 45 | end; |
| 46 | end; |
| 47 | |
| 48 | (* x, at least 0 and below 1e15, with places digits after the point *) |
| 49 | proc fixed(p: ^Piece; x: F64; places: INT); |
| 50 | var whole, scale: F64; ip, frac, one: U64; i: INT; |
| 51 | begin |
| 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; |
| 64 | end; |
| 65 | |
| 66 | proc writeReal(p: ^Piece; x: F64; places: INT); |
| 67 | var e, i: INT; m: F64; q: Piece; |
| 68 | begin |
| 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)); |
| 88 | end; |
| 89 | |
| 90 | (* The text of each piece of f, and of each argument, given to put. *) |
| 91 | proc format(f: []U8; args: []Arg; out: proc(s: []U8; b: ^Buf.Buf); b: ^Buf.Buf); |
| 92 | var i, start, next: INT; p: Piece; |
| 93 | begin |
| 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); |
| 122 | end; |
| 123 | |
| 124 | proc toOut(s: []U8; b: ^Buf.Buf); |
| 125 | begin |
| 126 | Out.str(s); |
| 127 | end; |
| 128 | |
| 129 | proc toBuf(s: []U8; b: ^Buf.Buf); |
| 130 | begin |
| 131 | Buf.str(b, s); |
| 132 | end; |
| 133 | |
| 134 | (* to standard output *) |
| 135 | proc print*(f: []U8; args: []Arg); |
| 136 | begin |
| 137 | format(f, args, toOut, nil); |
| 138 | end; |
| 139 | |
| 140 | (* added to b *) |
| 141 | proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); |
| 142 | begin |
| 143 | format(f, args, toBuf, b); |
| 144 | end; |
| 145 | |
| 146 | end Fmt. |