summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-09-29 10:25:54 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2010-09-29 10:25:54 (GMT)
commit137c34c0274954b2cdd1fd5b490c654528ff30f7 (patch)
treeec540a9b6bba9617af8afcb099fd4908a5bac230 /Objects
parent41a64a53a30f2d81de7b4a05e5e8e9b4089f7ca7 (diff)
downloadcpython-137c34c0274954b2cdd1fd5b490c654528ff30f7.zip
cpython-137c34c0274954b2cdd1fd5b490c654528ff30f7.tar.gz
cpython-137c34c0274954b2cdd1fd5b490c654528ff30f7.tar.bz2
Issue #9979: Create function PyUnicode_AsWideCharString().
Diffstat (limited to 'Objects')
-rw-r--r--Objects/unicodeobject.c62
1 files changed, 48 insertions, 14 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 489c98c..527e219 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -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)