From 86d04654a747f537c43e464adb227118d11bfd10 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 14 Jun 2021 06:51:02 -0700 Subject: format: new precision - character for zero. * stream.c (formatv): The - precision option character produces a "sign" that is a zero. If this is used with leading zeros, it will avoid generating a space. The requirements can use improvement here, but one step at a time. * tests/018/format.tl: Some tests. * txr.1: Documented. --- stream.c | 3 +++ tests/018/format.tl | 5 +++++ txr.1 | 12 ++++++++++++ 3 files changed, 20 insertions(+) diff --git a/stream.c b/stream.c index 248a51b2..77af0b37 100644 --- a/stream.c +++ b/stream.c @@ -3351,6 +3351,9 @@ val formatv(val stream_in, val fmtstr, struct args *al) case '+': case ' ': sign = ch; continue; + case '-': + sign = '0'; + continue; case '*': saved_state = state; state = vf_star; diff --git a/tests/018/format.tl b/tests/018/format.tl index b8cf7445..6c58d45b 100644 --- a/tests/018/format.tl +++ b/tests/018/format.tl @@ -24,6 +24,11 @@ (fmt "~4,03x" #xaf) " 0af" (fmt "~-4,03X" #xaf) "0AF ") +(mtest + (fmt "~8,02f" 12) " 0012.00" + (fmt "~8,+02f" 12) "+0012.00" + (fmt "~8,-02f" 12) "00012.00") + (test (fmt "~x" (sha256 "abc")) "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") diff --git a/txr.1 b/txr.1 index 3bbcf973..9ef21238 100644 --- a/txr.1 +++ b/txr.1 @@ -54526,9 +54526,21 @@ The precision specifier may begin with these optional characters: (the "leading zero indicator"), .coIP + (print a sign for positive values") +.coIP - +(print a zero in place of a positive sign). .IP space (print a space in place of a positive sign). .RE +If the +.codn + , +.code - +or +space are multiply specified, the rightmost one takes precedence. The +prefix character that these three options produce (plus sign, zero or space) is +produced unconditionally, even if it overflows the width of the field. +Leading zeros produced by the +.code 0 +option are not generated if they would overflow the field. The precision specifier itself follows: it must be either a decimal integer or the -- cgit v1.2.3