mirror of
https://github.com/python/cpython.git
synced 2025-11-02 14:41:33 +00:00
gh-139156: Use PyBytesWriter in the UTF-7 encoder (#139248)
Replace PyBytes_FromStringAndSize() and _PyBytes_Resize() with the PyBytesWriter API.
This commit is contained in:
parent
92ba2c92c4
commit
c863349f98
1 changed files with 16 additions and 24 deletions
|
|
@ -4897,33 +4897,27 @@ _PyUnicode_EncodeUTF7(PyObject *str,
|
||||||
int base64WhiteSpace,
|
int base64WhiteSpace,
|
||||||
const char *errors)
|
const char *errors)
|
||||||
{
|
{
|
||||||
int kind;
|
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
|
||||||
const void *data;
|
if (len == 0) {
|
||||||
Py_ssize_t len;
|
|
||||||
PyObject *v;
|
|
||||||
int inShift = 0;
|
|
||||||
Py_ssize_t i;
|
|
||||||
unsigned int base64bits = 0;
|
|
||||||
unsigned long base64buffer = 0;
|
|
||||||
char * out;
|
|
||||||
const char * start;
|
|
||||||
|
|
||||||
kind = PyUnicode_KIND(str);
|
|
||||||
data = PyUnicode_DATA(str);
|
|
||||||
len = PyUnicode_GET_LENGTH(str);
|
|
||||||
|
|
||||||
if (len == 0)
|
|
||||||
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
|
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
|
||||||
|
}
|
||||||
|
int kind = PyUnicode_KIND(str);
|
||||||
|
const void *data = PyUnicode_DATA(str);
|
||||||
|
|
||||||
/* It might be possible to tighten this worst case */
|
/* It might be possible to tighten this worst case */
|
||||||
if (len > PY_SSIZE_T_MAX / 8)
|
if (len > PY_SSIZE_T_MAX / 8) {
|
||||||
return PyErr_NoMemory();
|
return PyErr_NoMemory();
|
||||||
v = PyBytes_FromStringAndSize(NULL, len * 8);
|
}
|
||||||
if (v == NULL)
|
PyBytesWriter *writer = PyBytesWriter_Create(len * 8);
|
||||||
|
if (writer == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
start = out = PyBytes_AS_STRING(v);
|
int inShift = 0;
|
||||||
for (i = 0; i < len; ++i) {
|
unsigned int base64bits = 0;
|
||||||
|
unsigned long base64buffer = 0;
|
||||||
|
char *out = PyBytesWriter_GetData(writer);
|
||||||
|
for (Py_ssize_t i = 0; i < len; ++i) {
|
||||||
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
|
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
|
||||||
|
|
||||||
if (inShift) {
|
if (inShift) {
|
||||||
|
|
@ -4986,9 +4980,7 @@ _PyUnicode_EncodeUTF7(PyObject *str,
|
||||||
*out++= TO_BASE64(base64buffer << (6-base64bits) );
|
*out++= TO_BASE64(base64buffer << (6-base64bits) );
|
||||||
if (inShift)
|
if (inShift)
|
||||||
*out++ = '-';
|
*out++ = '-';
|
||||||
if (_PyBytes_Resize(&v, out - start) < 0)
|
return PyBytesWriter_FinishWithPointer(writer, out);
|
||||||
return NULL;
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef IS_BASE64
|
#undef IS_BASE64
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue