9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: erik quanstrom <quanstro@quanstro.net>
To: 9fans@9fans.net
Subject: Re: [9fans] print(2)'s format
Date: Sun,  2 Mar 2014 21:33:23 -0500	[thread overview]
Message-ID: <6d9a0a987ca4a34d918e75e0462e32a7@mikro.quanstro.net> (raw)
In-Reply-To: <5313cf8f.ELeQxkmDw4gWqdyW%trebol55555@aol.com>

> Hello, this is the little story:
> I usually write scripts needing numbers padded on the left with zeros, like in '%03.0f'.  In UNIX I use printf(1), but now I'm using more and more rc scripts with P9P, and I like to use Plan9 programs to make the scripts more portable to Plan9... so I tried with seq, like '9 seq -f%03.0f $i $i' or -f%03g, getting the desired output in P9P, but discovering that in Plan9, the floating point verbs doesn't include the flag '0'.
> I'm really curious about this.  Anyone knows the reason of letting out this flag (and including it in P9P)?
>

i am not sure if there is any knock-on to this, but i think this is all that's
required.  unfortunately %06s might do some wierd things.  shouldn't
be a big issue, since that's not specified anyway,  and
	g '%0[0-9]*(\.[0-9]*)?[sq]' /sys/src
turns up nothing.

i'm considering the patch.

- erik

diff -c /n/dump/2014/0302/sys/src/libc/fmt/dofmt.c ./dofmt.c
/n/dump/2014/0302/sys/src/libc/fmt/dofmt.c:87,99 - ./dofmt.c:87,100
  int
  _fmtpad(Fmt *f, int n)
  {
- 	char *t, *s;
+ 	char *t, *s, c;
  	int i;

  	t = f->to;
  	s = f->stop;
+ 	c = f->flags&FmtZero? '0': ' ';
  	for(i = 0; i < n; i++)
- 		FMTCHAR(f, t, s, ' ');
+ 		FMTCHAR(f, t, s, c);
  	f->nfmt += t - (char *)f->to;
  	f->to = t;
  	return 0;
/n/dump/2014/0302/sys/src/libc/fmt/dofmt.c:102,114 - ./dofmt.c:103,116
  int
  _rfmtpad(Fmt *f, int n)
  {
- 	Rune *t, *s;
+ 	Rune *t, *s, r;
  	int i;

  	t = f->to;
  	s = f->stop;
+ 	r = f->flags&FmtZero? '0': ' ';
  	for(i = 0; i < n; i++)
- 		FMTRCHAR(f, t, s, ' ');
+ 		FMTRCHAR(f, t, s, r);
  	f->nfmt += t - (Rune *)f->to;
  	f->to = t;
  	return 0;
diff -c /n/dump/2014/0302/sys/src/libc/fmt/fltfmt.c ./fltfmt.c
/n/dump/2014/0302/sys/src/libc/fmt/fltfmt.c:303,309 - ./fltfmt.c:303,309
  	 * which is 341 currently.
  	 */
  	xdtoa(fmt, s, f);
- 	fmt->flags &= FmtWidth|FmtLeft;
+ 	fmt->flags &= FmtZero|FmtWidth|FmtLeft;
  	_fmtcpy(fmt, s, strlen(s), strlen(s));
  	return 0;
  }




      reply	other threads:[~2014-03-03  2:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-03  0:40 trebol
2014-03-03  2:33 ` erik quanstrom [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6d9a0a987ca4a34d918e75e0462e32a7@mikro.quanstro.net \
    --to=quanstro@quanstro.net \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).