From 9b7dd8a6114201295c5dce3a6cd3d48550a88056 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 2 Oct 2012 00:33:47 +0200 Subject: Issue #15609: Optimize str%args for integer argument - Use _PyLong_FormatWriter() instead of formatlong() when possible, to avoid a temporary buffer - Enable the fast path when width is smaller or equals to the length, and when the precision is bigger or equals to the length - Add unit tests! - formatlong() uses PyUnicode_Resize() instead of _PyUnicode_FromASCII() to resize the output string --- Python/formatter_unicode.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Python/formatter_unicode.c') diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c index aa62502dbe..0ce9862a20 100644 --- a/Python/formatter_unicode.c +++ b/Python/formatter_unicode.c @@ -757,7 +757,8 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format, goto done; } - if (format->width == -1 && format->precision == -1) { + if ((format->width == -1 || format->width <= len) + && (format->precision == -1 || format->precision >= len)) { /* Fast path */ return _PyUnicodeWriter_WriteStr(writer, value); } -- cgit v1.2.1 From 4a7596083685d526076d006536188229dd1545d1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 3 Apr 2013 02:02:33 +0200 Subject: Close #16757: Avoid calling the expensive _PyUnicode_FindMaxChar() function when possible --- Python/formatter_unicode.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'Python/formatter_unicode.c') diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c index 79fa469697..009bc5fd03 100644 --- a/Python/formatter_unicode.c +++ b/Python/formatter_unicode.c @@ -771,9 +771,13 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format, calc_padding(len, format->width, format->align, &lpad, &rpad, &total); - maxchar = _PyUnicode_FindMaxChar(value, 0, len); + maxchar = writer->maxchar; if (lpad != 0 || rpad != 0) maxchar = Py_MAX(maxchar, format->fill_char); + if (PyUnicode_MAX_CHAR_VALUE(value) > maxchar) { + Py_UCS4 valmaxchar = _PyUnicode_FindMaxChar(value, 0, len); + maxchar = Py_MAX(maxchar, valmaxchar); + } /* allocate the resulting string */ if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1) -- cgit v1.2.1 From 157b5b3790c1d53767468c915a12abdfcde059e3 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 7 May 2013 23:50:03 +0200 Subject: Fix a compiler warning: use unsigned int type instead of enum PyUnicode_Kind to compare two Unicode kinds --- Python/formatter_unicode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/formatter_unicode.c') diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c index 009bc5fd03..548b49aa3e 100644 --- a/Python/formatter_unicode.c +++ b/Python/formatter_unicode.c @@ -556,7 +556,7 @@ fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec, { /* Used to keep track of digits, decimal, and remainder. */ Py_ssize_t d_pos = d_start; - const enum PyUnicode_Kind kind = writer->kind; + const unsigned int kind = writer->kind; const void *data = writer->data; Py_ssize_t r; -- cgit v1.2.1