static char*
formatfloat(PyObject *v, int flags, int prec, int type,
- PyObject **p_result, _PyBytesWriter *writer, char *str,
- Py_ssize_t prealloc)
+ PyObject **p_result, _PyBytesWriter *writer, char *str)
{
char *p;
PyObject *result;
len = strlen(p);
if (writer != NULL) {
- if ((Py_ssize_t)len > prealloc) {
- str = _PyBytesWriter_Prepare(writer, str, len - prealloc);
- if (str == NULL)
- return NULL;
- }
+ str = _PyBytesWriter_Prepare(writer, str, len);
+ if (str == NULL)
+ return NULL;
Py_MEMCPY(str, p, len);
str += len;
return str;
&& !(flags & (F_SIGN | F_BLANK)))
{
/* Fast path */
- res = formatfloat(v, flags, prec, c, NULL, &writer, res, 1);
+ writer.min_size -= 2; /* size preallocated by "%f" */
+ res = formatfloat(v, flags, prec, c, NULL, &writer, res);
if (res == NULL)
goto error;
continue;
}
- if (!formatfloat(v, flags, prec, c, &temp, NULL, res, 1))
+ if (!formatfloat(v, flags, prec, c, &temp, NULL, res))
goto error;
pbuf = PyBytes_AS_STRING(temp);
len = PyBytes_GET_SIZE(temp);
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;
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;
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);
/* Implementation of the "backslashreplace" error handler for 8-bit encodings:
ASCII, Latin1, UTF-8, etc. */
static char*
-backslashreplace(_PyBytesWriter *writer, Py_ssize_t prealloc_per_char,
- char *str,
+backslashreplace(_PyBytesWriter *writer, char *str,
PyObject *unicode, Py_ssize_t collstart, Py_ssize_t collend)
{
- Py_ssize_t size, i, prealloc;
+ Py_ssize_t size, i;
Py_UCS4 ch;
enum PyUnicode_Kind kind;
void *data;
size += incr;
}
- prealloc = prealloc_per_char * (collend - collstart);
- if (size > prealloc) {
- str = _PyBytesWriter_Prepare(writer, str, size - prealloc);
- if (str == NULL)
- return NULL;
- }
+ str = _PyBytesWriter_Prepare(writer, str, size);
+ if (str == NULL)
+ return NULL;
/* generate replacement */
for (i = collstart; i < collend; ++i) {
/* Implementation of the "xmlcharrefreplace" error handler for 8-bit encodings:
ASCII, Latin1, UTF-8, etc. */
static char*
-xmlcharrefreplace(_PyBytesWriter *writer, Py_ssize_t prealloc_per_char,
- char *str,
+xmlcharrefreplace(_PyBytesWriter *writer, char *str,
PyObject *unicode, Py_ssize_t collstart, Py_ssize_t collend)
{
- Py_ssize_t size, i, prealloc;
+ Py_ssize_t size, i;
Py_UCS4 ch;
enum PyUnicode_Kind kind;
void *data;
size += incr;
}
- prealloc = prealloc_per_char * (collend - collstart);
- if (size > prealloc) {
- str = _PyBytesWriter_Prepare(writer, str, size - prealloc);
- if (str == NULL)
- return NULL;
- }
+ str = _PyBytesWriter_Prepare(writer, str, size);
+ if (str == NULL)
+ return NULL;
/* generate replacement */
for (i = collstart; i < collend; ++i) {
break;
case _Py_ERROR_BACKSLASHREPLACE:
- str = backslashreplace(&writer, 1, str,
+ /* substract preallocated bytes */
+ writer.min_size -= (collend - collstart);
+ str = backslashreplace(&writer, str,
unicode, collstart, collend);
if (str == NULL)
goto onError;
break;
case _Py_ERROR_XMLCHARREFREPLACE:
- str = xmlcharrefreplace(&writer, 1, str,
+ /* substract preallocated bytes */
+ writer.min_size -= (collend - collstart);
+ str = xmlcharrefreplace(&writer, str,
unicode, collstart, collend);
if (str == NULL)
goto onError;
PyUnicode_READY(repunicode) == -1))
goto onError;
+ /* substract preallocated bytes */
+ writer.min_size -= 1;
+
if (PyBytes_Check(repunicode)) {
/* Directly copy bytes result to output. */
repsize = PyBytes_Size(repunicode);
- if (repsize > 1) {
- str = _PyBytesWriter_Prepare(&writer, str, repsize-1);
- if (str == NULL)
- goto onError;
- }
+
+ str = _PyBytesWriter_Prepare(&writer, str, repsize);
+ if (str == NULL)
+ goto onError;
+
memcpy(str, PyBytes_AsString(repunicode), repsize);
str += repsize;
pos = newpos;
have+the replacement+the rest of the string, so
we won't have to check space for encodable characters) */
repsize = PyUnicode_GET_LENGTH(repunicode);
- if (repsize > 1) {
- str = _PyBytesWriter_Prepare(&writer, str, repsize-1);
- if (str == NULL)
- goto onError;
- }
+
+ str = _PyBytesWriter_Prepare(&writer, str, repsize);
+ if (str == NULL)
+ goto onError;
/* check if there is anything unencodable in the replacement
and copy it to the output */