> 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 via email to