Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-119182: Optimize PyUnicode_FromFormat() #120796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Run ucs1lib_find_max_char() only once
  • Loading branch information
vstinner committed Jun 20, 2024
commit 57d71526a1c9c0ce5d6c5139b042be09cbe69156
32 changes: 15 additions & 17 deletions 32 Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2875,24 +2875,33 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
static int
unicode_from_format(_PyUnicodeWriter *writer, const char *format, va_list vargs)
{
writer->min_length += strlen(format) + 100;
Py_ssize_t len = strlen(format);
writer->min_length += len + 100;
writer->overallocate = 1;

va_list vargs2;
const char *f;

// Copy varags to be able to pass a reference to a subfunction.
va_list vargs2;
va_copy(vargs2, vargs);

for (f = format; *f; ) {
int is_ascii = (ucs1lib_find_max_char((Py_UCS1*)format, (Py_UCS1*)format + len) < 128);
vstinner marked this conversation as resolved.
Show resolved Hide resolved
if (!is_ascii) {
Py_ssize_t i;
for (i=0; i < len && (unsigned char)format[i] <= 127; i++);
PyErr_Format(PyExc_ValueError,
"PyUnicode_FromFormatV() expects an ASCII-encoded format "
"string, got a non-ASCII byte: 0x%02x",
(unsigned char)format[i]);
goto fail;
}

for (const char *f = format; *f; ) {
if (*f == '%') {
f = unicode_fromformat_arg(writer, f, &vargs2);
if (f == NULL)
goto fail;
}
else {
const char *p = strchr(f, '%');
Py_ssize_t len;
if (p != NULL) {
len = p - f;
}
Expand All @@ -2901,17 +2910,6 @@ unicode_from_format(_PyUnicodeWriter *writer, const char *format, va_list vargs)
writer->overallocate = 0;
}

int is_ascii = (ucs1lib_find_max_char((Py_UCS1*)f, (Py_UCS1*)f + len) < 128);
if (!is_ascii) {
Py_ssize_t i;
for (i=0; i < len && (unsigned char)f[i] <= 127; i++);
PyErr_Format(PyExc_ValueError,
"PyUnicode_FromFormatV() expects an ASCII-encoded format "
"string, got a non-ASCII byte: 0x%02x",
(unsigned char)f[i]);
goto fail;
}

if (_PyUnicodeWriter_WriteASCIIString(writer, f, len) < 0) {
goto fail;
}
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.