diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-10-09 10:38:53 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-10-09 10:38:53 (GMT) |
commit | ad7715891e3c6c51b4860e0d496843ee5417206b (patch) | |
tree | a3680f453a11828c92fa879095e5198b52764920 /Objects/stringlib | |
parent | 53926a1ce2772ea5dc1c4fb8c19f5f9ae461750d (diff) | |
download | cpython-ad7715891e3c6c51b4860e0d496843ee5417206b.zip cpython-ad7715891e3c6c51b4860e0d496843ee5417206b.tar.gz cpython-ad7715891e3c6c51b4860e0d496843ee5417206b.tar.bz2 |
_PyBytesWriter: simplify code to avoid "prealloc" parameters
Substract preallocate bytes from min_size before calling
_PyBytesWriter_Prepare().
Diffstat (limited to 'Objects/stringlib')
-rw-r--r-- | Objects/stringlib/codecs.h | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/Objects/stringlib/codecs.h b/Objects/stringlib/codecs.h index ae99d1a..6842f67 100644 --- a/Objects/stringlib/codecs.h +++ b/Objects/stringlib/codecs.h @@ -345,7 +345,9 @@ STRINGLIB(utf8_encoder)(PyObject *unicode, break; case _Py_ERROR_BACKSLASHREPLACE: - p = backslashreplace(&writer, max_char_size, p, + /* substract preallocated bytes */ + writer.min_size -= max_char_size * (endpos - startpos); + p = backslashreplace(&writer, p, unicode, startpos, endpos); if (p == NULL) goto error; @@ -353,7 +355,9 @@ STRINGLIB(utf8_encoder)(PyObject *unicode, break; case _Py_ERROR_XMLCHARREFREPLACE: - p = xmlcharrefreplace(&writer, max_char_size, p, + /* substract preallocated bytes */ + writer.min_size -= max_char_size * (endpos - startpos); + p = xmlcharrefreplace(&writer, p, unicode, startpos, endpos); if (p == NULL) goto error; @@ -381,17 +385,17 @@ STRINGLIB(utf8_encoder)(PyObject *unicode, if (!rep) goto error; + /* substract preallocated bytes */ + writer.min_size -= max_char_size; + if (PyBytes_Check(rep)) repsize = PyBytes_GET_SIZE(rep); else repsize = PyUnicode_GET_LENGTH(rep); - if (repsize > max_char_size) { - p = _PyBytesWriter_Prepare(&writer, p, - repsize - max_char_size); - if (p == NULL) - goto error; - } + p = _PyBytesWriter_Prepare(&writer, p, repsize); + if (p == NULL) + goto error; if (PyBytes_Check(rep)) { memcpy(p, PyBytes_AS_STRING(rep), repsize); |