| | @@ -26,12 +26,15 @@ |
| 26 | 26 | real(r: F64; places: INT); |
| 27 | 27 | end; |
| 28 | 28 | |
| 29 | | - (* Where the pieces go: to b, or to standard output if b is nil. |
| 29 | + (* Where the pieces go: to out if slice is set, else to b, or to |
| 30 | + standard output if b is nil. n counts the bytes given, so a |
| 31 | + slice holds the first LEN(out) of them and the rest are cut. |
| 30 | 32 | With dry set nothing is written and n only counts the bytes |
| 31 | 33 | that would be, to know how much a width must pad. *) |
| 32 | 34 | Sink = record |
| 33 | 35 | b: ^Buf.Buf; |
| 34 | | - dry: BOOL; |
| 36 | + out: []U8; |
| 37 | + slice, dry: BOOL; |
| 35 | 38 | n: INT; |
| 36 | 39 | end; |
| 37 | 40 | |
| | @@ -57,12 +60,30 @@ |
| 57 | 60 | return d, i; |
| 58 | 61 | end; |
| 59 | 62 | |
| 63 | +(* A sink that writes to b, or to standard output if b is nil. *) |
| 64 | +proc sink(b: ^Buf.Buf): Sink; |
| 65 | +var k: Sink; |
| 66 | +begin |
| 67 | + k.b := b; k.out := []; k.slice := false; k.dry := false; k.n := 0; |
| 68 | + return k; |
| 69 | +end; |
| 70 | + |
| 60 | 71 | (* s to k. *) |
| 61 | 72 | proc put(var k: Sink; s: []U8); |
| 73 | +var at, room: INT; |
| 62 | 74 | begin |
| 75 | + at := k.n; |
| 63 | 76 | 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; |
| 77 | + if k.dry then |
| 78 | + elsif k.slice then |
| 79 | + room := LEN(k.out) - at; (* what is past the end is cut *) |
| 80 | + if room > LEN(s) then room := LEN(s); end; |
| 81 | + if room > 0 then COPY(k.out[at..at + room], s[0..room]); end; |
| 82 | + elsif k.b = nil then |
| 83 | + Out.str(s); |
| 84 | + else |
| 85 | + Buf.str(k.b^, s); |
| 86 | + end; |
| 66 | 87 | end; |
| 67 | 88 | |
| 68 | 89 | (* n copies of c to k. *) |
| | @@ -154,7 +175,7 @@ |
| 154 | 175 | proc field(var k: Sink; a: Arg; const s: Spec); |
| 155 | 176 | var dry: Sink; pad: INT; |
| 156 | 177 | begin |
| 157 | | - dry.b := nil; dry.dry := true; dry.n := 0; |
| 178 | + dry := sink(nil); dry.dry := true; |
| 158 | 179 | arg(dry, a); |
| 159 | 180 | pad := s.width - dry.n; |
| 160 | 181 | if s.left then |
| | @@ -184,10 +205,10 @@ |
| 184 | 205 | return -1; |
| 185 | 206 | end; |
| 186 | 207 | |
| 187 | | -proc format(b: ^Buf.Buf; f: []U8; args: []Arg); |
| 188 | | -var i, start, n, close: INT; k: Sink; s: Spec; |
| 208 | +(* f to k, each {} replaced by the next of args. *) |
| 209 | +proc format(var k: Sink; f: []U8; args: []Arg); |
| 210 | +var i, start, n, close: INT; s: Spec; |
| 189 | 211 | begin |
| 190 | | - k.b := b; k.dry := false; k.n := 0; |
| 191 | 212 | i := 0; start := 0; n := 0; |
| 192 | 213 | while i < LEN(f) do |
| 193 | 214 | if (f[i] = '{') or (f[i] = '}') then |
| | @@ -215,8 +236,10 @@ |
| 215 | 236 | |
| 216 | 237 | (* Writes f to standard output, each {} replaced by the next of args. *) |
| 217 | 238 | proc print*(f: []U8; args: []Arg); |
| 239 | +var k: Sink; |
| 218 | 240 | begin |
| 219 | | - format(nil, f, args); |
| 241 | + k := sink(nil); |
| 242 | + format(k, f, args); |
| 220 | 243 | end; |
| 221 | 244 | |
| 222 | 245 | (* Writes f to standard error as print writes it, after flushing Out, |
| | @@ -227,12 +250,13 @@ |
| 227 | 250 | are mapped as they are written, and a text of more than a megabyte |
| 228 | 251 | stops the program, as a Buf does when it can't grow. *) |
| 229 | 252 | proc eprint*(f: []U8; args: []Arg); |
| 230 | | -var ar: Arena.Arena; b: Buf.Buf; s: []U8; n: INT; |
| 253 | +var ar: Arena.Arena; b: Buf.Buf; k: Sink; s: []U8; n: INT; |
| 231 | 254 | begin |
| 232 | 255 | Out.flush; |
| 233 | 256 | if not Arena.init(ar, 1 shl 20) then Rt.stop("Fmt.eprint: no arena"); end; |
| 234 | 257 | Buf.init(b, ADR(ar.base)); |
| 235 | | - format(ADR(b), f, args); |
| 258 | + k := sink(ADR(b)); |
| 259 | + format(k, f, args); |
| 236 | 260 | s := Buf.bytes(b); |
| 237 | 261 | while LEN(s) > 0 do |
| 238 | 262 | n := Sys.write(Sys.stderr, s); |
| | @@ -245,13 +269,28 @@ |
| 245 | 269 | |
| 246 | 270 | (* Appends f to b, each {} replaced by the next of args. *) |
| 247 | 271 | proc write*(var b: Buf.Buf; f: []U8; args: []Arg); |
| 272 | +var k: Sink; |
| 248 | 273 | begin |
| 249 | | - format(ADR(b), f, args); (* format's nil is standard output *) |
| 274 | + k := sink(ADR(b)); (* a sink's nil is standard output *) |
| 275 | + format(k, f, args); |
| 276 | +end; |
| 277 | + |
| 278 | +(* Formats f into out as write formats it: the bytes written, and false |
| 279 | + if out was too short and the text is cut there. It needs no memory |
| 280 | + of its own. *) |
| 281 | +proc into*(out: []U8; f: []U8; args: []Arg): (INT, BOOL); |
| 282 | +var k: Sink; |
| 283 | +begin |
| 284 | + k := sink(nil); |
| 285 | + k.out := out; k.slice := true; |
| 286 | + format(k, f, args); |
| 287 | + if k.n > LEN(out) then return LEN(out), false; end; |
| 288 | + return k.n, true; |
| 250 | 289 | end; |
| 251 | 290 | |
| 252 | 291 | (* The module's test: formats into a buffer over an arena. *) |
| 253 | 292 | proc main*; |
| 254 | | -var ar: Arena.Arena; b: Buf.Buf; zero: F64; |
| 293 | +var ar: Arena.Arena; b: Buf.Buf; zero: F64; small: [8]U8; n: INT; whole: BOOL; |
| 255 | 294 | begin |
| 256 | 295 | if not Arena.init(ar, 1 shl 16) then Rt.stop("Fmt: no arena"); end; |
| 257 | 296 | Buf.init(b, ADR(ar.base)); |
| | @@ -278,6 +317,12 @@ |
| 278 | 317 | Buf.reset(b); |
| 279 | 318 | write(b, "{x} {2x} {0} {-} {a} {1", [int(1), int(2)]); |
| 280 | 319 | if Buf.bytes(b) # "x 2x 1 - a 1" then Rt.stop("Fmt: a { that is no width"); end; |
| 320 | + n, whole := into(small, "{}:{02}", [int(-3), int(7)]); |
| 321 | + if (small[0..n] # "-3:07") or not whole then Rt.stop("Fmt.into"); end; |
| 322 | + n, whole := into(small, "line {}, {}", [int(12), str("x")]); |
| 323 | + if (small[0..n] # "line 12,") or whole then Rt.stop("Fmt.into: cut short"); end; |
| 324 | + n, whole := into(small[0..0], "{}", [int(1)]); |
| 325 | + if (n # 0) or whole then Rt.stop("Fmt.into: no room"); end; |
| 281 | 326 | Arena.done(ar); |
| 282 | 327 | end; |
| 283 | 328 | |