diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-04-03 00:02:33 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-04-03 00:02:33 (GMT) |
commit | eb4b5ac8afd797622e769e5742844224cfcb73b2 (patch) | |
tree | 8285d7e7aa0382679624bbe5833967af1655c306 /Objects/unicodeobject.c | |
parent | cfc4c13b04223705a43595579b46020c9e876ac4 (diff) | |
download | cpython-eb4b5ac8afd797622e769e5742844224cfcb73b2.zip cpython-eb4b5ac8afd797622e769e5742844224cfcb73b2.tar.gz cpython-eb4b5ac8afd797622e769e5742844224cfcb73b2.tar.bz2 |
Close #16757: Avoid calling the expensive _PyUnicode_FindMaxChar() function
when possible
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index a926e37..dee2953 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -13777,7 +13777,7 @@ unicode_format_arg_output(struct unicode_formatter_t *ctx, Py_ssize_t pindex; Py_UCS4 signchar; Py_ssize_t buflen; - Py_UCS4 maxchar, bufmaxchar; + Py_UCS4 maxchar; Py_ssize_t sublen; _PyUnicodeWriter *writer = &ctx->writer; Py_UCS4 fill; @@ -13830,23 +13830,26 @@ unicode_format_arg_output(struct unicode_formatter_t *ctx, arg->width = len; /* Prepare the writer */ - bufmaxchar = 127; + maxchar = writer->maxchar; if (!(arg->flags & F_LJUST)) { if (arg->sign) { if ((arg->width-1) > len) - bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); + maxchar = MAX_MAXCHAR(maxchar, fill); } else { if (arg->width > len) - bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); + maxchar = MAX_MAXCHAR(maxchar, fill); } } - maxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); - bufmaxchar = MAX_MAXCHAR(bufmaxchar, maxchar); + if (PyUnicode_MAX_CHAR_VALUE(str) > maxchar) { + Py_UCS4 strmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); + maxchar = MAX_MAXCHAR(maxchar, strmaxchar); + } + buflen = arg->width; if (arg->sign && len == arg->width) buflen++; - if (_PyUnicodeWriter_Prepare(writer, buflen, bufmaxchar) == -1) + if (_PyUnicodeWriter_Prepare(writer, buflen, maxchar) == -1) return -1; /* Write the sign if needed */ |