mirror of
https://github.com/python/cpython.git
synced 2025-07-15 23:35:23 +00:00
Create PyUnicode_strdup() function
This commit is contained in:
parent
c4eb765fc1
commit
71133ff368
2 changed files with 32 additions and 1 deletions
|
@ -10014,6 +10014,28 @@ Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
Py_UNICODE*
|
||||
PyUnicode_strdup(PyObject *object)
|
||||
{
|
||||
PyUnicodeObject *unicode = (PyUnicodeObject *)object;
|
||||
Py_UNICODE *copy;
|
||||
Py_ssize_t size;
|
||||
|
||||
/* Ensure we won't overflow the size. */
|
||||
if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) {
|
||||
PyErr_NoMemory();
|
||||
return NULL;
|
||||
}
|
||||
size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */
|
||||
size *= sizeof(Py_UNICODE);
|
||||
copy = PyMem_Malloc(size);
|
||||
if (copy == NULL) {
|
||||
PyErr_NoMemory();
|
||||
return NULL;
|
||||
}
|
||||
memcpy(copy, PyUnicode_AS_UNICODE(unicode), size);
|
||||
return copy;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue