PyLocale_strxfrm() uses the new Unicode API

This commit is contained in:
Victor Stinner 2011-09-29 23:32:06 +02:00
parent fe9a861e74
commit 6364927cec
1 changed files with 15 additions and 24 deletions

View File

@ -271,31 +271,22 @@ Return a string that can be used as a key for locale-aware comparisons.");
static PyObject* static PyObject*
PyLocale_strxfrm(PyObject* self, PyObject* args) PyLocale_strxfrm(PyObject* self, PyObject* args)
{ {
Py_UNICODE *s0; PyObject *str;
Py_ssize_t n0; Py_ssize_t n1;
wchar_t *s, *buf = NULL; wchar_t *s = NULL, *buf = NULL;
size_t n1, n2; size_t n2;
PyObject *result = NULL; PyObject *result = NULL;
#if Py_UNICODE_SIZE != SIZEOF_WCHAR_T
Py_ssize_t i;
#endif
if (!PyArg_ParseTuple(args, "u#:strxfrm", &s0, &n0)) if (!PyArg_ParseTuple(args, "U:strxfrm", &str))
return NULL; return NULL;
#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T s = PyUnicode_AsWideCharString(str, &n1);
s = (wchar_t *) s0; if (s == NULL)
#else goto exit;
s = PyMem_Malloc((n0+1)*sizeof(wchar_t));
if (!s)
return PyErr_NoMemory();
for (i=0; i<=n0; i++)
s[i] = s0[i];
#endif
/* assume no change in size, first */ /* assume no change in size, first */
n1 = wcslen(s) + 1; n1 = n1 + 1;
buf = PyMem_Malloc(n1*sizeof(wchar_t)); buf = PyMem_Malloc(n1 * sizeof(wchar_t));
if (!buf) { if (!buf) {
PyErr_NoMemory(); PyErr_NoMemory();
goto exit; goto exit;
@ -311,11 +302,11 @@ PyLocale_strxfrm(PyObject* self, PyObject* args)
n2 = wcsxfrm(buf, s, n2+1); n2 = wcsxfrm(buf, s, n2+1);
} }
result = PyUnicode_FromWideChar(buf, n2); result = PyUnicode_FromWideChar(buf, n2);
exit: exit:
if (buf) PyMem_Free(buf); if (buf)
#if Py_UNICODE_SIZE != SIZEOF_WCHAR_T PyMem_Free(buf);
PyMem_Free(s); if (s)
#endif PyMem_Free(s);
return result; return result;
} }
#endif #endif