this got lost in a large patch that didn't get accepted, and i just turned it 
up.  i'd hate for
anyone else to trip over the same troubles.  this fixes
- sneaky UTFmax dependencies
- a bug if %r is given with no matching format for rune r, where r>127.
- %, interacting badly with 0 padding.
- bad formatting of %#b.

/n/sources/plan9//sys/src/libc/fmt/dofmt.c:415,426 - dofmt.c:415,431
                *p-- = '0';
                n = 1;
        }
-       for(w = f->prec; n < w && p > buf+3; n++)
+       for(w = f->prec; n < w && p > buf+4; n++){
+               if((fl & FmtComma) && n % 4 == 3){
+                       *p-- = ',';
+                       n++;
+               }
                *p-- = '0';
+       }
        if(neg || (fl & (FmtSign|FmtSpace)))
                n++;
        if(fl & FmtSharp){
-               if(base == 16)
+               if(base == 16 || base == 2)
                        n += 2;
                else if(base == 8){
                        if(p[1] == '0')
/n/sources/plan9//sys/src/libc/fmt/dofmt.c:430,443 - dofmt.c:435,453
                }
        }
        if((fl & FmtZero) && !(fl & (FmtLeft|FmtPrec))){
-               for(w = f->width; n < w && p > buf+3; n++)
+               for(w = f->width; n < w && p > buf+4; n++){
+                       if((fl & FmtComma) && n % 4 == 3){
+                               *p-- = ',';
+                               n++;
+                       }
                        *p-- = '0';
+               }
                f->width = 0;
        }
        if(fl & FmtSharp){
-               if(base == 16)
+               if(base == 16 || base == 2)
                        *p-- = f->r;
-               if(base == 16 || base == 8)
+               if(base == 16 || base == 8 || base == 2)
                        *p-- = '0';
        }
        if(neg)
/n/sources/plan9//sys/src/libc/fmt/dofmt.c:512,523 - dofmt.c:522,534
  int
  _badfmt(Fmt *f)
  {
-       char x[3];
+       char x[2+UTFmax];
+       int n;
  
        x[0] = '%';
-       x[1] = f->r;
-       x[2] = '%';
-       f->prec = 3;
-       _fmtcpy(f, x, 3, 3);
+       n = 1 + runetochar(x+1, (Rune*)&f->r);
+       x[n++] = '%';
+       f->prec = n;
+       _fmtcpy(f, x, n, n);
        return 0;
  }



- erik

Reply via email to