commit 85231fd8cdf32d861e196d7dfa827b7239157817 from: rsc date: Sun May 21 20:49:16 2006 UTC fmt changes from Google commit - 8d7133308db580d2356d5d1dd30f0b9a1f0a7417 commit + 85231fd8cdf32d861e196d7dfa827b7239157817 blob - 1744ddc061ff1b1d0b3a5101136a28cdb622f55b blob + 480ccad58d4538b5bf912b209231d4b41849e925 --- include/fmt.h +++ include/fmt.h @@ -34,6 +34,18 @@ struct Fmt{ int width; int prec; unsigned long flags; + char *decimal; /* decimal point; cannot be "" */ + + /* For %'d */ + char *thousands; /* separator for thousands */ + + /* + * Each char is an integer indicating #digits before next separator. Values: + * \xFF: no more grouping (or \x7F; defined to be CHAR_MAX in POSIX) + * \x00: repeat previous indefinitely + * \x**: count that many + */ + char *grouping; /* descriptor of separator placement */ }; enum{ @@ -43,7 +55,8 @@ enum{ FmtSharp = FmtPrec << 1, FmtSpace = FmtSharp << 1, FmtSign = FmtSpace << 1, - FmtZero = FmtSign << 1, + FmtApost = FmtSign << 1, + FmtZero = FmtApost << 1, FmtUnsigned = FmtZero << 1, FmtShort = FmtUnsigned << 1, FmtLong = FmtShort << 1, @@ -64,6 +77,8 @@ double fmtcharstod(int(*f)(void*), void *vp); int fmtfdflush(Fmt *f); int fmtfdinit(Fmt *f, int fd, char *buf, int size); int fmtinstall(int c, int (*f)(Fmt*)); +int fmtnullinit(Fmt*); +void fmtlocaleinit(Fmt*, char*, char*, char*); int fmtprint(Fmt *f, char *fmt, ...); int fmtrune(Fmt *f, int r); int fmtrunestrcpy(Fmt *f, Rune *s); blob - 5dc21cb5f04d1288ed2c03ba141e5b89f04deeea blob + f9ae4fccc95fb497567b5c1cc0f580e3ce39cc27 --- src/lib9/fmt/LICENSE +++ src/lib9/fmt/LICENSE @@ -1,6 +1,8 @@ /* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. + * The authors of this software are Rob Pike and Ken Thompson, + * with contributions from Mike Burrows and Sean Dorward. + * + * Copyright (c) 2002-2006 by Lucent Technologies. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy @@ -10,10 +12,9 @@ * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. -*/ + */ This is a Unix port of the Plan 9 formatted I/O package. -Please send comments about the packaging -to Russ Cox . +Please send comments about the packaging to Russ Cox . blob - cbe6d3dda080f05bcfc3b5f57354054d6112fd3d blob + 6ff57676508480a1b55d5bc02c317631959034fb --- src/lib9/fmt/charstod.c +++ src/lib9/fmt/charstod.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" blob - 97bbc928ff5d0fc0bdec10d658522407619029d4 blob + 06f0a3d93eb5c490e863120cb2afc2da7abef2b9 --- src/lib9/fmt/dofmt.c +++ src/lib9/fmt/dofmt.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" @@ -97,7 +85,7 @@ __fmtflush(Fmt *f, void *t, int len) /* * put a formatted block of memory sz bytes long of n runes into the output buffer, - * left/right justified in a field of at least f->width charactes + * left/right justified in a field of at least f->width characters (if FmtWidth is set) */ int __fmtpad(Fmt *f, int n) @@ -139,8 +127,10 @@ __fmtcpy(Fmt *f, const void *vm, int n, int sz) m = (char*)vm; me = m + sz; - w = f->width; fl = f->flags; + w = 0; + if(fl & FmtWidth) + w = f->width; if((fl & FmtPrec) && n > f->prec) n = f->prec; if(f->runes){ @@ -194,8 +184,10 @@ __fmtrcpy(Fmt *f, const void *vm, int n) int w; m = (Rune*)vm; - w = f->width; fl = f->flags; + w = 0; + if(fl & FmtWidth) + w = f->width; if((fl & FmtPrec) && n > f->prec) n = f->prec; if(f->runes){ @@ -324,10 +316,14 @@ __percentfmt(Fmt *f) int __ifmt(Fmt *f) { - char buf[70], *p, *conv; + char buf[140], *p, *conv; + /* 140: for 64 bits of binary + 3-byte sep every 4 digits */ uvlong vu; ulong u; int neg, base, i, n, fl, w, isv; + int ndig, len, excess, bytelen; + char *grouping; + char *thousands; neg = 0; fl = f->flags; @@ -339,11 +335,11 @@ __ifmt(Fmt *f) * Unsigned verbs for ANSI C */ switch(f->r){ - case 'x': - case 'X': case 'o': - case 'u': case 'p': + case 'u': + case 'x': + case 'X': fl |= FmtUnsigned; fl &= ~(FmtSign|FmtSpace); break; @@ -381,21 +377,25 @@ __ifmt(Fmt *f) u = va_arg(f->args, int); } conv = "0123456789abcdef"; + grouping = "\4"; /* for hex, octal etc. (undefined by spec but nice) */ + thousands = f->thousands; switch(f->r){ case 'd': case 'i': case 'u': base = 10; + grouping = f->grouping; break; + case 'X': + conv = "0123456789ABCDEF"; + /* fall through */ case 'x': base = 16; + thousands = ":"; break; - case 'X': - base = 16; - conv = "0123456789ABCDEF"; - break; case 'b': base = 2; + thousands = ":"; break; case 'o': base = 8; @@ -413,7 +413,11 @@ __ifmt(Fmt *f) } } p = buf + sizeof buf - 1; - n = 0; + n = 0; /* in runes */ + excess = 0; /* number of bytes > number runes */ + ndig = 0; + len = utflen(thousands); + bytelen = strlen(thousands); if(isv){ while(vu){ i = vu % base; @@ -422,6 +426,12 @@ __ifmt(Fmt *f) *p-- = ','; n++; } + if((fl & FmtApost) && __needsep(&ndig, &grouping)){ + n += len; + excess += bytelen - len; + p -= bytelen; + memmove(p+1, thousands, bytelen); + } *p-- = conv[i]; n++; } @@ -433,6 +443,12 @@ __ifmt(Fmt *f) *p-- = ','; n++; } + if((fl & FmtApost) && __needsep(&ndig, &grouping)){ + n += len; + excess += bytelen - len; + p -= bytelen; + memmove(p+1, thousands, bytelen); + } *p-- = conv[i]; n++; } @@ -440,9 +456,19 @@ __ifmt(Fmt *f) if(n == 0){ *p-- = '0'; n = 1; + if(fl & FmtApost) + __needsep(&ndig, &grouping); + fl &= ~FmtSharp; /* ??? */ } - for(w = f->prec; n < w && p > buf+3; n++) + for(w = f->prec; n < w && p > buf+3; n++){ + if((fl & FmtApost) && __needsep(&ndig, &grouping)){ + n += len; + excess += bytelen - len; + p -= bytelen; + memmove(p+1, thousands, bytelen); + } *p-- = '0'; + } if(neg || (fl & (FmtSign|FmtSpace))) n++; if(fl & FmtSharp){ @@ -456,9 +482,19 @@ __ifmt(Fmt *f) } } if((fl & FmtZero) && !(fl & (FmtLeft|FmtPrec))){ - for(w = f->width; n < w && p > buf+3; n++) + w = 0; + if(fl & FmtWidth) + w = f->width; + for(; n < w && p > buf+3; n++){ + if((fl & FmtApost) && __needsep(&ndig, &grouping)){ + n += len; + excess += bytelen - len; + p -= bytelen; + memmove(p+1, thousands, bytelen); + } *p-- = '0'; - f->width = 0; + } + f->flags &= ~FmtWidth; } if(fl & FmtSharp){ if(base == 16) @@ -473,7 +509,7 @@ __ifmt(Fmt *f) else if(fl & FmtSpace) *p-- = ' '; f->flags &= ~FmtPrec; - return __fmtcpy(f, p + 1, n, n); + return __fmtcpy(f, p + 1, n, n + excess); } int @@ -514,6 +550,9 @@ __flagfmt(Fmt *f) case '#': f->flags |= FmtSharp; break; + case '\'': + f->flags |= FmtApost; + break; case ' ': f->flags |= FmtSpace; break; blob - 034dea14e912e1680ae2bf00a4edabbba6f14b33 blob + 21e42ebc4516f61438c56ae550ce720f778f5a5a --- src/lib9/fmt/dorfmt.c +++ src/lib9/fmt/dorfmt.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" @@ -19,6 +7,7 @@ /* format the output into f->to and return the number of characters fmted */ +/* BUG: THIS FILE IS NOT UPDATED TO THE NEW SPEC */ int dorfmt(Fmt *f, const Rune *fmt) { @@ -30,8 +19,8 @@ dorfmt(Fmt *f, const Rune *fmt) nfmt = f->nfmt; for(;;){ if(f->runes){ - rt = f->to; - rs = f->stop; + rt = (Rune*)f->to; + rs = (Rune*)f->stop; while((r = *fmt++) && r != '%'){ FMTRCHAR(f, rt, rs, r); } @@ -41,8 +30,8 @@ dorfmt(Fmt *f, const Rune *fmt) return f->nfmt - nfmt; f->stop = rs; }else{ - t = f->to; - s = f->stop; + t = (char*)f->to; + s = (char*)f->stop; while((r = *fmt++) && r != '%'){ FMTRUNE(f, t, f->stop, r); } @@ -53,7 +42,7 @@ dorfmt(Fmt *f, const Rune *fmt) f->stop = s; } - fmt = __fmtdispatch(f, (Rune*)fmt, 1); + fmt = (Rune*)__fmtdispatch(f, (Rune*)fmt, 1); if(fmt == nil) return -1; } blob - b0eae7354535154353287c86dd4c4db42a3fb5b4 blob + 7d1947053168ccbee3dd19d23de1e508d8a666c4 --- src/lib9/fmt/errfmt.c +++ src/lib9/fmt/errfmt.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include blob - 9c94f156c75b8445c2ef711b65ad4130aab107af blob + 8288a9667f8875c3bfd54c3891d5b5751eed5378 --- src/lib9/fmt/fltfmt.c +++ src/lib9/fmt/fltfmt.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include blob - 642de8c7e660957fe110e51ac1ea9585f8bc42d9 blob + 66093fd0db28376f5df9dcbc5f45fcbc59ff9eef --- src/lib9/fmt/fmt.c +++ src/lib9/fmt/fmt.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" @@ -29,7 +17,7 @@ struct Convfmt volatile Fmts fmt; /* for spin lock in fmtfmt; avoids race due to write order */ }; -struct +static struct { /* lock by calling __fmtlock, __fmtunlock */ int nfmt; @@ -40,6 +28,7 @@ static Convfmt knownfmt[] = { ' ', __flagfmt, '#', __flagfmt, '%', __percentfmt, + '\'', __flagfmt, '+', __flagfmt, ',', __flagfmt, '-', __flagfmt, blob - 5a63f9be9b7f50cfb0dcaf8fd3efe8c976c4a8ff blob + 13d7f81ee2825f71123fe93e6b92a0d2bdd4d3fc --- src/lib9/fmt/fmtdef.h +++ src/lib9/fmt/fmtdef.h @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY - * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ /* * dofmt -- format to a buffer @@ -66,9 +54,9 @@ int __strfmt(Fmt *f); #define FMTCHAR(f, t, s, c)\ do{\ if(t + 1 > (char*)s){\ - t = __fmtflush(f, t, 1);\ + t = (char*)__fmtflush(f, t, 1);\ if(t != nil)\ - s = f->stop;\ + s = (char*)f->stop;\ else\ return -1;\ }\ @@ -78,9 +66,9 @@ int __strfmt(Fmt *f); #define FMTRCHAR(f, t, s, c)\ do{\ if(t + 1 > (Rune*)s){\ - t = __fmtflush(f, t, sizeof(Rune));\ + t = (Rune*)__fmtflush(f, t, sizeof(Rune));\ if(t != nil)\ - s = f->stop;\ + s = (Rune*)f->stop;\ else\ return -1;\ }\ @@ -92,9 +80,9 @@ int __strfmt(Fmt *f); Rune _rune;\ int _runelen;\ if(t + UTFmax > (char*)s && t + (_runelen = runelen(r)) > (char*)s){\ - t = __fmtflush(f, t, _runelen);\ + t = (char*)__fmtflush(f, t, _runelen);\ if(t != nil)\ - s = f->stop;\ + s = (char*)f->stop;\ else\ return -1;\ }\ blob - 9f35f02adb597f729aa07886ccd119ea21b82696 blob + 1e7998b0c3a7c5d0671a5658c429200014ec76e9 --- src/lib9/fmt/fmtfd.c +++ src/lib9/fmt/fmtfd.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" @@ -41,6 +29,8 @@ fmtfdinit(Fmt *f, int fd, char *buf, int size) f->stop = buf + size; f->flush = __fmtFdFlush; f->farg = (void*)(uintptr_t)fd; + f->flags = 0; f->nfmt = 0; + fmtlocaleinit(f, nil, nil, nil); return 0; } blob - b615416a072cc3773548c3985143b8f22ee2de47 blob + 7abb982a274386739159e2b29d868783baaa2b88 --- src/lib9/fmt/fmtfdflush.c +++ src/lib9/fmt/fmtfdflush.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" blob - 5c7afbc046990270833420f06f59fabebf22538a blob + cabe05f4a8b439238527e0ffb421a50c162ad7a1 --- src/lib9/fmt/fmtlock.c +++ src/lib9/fmt/fmtlock.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include "plan9.h" #include "fmt.h" blob - /dev/null blob + f354efc8aca02c7a1aa3485084792a801b8d1384 (mode 644) --- /dev/null +++ src/lib9/fmt/fmtlocale.c @@ -0,0 +1,56 @@ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ +#include +#include +#include "plan9.h" +#include "fmt.h" +#include "fmtdef.h" + +/* XXX GOOGLE COPYRIGHT */ + +/* + * Fill in the internationalization stuff in the State structure. + * For nil arguments, provide the sensible defaults: + * decimal is a period + * thousands separator is a comma + * thousands are marked every three digits + */ +void +fmtlocaleinit(Fmt *f, char *decimal, char *thousands, char *grouping) +{ + if(decimal == nil || decimal[0] == '\0') + decimal = "."; + if(thousands == nil) + thousands = ","; + if(grouping == nil) + grouping = "\3"; + f->decimal = decimal; + f->thousands = thousands; + f->grouping = grouping; +} + +/* + * We are about to emit a digit in e.g. %'d. If that digit would + * overflow a thousands (e.g.) grouping, tell the caller to emit + * the thousands separator. Always advance the digit counter + * and pointer into the grouping descriptor. + */ +int +__needsep(int *ndig, const char **grouping) +{ + int group; + + (*ndig)++; + group = *(unsigned char*)*grouping; + /* CHAR_MAX means no further grouping. \0 means we got the empty string */ + if(group == 0xFF || group == 0x7f || group == 0x00) + return 0; + if(*ndig > group){ + /* if we're at end of string, continue with this grouping; else advance */ + if((*grouping)[1] != '\0') + (*grouping)++; + *ndig = 1; + return 1; + } + return 0; +} + blob - 5ba59d343f1d907a0ea0a6429aa7c547a4dd7d6f blob + 868127e0a7c048bb6135449e9ef912075a61b683 --- src/lib9/fmt/fmtprint.c +++ src/lib9/fmt/fmtprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" blob - 2304c4e5445661c21d80d0e11d61498acbe61219 blob + 5b8f9fd4616c3de8dd4d49b66b666c175cab8de2 --- src/lib9/fmt/fmtquote.c +++ src/lib9/fmt/fmtquote.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" @@ -120,8 +108,10 @@ qstrfmt(char *sin, Rune *rin, Quoteinfo *q, Fmt *f) rm = rin; rme = rm + q->nrunesin; - w = f->width; fl = f->flags; + w = 0; + if(fl & FmtWidth) + w = f->width; if(f->runes){ if(!(fl & FmtLeft) && __rfmtpad(f, w - q->nrunesout) < 0) return -1; blob - b1ddd3b6ac0a1d038628701116a7700552bdfaf2 blob + fa4246874bd4b7968a65aeb8cc74a50d529c7c09 --- src/lib9/fmt/fmtrune.c +++ src/lib9/fmt/fmtrune.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" blob - a5f8f8d7d39ca49c51ce2eba1303de583b9c4a2b blob + 34d4a588b80b1bf0488fd23cd84658b3f5f867f5 --- src/lib9/fmt/fmtstr.c +++ src/lib9/fmt/fmtstr.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" @@ -23,5 +11,6 @@ fmtstrflush(Fmt *f) if(f->start == nil) return nil; *(char*)f->to = '\0'; + f->to = f->start; return (char*)f->start; } blob - cb7ffbe8caaf87716ae9ddcc1e0fa3106ee0d78e blob + 66d3929f005ad52580328112cfd6d470916dd4e7 --- src/lib9/fmt/fmtvprint.c +++ src/lib9/fmt/fmtvprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" blob - 043202c8db10556655266fcfbd3484e10c78ceca blob + 43b07af367ea25e6d03be5f1e449a76c7021632d --- src/lib9/fmt/fprint.c +++ src/lib9/fmt/fprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include "plan9.h" #include "fmt.h" blob - 5d578f92425650326ffb7bab8a315d2e13a36641 blob + 7e2cde2ac46468199d8aa95204193e6e2333534a --- src/lib9/fmt/pow10.c +++ src/lib9/fmt/pow10.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" blob - d038cee7270472a62c34db0fd3e1c0ad67ddb4cb blob + c0b083444fbb626df90d1b359c4f3a3625c4a4e3 --- src/lib9/fmt/print.c +++ src/lib9/fmt/print.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include "plan9.h" #include "fmt.h" blob - e17bc166b50e0969d5ccbf117a276f1aa227daa8 blob + 52ee96a0339ea87408583d392ab155a9a6721aec --- src/lib9/fmt/runefmtstr.c +++ src/lib9/fmt/runefmtstr.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" @@ -23,5 +11,6 @@ runefmtstrflush(Fmt *f) if(f->start == nil) return nil; *(Rune*)f->to = '\0'; + f->to = f->start; return f->start; } blob - 8e01c079312565d68afbea2e002ed7465d7f41c8 blob + ffc72cb9bb77e3414721a927fccef55566d6a600 --- src/lib9/fmt/runeseprint.c +++ src/lib9/fmt/runeseprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" blob - 03143a94ba4c15e8dad6112711fed7768a2e2902 blob + 5ddb2ba65e008e465ac65a100f981048c39dd302 --- src/lib9/fmt/runesmprint.c +++ src/lib9/fmt/runesmprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" blob - 2e6ad6c60c53173e85774c2dcc784e7122366cd8 blob + 50b4813d528b3d981c05bf48a3cdf53160edf3bc --- src/lib9/fmt/runesnprint.c +++ src/lib9/fmt/runesnprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" blob - 1dad4df7aafbbb2c320d5cc154a5fe18e360d325 blob + f7f4d29a3ffefb3142c0bb4a3c5455b5f1d6dc75 --- src/lib9/fmt/runesprint.c +++ src/lib9/fmt/runesprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" blob - 2359c817166dcccc94119cc1709e226c2f409662 blob + d789cbe7d62951a412c8397d779fab76313d79bb --- src/lib9/fmt/runevseprint.c +++ src/lib9/fmt/runevseprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" blob - f83dce299e9c6848e83b0424458034055ce4d08f blob + ef273752afa7917e44bed93bfe7f75ba2c52dc9b --- src/lib9/fmt/runevsmprint.c +++ src/lib9/fmt/runevsmprint.c @@ -1,17 +1,5 @@ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ /* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ -/* * Plan 9 port version must include libc.h in order to * get Plan 9 debugging malloc, which sometimes returns * different pointers than the standard malloc. @@ -69,6 +57,7 @@ runefmtstrinit(Fmt *f) f->flush = runeFmtStrFlush; f->farg = (void*)(uintptr)n; f->nfmt = 0; + fmtlocaleinit(f, nil, nil, nil); return 0; } blob - 431868a3ad305dbbfe14e787dd63e7a8921e5886 blob + f1e28e74aecacc9337aaef9448553c99e46de2a8 --- src/lib9/fmt/runevsnprint.c +++ src/lib9/fmt/runevsnprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" @@ -32,6 +20,7 @@ runevsnprint(Rune *buf, int len, char *fmt, va_list ar f.farg = nil; f.nfmt = 0; VA_COPY(f.args,args); + fmtlocaleinit(&f, nil, nil, nil); dofmt(&f, fmt); VA_END(f.args); *(Rune*)f.to = '\0'; blob - 38bdf5f5ae8b2943e9ed332e607fa0b8b1b78bda blob + 0729b4d7262fabdcfb556417cd782bff68f377b9 --- src/lib9/fmt/seprint.c +++ src/lib9/fmt/seprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include "plan9.h" #include "fmt.h" blob - f46c964dd70cc59f17b17203bc172c628da358b7 blob + 8640a5fd8e5eeabc6d784b370fe7bd9a077f0171 --- src/lib9/fmt/smprint.c +++ src/lib9/fmt/smprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include "plan9.h" #include "fmt.h" blob - 17ce0e2dbbcd25cb9b880b9ac9d2d14dc2454d43 blob + 64d2da433d26bbf85c001daee85cef8170f2cbdc --- src/lib9/fmt/snprint.c +++ src/lib9/fmt/snprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include "plan9.h" #include "fmt.h" blob - 1ab5ce628d3c7cb7b89b5e7ce6392faf1a334c13 blob + 9e3cb63ec060f5859dedb96bf4d89a5626e8e7fd --- src/lib9/fmt/sprint.c +++ src/lib9/fmt/sprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" blob - c19c2849843633da674904b721456ceee00e0c41 blob + 1dfd1b0778f2cbd79260fb39254c6c0313d1eaab --- src/lib9/fmt/strtod.c +++ src/lib9/fmt/strtod.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include @@ -239,7 +227,7 @@ fmtstrtod(const char *as, char **aas) /* close approx by naive conversion */ mid[0] = 0; mid[1] = 1; - for(i=0; c=a[i]; i++) { + for(i=0; (c=a[i]) != '\0'; i++) { mid[0] = mid[0]*10 + (c-'0'); mid[1] = mid[1]*10; if(i >= 8) @@ -521,7 +509,7 @@ xcmp(char *a, char *b) { int c1, c2; - while(c1 = *b++) { + while((c1 = *b++) != '\0') { c2 = *a++; if(isupper(c2)) c2 = tolower(c2); blob - 04296e219e6ec581280e7d9bfea303a22d23ec79 blob + 146692e50a8562fbfbaede597bd2f7ea40e67aad --- src/lib9/fmt/test.c +++ src/lib9/fmt/test.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include @@ -40,5 +28,24 @@ main(int argc, char *argv[]) print("%d\n", 23); print("%i\n", 23); print("%0.10d\n", 12345); + + /* test %4$d formats */ + print("%3$d %4$06d %2$d %1$d\n", 444, 333, 111, 222); + print("%3$d %4$06d %2$d %1$d\n", 444, 333, 111, 222); + print("%3$d %4$*5$06d %2$d %1$d\n", 444, 333, 111, 222, 20); + print("%3$hd %4$*5$06d %2$d %1$d\n", 444, 333, (short)111, 222, 20); + print("%3$lld %4$*5$06d %2$d %1$d\n", 444, 333, 111LL, 222, 20); + + /* test %'d formats */ + print("%'d %'d %'d\n", 1, 2222, 33333333); + print("%'019d\n", 0); + print("%08d %08d %08d\n", 1, 2222, 33333333); + print("%'08d %'08d %'08d\n", 1, 2222, 33333333); + print("%'x %'X %'b\n", 0x11111111, 0xabcd1234, 12345); + print("%'lld %'lld %'lld\n", 1LL, 222222222LL, 3333333333333LL); + print("%019lld %019lld %019lld\n", 1LL, 222222222LL, 3333333333333LL); + print("%'019lld %'019lld %'019lld\n", 1LL, 222222222LL, 3333333333333LL); + print("%'020lld %'020lld %'020lld\n", 1LL, 222222222LL, 3333333333333LL); + print("%'llx %'llX %'llb\n", 0x111111111111LL, 0xabcd12345678LL, 112342345LL); return 0; } blob - e5a7da3a4c80f8e542cfce6548d8bb85242c7f82 blob + a75e224fd2814fb81330787ec70d803640ae302b --- src/lib9/fmt/vfprint.c +++ src/lib9/fmt/vfprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include "plan9.h" #include "fmt.h" blob - b6dfee6380579b08738f744b777e701d019fdd63 blob + 0f7ba9dbfa027a3a56cb0bd456b4a442c6ad9b6e --- src/lib9/fmt/vseprint.c +++ src/lib9/fmt/vseprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include "plan9.h" #include "fmt.h" blob - 6f4937336313168ef7f1705a4da4e224b4d0a658 blob + 9576f8003037c2d9df319309926ccb5236c13491 --- src/lib9/fmt/vsmprint.c +++ src/lib9/fmt/vsmprint.c @@ -1,17 +1,5 @@ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ /* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ -/* * Plan 9 port version must include libc.h in order to * get Plan 9 debugging malloc, which sometimes returns * different pointers than the standard malloc. @@ -69,6 +57,7 @@ fmtstrinit(Fmt *f) f->flush = fmtStrFlush; f->farg = (void*)(uintptr)n; f->nfmt = 0; + fmtlocaleinit(f, nil, nil, nil); return 0; } blob - ed703250629496d85e2dcda54e5723c77be218ab blob + 1859265b5164fb2955040ece1ac13190a612f2ce --- src/lib9/fmt/vsnprint.c +++ src/lib9/fmt/vsnprint.c @@ -1,16 +1,4 @@ -/* - * The authors of this software are Rob Pike and Ken Thompson. - * Copyright (c) 2002 by Lucent Technologies. - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE - * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - */ +/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */ #include #include #include "plan9.h" @@ -32,6 +20,7 @@ vsnprint(char *buf, int len, char *fmt, va_list args) f.farg = nil; f.nfmt = 0; VA_COPY(f.args,args); + fmtlocaleinit(&f, nil, nil, nil); dofmt(&f, fmt); VA_END(f.args); *(char*)f.to = '\0'; blob - /dev/null blob + 540504225970a97a176f2d6fee511ebffe0f5fe7 (mode 644) --- /dev/null +++ src/lib9/testfltfmt.c @@ -0,0 +1,166 @@ +#include +#include +#include + +/* + * try all combination of flags and float conversions + * with some different widths & precisions + */ + +#define Njust 2 +#define Nplus 3 +#define Nalt 2 +#define Nzero 2 +#define Nspec 5 +#define Nwidth 5 +#define Nprec 5 + +static double fmtvals[] = { + 3.1415925535897932e15, + 3.1415925535897932e14, + 3.1415925535897932e13, + 3.1415925535897932e12, + 3.1415925535897932e11, + 3.1415925535897932e10, + 3.1415925535897932e9, + 3.1415925535897932e8, + 3.1415925535897932e7, + 3.1415925535897932e6, + 3.1415925535897932e5, + 3.1415925535897932e4, + 3.1415925535897932e3, + 3.1415925535897932e2, + 3.1415925535897932e1, + 3.1415925535897932e0, + 3.1415925535897932e-1, + 3.1415925535897932e-2, + 3.1415925535897932e-3, + 3.1415925535897932e-4, + 3.1415925535897932e-5, + 3.1415925535897932e-6, + 3.1415925535897932e-7, + 3.1415925535897932e-8, + 3.1415925535897932e-9, + 3.1415925535897932e-10, + 3.1415925535897932e-11, + 3.1415925535897932e-12, + 3.1415925535897932e-13, + 3.1415925535897932e-14, + 3.1415925535897932e-15, +}; + +/* + * are the numbers close? + * used to compare long numbers where the last few digits are garbage + * due to precision problems + */ +static int +numclose(char *num1, char *num2) +{ + int ndig; + enum { MAXDIG = 15 }; + + ndig = 0; + while (*num1) { + if (*num1 >= '0' && *num1 <= '9') { + ndig++; + if (ndig > MAXDIG) { + if (!(*num2 >= '0' && *num2 <= '9')) { + return 0; + } + } else if (*num1 != *num2) { + return 0; + } + } else if (*num1 != *num2) { + return 0; + } else if (*num1 == 'e' || *num1 == 'E') { + ndig = 0; + } + num1++; + num2++; + } + if (*num1 || !num2) + return 0; + return 1; +} + +static void +doit(int just, int plus, int alt, int zero, int width, int prec, int spec) +{ + char format[256]; + char *p; + const char *s; + int i; + + p = format; + *p++ = '%'; + if (just > 0) + *p++ = "-"[just - 1]; + if (plus > 0) + *p++ = "+ "[plus - 1]; + if (alt > 0) + *p++ = "#"[alt - 1]; + if (zero > 0) + *p++ = "0"[zero - 1]; + + s = ""; + switch (width) { + case 1: s = "1"; break; + case 2: s = "5"; break; + case 3: s = "10"; break; + case 4: s = "15"; break; + } + strcpy(p, s); + + s = ""; + switch (prec) { + case 1: s = ".0"; break; + case 2: s = ".2"; break; + case 3: s = ".5"; break; + case 4: s = ".15"; break; + } + strcat(p, s); + + p = strchr(p, '\0'); + *p++ = "efgEG"[spec]; + *p = '\0'; + + for (i = 0; i < sizeof(fmtvals) / sizeof(fmtvals[0]); i++) { + char ref[256], buf[256]; + Rune rbuf[256]; + + sprintf(ref, format, fmtvals[i]); + snprint(buf, sizeof(buf), format, fmtvals[i]); + if (strcmp(ref, buf) != 0 + && !numclose(ref, buf)) { + fprintf(stderr, "%s: ref='%s' fmt='%s'\n", format, ref, buf); + exit(1); + } + + /* Check again with output to rune string */ + runesnprint(rbuf, 256, format, fmtvals[i]); + snprint(buf, sizeof(buf), "%S", rbuf); + if (strcmp(ref, buf) != 0 + && !numclose(ref, buf)) { + fprintf(stderr, "%s: rune ref='%s' fmt='%s'\n", format, ref, buf); + exits("oops"); + } + } +} + +void +main(int argc, char **argv) +{ + int just, plus, alt, zero, width, prec, spec; + + for (just = 0; just < Njust; just++) + for (plus = 0; plus < Nplus; plus++) + for (alt = 0; alt < Nalt; alt++) + for (zero = 0; zero < Nzero; zero++) + for (width = 0; width < Nwidth; width++) + for (prec = 0; prec < Nprec; prec++) + for (spec = 0; spec < Nspec; spec++) + doit(just, plus, alt, zero, width, prec, spec); + + exits(0); +} blob - /dev/null blob + c1a91400ea705d038deb95c6590ec75b3875be0c (mode 644) --- /dev/null +++ src/lib9/testfmt.c @@ -0,0 +1,114 @@ +#include +#include +#include + +int failed; + +/* Consume argument and ignore it */ +int +Zflag(Fmt* f) +{ + if(va_arg(f->args, int)) + ; + return 1; /* it's a flag */ +} + +void +verify(char *s, char *t) +{ + if(strcmp(s, t) != 0){ + failed = 1; + fprintf(stderr, "error: (%s) != (%s)\n", s, t); + } + free(s); +} + +Rune lightsmiley = 0x263a; +Rune darksmiley = 0x263b; + +/* Test printer that loads unusual decimal point and separator */ +char* +mysmprint(char *fmt, ...) +{ + Fmt f; + + if(fmtstrinit(&f) < 0) + return 0; + va_start(f.args, fmt); + f.decimal = smprint("%C", lightsmiley); + f.thousands = smprint("%C", darksmiley); + f.grouping = "\1\2\3\4"; + if(dofmt(&f, fmt) < 0) + return 0; + va_end(f.args); + return fmtstrflush(&f); +} + + +void +main(int argc, char **argv) +{ + quotefmtinstall(); + fmtinstall('Z', Zflag); + fmtinstall(L'\x263a', Zflag); + + verify(smprint("hello world"), "hello world"); +#ifdef PLAN9PORT + verify(smprint("x: %ux", 0x87654321), "x: 87654321"); +#else + verify(smprint("x: %x", 0x87654321), "x: 87654321"); +#endif + verify(smprint("d: %d", 0x87654321), "d: -2023406815"); + verify(smprint("s: %s", "hi there"), "s: hi there"); + verify(smprint("q: %q", "hi i'm here"), "q: 'hi i''m here'"); + verify(smprint("c: %c", '!'), "c: !"); + verify(smprint("g: %g %g %g", 3.14159, 3.14159e10, 3.14159e-10), "g: 3.14159 3.14159e+10 3.14159e-10"); + verify(smprint("e: %e %e %e", 3.14159, 3.14159e10, 3.14159e-10), "e: 3.141590e+00 3.141590e+10 3.141590e-10"); + verify(smprint("f: %f %f %f", 3.14159, 3.14159e10, 3.14159e-10), "f: 3.141590 31415900000.000000 0.000000"); + verify(smprint("smiley: %C", (Rune)0x263a), "smiley: \xe2\x98\xba"); + verify(smprint("%g %.18g", 2e25, 2e25), "2e+25 2e+25"); + verify(smprint("%2.18g", 1.0), " 1"); + verify(smprint("%f", 3.1415927/4), "0.785398"); + verify(smprint("%d", 23), "23"); + verify(smprint("%i", 23), "23"); + verify(smprint("%Zi", 1234, 23), "23"); + + /* test $ reorderings */ + verify(smprint("%3$d %4$06d %2$d %1$d", 444, 333, 111, 222), "111 000222 333 444"); + verify(smprint("%3$Zd %5$06d %2$d %1$d", 444, 333, 555, 111, 222), "111 000222 333 444"); + verify(smprint("%3$d %4$*5$06d %2$d %1$d", 444, 333, 111, 222, 20), "111 000222 333 444"); + verify(smprint("%3$hd %4$*5$06d %2$d %1$d", 444, 333, (short)111, 222, 20), "111 000222 333 444"); + verify(smprint("%3$\xe2\x98\xba""d %5$06d %2$d %1$d", 444, 333, 555, 111, 222), "111 000222 333 444"); + + /* test %'d formats */ + verify(smprint("%'d %'d %'d", 1, 2222, 33333333), "1 2,222 33,333,333"); + verify(smprint("%'019d", 0), "000,000,000,000,000"); + verify(smprint("%'08d %'08d %'08d", 1, 2222, 33333333), "0,000,001 0,002,222 33,333,333"); +#ifdef PLAN9PORT + verify(smprint("%'ux %'uX %'ub", 0x11111111, 0xabcd1234, 12345), "1111:1111 ABCD:1234 11:0000:0011:1001"); +#else + verify(smprint("%'x %'X %'b", 0x11111111, 0xabcd1234, 12345), "1111:1111 ABCD:1234 11:0000:0011:1001"); +#endif + verify(smprint("%'lld %'lld %'lld", 1LL, 222222222LL, 3333333333333LL), "1 222,222,222 3,333,333,333,333"); + verify(smprint("%'019lld %'019lld %'019lld", 1LL, 222222222LL, 3333333333333LL), "000,000,000,000,001 000,000,222,222,222 003,333,333,333,333"); +#ifdef PLAN9PORT + verify(smprint("%'llux %'lluX %'llub", 0x111111111111LL, 0xabcd12345678LL, 112342345LL), "1111:1111:1111 ABCD:1234:5678 110:1011:0010:0011:0101:0100:1001"); +#else + verify(smprint("%'llx %'llX %'llb", 0x111111111111LL, 0xabcd12345678LL, 112342345LL), "1111:1111:1111 ABCD:1234:5678 110:1011:0010:0011:0101:0100:1001"); +#endif + + /* test %'d with custom (utf-8!) separators */ + /* x and b still use : */ + verify(mysmprint("%'d %'d %'d", 1, 2222, 33333333), "1 2\xe2\x98\xbb""22\xe2\x98\xbb""2 33\xe2\x98\xbb""333\xe2\x98\xbb""33\xe2\x98\xbb""3"); +#ifdef PLAN9PORT + verify(mysmprint("%'ux %'uX %'ub", 0x11111111, 0xabcd1234, 12345), "1111:1111 ABCD:1234 11:0000:0011:1001"); +#else + verify(mysmprint("%'x %'X %'b", 0x11111111, 0xabcd1234, 12345), "1111:1111 ABCD:1234 11:0000:0011:1001"); +#endif + verify(mysmprint("%'lld %'lld %'lld", 1LL, 222222222LL, 3333333333333LL), "1 222\xe2\x98\xbb""222\xe2\x98\xbb""22\xe2\x98\xbb""2 333\xe2\x98\xbb""3333\xe2\x98\xbb""333\xe2\x98\xbb""33\xe2\x98\xbb""3"); + verify(mysmprint("%'llx %'llX %'llb", 0x111111111111LL, 0xabcd12345678LL, 112342345LL), "1111:1111:1111 ABCD:1234:5678 110:1011:0010:0011:0101:0100:1001"); + + if(failed) + sysfatal("tests failed"); + exits(0); +}