From 806b6657a3a6937c0ec969354387fd8876d25557 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 12 Sep 2024 19:58:55 +0530 Subject: [PATCH] Move html entity conversion to C --- setup/extensions.json | 5 +---- .../{html_entities.cpp => html_entities.c} | 17 ++++++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) rename src/calibre/ebooks/{html_entities.cpp => html_entities.c} (92%) diff --git a/setup/extensions.json b/setup/extensions.json index 7a3f7895bc..614ceed07f 100644 --- a/setup/extensions.json +++ b/setup/extensions.json @@ -140,10 +140,7 @@ }, { "name": "fast_html_entities", - "headers": "calibre/utils/cpp_binding.h calibre/utils/stb_sprintf.h", - "sources": "calibre/ebooks/html_entities.cpp", - "inc_dirs": "perfect-hashing", - "needs_c++": "14" + "sources": "calibre/ebooks/html_entities.c" }, { "name": "rcc_backend", diff --git a/src/calibre/ebooks/html_entities.cpp b/src/calibre/ebooks/html_entities.c similarity index 92% rename from src/calibre/ebooks/html_entities.cpp rename to src/calibre/ebooks/html_entities.c index 3b8ba7f29f..dfd05fc8fa 100644 --- a/src/calibre/ebooks/html_entities.cpp +++ b/src/calibre/ebooks/html_entities.c @@ -9,7 +9,7 @@ #define UNICODE #define _UNICODE #include -#include "../utils/cpp_binding.h" +#include unsigned int 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; } if (nargs > 1) keep_xml_entities = PyObject_IsTrue(args[1]); - generic_raii output((char*)PyMem_Malloc(input_sz + 1)); + char *output = malloc(input_sz + 1); if (!output) { return PyErr_NoMemory(); } - size_t output_sz = replace(input, input_sz, output.ptr(), keep_xml_entities); - if (PyErr_Occurred()) return NULL; - if (!output_sz) return Py_NewRef(args[0]); - if (PyUnicode_Check(args[0])) return PyUnicode_FromStringAndSize(output.ptr(), output_sz); - return PyBytes_FromStringAndSize(output.ptr(), output_sz); + size_t output_sz = replace(input, input_sz, output, keep_xml_entities); + PyObject *retval; + if (PyErr_Occurred()) retval = NULL; + if (!output_sz) retval = Py_NewRef(args[0]); + if (PyUnicode_Check(args[0])) retval = PyUnicode_FromStringAndSize(output, output_sz); + retval = PyBytes_FromStringAndSize(output, output_sz); + free(output); + return retval; } static PyMethodDef methods[] = {