raz / commit a700688d

commit
a700688d317fcc2448db6b7eab483cab5d20dffc
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
d523cd1c
Fmt: short is a procedure writing into a ShortMost array, not an Arg, so programs that don't call it don't carry its tables (hello world 49,200 to 40,504 bytes)

patch · log

3 files changed, +67 -37

doc/raz-lib.md (history) modified +9 -3

@@ -763,8 +763,9 @@
763763 char(c: U8);
764764 bool(b: BOOL);
765765 real(r: F64; places: INT);
766- short(r: F64);
767766 end;
767+const ShortMost* = 24; (* the longest Fmt.short *)
768+proc short*(x: F64; var out: [ShortMost]U8): []U8; (* x in the fewest digits that read back, in out *)
768769 proc print*(f: []U8; args: []Arg); (* to standard output *)
769770 proc eprint*(f: []U8; args: []Arg); (* to standard error, once Out is flushed *)
770771 proc write*(var b: Buf.Buf; f: []U8; args: []Arg); (* added to b *)
@@ -968,12 +969,17 @@
968969 default rounding, as Pascal's `Round`, where `round` is C's. `Fmt.real(x, places)` writes
969970 `x` with `places` digits after the point, at most 9, rounded to the
970971 nearest; from 1E15 on, with an exponent, as `1.50E17`; and `nan`,
971-`inf` and `-inf` as themselves. `Fmt.short(x)` writes `x` in the
972-fewest significant digits that `Math.toReal` reads back as the same
972+`inf` and `-inf` as themselves. `Fmt.short(x, out)` writes `x` into
973+`out`, from 0, and gives those bytes: the fewest significant digits
974+that `Math.toReal` reads back as the same
973975 `F64`, and of two such the nearer `x`: `0.1`, not
974976 `0.10000000000000001`; `42`, with no point; `0.0001`; and below 1E-4
975977 or from 1E15 on with an exponent, as `1.5E17` or `5E-324`. Zero keeps
976978 its sign, `0` or `-0`, and `nan` and `inf` are as `real` writes them.
979+The longest is `Fmt.ShortMost` bytes, so a call is
980+`var d: [Fmt.ShortMost]U8; Fmt.print("{}", [Fmt.str(Fmt.short(x, d))]);`,
981+and a width goes with `str`. The bytes are `out`'s: a second call with the same
982+`out` overwrites them.
977983 `hex` writes capitals and `lowHex`
978984 lower case, neither with a `0x`. A width between the braces pads an
979985 argument to that many bytes: `{2}` with spaces on the left, `{02}`

lib/Fmt.raz (history) modified +56 -33

@@ -21,17 +21,13 @@
2121 TwoStep = 29; (* doublings at once: 10^9 * 2^29 < 2^63 *)
2222 FiveStep = 13; (* fives multiplied at once: 10^9 * 5^13 < 2^63 *)
2323
24+const ShortMost* = 24; (* the longest short: -2.2250738585072014E-308 *)
25+
2426 type
2527 (* An argument, and how it is printed. A real has places digits
2628 after the point, at most 9, rounded to the nearest, half away
2729 from zero; from 1E15 on, it has the exponent that makes it less
28- than 10, as 1.50E17; nan and inf are spelled so. A short has the
29- fewest significant digits that Math.toReal reads back as the
30- same F64, the nearer of two such, and of two as near the one
31- whose last digit is even; it is written as 0.0001, 0.1, 42 or
32- 123456789012345, and with an exponent below 1E-4 and from 1E15
33- on, as 1.5E17 or 5E-324. Zero is 0 or -0, keeping its sign so
34- that it too reads back; nan and inf are spelled as real's. *)
30+ than 10, as 1.50E17; nan and inf are spelled so. *)
3531 Arg* = variant
3632 int(i: I64); (* in decimal *)
3733 uint(u: U64); (* in decimal *)
@@ -41,7 +37,6 @@
4137 char(c: U8); (* the one byte *)
4238 bool(b: BOOL); (* true or false *)
4339 real(r: F64; places: INT);
44- short(r: F64);
4540 end;
4641
4742 (* Where the pieces go: to out if slice is set, else to b, or to
@@ -361,32 +356,52 @@
361356 end;
362357 end;
363358
364-(* x as Arg's short says, but for the minus sign, which arg writes. *)
365-proc putShort(var k: Sink; x: F64);
366-var s: Short; d: [24]U8; at, e: INT;
359+(* s added to out at n, which moves past it. *)
360+proc emit(var out: [ShortMost]U8; var n: INT; s: []U8);
361+begin
362+ COPY(out[n..n + LEN(s)], s);
363+ INC(n, LEN(s));
364+end;
365+
366+(* x in the fewest digits Math.toReal reads back as x, the nearer of
367+ two such, and of two as near the one whose last digit is even,
368+ written into out from 0: those bytes. From 1E-4 to below 1E15 it is
369+ plain, as 0.0001, 0.1, 42 or 123456789012345; else a digit, a point
370+ and the rest, an E and the exponent, as 1.5E17 or 5E-324. Zero is 0
371+ or -0, keeping its sign so that it too reads back; nan, inf and
372+ -inf are spelled so. The longest is ShortMost, 24 bytes, as
373+ -2.2250738585072014E-308: a sign, 17 digits, a point and E-308; the
374+ longest plain one, -0.00012345678901234567, has 22. *)
375+proc short*(x: F64; var out: [ShortMost]U8): []U8;
376+var s: Short; d: [24]U8; n, at, e, z: INT;
367377 begin
368- if x # x then put(k, "nan"); return; end;
369- if x < 0.0 then x := -x; end;
370- if x > 1.7976931348623157E308 then put(k, "inf"); return; end;
371- if x = 0.0 then put(k, "0"); return; end; (* -0.0 too *)
378+ n := 0;
379+ if x # x then emit(out, n, "nan"); return out[0..n]; end;
380+ if bits(x) shr 63 = 1 then emit(out, n, "-"); x := -x; end; (* -0.0 too *)
381+ if x > 1.7976931348623157E308 then emit(out, n, "inf"); return out[0..n]; end;
382+ if x = 0.0 then emit(out, n, "0"); return out[0..n]; end;
372383 shortest(x, s);
373384 if (s.e >= -3) and (s.e <= 15) then (* from 1E-4 to below 1E15 *)
374385 if s.e <= 0 then
375- put(k, "0."); fill(k, '0', -s.e); put(k, s.d[0..s.n]);
386+ emit(out, n, "0.");
387+ for z := 1 to -s.e do emit(out, n, "0"); end;
388+ emit(out, n, s.d[0..s.n]);
376389 elsif s.e >= s.n then
377- put(k, s.d[0..s.n]); fill(k, '0', s.e - s.n);
390+ emit(out, n, s.d[0..s.n]);
391+ for z := 1 to s.e - s.n do emit(out, n, "0"); end;
378392 else
379- put(k, s.d[0..s.e]); put(k, "."); put(k, s.d[s.e..s.n]);
393+ emit(out, n, s.d[0..s.e]); emit(out, n, "."); emit(out, n, s.d[s.e..s.n]);
380394 end;
381395 else
382- put(k, s.d[0..1]);
383- if s.n > 1 then put(k, "."); put(k, s.d[1..s.n]); end;
384- put(k, "E");
396+ emit(out, n, s.d[0..1]);
397+ if s.n > 1 then emit(out, n, "."); emit(out, n, s.d[1..s.n]); end;
398+ emit(out, n, "E");
385399 e := s.e - 1;
386- if e < 0 then put(k, "-"); e := -e; end;
400+ if e < 0 then emit(out, n, "-"); e := -e; end;
387401 d, at := digits(U64(e), 10, 'A');
388- put(k, d[at..24]);
402+ emit(out, n, d[at..24]);
389403 end;
404+ return out[0..n];
390405 end;
391406
392407 (* Whether a is printed with a minus sign. *)
@@ -396,7 +411,6 @@
396411 match a of
397412 | int(i): neg := i < 0;
398413 | real(r, _places): neg := r < 0.0;
399- | short(r): neg := (r = r) and (bits(r) shr 63 = 1); (* -0.0 too, but no nan *)
400414 | else: neg := false;
401415 end;
402416 return neg;
@@ -421,7 +435,6 @@
421435 | char(c): one[0] := c; put(k, one);
422436 | bool(t): if t then put(k, "true"); else put(k, "false"); end;
423437 | real(r, places): putReal(k, r, places);
424- | short(r): putShort(k, r);
425438 end;
426439 end;
427440
@@ -550,9 +563,19 @@
550563 return k.n, true;
551564 end;
552565
566+(* Each of xs, shorts, with a space between, added to b. *)
567+proc shorts(var b: Buf.Buf; xs: []F64);
568+var d: [ShortMost]U8; i: INT;
569+begin
570+ for i := 0 to LEN(xs) - 1 do
571+ if i > 0 then Buf.add(b, ' '); end;
572+ Buf.str(b, short(xs[i], d));
573+ end;
574+end;
575+
553576 (* The module's test: formats into a buffer over an arena. *)
554577 proc main*;
555-var ar: Arena.Arena; b: Buf.Buf; zero: F64; small: [8]U8; n: INT; whole: BOOL;
578+var ar: Arena.Arena; b: Buf.Buf; zero: F64; small: [8]U8; d: [ShortMost]U8; n: INT; whole: BOOL;
556579 begin
557580 if not Arena.init(ar, 1 shl 16) then Rt.stop("Fmt: no arena"); end;
558581 Buf.init(b, ADR(ar.base));
@@ -580,19 +603,19 @@
580603 write(b, "{x} {2x} {0} {-} {a} {1", [int(1), int(2)]);
581604 if Buf.bytes(b) # "x 2x 1 - a 1" then Rt.stop("Fmt: a { that is no width"); end;
582605 Buf.reset(b);
583- write(b, "{} {} {} {} {} {} {} {}", [short(0.1), short(1.0 / 3.0), short(-2.5), short(100.0), short(42.0E3),
584- short(0.0001), short(0.00012), short(123456789012345.0)]);
606+ shorts(b, [0.1, 1.0 / 3.0, -2.5, 100.0, 42.0E3, 0.0001, 0.00012, 123456789012345.0]);
585607 if Buf.bytes(b) # "0.1 0.3333333333333333 -2.5 100 42000 0.0001 0.00012 123456789012345" then Rt.stop("Fmt.short"); end;
586608 Buf.reset(b);
587- write(b, "{} {} {} {} {} {} {}", [short(1.0E15), short(1.5E17), short(9.9E-5), short(5.0E-324),
588- short(1.7976931348623157E308), short(2.2250738585072014E-308), short(9007199254740992.0)]);
609+ shorts(b, [1.0E15, 1.5E17, 9.9E-5, 5.0E-324, 1.7976931348623157E308, 2.2250738585072014E-308,
610+ 9007199254740992.0]);
589611 if Buf.bytes(b) # "1E15 1.5E17 9.9E-5 5E-324 1.7976931348623157E308 2.2250738585072014E-308 9.007199254740992E15" then
590612 Rt.stop("Fmt.short: an exponent");
591613 end;
592614 Buf.reset(b);
593- write(b, "{} {} {} {} {} {6}|{-6}|{06}", [short(zero), short(-zero), short(1.0 / zero), short(-1.0 / zero),
594- short(zero / zero), short(0.5), short(0.5), short(-0.5)]);
595- if Buf.bytes(b) # "0 -0 inf -inf nan 0.5|0.5 |-000.5" then Rt.stop("Fmt.short: zero, inf, nan and a width"); end;
615+ shorts(b, [zero, -zero, 1.0 / zero, -1.0 / zero, zero / zero]);
616+ write(b, " {6}|{-6}", [str(short(0.5, d)), str(short(0.5, d))]);
617+ if Buf.bytes(b) # "0 -0 inf -inf nan 0.5|0.5 " then Rt.stop("Fmt.short: zero, inf, nan and a width"); end;
618+ if LEN(short(-2.2250738585072014E-308, d)) # ShortMost then Rt.stop("Fmt.short: the longest"); end;
596619 n, whole := into(small, "{}:{02}", [int(-3), int(7)]);
597620 if (small[0..n] # "-3:07") or not whole then Rt.stop("Fmt.into"); end;
598621 n, whole := into(small, "line {}, {}", [int(12), str("x")]);

test/run/FmtShort.raz (history) modified +2 -1

@@ -6,6 +6,7 @@
66
77 var
88 text, fewer: Buf.Buf;
9+ digitsOut: [Fmt.ShortMost]U8;
910 tried, bad: INT;
1011
1112 (* What toReal reads s as; a nan if it reads nothing. *)
@@ -61,7 +62,7 @@
6162 begin
6263 INC(tried);
6364 Buf.reset(text);
64- Fmt.write(text, "{}", [Fmt.short(x)]);
65+ Fmt.write(text, "{}", [Fmt.str(Fmt.short(x, digitsOut))]);
6566 if not same(read(Buf.bytes(text)), x) then
6667 INC(bad);
6768 Fmt.print("{} reads back as another F64\n", [Fmt.str(Buf.bytes(text))]);