mirror of
https://github.com/python/cpython.git
synced 2025-08-28 20:56:54 +00:00
Issue #9979: Create function PyUnicode_AsWideCharString().
This commit is contained in:
parent
41a64a53a3
commit
137c34c027
4 changed files with 80 additions and 16 deletions
|
@ -1153,9 +1153,26 @@ PyUnicode_FromFormat(const char *format, ...)
|
|||
return ret;
|
||||
}
|
||||
|
||||
Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode,
|
||||
wchar_t *w,
|
||||
Py_ssize_t size)
|
||||
static void
|
||||
unicode_aswidechar(PyUnicodeObject *unicode,
|
||||
wchar_t *w,
|
||||
Py_ssize_t size)
|
||||
{
|
||||
#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T
|
||||
memcpy(w, unicode->str, size * sizeof(wchar_t));
|
||||
#else
|
||||
register Py_UNICODE *u;
|
||||
register Py_ssize_t i;
|
||||
u = PyUnicode_AS_UNICODE(unicode);
|
||||
for (i = size; i > 0; i--)
|
||||
*w++ = *u++;
|
||||
#endif
|
||||
}
|
||||
|
||||
Py_ssize_t
|
||||
PyUnicode_AsWideChar(PyUnicodeObject *unicode,
|
||||
wchar_t *w,
|
||||
Py_ssize_t size)
|
||||
{
|
||||
if (unicode == NULL) {
|
||||
PyErr_BadInternalCall();
|
||||
|
@ -1166,17 +1183,7 @@ Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode,
|
|||
if (size > PyUnicode_GET_SIZE(unicode))
|
||||
size = PyUnicode_GET_SIZE(unicode) + 1;
|
||||
|
||||
#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T
|
||||
memcpy(w, unicode->str, size * sizeof(wchar_t));
|
||||
#else
|
||||
{
|
||||
register Py_UNICODE *u;
|
||||
register Py_ssize_t i;
|
||||
u = PyUnicode_AS_UNICODE(unicode);
|
||||
for (i = size; i > 0; i--)
|
||||
*w++ = *u++;
|
||||
}
|
||||
#endif
|
||||
unicode_aswidechar(unicode, w, size);
|
||||
|
||||
if (size > PyUnicode_GET_SIZE(unicode))
|
||||
return PyUnicode_GET_SIZE(unicode);
|
||||
|
@ -1184,6 +1191,33 @@ Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode,
|
|||
return size;
|
||||
}
|
||||
|
||||
wchar_t*
|
||||
PyUnicode_AsWideCharString(PyUnicodeObject *unicode,
|
||||
Py_ssize_t *size)
|
||||
{
|
||||
wchar_t* buffer;
|
||||
Py_ssize_t buflen;
|
||||
|
||||
if (unicode == NULL) {
|
||||
PyErr_BadInternalCall();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) < PyUnicode_GET_SIZE(unicode)) {
|
||||
PyErr_NoMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buflen = PyUnicode_GET_SIZE(unicode) + 1; /* copy L'\0' */
|
||||
buffer = PyMem_MALLOC(buflen * sizeof(wchar_t));
|
||||
if (buffer == NULL) {
|
||||
PyErr_NoMemory();
|
||||
return NULL;
|
||||
}
|
||||
unicode_aswidechar(unicode, buffer, buflen);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
PyObject *PyUnicode_FromOrdinal(int ordinal)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue