diff --git a/src/calibre/ebooks/oeb/parse_utils.py b/src/calibre/ebooks/oeb/parse_utils.py index 6ada357b5d..55decb1d61 100644 --- a/src/calibre/ebooks/oeb/parse_utils.py +++ b/src/calibre/ebooks/oeb/parse_utils.py @@ -27,12 +27,14 @@ class NotHTML(Exception): self.root_tag = root_tag -def barename(name): - return name.rpartition('}')[-1] +try: + from calibre_extensions.speedup import barename, namespace +except ImportError: + def barename(name: str) -> str: + return name.rpartition('}')[-1] - -def namespace(name): - return name.rpartition('}')[0][1:] + def namespace(name: str) -> str: + return name.rpartition('}')[0][1:] def XHTML(name): diff --git a/src/calibre/utils/speedup.c b/src/calibre/utils/speedup.c index 590f854966..f6eab24b9a 100644 --- a/src/calibre/utils/speedup.c +++ b/src/calibre/utils/speedup.c @@ -29,6 +29,29 @@ typedef unsigned __int8 uint8_t; #include #endif +static PyObject* +barename(PyObject *self, PyObject *tag) { + if (!PyUnicode_Check(tag)) { PyErr_Format(PyExc_TypeError, "%R is not a unicode string", tag); return NULL; } + Py_ssize_t pos = PyUnicode_FindChar(tag, '}', 0, PyUnicode_GetLength(tag), -1); + switch(pos) { + case -2: return NULL; + case -1: Py_INCREF(tag); return tag; + default: return PyUnicode_Substring(tag, pos + 1, PyUnicode_GetLength(tag)); + } +} + +static PyObject* +namespace(PyObject *self, PyObject *tag) { + if (!PyUnicode_Check(tag)) { PyErr_Format(PyExc_TypeError, "%R is not a unicode string", tag); return NULL; } + Py_ssize_t pos = PyUnicode_FindChar(tag, '}', 0, PyUnicode_GetLength(tag), -1); + switch(pos) { + case -2: return NULL; + case -1: return PyUnicode_FromString(""); + default: return PyUnicode_Substring(tag, 1, pos); + } +} + + static PyObject * speedup_parse_date(PyObject *self, PyObject *args) { const char *raw, *orig, *tz; @@ -733,6 +756,14 @@ static PyMethodDef speedup_methods[] = { "get_num_of_significant_chars(elem)\n\nGet the number of chars in specified tag" }, + {"barename", barename, METH_O, + "barename(tag)\n\nGet bare tag name without namespace" + }, + + {"namespace", namespace, METH_O, + "namespace(tag)\n\nGet namespace of the tag" + }, + {NULL, NULL, 0, NULL} };