Speedup barename and namespace

This commit is contained in:
Kovid Goyal 2025-02-17 11:57:42 +05:30
parent b033d78e8e
commit 8492dcc53a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 38 additions and 5 deletions

View File

@ -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):

View File

@ -29,6 +29,29 @@ typedef unsigned __int8 uint8_t;
#include <stdint.h>
#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}
};