From mboxrd@z Thu Jan 1 00:00:00 1970 From: erik quanstrom Date: Fri, 1 Jul 2011 22:13:14 -0400 To: 9fans@9fans.net Message-ID: <8ebc8bbdc761a4b89f618c0532934246@ladd.quanstro.net> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Subject: [9fans] dofmt.c patch Topicbox-Message-UUID: f90ac5de-ead6-11e9-9d60-3106f5b1d025 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