Move html entity conversion to C

This commit is contained in:
Kovid Goyal 2024-09-12 19:58:55 +05:30
parent 741ddaadb8
commit 806b6657a3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 11 additions and 11 deletions

View File

@ -140,10 +140,7 @@
}, },
{ {
"name": "fast_html_entities", "name": "fast_html_entities",
"headers": "calibre/utils/cpp_binding.h calibre/utils/stb_sprintf.h", "sources": "calibre/ebooks/html_entities.c"
"sources": "calibre/ebooks/html_entities.cpp",
"inc_dirs": "perfect-hashing",
"needs_c++": "14"
}, },
{ {
"name": "rcc_backend", "name": "rcc_backend",

View File

@ -9,7 +9,7 @@
#define UNICODE #define UNICODE
#define _UNICODE #define _UNICODE
#include <Python.h> #include <Python.h>
#include "../utils/cpp_binding.h" #include <stdbool.h>
unsigned int unsigned int
encode_utf8(uint32_t ch, char* dest) { encode_utf8(uint32_t ch, char* dest) {
@ -131,13 +131,16 @@ replace_entities(PyObject *self, PyObject *const *args, Py_ssize_t nargs) {
PyErr_SetString(PyExc_TypeError, "string must be unicode object or UTF-8 encoded bytes"); return NULL; PyErr_SetString(PyExc_TypeError, "string must be unicode object or UTF-8 encoded bytes"); return NULL;
} }
if (nargs > 1) keep_xml_entities = PyObject_IsTrue(args[1]); if (nargs > 1) keep_xml_entities = PyObject_IsTrue(args[1]);
generic_raii<char*, pymem_free> output((char*)PyMem_Malloc(input_sz + 1)); char *output = malloc(input_sz + 1);
if (!output) { return PyErr_NoMemory(); } if (!output) { return PyErr_NoMemory(); }
size_t output_sz = replace(input, input_sz, output.ptr(), keep_xml_entities); size_t output_sz = replace(input, input_sz, output, keep_xml_entities);
if (PyErr_Occurred()) return NULL; PyObject *retval;
if (!output_sz) return Py_NewRef(args[0]); if (PyErr_Occurred()) retval = NULL;
if (PyUnicode_Check(args[0])) return PyUnicode_FromStringAndSize(output.ptr(), output_sz); if (!output_sz) retval = Py_NewRef(args[0]);
return PyBytes_FromStringAndSize(output.ptr(), output_sz); if (PyUnicode_Check(args[0])) retval = PyUnicode_FromStringAndSize(output, output_sz);
retval = PyBytes_FromStringAndSize(output, output_sz);
free(output);
return retval;
} }
static PyMethodDef methods[] = { static PyMethodDef methods[] = {