From d3c2d8cd6fd54aadde6269d25cd23631b14203db Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 12 May 2014 18:12:19 +0530 Subject: [PATCH] Faster implementation of utf16_length --- src/calibre/utils/icu.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/calibre/utils/icu.c b/src/calibre/utils/icu.c index 0cf53abd0a..ec46aeb0d8 100644 --- a/src/calibre/utils/icu.c +++ b/src/calibre/utils/icu.c @@ -993,14 +993,26 @@ icu_string_length(PyObject *self, PyObject *args) { // utf16_length {{{ static PyObject * icu_utf16_length(PyObject *self, PyObject *args) { +#if PY_VERSION_HEX >= 0x03030000 +#error Not implemented for python >= 3.3 +#endif + int32_t sz = 0; - UChar *icu = NULL; PyObject *src = NULL; +#ifdef Py_UNICODE_WIDE + int32_t i = 0, t = 0; + Py_UNICODE *data = NULL; +#endif - if (!PyArg_ParseTuple(args, "O", &src)) return NULL; - icu = python_to_icu(src, &sz, 1); - if (icu == NULL) return NULL; - free(icu); + 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); } // }}}