diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-04-02 23:48:39 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-04-02 23:48:39 (GMT) |
commit | cfc4c13b04223705a43595579b46020c9e876ac4 (patch) | |
tree | 238b3c5e9e2ac723e86fa2b3eb711cd5eb45eebf /Objects/stringlib | |
parent | 4489e927a6e030f19fee77783ebb209119f4ad60 (diff) | |
download | cpython-cfc4c13b04223705a43595579b46020c9e876ac4.zip cpython-cfc4c13b04223705a43595579b46020c9e876ac4.tar.gz cpython-cfc4c13b04223705a43595579b46020c9e876ac4.tar.bz2 |
Add _PyUnicodeWriter_WriteSubstring() function
Write a function to enable more optimizations:
* If the substring is the whole string and overallocation is disabled, just
keep a reference to the string, don't copy characters
* Avoid a call to the expensive _PyUnicode_FindMaxChar() function when
possible
Diffstat (limited to 'Objects/stringlib')
-rw-r--r-- | Objects/stringlib/unicode_format.h | 18 |
1 files changed, 6 insertions, 12 deletions
diff --git a/Objects/stringlib/unicode_format.h b/Objects/stringlib/unicode_format.h index e9be516..2f58946 100644 --- a/Objects/stringlib/unicode_format.h +++ b/Objects/stringlib/unicode_format.h @@ -869,25 +869,19 @@ do_markup(SubString *input, PyObject *args, PyObject *kwargs, SubString literal; SubString field_name; SubString format_spec; - Py_UCS4 conversion, maxchar; - Py_ssize_t sublen; - int err; + Py_UCS4 conversion; MarkupIterator_init(&iter, input->str, input->start, input->end); while ((result = MarkupIterator_next(&iter, &literal, &field_present, &field_name, &format_spec, &conversion, &format_spec_needs_expanding)) == 2) { - sublen = literal.end - literal.start; - if (sublen) { - maxchar = _PyUnicode_FindMaxChar(literal.str, - literal.start, literal.end); - err = _PyUnicodeWriter_Prepare(writer, sublen, maxchar); - if (err == -1) + if (literal.end != literal.start) { + if (!field_present && iter.str.start == iter.str.end) + writer->overallocate = 0; + if (_PyUnicodeWriter_WriteSubstring(writer, literal.str, + literal.start, literal.end) < 0) return 0; - _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, - literal.str, literal.start, sublen); - writer->pos += sublen; } if (field_present) { |