9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] dofmt.c patch
@ 2011-07-02  2:13 erik quanstrom
  0 siblings, 0 replies; only message in thread
From: erik quanstrom @ 2011-07-02  2:13 UTC (permalink / raw)
  To: 9fans

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



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2011-07-02  2:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-02  2:13 [9fans] dofmt.c patch erik quanstrom

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).