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,30 +271,21 @@ Return a string that can be used as a key for locale-aware comparisons.");
static PyObject*
PyLocale_strxfrm(PyObject* self, PyObject* args)
{
Py_UNICODE *s0;
Py_ssize_t n0;
wchar_t *s, *buf = NULL;
size_t n1, n2;
PyObject *str;
Py_ssize_t n1;
wchar_t *s = NULL, *buf = NULL;
size_t n2;
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;
#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T
s = (wchar_t *) s0;
#else
s = PyMem_Malloc((n0+1)*sizeof(wchar_t));
if (!s)
return PyErr_NoMemory();
for (i=0; i<=n0; i++)
s[i] = s0[i];
#endif
s = PyUnicode_AsWideCharString(str, &n1);
if (s == NULL)
goto exit;
/* assume no change in size, first */
n1 = wcslen(s) + 1;
n1 = n1 + 1;
buf = PyMem_Malloc(n1 * sizeof(wchar_t));
if (!buf) {
PyErr_NoMemory();
@ -312,10 +303,10 @@ PyLocale_strxfrm(PyObject* self, PyObject* args)
}
result = PyUnicode_FromWideChar(buf, n2);
exit:
if (buf) PyMem_Free(buf);
#if Py_UNICODE_SIZE != SIZEOF_WCHAR_T
if (buf)
PyMem_Free(buf);
if (s)
PyMem_Free(s);
#endif
return result;
}
#endif