Faster implementation of utf16_length

This commit is contained in:
Kovid Goyal 2014-05-12 18:12:19 +05:30
parent d337debc92
commit d3c2d8cd6f

View File

@ -993,14 +993,26 @@ icu_string_length(PyObject *self, PyObject *args) {
// utf16_length {{{ // utf16_length {{{
static PyObject * static PyObject *
icu_utf16_length(PyObject *self, PyObject *args) { icu_utf16_length(PyObject *self, PyObject *args) {
int32_t sz = 0; #if PY_VERSION_HEX >= 0x03030000
UChar *icu = NULL; #error Not implemented for python >= 3.3
PyObject *src = NULL; #endif
if (!PyArg_ParseTuple(args, "O", &src)) return NULL; int32_t sz = 0;
icu = python_to_icu(src, &sz, 1); PyObject *src = NULL;
if (icu == NULL) return NULL; #ifdef Py_UNICODE_WIDE
free(icu); int32_t i = 0, t = 0;
Py_UNICODE *data = NULL;
#endif
if (!PyArg_ParseTuple(args, "U", &src)) return NULL;
sz = PyUnicode_GET_SIZE(src);
#ifdef Py_UNICODE_WIDE
data = PyUnicode_AS_UNICODE(src);
for (i = 0; i < sz; i++) {
t += (data[i] > 0xffff) ? 2 : 1;
}
sz = t;
#endif
return Py_BuildValue("i", sz); return Py_BuildValue("i", sz);
} // }}} } // }}}