raz / commit 8e8cefde

commit
8e8cefded44da7ae2983ca848fdd98edf90a63c9
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
c473acc2
Fmt: into, formatting into a caller's slice with no heap

A Sink now writes to a slice as well as to a Buf or standard output,
counting every byte given so the slice keeps the first LEN(out) and the
rest are cut; into gives the bytes written and false when cut short.

patch · log

2 files changed, +63 -14

doc/raz-lib.md (history) modified +5 -1

@@ -708,6 +708,7 @@
708708 proc print*(f: []U8; args: []Arg); (* to standard output *)
709709 proc eprint*(f: []U8; args: []Arg); (* to standard error, once Out is flushed *)
710710 proc write*(var b: Buf.Buf; f: []U8; args: []Arg); (* added to b *)
711+proc into*(out: []U8; f: []U8; args: []Arg): (INT, BOOL); (* into out: the bytes, and false if cut short *)
711712
712713 module List(T);
713714 type
@@ -914,7 +915,10 @@
914915 with spaces on the right. A longer argument is written whole. A `{`
915916 followed by anything but `}` or a width is dropped, as before.
916917 `Fmt.print("{2}:{02} U+{04}", [Fmt.int(3), Fmt.int(7), Fmt.hex(0E9H)])`
917-writes ` 3:07 U+00E9`.
918+writes ` 3:07 U+00E9`. `Fmt.into` formats into a slice the caller
919+gives, with no memory of its own, for a library that has no heap: it
920+gives the bytes written, and false if the slice was too short and the
921+text is cut there.
918922
919923 **Files and Opt** are what most programs need of their command line.
920924 `Files.writeAll` writes again after a short write or a signal, as a

lib/Fmt.raz (history) modified +58 -13

@@ -26,12 +26,15 @@
2626 real(r: F64; places: INT);
2727 end;
2828
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.
3032 With dry set nothing is written and n only counts the bytes
3133 that would be, to know how much a width must pad. *)
3234 Sink = record
3335 b: ^Buf.Buf;
34- dry: BOOL;
36+ out: []U8;
37+ slice, dry: BOOL;
3538 n: INT;
3639 end;
3740
@@ -57,12 +60,30 @@
5760 return d, i;
5861 end;
5962
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+
6071 (* s to k. *)
6172 proc put(var k: Sink; s: []U8);
73+var at, room: INT;
6274 begin
75+ at := k.n;
6376 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;
6687 end;
6788
6889 (* n copies of c to k. *)
@@ -154,7 +175,7 @@
154175 proc field(var k: Sink; a: Arg; const s: Spec);
155176 var dry: Sink; pad: INT;
156177 begin
157- dry.b := nil; dry.dry := true; dry.n := 0;
178+ dry := sink(nil); dry.dry := true;
158179 arg(dry, a);
159180 pad := s.width - dry.n;
160181 if s.left then
@@ -184,10 +205,10 @@
184205 return -1;
185206 end;
186207
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;
189211 begin
190- k.b := b; k.dry := false; k.n := 0;
191212 i := 0; start := 0; n := 0;
192213 while i < LEN(f) do
193214 if (f[i] = '{') or (f[i] = '}') then
@@ -215,8 +236,10 @@
215236
216237 (* Writes f to standard output, each {} replaced by the next of args. *)
217238 proc print*(f: []U8; args: []Arg);
239+var k: Sink;
218240 begin
219- format(nil, f, args);
241+ k := sink(nil);
242+ format(k, f, args);
220243 end;
221244
222245 (* Writes f to standard error as print writes it, after flushing Out,
@@ -227,12 +250,13 @@
227250 are mapped as they are written, and a text of more than a megabyte
228251 stops the program, as a Buf does when it can't grow. *)
229252 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;
231254 begin
232255 Out.flush;
233256 if not Arena.init(ar, 1 shl 20) then Rt.stop("Fmt.eprint: no arena"); end;
234257 Buf.init(b, ADR(ar.base));
235- format(ADR(b), f, args);
258+ k := sink(ADR(b));
259+ format(k, f, args);
236260 s := Buf.bytes(b);
237261 while LEN(s) > 0 do
238262 n := Sys.write(Sys.stderr, s);
@@ -245,13 +269,28 @@
245269
246270 (* Appends f to b, each {} replaced by the next of args. *)
247271 proc write*(var b: Buf.Buf; f: []U8; args: []Arg);
272+var k: Sink;
248273 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;
250289 end;
251290
252291 (* The module's test: formats into a buffer over an arena. *)
253292 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;
255294 begin
256295 if not Arena.init(ar, 1 shl 16) then Rt.stop("Fmt: no arena"); end;
257296 Buf.init(b, ADR(ar.base));
@@ -278,6 +317,12 @@
278317 Buf.reset(b);
279318 write(b, "{x} {2x} {0} {-} {a} {1", [int(1), int(2)]);
280319 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;
281326 Arena.done(ar);
282327 end;
283328