diff options
author | Tim Peters <tim.peters@gmail.com> | 2006-05-13 23:28:20 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2006-05-13 23:28:20 (GMT) |
commit | 8931ff1f670c3588f3cb60d2f56a1e9cae964b40 (patch) | |
tree | a71837228bcf79f13f6a2752b3e6edde560a9015 /Objects/stringobject.c | |
parent | 822f34a848de4d19f9d7cca5bb99424e5dd34303 (diff) | |
download | cpython-8931ff1f670c3588f3cb60d2f56a1e9cae964b40.zip cpython-8931ff1f670c3588f3cb60d2f56a1e9cae964b40.tar.gz cpython-8931ff1f670c3588f3cb60d2f56a1e9cae964b40.tar.bz2 |
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
about "%u", "%lu" and "%zu" formats.
Since PyString_FromFormat and PyErr_Format have exactly the same rules
(both inherited from PyString_FromFormatV), it would be good if someone
with more LaTeX Fu changed one of them to just point to the other.
Their docs were way out of synch before this patch, and I just did a
mass copy+paste to repair that.
Not a backport candidate (this is a new feature).
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r-- | Objects/stringobject.c | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 01a4bb4..536caef 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -176,14 +176,11 @@ PyString_FromFormatV(const char *format, va_list vargs) while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f))) ; - /* skip the 'l' in %ld, since it doesn't change the - width. although only %d is supported (see - "expand" section below), others can be easily - added */ - if (*f == 'l' && *(f+1) == 'd') - ++f; - /* likewise for %zd */ - if (*f == 'z' && *(f+1) == 'd') + /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since + * they don't affect the amount of space we reserve. + */ + if ((*f == 'l' || *f == 'z') && + (f[1] == 'd' || f[1] == 'u')) ++f; switch (*f) { @@ -193,7 +190,7 @@ PyString_FromFormatV(const char *format, va_list vargs) case '%': n++; break; - case 'd': case 'i': case 'x': + case 'd': case 'u': case 'i': case 'x': (void) va_arg(count, int); /* 20 bytes is enough to hold a 64-bit integer. Decimal takes the most space. @@ -255,14 +252,14 @@ PyString_FromFormatV(const char *format, va_list vargs) } while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f))) f++; - /* handle the long flag, but only for %ld. others - can be added when necessary. */ - if (*f == 'l' && *(f+1) == 'd') { + /* handle the long flag, but only for %ld and %lu. + others can be added when necessary. */ + if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { longflag = 1; ++f; } /* handle the size_t flag. */ - if (*f == 'z' && *(f+1) == 'd') { + if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { size_tflag = 1; ++f; } @@ -281,6 +278,18 @@ PyString_FromFormatV(const char *format, va_list vargs) sprintf(s, "%d", va_arg(vargs, int)); s += strlen(s); break; + case 'u': + if (longflag) + sprintf(s, "%lu", + va_arg(vargs, unsigned long)); + else if (size_tflag) + sprintf(s, "%" PY_FORMAT_SIZE_T "u", + va_arg(vargs, size_t)); + else + sprintf(s, "%u", + va_arg(vargs, unsigned int)); + s += strlen(s); + break; case 'i': sprintf(s, "%i", va_arg(vargs, int)); s += strlen(s); |