Debian Codegraph @

src/unicode_names/unicode_names.c

raw
1 /*
2 * unicode_names.c
3 * Copyright (C) 2018 Kovid Goyal <kovid at kovidgoyal.net>
4 *
5 * Distributed under terms of the GPL3 license.
6 */
7
8 #include "names.h"
9
10 static inline void
11 add_matches(const word_trie *wt, char_type *codepoints, size_t *pos, const size_t sz) {
12 size_t num = mark_groups[wt->match_offset];
13 for (size_t i = wt->match_offset + 1; i < wt->match_offset + 1 + num && *pos < sz; i++, (*pos)++) { codepoints[*pos] = mark_to_cp[mark_groups[i]]; }
14 }
15
16 static void
17 process_trie_node(const word_trie *wt, char_type *codepoints, size_t *pos, const size_t sz) {
18 if (wt->match_offset) add_matches(wt, codepoints, pos, sz);
19 size_t num_children = children_array[wt->children_offset];
20 if (!num_children) return;
21 for (size_t c = wt->children_offset + 1; c < wt->children_offset + 1 + num_children; c++) {
22 if (*pos > sz) return;
23 uint32_t x = children_array[c];
24 process_trie_node(&all_trie_nodes[x >> 8], codepoints, pos, sz);
25 }
26 }
27
28 static inline PyObject *
29 codepoints_for_word(const char *word, size_t len) {
30 const word_trie *wt = all_trie_nodes;
31 for (size_t i = 0; i < len; i++) {
32 unsigned char ch = word[i];
33 size_t num_children = children_array[wt->children_offset];
34 if (!num_children) return PyFrozenSet_New(NULL);
35 bool found = false;
36 for (size_t c = wt->children_offset + 1; c < wt->children_offset + 1 + num_children; c++) {
37 uint32_t x = children_array[c];
38 if ((x & 0xff) == ch) {
39 found = true;
40 wt = &all_trie_nodes[x >> 8];
41 break;
42 }
43 }
44 if (!found) return PyFrozenSet_New(NULL);
45 }
46 static char_type codepoints[1024];
47 size_t cpos = 0;
48 process_trie_node(wt, codepoints, &cpos, arraysz(codepoints));
49 PyObject *ans = PyFrozenSet_New(NULL);
50 if (ans == NULL) return NULL;
51 for (size_t i = 0; i < cpos; i++) {
52 PyObject *t = PyLong_FromUnsignedLong(codepoints[i]);
53 if (t == NULL) {
54 Py_DECREF(ans);
55 return NULL;
56 }
57 int ret = PySet_Add(ans, t);
58 Py_DECREF(t);
59 if (ret != 0) {
60 Py_DECREF(ans);
61 return NULL;
62 }
63 }
64 return ans;
65 }
66
67 static PyObject *
68 cfw(PyObject *self UNUSED, PyObject *args) {
69 const char *word;
70 if (!PyArg_ParseTuple(args, "s", &word)) return NULL;
71 return codepoints_for_word(word, strlen(word));
72 }
73
74 static PyObject *
75 nfc(PyObject *self UNUSED, PyObject *args) {
76 unsigned int cp;
77 if (!PyArg_ParseTuple(args, "I", &cp)) return NULL;
78 const char *n = name_for_codepoint(cp);
79 if (n == NULL) Py_RETURN_NONE;
80 return PyUnicode_FromString(n);
81 }
82
83 static PyMethodDef unicode_names_methods[] = {
84 {"codepoints_for_word",
85 (PyCFunction)cfw,
86 METH_VARARGS,
87 "Return a set of integer codepoints for where each codepoint's name "
88 "contains ``word``,"},
89 {"name_for_codepoint", (PyCFunction)nfc, METH_VARARGS, "Returns the given codepoint's name"},
90 {NULL, NULL, 0, NULL} /* Sentinel */
91 };
92
93 static int
94 exec_module(PyObject *module) {
95 return 0;
96 }
97
98 static PyModuleDef_Slot slots[] = {{Py_mod_exec, exec_module}, {0, NULL}};
99
100 static struct PyModuleDef module_def = {
101 .m_base = PyModuleDef_HEAD_INIT,
102 .m_name = "unicode_names",
103 .m_doc = "A library to assist with selecting special characters",
104 .m_methods = unicode_names_methods,
105 .m_slots = slots,
106 };
107
108 CALIBRE_MODINIT_FUNC
109 PyInit_unicode_names(void) { return PyModuleDef_Init(&module_def); }