Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 1 | /* ------------------------------------------------------------------------ |
| 2 | |
Ezio Melotti | 98d2c0a | 2011-11-10 09:36:34 +0200 | [diff] [blame] | 3 | unicodedata -- Provides access to the Unicode database. |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 4 | |
Ezio Melotti | 98d2c0a | 2011-11-10 09:36:34 +0200 | [diff] [blame] | 5 | Data was extracted from the UnicodeData.txt file. |
| 6 | The current version number is reported in the unidata_version constant. |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 7 | |
Fredrik Lundh | cfcea49 | 2000-09-25 08:07:06 +0000 | [diff] [blame] | 8 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 9 | Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com) |
Florent Xicluna | c934f32 | 2010-09-03 23:47:32 +0000 | [diff] [blame] | 10 | Modified by Martin v. Löwis (martin@v.loewis.de) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | cfcea49 | 2000-09-25 08:07:06 +0000 | [diff] [blame] | 12 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 13 | |
| 14 | ------------------------------------------------------------------------ */ |
| 15 | |
Victor Stinner | 65a3144 | 2014-07-01 16:45:52 +0200 | [diff] [blame] | 16 | #define PY_SSIZE_T_CLEAN |
| 17 | |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 18 | #include "Python.h" |
Victor Stinner | 47e1afd | 2020-10-26 16:43:47 +0100 | [diff] [blame] | 19 | #include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 20 | #include "structmember.h" // PyMemberDef |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 21 | |
Greg Price | 2f09413 | 2019-09-03 19:45:44 -0700 | [diff] [blame] | 22 | #include <stdbool.h> |
| 23 | |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 24 | _Py_IDENTIFIER(NFC); |
| 25 | _Py_IDENTIFIER(NFD); |
| 26 | _Py_IDENTIFIER(NFKC); |
| 27 | _Py_IDENTIFIER(NFKD); |
| 28 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 29 | /*[clinic input] |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 30 | module unicodedata |
Victor Stinner | c8c4200 | 2020-10-26 23:19:22 +0100 | [diff] [blame] | 31 | class unicodedata.UCD 'PreviousDBVersion *' '<not used>' |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 32 | [clinic start generated code]*/ |
Victor Stinner | c8c4200 | 2020-10-26 23:19:22 +0100 | [diff] [blame] | 33 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=e47113e05924be43]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 34 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 35 | /* character properties */ |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 36 | |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 37 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | const unsigned char category; /* index into |
| 39 | _PyUnicode_CategoryNames */ |
| 40 | const unsigned char combining; /* combining class value 0 - 255 */ |
| 41 | const unsigned char bidirectional; /* index into |
| 42 | _PyUnicode_BidirectionalNames */ |
| 43 | const unsigned char mirrored; /* true if mirrored in bidir mode */ |
| 44 | const unsigned char east_asian_width; /* index into |
| 45 | _PyUnicode_EastAsianWidth */ |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 46 | const unsigned char normalization_quick_check; /* see is_normalized() */ |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 47 | } _PyUnicode_DatabaseRecord; |
| 48 | |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 49 | typedef struct change_record { |
| 50 | /* sequence of fields should be the same as in merge_old_version */ |
| 51 | const unsigned char bidir_changed; |
| 52 | const unsigned char category_changed; |
| 53 | const unsigned char decimal_changed; |
Martin v. Löwis | 93cbca3 | 2008-09-10 14:08:48 +0000 | [diff] [blame] | 54 | const unsigned char mirrored_changed; |
Benjamin Peterson | 6775231 | 2016-09-14 23:53:47 -0700 | [diff] [blame] | 55 | const unsigned char east_asian_width_changed; |
Amaury Forgeot d'Arc | 7d52079 | 2009-10-06 21:03:20 +0000 | [diff] [blame] | 56 | const double numeric_changed; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 57 | } change_record; |
| 58 | |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 59 | /* data file generated by Tools/unicode/makeunicodedata.py */ |
| 60 | #include "unicodedata_db.h" |
| 61 | |
| 62 | static const _PyUnicode_DatabaseRecord* |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 63 | _getrecord_ex(Py_UCS4 code) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 64 | { |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 65 | int index; |
Neal Norwitz | e9c571f | 2003-02-28 03:14:37 +0000 | [diff] [blame] | 66 | if (code >= 0x110000) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 67 | index = 0; |
| 68 | else { |
| 69 | index = index1[(code>>SHIFT)]; |
| 70 | index = index2[(index<<SHIFT)+(code&((1<<SHIFT)-1))]; |
| 71 | } |
| 72 | |
| 73 | return &_PyUnicode_Database_Records[index]; |
| 74 | } |
| 75 | |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 76 | /* ------------- Previous-version API ------------------------------------- */ |
| 77 | typedef struct previous_version { |
| 78 | PyObject_HEAD |
| 79 | const char *name; |
| 80 | const change_record* (*getrecord)(Py_UCS4); |
| 81 | Py_UCS4 (*normalization)(Py_UCS4); |
| 82 | } PreviousDBVersion; |
| 83 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 84 | #include "clinic/unicodedata.c.h" |
| 85 | |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 86 | #define get_old_record(self, v) ((((PreviousDBVersion*)self)->getrecord)(v)) |
| 87 | |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 88 | static PyMemberDef DB_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | {"unidata_version", T_STRING, offsetof(PreviousDBVersion, name), READONLY}, |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 90 | {NULL} |
| 91 | }; |
| 92 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 93 | // Check if self is an unicodedata.UCD instance. |
| 94 | // If self is NULL (when the PyCapsule C API is used), return 0. |
| 95 | // PyModule_Check() is used to avoid having to retrieve the ucd_type. |
| 96 | // See unicodedata_functions comment to the rationale of this macro. |
| 97 | #define UCD_Check(self) (self != NULL && !PyModule_Check(self)) |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 98 | |
| 99 | static PyObject* |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 100 | new_previous_version(PyTypeObject *ucd_type, |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 101 | const char*name, const change_record* (*getrecord)(Py_UCS4), |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 102 | Py_UCS4 (*normalization)(Py_UCS4)) |
| 103 | { |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 104 | PreviousDBVersion *self; |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 105 | self = PyObject_New(PreviousDBVersion, ucd_type); |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 106 | if (self == NULL) |
| 107 | return NULL; |
| 108 | self->name = name; |
| 109 | self->getrecord = getrecord; |
| 110 | self->normalization = normalization; |
| 111 | return (PyObject*)self; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 115 | /* --- Module API --------------------------------------------------------- */ |
| 116 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 117 | /*[clinic input] |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 118 | unicodedata.UCD.decimal |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 119 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 120 | self: self |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 121 | chr: int(accept={str}) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 122 | default: object=NULL |
| 123 | / |
| 124 | |
| 125 | Converts a Unicode character into its equivalent decimal value. |
| 126 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 127 | Returns the decimal value assigned to the character chr as integer. |
| 128 | If no such value is defined, default is returned, or, if not given, |
| 129 | ValueError is raised. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 130 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 131 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 132 | static PyObject * |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 133 | unicodedata_UCD_decimal_impl(PyObject *self, int chr, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 134 | PyObject *default_value) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 135 | /*[clinic end generated code: output=be23376e1a185231 input=933f8107993f23d0]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 136 | { |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 137 | int have_old = 0; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 138 | long rc; |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 139 | Py_UCS4 c = (Py_UCS4)chr; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 140 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 141 | if (UCD_Check(self)) { |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 142 | const change_record *old = get_old_record(self, c); |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 143 | if (old->category_changed == 0) { |
| 144 | /* unassigned */ |
| 145 | have_old = 1; |
| 146 | rc = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | } |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 148 | else if (old->decimal_changed != 0xFF) { |
| 149 | have_old = 1; |
| 150 | rc = old->decimal_changed; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (!have_old) |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 155 | rc = Py_UNICODE_TODECIMAL(c); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 156 | if (rc < 0) { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 157 | if (default_value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | PyErr_SetString(PyExc_ValueError, |
| 159 | "not a decimal"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 160 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 161 | } |
| 162 | else { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 163 | Py_INCREF(default_value); |
| 164 | return default_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | } |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 166 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 167 | return PyLong_FromLong(rc); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 170 | /*[clinic input] |
| 171 | unicodedata.UCD.digit |
| 172 | |
| 173 | self: self |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 174 | chr: int(accept={str}) |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 175 | default: object=NULL |
| 176 | / |
| 177 | |
| 178 | Converts a Unicode character into its equivalent digit value. |
| 179 | |
| 180 | Returns the digit value assigned to the character chr as integer. |
| 181 | If no such value is defined, default is returned, or, if not given, |
| 182 | ValueError is raised. |
| 183 | [clinic start generated code]*/ |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 185 | static PyObject * |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 186 | unicodedata_UCD_digit_impl(PyObject *self, int chr, PyObject *default_value) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 187 | /*[clinic end generated code: output=96e18c950171fd2f input=e27d6e4565cd29f2]*/ |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 188 | { |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 189 | long rc; |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 190 | Py_UCS4 c = (Py_UCS4)chr; |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 191 | rc = Py_UNICODE_TODIGIT(c); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 192 | if (rc < 0) { |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 193 | if (default_value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | PyErr_SetString(PyExc_ValueError, "not a digit"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 195 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | } |
| 197 | else { |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 198 | Py_INCREF(default_value); |
| 199 | return default_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | } |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 201 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 202 | return PyLong_FromLong(rc); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 205 | /*[clinic input] |
| 206 | unicodedata.UCD.numeric |
| 207 | |
| 208 | self: self |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 209 | chr: int(accept={str}) |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 210 | default: object=NULL |
| 211 | / |
| 212 | |
| 213 | Converts a Unicode character into its equivalent numeric value. |
| 214 | |
| 215 | Returns the numeric value assigned to the character chr as float. |
| 216 | If no such value is defined, default is returned, or, if not given, |
| 217 | ValueError is raised. |
| 218 | [clinic start generated code]*/ |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 220 | static PyObject * |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 221 | unicodedata_UCD_numeric_impl(PyObject *self, int chr, |
| 222 | PyObject *default_value) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 223 | /*[clinic end generated code: output=53ce281fe85b10c4 input=fdf5871a5542893c]*/ |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 224 | { |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 225 | int have_old = 0; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 226 | double rc; |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 227 | Py_UCS4 c = (Py_UCS4)chr; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 228 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 229 | if (UCD_Check(self)) { |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 230 | const change_record *old = get_old_record(self, c); |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 231 | if (old->category_changed == 0) { |
| 232 | /* unassigned */ |
| 233 | have_old = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 234 | rc = -1.0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | } |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 236 | else if (old->decimal_changed != 0xFF) { |
| 237 | have_old = 1; |
| 238 | rc = old->decimal_changed; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if (!have_old) |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 243 | rc = Py_UNICODE_TONUMERIC(c); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 244 | if (rc == -1.0) { |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 245 | if (default_value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | PyErr_SetString(PyExc_ValueError, "not a numeric character"); |
| 247 | return NULL; |
| 248 | } |
| 249 | else { |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 250 | Py_INCREF(default_value); |
| 251 | return default_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | } |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 253 | } |
| 254 | return PyFloat_FromDouble(rc); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 257 | /*[clinic input] |
| 258 | unicodedata.UCD.category |
| 259 | |
| 260 | self: self |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 261 | chr: int(accept={str}) |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 262 | / |
| 263 | |
| 264 | Returns the general category assigned to the character chr as string. |
| 265 | [clinic start generated code]*/ |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 266 | |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 267 | static PyObject * |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 268 | unicodedata_UCD_category_impl(PyObject *self, int chr) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 269 | /*[clinic end generated code: output=8571539ee2e6783a input=27d6f3d85050bc06]*/ |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 270 | { |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 271 | int index; |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 272 | Py_UCS4 c = (Py_UCS4)chr; |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 273 | index = (int) _getrecord_ex(c)->category; |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 274 | if (UCD_Check(self)) { |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 275 | const change_record *old = get_old_record(self, c); |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 276 | if (old->category_changed != 0xFF) |
| 277 | index = old->category_changed; |
| 278 | } |
Walter Dörwald | 4254e76 | 2007-06-05 16:04:09 +0000 | [diff] [blame] | 279 | return PyUnicode_FromString(_PyUnicode_CategoryNames[index]); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 282 | /*[clinic input] |
| 283 | unicodedata.UCD.bidirectional |
| 284 | |
| 285 | self: self |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 286 | chr: int(accept={str}) |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 287 | / |
| 288 | |
| 289 | Returns the bidirectional class assigned to the character chr as string. |
| 290 | |
| 291 | If no such value is defined, an empty string is returned. |
| 292 | [clinic start generated code]*/ |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 293 | |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 294 | static PyObject * |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 295 | unicodedata_UCD_bidirectional_impl(PyObject *self, int chr) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 296 | /*[clinic end generated code: output=d36310ce2039bb92 input=b3d8f42cebfcf475]*/ |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 297 | { |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 298 | int index; |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 299 | Py_UCS4 c = (Py_UCS4)chr; |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 300 | index = (int) _getrecord_ex(c)->bidirectional; |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 301 | if (UCD_Check(self)) { |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 302 | const change_record *old = get_old_record(self, c); |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 303 | if (old->category_changed == 0) |
| 304 | index = 0; /* unassigned */ |
| 305 | else if (old->bidir_changed != 0xFF) |
| 306 | index = old->bidir_changed; |
| 307 | } |
Walter Dörwald | 4254e76 | 2007-06-05 16:04:09 +0000 | [diff] [blame] | 308 | return PyUnicode_FromString(_PyUnicode_BidirectionalNames[index]); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 311 | /*[clinic input] |
| 312 | unicodedata.UCD.combining -> int |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 313 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 314 | self: self |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 315 | chr: int(accept={str}) |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 316 | / |
| 317 | |
| 318 | Returns the canonical combining class assigned to the character chr as integer. |
| 319 | |
| 320 | Returns 0 if no combining class is defined. |
| 321 | [clinic start generated code]*/ |
| 322 | |
| 323 | static int |
| 324 | unicodedata_UCD_combining_impl(PyObject *self, int chr) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 325 | /*[clinic end generated code: output=cad056d0cb6a5920 input=9f2d6b2a95d0a22a]*/ |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 326 | { |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 327 | int index; |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 328 | Py_UCS4 c = (Py_UCS4)chr; |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 329 | index = (int) _getrecord_ex(c)->combining; |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 330 | if (UCD_Check(self)) { |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 331 | const change_record *old = get_old_record(self, c); |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 332 | if (old->category_changed == 0) |
| 333 | index = 0; /* unassigned */ |
| 334 | } |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 335 | return index; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 338 | /*[clinic input] |
| 339 | unicodedata.UCD.mirrored -> int |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 340 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 341 | self: self |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 342 | chr: int(accept={str}) |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 343 | / |
| 344 | |
| 345 | Returns the mirrored property assigned to the character chr as integer. |
| 346 | |
| 347 | Returns 1 if the character has been identified as a "mirrored" |
| 348 | character in bidirectional text, 0 otherwise. |
| 349 | [clinic start generated code]*/ |
| 350 | |
| 351 | static int |
| 352 | unicodedata_UCD_mirrored_impl(PyObject *self, int chr) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 353 | /*[clinic end generated code: output=2532dbf8121b50e6 input=5dd400d351ae6f3b]*/ |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 354 | { |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 355 | int index; |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 356 | Py_UCS4 c = (Py_UCS4)chr; |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 357 | index = (int) _getrecord_ex(c)->mirrored; |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 358 | if (UCD_Check(self)) { |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 359 | const change_record *old = get_old_record(self, c); |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 360 | if (old->category_changed == 0) |
| 361 | index = 0; /* unassigned */ |
Martin v. Löwis | 93cbca3 | 2008-09-10 14:08:48 +0000 | [diff] [blame] | 362 | else if (old->mirrored_changed != 0xFF) |
| 363 | index = old->mirrored_changed; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 364 | } |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 365 | return index; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 368 | /*[clinic input] |
| 369 | unicodedata.UCD.east_asian_width |
| 370 | |
| 371 | self: self |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 372 | chr: int(accept={str}) |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 373 | / |
| 374 | |
| 375 | Returns the east asian width assigned to the character chr as string. |
| 376 | [clinic start generated code]*/ |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 377 | |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 378 | static PyObject * |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 379 | unicodedata_UCD_east_asian_width_impl(PyObject *self, int chr) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 380 | /*[clinic end generated code: output=484e8537d9ee8197 input=c4854798aab026e0]*/ |
Hye-Shik Chang | e9ddfbb | 2004-08-04 07:38:35 +0000 | [diff] [blame] | 381 | { |
Hye-Shik Chang | e9ddfbb | 2004-08-04 07:38:35 +0000 | [diff] [blame] | 382 | int index; |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 383 | Py_UCS4 c = (Py_UCS4)chr; |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 384 | index = (int) _getrecord_ex(c)->east_asian_width; |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 385 | if (UCD_Check(self)) { |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 386 | const change_record *old = get_old_record(self, c); |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 387 | if (old->category_changed == 0) |
| 388 | index = 0; /* unassigned */ |
Benjamin Peterson | 6775231 | 2016-09-14 23:53:47 -0700 | [diff] [blame] | 389 | else if (old->east_asian_width_changed != 0xFF) |
| 390 | index = old->east_asian_width_changed; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 391 | } |
Walter Dörwald | 4254e76 | 2007-06-05 16:04:09 +0000 | [diff] [blame] | 392 | return PyUnicode_FromString(_PyUnicode_EastAsianWidthNames[index]); |
Hye-Shik Chang | e9ddfbb | 2004-08-04 07:38:35 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 395 | /*[clinic input] |
| 396 | unicodedata.UCD.decomposition |
| 397 | |
| 398 | self: self |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 399 | chr: int(accept={str}) |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 400 | / |
| 401 | |
| 402 | Returns the character decomposition mapping assigned to the character chr as string. |
| 403 | |
| 404 | An empty string is returned in case no such mapping is defined. |
| 405 | [clinic start generated code]*/ |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 406 | |
Hye-Shik Chang | e9ddfbb | 2004-08-04 07:38:35 +0000 | [diff] [blame] | 407 | static PyObject * |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 408 | unicodedata_UCD_decomposition_impl(PyObject *self, int chr) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 409 | /*[clinic end generated code: output=7d699f3ec7565d27 input=e4c12459ad68507b]*/ |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 410 | { |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 411 | char decomp[256]; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 412 | int code, index, count; |
| 413 | size_t i; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 414 | unsigned int prefix_index; |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 415 | Py_UCS4 c = (Py_UCS4)chr; |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 416 | |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 417 | code = (int)c; |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 418 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 419 | if (UCD_Check(self)) { |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 420 | const change_record *old = get_old_record(self, c); |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 421 | if (old->category_changed == 0) |
Walter Dörwald | 4254e76 | 2007-06-05 16:04:09 +0000 | [diff] [blame] | 422 | return PyUnicode_FromString(""); /* unassigned */ |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Martin v. Löwis | 9def6a3 | 2002-10-18 16:11:54 +0000 | [diff] [blame] | 425 | if (code < 0 || code >= 0x110000) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 426 | index = 0; |
| 427 | else { |
| 428 | index = decomp_index1[(code>>DECOMP_SHIFT)]; |
| 429 | index = decomp_index2[(index<<DECOMP_SHIFT)+ |
| 430 | (code&((1<<DECOMP_SHIFT)-1))]; |
| 431 | } |
| 432 | |
Tim Peters | 69b83b1 | 2001-11-30 07:23:05 +0000 | [diff] [blame] | 433 | /* high byte is number of hex bytes (usually one or two), low byte |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 434 | is prefix code (from*/ |
| 435 | count = decomp_data[index] >> 8; |
| 436 | |
| 437 | /* XXX: could allocate the PyString up front instead |
| 438 | (strlen(prefix) + 5 * count + 1 bytes) */ |
| 439 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 440 | /* Based on how index is calculated above and decomp_data is generated |
| 441 | from Tools/unicode/makeunicodedata.py, it should not be possible |
| 442 | to overflow decomp_prefix. */ |
| 443 | prefix_index = decomp_data[index] & 255; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 444 | assert(prefix_index < Py_ARRAY_LENGTH(decomp_prefix)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 445 | |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 446 | /* copy prefix */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 447 | i = strlen(decomp_prefix[prefix_index]); |
| 448 | memcpy(decomp, decomp_prefix[prefix_index], i); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 449 | |
| 450 | while (count-- > 0) { |
| 451 | if (i) |
| 452 | decomp[i++] = ' '; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 453 | assert(i < sizeof(decomp)); |
Tim Peters | 69b83b1 | 2001-11-30 07:23:05 +0000 | [diff] [blame] | 454 | PyOS_snprintf(decomp + i, sizeof(decomp) - i, "%04X", |
| 455 | decomp_data[++index]); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 456 | i += strlen(decomp + i); |
| 457 | } |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 458 | return PyUnicode_FromStringAndSize(decomp, i); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 461 | static void |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 462 | get_decomp_record(PyObject *self, Py_UCS4 code, |
| 463 | int *index, int *prefix, int *count) |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 464 | { |
Neal Norwitz | e9c571f | 2003-02-28 03:14:37 +0000 | [diff] [blame] | 465 | if (code >= 0x110000) { |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 466 | *index = 0; |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 467 | } |
| 468 | else if (UCD_Check(self) |
| 469 | && get_old_record(self, code)->category_changed==0) { |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 470 | /* unassigned in old version */ |
| 471 | *index = 0; |
| 472 | } |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 473 | else { |
| 474 | *index = decomp_index1[(code>>DECOMP_SHIFT)]; |
| 475 | *index = decomp_index2[(*index<<DECOMP_SHIFT)+ |
| 476 | (code&((1<<DECOMP_SHIFT)-1))]; |
| 477 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 479 | /* high byte is number of hex bytes (usually one or two), low byte |
| 480 | is prefix code (from*/ |
| 481 | *count = decomp_data[*index] >> 8; |
| 482 | *prefix = decomp_data[*index] & 255; |
| 483 | |
| 484 | (*index)++; |
| 485 | } |
| 486 | |
| 487 | #define SBase 0xAC00 |
| 488 | #define LBase 0x1100 |
| 489 | #define VBase 0x1161 |
| 490 | #define TBase 0x11A7 |
| 491 | #define LCount 19 |
| 492 | #define VCount 21 |
| 493 | #define TCount 28 |
| 494 | #define NCount (VCount*TCount) |
| 495 | #define SCount (LCount*NCount) |
| 496 | |
| 497 | static PyObject* |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 498 | nfd_nfkd(PyObject *self, PyObject *input, int k) |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 499 | { |
| 500 | PyObject *result; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 501 | Py_UCS4 *output; |
| 502 | Py_ssize_t i, o, osize; |
| 503 | int kind; |
Serhiy Storchaka | cd8295f | 2020-04-11 10:48:40 +0300 | [diff] [blame] | 504 | const void *data; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 505 | /* Longest decomposition in Unicode 3.2: U+FDFA */ |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 506 | Py_UCS4 stack[20]; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 507 | Py_ssize_t space, isize; |
| 508 | int index, prefix, count, stackptr; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 509 | unsigned char prev, cur; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 511 | stackptr = 0; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 512 | isize = PyUnicode_GET_LENGTH(input); |
Benjamin Peterson | b779bfb | 2015-03-02 11:17:05 -0500 | [diff] [blame] | 513 | space = isize; |
Ezio Melotti | 7c4a7e6 | 2013-08-26 01:32:56 +0300 | [diff] [blame] | 514 | /* Overallocate at most 10 characters. */ |
Benjamin Peterson | b779bfb | 2015-03-02 11:17:05 -0500 | [diff] [blame] | 515 | if (space > 10) { |
| 516 | if (space <= PY_SSIZE_T_MAX - 10) |
| 517 | space += 10; |
| 518 | } |
| 519 | else { |
| 520 | space *= 2; |
| 521 | } |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 522 | osize = space; |
Benjamin Peterson | b779bfb | 2015-03-02 11:17:05 -0500 | [diff] [blame] | 523 | output = PyMem_NEW(Py_UCS4, space); |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 524 | if (!output) { |
| 525 | PyErr_NoMemory(); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 526 | return NULL; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 527 | } |
| 528 | i = o = 0; |
| 529 | kind = PyUnicode_KIND(input); |
| 530 | data = PyUnicode_DATA(input); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 531 | |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 532 | while (i < isize) { |
| 533 | stack[stackptr++] = PyUnicode_READ(kind, data, i++); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 534 | while(stackptr) { |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 535 | Py_UCS4 code = stack[--stackptr]; |
Martin v. Löwis | d2171d2 | 2003-11-06 20:47:57 +0000 | [diff] [blame] | 536 | /* Hangul Decomposition adds three characters in |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 537 | a single step, so we need at least that much room. */ |
Martin v. Löwis | d2171d2 | 2003-11-06 20:47:57 +0000 | [diff] [blame] | 538 | if (space < 3) { |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 539 | Py_UCS4 *new_output; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 540 | osize += 10; |
Martin v. Löwis | d2171d2 | 2003-11-06 20:47:57 +0000 | [diff] [blame] | 541 | space += 10; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 542 | new_output = PyMem_Realloc(output, osize*sizeof(Py_UCS4)); |
| 543 | if (new_output == NULL) { |
| 544 | PyMem_Free(output); |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 545 | PyErr_NoMemory(); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 546 | return NULL; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 547 | } |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 548 | output = new_output; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 549 | } |
| 550 | /* Hangul Decomposition. */ |
| 551 | if (SBase <= code && code < (SBase+SCount)) { |
| 552 | int SIndex = code - SBase; |
| 553 | int L = LBase + SIndex / NCount; |
| 554 | int V = VBase + (SIndex % NCount) / TCount; |
| 555 | int T = TBase + SIndex % TCount; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 556 | output[o++] = L; |
| 557 | output[o++] = V; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 558 | space -= 2; |
| 559 | if (T != TBase) { |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 560 | output[o++] = T; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 561 | space --; |
| 562 | } |
| 563 | continue; |
| 564 | } |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 565 | /* normalization changes */ |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 566 | if (UCD_Check(self)) { |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 567 | Py_UCS4 value = ((PreviousDBVersion*)self)->normalization(code); |
| 568 | if (value != 0) { |
| 569 | stack[stackptr++] = value; |
| 570 | continue; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /* Other decompositions. */ |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 575 | get_decomp_record(self, code, &index, &prefix, &count); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 576 | |
| 577 | /* Copy character if it is not decomposable, or has a |
| 578 | compatibility decomposition, but we do NFD. */ |
| 579 | if (!count || (prefix && !k)) { |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 580 | output[o++] = code; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 581 | space--; |
| 582 | continue; |
| 583 | } |
| 584 | /* Copy decomposition onto the stack, in reverse |
| 585 | order. */ |
| 586 | while(count) { |
| 587 | code = decomp_data[index + (--count)]; |
| 588 | stack[stackptr++] = code; |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 593 | result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, |
| 594 | output, o); |
| 595 | PyMem_Free(output); |
| 596 | if (!result) |
| 597 | return NULL; |
| 598 | /* result is guaranteed to be ready, as it is compact. */ |
| 599 | kind = PyUnicode_KIND(result); |
| 600 | data = PyUnicode_DATA(result); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 601 | |
| 602 | /* Sort canonically. */ |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 603 | i = 0; |
| 604 | prev = _getrecord_ex(PyUnicode_READ(kind, data, i))->combining; |
| 605 | for (i++; i < PyUnicode_GET_LENGTH(result); i++) { |
| 606 | cur = _getrecord_ex(PyUnicode_READ(kind, data, i))->combining; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 607 | if (prev == 0 || cur == 0 || prev <= cur) { |
| 608 | prev = cur; |
| 609 | continue; |
| 610 | } |
| 611 | /* Non-canonical order. Need to switch *i with previous. */ |
| 612 | o = i - 1; |
| 613 | while (1) { |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 614 | Py_UCS4 tmp = PyUnicode_READ(kind, data, o+1); |
| 615 | PyUnicode_WRITE(kind, data, o+1, |
| 616 | PyUnicode_READ(kind, data, o)); |
| 617 | PyUnicode_WRITE(kind, data, o, tmp); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 618 | o--; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 619 | if (o < 0) |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 620 | break; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 621 | prev = _getrecord_ex(PyUnicode_READ(kind, data, o))->combining; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 622 | if (prev == 0 || prev <= cur) |
| 623 | break; |
| 624 | } |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 625 | prev = _getrecord_ex(PyUnicode_READ(kind, data, i))->combining; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 626 | } |
| 627 | return result; |
| 628 | } |
| 629 | |
| 630 | static int |
Andy Lester | 982307b | 2020-03-17 11:38:12 -0500 | [diff] [blame] | 631 | find_nfc_index(const struct reindex* nfc, Py_UCS4 code) |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 632 | { |
Antoine Pitrou | 1d4bd25 | 2011-10-06 15:44:15 +0200 | [diff] [blame] | 633 | unsigned int index; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 634 | for (index = 0; nfc[index].start; index++) { |
Antoine Pitrou | 1d4bd25 | 2011-10-06 15:44:15 +0200 | [diff] [blame] | 635 | unsigned int start = nfc[index].start; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 636 | if (code < start) |
| 637 | return -1; |
| 638 | if (code <= start + nfc[index].count) { |
Antoine Pitrou | 1d4bd25 | 2011-10-06 15:44:15 +0200 | [diff] [blame] | 639 | unsigned int delta = code - start; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 640 | return nfc[index].index + delta; |
| 641 | } |
| 642 | } |
| 643 | return -1; |
| 644 | } |
| 645 | |
| 646 | static PyObject* |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 647 | nfc_nfkc(PyObject *self, PyObject *input, int k) |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 648 | { |
| 649 | PyObject *result; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 650 | int kind; |
Serhiy Storchaka | cd8295f | 2020-04-11 10:48:40 +0300 | [diff] [blame] | 651 | const void *data; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 652 | Py_UCS4 *output; |
| 653 | Py_ssize_t i, i1, o, len; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 654 | int f,l,index,index1,comb; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 655 | Py_UCS4 code; |
| 656 | Py_ssize_t skipped[20]; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 657 | int cskipped = 0; |
| 658 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 659 | result = nfd_nfkd(self, input, k); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 660 | if (!result) |
| 661 | return NULL; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 662 | /* result will be "ready". */ |
| 663 | kind = PyUnicode_KIND(result); |
| 664 | data = PyUnicode_DATA(result); |
| 665 | len = PyUnicode_GET_LENGTH(result); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 666 | |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 667 | /* We allocate a buffer for the output. |
| 668 | If we find that we made no changes, we still return |
| 669 | the NFD result. */ |
Benjamin Peterson | b779bfb | 2015-03-02 11:17:05 -0500 | [diff] [blame] | 670 | output = PyMem_NEW(Py_UCS4, len); |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 671 | if (!output) { |
| 672 | PyErr_NoMemory(); |
| 673 | Py_DECREF(result); |
| 674 | return 0; |
| 675 | } |
| 676 | i = o = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 678 | again: |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 679 | while (i < len) { |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 680 | for (index = 0; index < cskipped; index++) { |
| 681 | if (skipped[index] == i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | /* *i character is skipped. |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 683 | Remove from list. */ |
| 684 | skipped[index] = skipped[cskipped-1]; |
| 685 | cskipped--; |
| 686 | i++; |
Martin v. Löwis | 2fb661f | 2002-12-07 14:56:36 +0000 | [diff] [blame] | 687 | goto again; /* continue while */ |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | /* Hangul Composition. We don't need to check for <LV,T> |
| 691 | pairs, since we always have decomposed data. */ |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 692 | code = PyUnicode_READ(kind, data, i); |
| 693 | if (LBase <= code && code < (LBase+LCount) && |
| 694 | i + 1 < len && |
| 695 | VBase <= PyUnicode_READ(kind, data, i+1) && |
Wonsup Yoon | d134809 | 2018-06-15 21:03:14 +0900 | [diff] [blame] | 696 | PyUnicode_READ(kind, data, i+1) < (VBase+VCount)) { |
| 697 | /* check L character is a modern leading consonant (0x1100 ~ 0x1112) |
| 698 | and V character is a modern vowel (0x1161 ~ 0x1175). */ |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 699 | int LIndex, VIndex; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 700 | LIndex = code - LBase; |
| 701 | VIndex = PyUnicode_READ(kind, data, i+1) - VBase; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 702 | code = SBase + (LIndex*VCount+VIndex)*TCount; |
| 703 | i+=2; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 704 | if (i < len && |
Wonsup Yoon | d134809 | 2018-06-15 21:03:14 +0900 | [diff] [blame] | 705 | TBase < PyUnicode_READ(kind, data, i) && |
| 706 | PyUnicode_READ(kind, data, i) < (TBase+TCount)) { |
| 707 | /* check T character is a modern trailing consonant |
| 708 | (0x11A8 ~ 0x11C2). */ |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 709 | code += PyUnicode_READ(kind, data, i)-TBase; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 710 | i++; |
| 711 | } |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 712 | output[o++] = code; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 713 | continue; |
| 714 | } |
| 715 | |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 716 | /* code is still input[i] here */ |
Andy Lester | 982307b | 2020-03-17 11:38:12 -0500 | [diff] [blame] | 717 | f = find_nfc_index(nfc_first, code); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 718 | if (f == -1) { |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 719 | output[o++] = code; |
| 720 | i++; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 721 | continue; |
| 722 | } |
| 723 | /* Find next unblocked character. */ |
| 724 | i1 = i+1; |
| 725 | comb = 0; |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 726 | /* output base character for now; might be updated later. */ |
| 727 | output[o] = PyUnicode_READ(kind, data, i); |
| 728 | while (i1 < len) { |
| 729 | Py_UCS4 code1 = PyUnicode_READ(kind, data, i1); |
| 730 | int comb1 = _getrecord_ex(code1)->combining; |
Alexander Belopolsky | 86f65d5 | 2010-12-23 02:27:37 +0000 | [diff] [blame] | 731 | if (comb) { |
| 732 | if (comb1 == 0) |
| 733 | break; |
| 734 | if (comb >= comb1) { |
| 735 | /* Character is blocked. */ |
| 736 | i1++; |
| 737 | continue; |
| 738 | } |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 739 | } |
Andy Lester | 982307b | 2020-03-17 11:38:12 -0500 | [diff] [blame] | 740 | l = find_nfc_index(nfc_last, code1); |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 741 | /* i1 cannot be combined with i. If i1 |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 742 | is a starter, we don't need to look further. |
| 743 | Otherwise, record the combining class. */ |
| 744 | if (l == -1) { |
| 745 | not_combinable: |
| 746 | if (comb1 == 0) |
| 747 | break; |
| 748 | comb = comb1; |
| 749 | i1++; |
| 750 | continue; |
| 751 | } |
| 752 | index = f*TOTAL_LAST + l; |
| 753 | index1 = comp_index[index >> COMP_SHIFT]; |
| 754 | code = comp_data[(index1<<COMP_SHIFT)+ |
| 755 | (index&((1<<COMP_SHIFT)-1))]; |
| 756 | if (code == 0) |
| 757 | goto not_combinable; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 759 | /* Replace the original character. */ |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 760 | output[o] = code; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 761 | /* Mark the second character unused. */ |
Alexander Belopolsky | 86f65d5 | 2010-12-23 02:27:37 +0000 | [diff] [blame] | 762 | assert(cskipped < 20); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 763 | skipped[cskipped++] = i1; |
| 764 | i1++; |
Andy Lester | 982307b | 2020-03-17 11:38:12 -0500 | [diff] [blame] | 765 | f = find_nfc_index(nfc_first, output[o]); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 766 | if (f == -1) |
| 767 | break; |
| 768 | } |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 769 | /* Output character was already written. |
| 770 | Just advance the indices. */ |
| 771 | o++; i++; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 772 | } |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 773 | if (o == len) { |
| 774 | /* No changes. Return original string. */ |
| 775 | PyMem_Free(output); |
| 776 | return result; |
| 777 | } |
| 778 | Py_DECREF(result); |
| 779 | result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, |
| 780 | output, o); |
| 781 | PyMem_Free(output); |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 782 | return result; |
| 783 | } |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 784 | |
Greg Price | 2f09413 | 2019-09-03 19:45:44 -0700 | [diff] [blame] | 785 | // This needs to match the logic in makeunicodedata.py |
| 786 | // which constructs the quickcheck data. |
| 787 | typedef enum {YES = 0, MAYBE = 1, NO = 2} QuickcheckResult; |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 788 | |
Greg Price | 2f09413 | 2019-09-03 19:45:44 -0700 | [diff] [blame] | 789 | /* Run the Unicode normalization "quickcheck" algorithm. |
| 790 | * |
| 791 | * Return YES or NO if quickcheck determines the input is certainly |
| 792 | * normalized or certainly not, and MAYBE if quickcheck is unable to |
| 793 | * tell. |
| 794 | * |
| 795 | * If `yes_only` is true, then return MAYBE as soon as we determine |
| 796 | * the answer is not YES. |
| 797 | * |
| 798 | * For background and details on the algorithm, see UAX #15: |
| 799 | * https://www.unicode.org/reports/tr15/#Detecting_Normalization_Forms |
| 800 | */ |
| 801 | static QuickcheckResult |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 802 | is_normalized_quickcheck(PyObject *self, PyObject *input, bool nfc, bool k, |
| 803 | bool yes_only) |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 804 | { |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 805 | /* UCD 3.2.0 is requested, quickchecks must be disabled. */ |
| 806 | if (UCD_Check(self)) { |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 807 | return NO; |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 808 | } |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 809 | |
Greg Price | 2f09413 | 2019-09-03 19:45:44 -0700 | [diff] [blame] | 810 | Py_ssize_t i, len; |
| 811 | int kind; |
Serhiy Storchaka | cd8295f | 2020-04-11 10:48:40 +0300 | [diff] [blame] | 812 | const void *data; |
Greg Price | 2f09413 | 2019-09-03 19:45:44 -0700 | [diff] [blame] | 813 | unsigned char prev_combining = 0; |
| 814 | |
| 815 | /* The two quickcheck bits at this shift have type QuickcheckResult. */ |
| 816 | int quickcheck_shift = (nfc ? 4 : 0) + (k ? 2 : 0); |
| 817 | |
| 818 | QuickcheckResult result = YES; /* certainly normalized, unless we find something */ |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 819 | |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 820 | i = 0; |
| 821 | kind = PyUnicode_KIND(input); |
| 822 | data = PyUnicode_DATA(input); |
| 823 | len = PyUnicode_GET_LENGTH(input); |
| 824 | while (i < len) { |
| 825 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
| 826 | const _PyUnicode_DatabaseRecord *record = _getrecord_ex(ch); |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 827 | |
Greg Price | 2f09413 | 2019-09-03 19:45:44 -0700 | [diff] [blame] | 828 | unsigned char combining = record->combining; |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 829 | if (combining && prev_combining > combining) |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 830 | return NO; /* non-canonical sort order, not normalized */ |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 831 | prev_combining = combining; |
Greg Price | 2f09413 | 2019-09-03 19:45:44 -0700 | [diff] [blame] | 832 | |
| 833 | unsigned char quickcheck_whole = record->normalization_quick_check; |
| 834 | if (yes_only) { |
| 835 | if (quickcheck_whole & (3 << quickcheck_shift)) |
| 836 | return MAYBE; |
| 837 | } else { |
| 838 | switch ((quickcheck_whole >> quickcheck_shift) & 3) { |
| 839 | case NO: |
| 840 | return NO; |
| 841 | case MAYBE: |
| 842 | result = MAYBE; /* this string might need normalization */ |
| 843 | } |
| 844 | } |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 845 | } |
Greg Price | 2f09413 | 2019-09-03 19:45:44 -0700 | [diff] [blame] | 846 | return result; |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 849 | /*[clinic input] |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 850 | unicodedata.UCD.is_normalized |
| 851 | |
| 852 | self: self |
| 853 | form: unicode |
| 854 | unistr as input: unicode |
| 855 | / |
| 856 | |
| 857 | Return whether the Unicode string unistr is in the normal form 'form'. |
| 858 | |
| 859 | Valid values for form are 'NFC', 'NFKC', 'NFD', and 'NFKD'. |
| 860 | [clinic start generated code]*/ |
| 861 | |
| 862 | static PyObject * |
| 863 | unicodedata_UCD_is_normalized_impl(PyObject *self, PyObject *form, |
| 864 | PyObject *input) |
| 865 | /*[clinic end generated code: output=11e5a3694e723ca5 input=a544f14cea79e508]*/ |
| 866 | { |
| 867 | if (PyUnicode_READY(input) == -1) { |
| 868 | return NULL; |
| 869 | } |
| 870 | |
| 871 | if (PyUnicode_GET_LENGTH(input) == 0) { |
| 872 | /* special case empty input strings. */ |
| 873 | Py_RETURN_TRUE; |
| 874 | } |
| 875 | |
| 876 | PyObject *result; |
Greg Price | 7669cb8 | 2019-09-09 02:16:31 -0700 | [diff] [blame] | 877 | bool nfc = false; |
| 878 | bool k = false; |
Greg Price | 2f09413 | 2019-09-03 19:45:44 -0700 | [diff] [blame] | 879 | QuickcheckResult m; |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 880 | |
| 881 | PyObject *cmp; |
| 882 | int match = 0; |
| 883 | |
| 884 | if (_PyUnicode_EqualToASCIIId(form, &PyId_NFC)) { |
Greg Price | 7669cb8 | 2019-09-09 02:16:31 -0700 | [diff] [blame] | 885 | nfc = true; |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 886 | } |
| 887 | else if (_PyUnicode_EqualToASCIIId(form, &PyId_NFKC)) { |
Greg Price | 7669cb8 | 2019-09-09 02:16:31 -0700 | [diff] [blame] | 888 | nfc = true; |
| 889 | k = true; |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 890 | } |
| 891 | else if (_PyUnicode_EqualToASCIIId(form, &PyId_NFD)) { |
| 892 | /* matches default values for `nfc` and `k` */ |
| 893 | } |
| 894 | else if (_PyUnicode_EqualToASCIIId(form, &PyId_NFKD)) { |
Greg Price | 7669cb8 | 2019-09-09 02:16:31 -0700 | [diff] [blame] | 895 | k = true; |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 896 | } |
| 897 | else { |
| 898 | PyErr_SetString(PyExc_ValueError, "invalid normalization form"); |
| 899 | return NULL; |
| 900 | } |
| 901 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 902 | m = is_normalized_quickcheck(self, input, nfc, k, false); |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 903 | |
| 904 | if (m == MAYBE) { |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 905 | cmp = (nfc ? nfc_nfkc : nfd_nfkd)(self, input, k); |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 906 | if (cmp == NULL) { |
| 907 | return NULL; |
| 908 | } |
| 909 | match = PyUnicode_Compare(input, cmp); |
| 910 | Py_DECREF(cmp); |
| 911 | result = (match == 0) ? Py_True : Py_False; |
| 912 | } |
| 913 | else { |
| 914 | result = (m == YES) ? Py_True : Py_False; |
| 915 | } |
| 916 | |
| 917 | Py_INCREF(result); |
| 918 | return result; |
| 919 | } |
| 920 | |
| 921 | |
| 922 | /*[clinic input] |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 923 | unicodedata.UCD.normalize |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 924 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 925 | self: self |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 926 | form: unicode |
Serhiy Storchaka | f8d7d41 | 2016-10-23 15:12:25 +0300 | [diff] [blame] | 927 | unistr as input: unicode |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 928 | / |
| 929 | |
| 930 | Return the normal form 'form' for the Unicode string unistr. |
| 931 | |
| 932 | Valid values for form are 'NFC', 'NFKC', 'NFD', and 'NFKD'. |
| 933 | [clinic start generated code]*/ |
| 934 | |
| 935 | static PyObject * |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 936 | unicodedata_UCD_normalize_impl(PyObject *self, PyObject *form, |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 937 | PyObject *input) |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 938 | /*[clinic end generated code: output=05ca4385a2ad6983 input=3a5206c0ad2833fb]*/ |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 939 | { |
Martin v. Löwis | 2297066 | 2011-09-29 13:39:38 +0200 | [diff] [blame] | 940 | if (PyUnicode_GET_LENGTH(input) == 0) { |
Martin v. Löwis | 61e40bd | 2004-04-17 19:36:48 +0000 | [diff] [blame] | 941 | /* Special case empty input strings, since resizing |
| 942 | them later would cause internal errors. */ |
| 943 | Py_INCREF(input); |
| 944 | return input; |
| 945 | } |
| 946 | |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 947 | if (_PyUnicode_EqualToASCIIId(form, &PyId_NFC)) { |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 948 | if (is_normalized_quickcheck(self, input, |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 949 | true, false, true) == YES) { |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 950 | Py_INCREF(input); |
| 951 | return input; |
| 952 | } |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 953 | return nfc_nfkc(self, input, 0); |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 954 | } |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 955 | if (_PyUnicode_EqualToASCIIId(form, &PyId_NFKC)) { |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 956 | if (is_normalized_quickcheck(self, input, |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 957 | true, true, true) == YES) { |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 958 | Py_INCREF(input); |
| 959 | return input; |
| 960 | } |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 961 | return nfc_nfkc(self, input, 1); |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 962 | } |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 963 | if (_PyUnicode_EqualToASCIIId(form, &PyId_NFD)) { |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 964 | if (is_normalized_quickcheck(self, input, |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 965 | false, false, true) == YES) { |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 966 | Py_INCREF(input); |
| 967 | return input; |
| 968 | } |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 969 | return nfd_nfkd(self, input, 0); |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 970 | } |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 971 | if (_PyUnicode_EqualToASCIIId(form, &PyId_NFKD)) { |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 972 | if (is_normalized_quickcheck(self, input, |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 973 | false, true, true) == YES) { |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 974 | Py_INCREF(input); |
| 975 | return input; |
| 976 | } |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 977 | return nfd_nfkd(self, input, 1); |
Antoine Pitrou | 7a0fedf | 2009-04-27 22:31:40 +0000 | [diff] [blame] | 978 | } |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 979 | PyErr_SetString(PyExc_ValueError, "invalid normalization form"); |
| 980 | return NULL; |
| 981 | } |
| 982 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 983 | /* -------------------------------------------------------------------- */ |
| 984 | /* unicode character name tables */ |
| 985 | |
| 986 | /* data file generated by Tools/unicode/makeunicodedata.py */ |
| 987 | #include "unicodename_db.h" |
| 988 | |
| 989 | /* -------------------------------------------------------------------- */ |
| 990 | /* database code (cut and pasted from the unidb package) */ |
| 991 | |
| 992 | static unsigned long |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 993 | _gethash(const char *s, int len, int scale) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 994 | { |
| 995 | int i; |
| 996 | unsigned long h = 0; |
| 997 | unsigned long ix; |
| 998 | for (i = 0; i < len; i++) { |
Jordon Xu | 2ec7010 | 2019-09-11 00:04:08 +0800 | [diff] [blame] | 999 | h = (h * scale) + (unsigned char) Py_TOUPPER(s[i]); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1000 | ix = h & 0xff000000; |
| 1001 | if (ix) |
| 1002 | h = (h ^ ((ix>>24) & 0xff)) & 0x00ffffff; |
| 1003 | } |
| 1004 | return h; |
| 1005 | } |
| 1006 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1007 | static const char * const hangul_syllables[][3] = { |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 1008 | { "G", "A", "" }, |
| 1009 | { "GG", "AE", "G" }, |
| 1010 | { "N", "YA", "GG" }, |
| 1011 | { "D", "YAE", "GS" }, |
| 1012 | { "DD", "EO", "N", }, |
| 1013 | { "R", "E", "NJ" }, |
| 1014 | { "M", "YEO", "NH" }, |
| 1015 | { "B", "YE", "D" }, |
| 1016 | { "BB", "O", "L" }, |
| 1017 | { "S", "WA", "LG" }, |
| 1018 | { "SS", "WAE", "LM" }, |
| 1019 | { "", "OE", "LB" }, |
| 1020 | { "J", "YO", "LS" }, |
| 1021 | { "JJ", "U", "LT" }, |
| 1022 | { "C", "WEO", "LP" }, |
| 1023 | { "K", "WE", "LH" }, |
| 1024 | { "T", "WI", "M" }, |
| 1025 | { "P", "YU", "B" }, |
| 1026 | { "H", "EU", "BS" }, |
| 1027 | { 0, "YI", "S" }, |
| 1028 | { 0, "I", "SS" }, |
| 1029 | { 0, 0, "NG" }, |
| 1030 | { 0, 0, "J" }, |
| 1031 | { 0, 0, "C" }, |
| 1032 | { 0, 0, "K" }, |
| 1033 | { 0, 0, "T" }, |
| 1034 | { 0, 0, "P" }, |
| 1035 | { 0, 0, "H" } |
| 1036 | }; |
| 1037 | |
Martin v. Löwis | 5cbc71e | 2010-11-22 09:00:02 +0000 | [diff] [blame] | 1038 | /* These ranges need to match makeunicodedata.py:cjk_ranges. */ |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1039 | static int |
Martin v. Löwis | 8d93ca1 | 2002-11-23 22:10:29 +0000 | [diff] [blame] | 1040 | is_unified_ideograph(Py_UCS4 code) |
| 1041 | { |
Martin v. Löwis | 5cbc71e | 2010-11-22 09:00:02 +0000 | [diff] [blame] | 1042 | return |
Benjamin Peterson | 051b9d0 | 2020-03-10 20:41:34 -0700 | [diff] [blame] | 1043 | (0x3400 <= code && code <= 0x4DBF) || /* CJK Ideograph Extension A */ |
| 1044 | (0x4E00 <= code && code <= 0x9FFC) || /* CJK Ideograph */ |
| 1045 | (0x20000 <= code && code <= 0x2A6DD) || /* CJK Ideograph Extension B */ |
Martin v. Löwis | 5cbc71e | 2010-11-22 09:00:02 +0000 | [diff] [blame] | 1046 | (0x2A700 <= code && code <= 0x2B734) || /* CJK Ideograph Extension C */ |
Benjamin Peterson | 4801383 | 2015-06-27 15:45:56 -0500 | [diff] [blame] | 1047 | (0x2B740 <= code && code <= 0x2B81D) || /* CJK Ideograph Extension D */ |
Benjamin Peterson | 279a962 | 2017-06-22 22:31:08 -0700 | [diff] [blame] | 1048 | (0x2B820 <= code && code <= 0x2CEA1) || /* CJK Ideograph Extension E */ |
Benjamin Peterson | 051b9d0 | 2020-03-10 20:41:34 -0700 | [diff] [blame] | 1049 | (0x2CEB0 <= code && code <= 0x2EBE0) || /* CJK Ideograph Extension F */ |
| 1050 | (0x30000 <= code && code <= 0x3134A); /* CJK Ideograph Extension G */ |
Martin v. Löwis | 8d93ca1 | 2002-11-23 22:10:29 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 1053 | /* macros used to determine if the given code point is in the PUA range that |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1054 | * we are using to store aliases and named sequences */ |
| 1055 | #define IS_ALIAS(cp) ((cp >= aliases_start) && (cp < aliases_end)) |
| 1056 | #define IS_NAMED_SEQ(cp) ((cp >= named_sequences_start) && \ |
| 1057 | (cp < named_sequences_end)) |
| 1058 | |
Martin v. Löwis | 8d93ca1 | 2002-11-23 22:10:29 +0000 | [diff] [blame] | 1059 | static int |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1060 | _getucname(PyObject *self, |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 1061 | Py_UCS4 code, char* buffer, int buflen, int with_alias_and_seq) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1062 | { |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 1063 | /* Find the name associated with the given code point. |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1064 | * If with_alias_and_seq is 1, check for names in the Private Use Area 15 |
| 1065 | * that we are using for aliases and named sequences. */ |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1066 | int offset; |
| 1067 | int i; |
| 1068 | int word; |
Inada Naoki | 6fec905 | 2019-04-17 08:40:34 +0900 | [diff] [blame] | 1069 | const unsigned char* w; |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1070 | |
Martin v. Löwis | c350912 | 2006-03-11 12:16:23 +0000 | [diff] [blame] | 1071 | if (code >= 0x110000) |
| 1072 | return 0; |
| 1073 | |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 1074 | /* XXX should we just skip all the code points in the PUAs here? */ |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1075 | if (!with_alias_and_seq && (IS_ALIAS(code) || IS_NAMED_SEQ(code))) |
| 1076 | return 0; |
| 1077 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1078 | if (UCD_Check(self)) { |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1079 | /* in 3.2.0 there are no aliases and named sequences */ |
Ezio Melotti | 4837e39 | 2011-10-22 00:24:17 +0300 | [diff] [blame] | 1080 | const change_record *old; |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1081 | if (IS_ALIAS(code) || IS_NAMED_SEQ(code)) |
| 1082 | return 0; |
Ezio Melotti | 4837e39 | 2011-10-22 00:24:17 +0300 | [diff] [blame] | 1083 | old = get_old_record(self, code); |
Martin v. Löwis | c350912 | 2006-03-11 12:16:23 +0000 | [diff] [blame] | 1084 | if (old->category_changed == 0) { |
| 1085 | /* unassigned */ |
| 1086 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | } |
Martin v. Löwis | c350912 | 2006-03-11 12:16:23 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Martin v. Löwis | 2f4be4e | 2002-11-23 17:11:06 +0000 | [diff] [blame] | 1090 | if (SBase <= code && code < SBase+SCount) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | /* Hangul syllable. */ |
| 1092 | int SIndex = code - SBase; |
| 1093 | int L = SIndex / NCount; |
| 1094 | int V = (SIndex % NCount) / TCount; |
| 1095 | int T = SIndex % TCount; |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 1096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | if (buflen < 27) |
| 1098 | /* Worst case: HANGUL SYLLABLE <10chars>. */ |
| 1099 | return 0; |
| 1100 | strcpy(buffer, "HANGUL SYLLABLE "); |
| 1101 | buffer += 16; |
| 1102 | strcpy(buffer, hangul_syllables[L][0]); |
| 1103 | buffer += strlen(hangul_syllables[L][0]); |
| 1104 | strcpy(buffer, hangul_syllables[V][1]); |
| 1105 | buffer += strlen(hangul_syllables[V][1]); |
| 1106 | strcpy(buffer, hangul_syllables[T][2]); |
| 1107 | buffer += strlen(hangul_syllables[T][2]); |
| 1108 | *buffer = '\0'; |
| 1109 | return 1; |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Martin v. Löwis | 8d93ca1 | 2002-11-23 22:10:29 +0000 | [diff] [blame] | 1112 | if (is_unified_ideograph(code)) { |
Martin v. Löwis | ef7fe2e | 2002-11-23 18:01:32 +0000 | [diff] [blame] | 1113 | if (buflen < 28) |
| 1114 | /* Worst case: CJK UNIFIED IDEOGRAPH-20000 */ |
| 1115 | return 0; |
| 1116 | sprintf(buffer, "CJK UNIFIED IDEOGRAPH-%X", code); |
| 1117 | return 1; |
| 1118 | } |
| 1119 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1120 | /* get offset into phrasebook */ |
| 1121 | offset = phrasebook_offset1[(code>>phrasebook_shift)]; |
| 1122 | offset = phrasebook_offset2[(offset<<phrasebook_shift) + |
| 1123 | (code&((1<<phrasebook_shift)-1))]; |
| 1124 | if (!offset) |
| 1125 | return 0; |
| 1126 | |
| 1127 | i = 0; |
| 1128 | |
| 1129 | for (;;) { |
| 1130 | /* get word index */ |
| 1131 | word = phrasebook[offset] - phrasebook_short; |
| 1132 | if (word >= 0) { |
| 1133 | word = (word << 8) + phrasebook[offset+1]; |
| 1134 | offset += 2; |
| 1135 | } else |
| 1136 | word = phrasebook[offset++]; |
| 1137 | if (i) { |
| 1138 | if (i > buflen) |
| 1139 | return 0; /* buffer overflow */ |
| 1140 | buffer[i++] = ' '; |
| 1141 | } |
| 1142 | /* copy word string from lexicon. the last character in the |
| 1143 | word has bit 7 set. the last word in a string ends with |
| 1144 | 0x80 */ |
| 1145 | w = lexicon + lexicon_offset[word]; |
| 1146 | while (*w < 128) { |
| 1147 | if (i >= buflen) |
| 1148 | return 0; /* buffer overflow */ |
| 1149 | buffer[i++] = *w++; |
| 1150 | } |
| 1151 | if (i >= buflen) |
| 1152 | return 0; /* buffer overflow */ |
| 1153 | buffer[i++] = *w & 127; |
| 1154 | if (*w == 128) |
| 1155 | break; /* end of word */ |
| 1156 | } |
| 1157 | |
| 1158 | return 1; |
| 1159 | } |
| 1160 | |
| 1161 | static int |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1162 | capi_getucname(Py_UCS4 code, |
Victor Stinner | 47e1afd | 2020-10-26 16:43:47 +0100 | [diff] [blame] | 1163 | char* buffer, int buflen, |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 1164 | int with_alias_and_seq) |
| 1165 | { |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1166 | return _getucname(NULL, code, buffer, buflen, with_alias_and_seq); |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 1167 | |
| 1168 | } |
| 1169 | |
| 1170 | static int |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1171 | _cmpname(PyObject *self, int code, const char* name, int namelen) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1172 | { |
| 1173 | /* check if code corresponds to the given name */ |
| 1174 | int i; |
Christian Heimes | 2f366ca | 2016-09-23 20:20:27 +0200 | [diff] [blame] | 1175 | char buffer[NAME_MAXLEN+1]; |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1176 | if (!_getucname(self, code, buffer, NAME_MAXLEN, 1)) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1177 | return 0; |
| 1178 | for (i = 0; i < namelen; i++) { |
Jordon Xu | 2ec7010 | 2019-09-11 00:04:08 +0800 | [diff] [blame] | 1179 | if (Py_TOUPPER(name[i]) != buffer[i]) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1180 | return 0; |
| 1181 | } |
| 1182 | return buffer[namelen] == '\0'; |
| 1183 | } |
| 1184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | static void |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 1186 | find_syllable(const char *str, int *len, int *pos, int count, int column) |
| 1187 | { |
| 1188 | int i, len1; |
| 1189 | *len = -1; |
| 1190 | for (i = 0; i < count; i++) { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1191 | const char *s = hangul_syllables[i][column]; |
Antoine Pitrou | 1d4bd25 | 2011-10-06 15:44:15 +0200 | [diff] [blame] | 1192 | len1 = Py_SAFE_DOWNCAST(strlen(s), size_t, int); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | if (len1 <= *len) |
| 1194 | continue; |
| 1195 | if (strncmp(str, s, len1) == 0) { |
| 1196 | *len = len1; |
| 1197 | *pos = i; |
| 1198 | } |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 1199 | } |
| 1200 | if (*len == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | *len = 0; |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1205 | static int |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1206 | _check_alias_and_seq(unsigned int cp, Py_UCS4* code, int with_named_seq) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1207 | { |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1208 | /* check if named sequences are allowed */ |
| 1209 | if (!with_named_seq && IS_NAMED_SEQ(cp)) |
| 1210 | return 0; |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 1211 | /* if the code point is in the PUA range that we use for aliases, |
| 1212 | * convert it to obtain the right code point */ |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1213 | if (IS_ALIAS(cp)) |
| 1214 | *code = name_aliases[cp-aliases_start]; |
| 1215 | else |
| 1216 | *code = cp; |
| 1217 | return 1; |
| 1218 | } |
| 1219 | |
| 1220 | static int |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1221 | _getcode(PyObject* self, |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 1222 | const char* name, int namelen, Py_UCS4* code, int with_named_seq) |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1223 | { |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 1224 | /* Return the code point associated with the given name. |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1225 | * Named aliases are resolved too (unless self != NULL (i.e. we are using |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 1226 | * 3.2.0)). If with_named_seq is 1, returns the PUA code point that we are |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1227 | * using for the named sequence, and the caller must then convert it. */ |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1228 | unsigned int h, v; |
| 1229 | unsigned int mask = code_size-1; |
| 1230 | unsigned int i, incr; |
| 1231 | |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 1232 | /* Check for hangul syllables. */ |
| 1233 | if (strncmp(name, "HANGUL SYLLABLE ", 16) == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | int len, L = -1, V = -1, T = -1; |
| 1235 | const char *pos = name + 16; |
| 1236 | find_syllable(pos, &len, &L, LCount, 0); |
| 1237 | pos += len; |
| 1238 | find_syllable(pos, &len, &V, VCount, 1); |
| 1239 | pos += len; |
| 1240 | find_syllable(pos, &len, &T, TCount, 2); |
| 1241 | pos += len; |
| 1242 | if (L != -1 && V != -1 && T != -1 && pos-name == namelen) { |
| 1243 | *code = SBase + (L*VCount+V)*TCount + T; |
| 1244 | return 1; |
| 1245 | } |
Martin v. Löwis | ef7fe2e | 2002-11-23 18:01:32 +0000 | [diff] [blame] | 1246 | /* Otherwise, it's an illegal syllable name. */ |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | /* Check for unified ideographs. */ |
| 1251 | if (strncmp(name, "CJK UNIFIED IDEOGRAPH-", 22) == 0) { |
| 1252 | /* Four or five hexdigits must follow. */ |
| 1253 | v = 0; |
| 1254 | name += 22; |
| 1255 | namelen -= 22; |
| 1256 | if (namelen != 4 && namelen != 5) |
| 1257 | return 0; |
| 1258 | while (namelen--) { |
| 1259 | v *= 16; |
| 1260 | if (*name >= '0' && *name <= '9') |
| 1261 | v += *name - '0'; |
| 1262 | else if (*name >= 'A' && *name <= 'F') |
| 1263 | v += *name - 'A' + 10; |
| 1264 | else |
| 1265 | return 0; |
| 1266 | name++; |
| 1267 | } |
Martin v. Löwis | 8d93ca1 | 2002-11-23 22:10:29 +0000 | [diff] [blame] | 1268 | if (!is_unified_ideograph(v)) |
| 1269 | return 0; |
Martin v. Löwis | ef7fe2e | 2002-11-23 18:01:32 +0000 | [diff] [blame] | 1270 | *code = v; |
| 1271 | return 1; |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1274 | /* the following is the same as python's dictionary lookup, with |
| 1275 | only minor changes. see the makeunicodedata script for more |
| 1276 | details */ |
| 1277 | |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 1278 | h = (unsigned int) _gethash(name, namelen, code_magic); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1279 | i = (~h) & mask; |
| 1280 | v = code_hash[i]; |
| 1281 | if (!v) |
| 1282 | return 0; |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1283 | if (_cmpname(self, v, name, namelen)) { |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1284 | return _check_alias_and_seq(v, code, with_named_seq); |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 1285 | } |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1286 | incr = (h ^ (h >> 3)) & mask; |
| 1287 | if (!incr) |
| 1288 | incr = mask; |
| 1289 | for (;;) { |
| 1290 | i = (i + incr) & mask; |
| 1291 | v = code_hash[i]; |
| 1292 | if (!v) |
Fredrik Lundh | ae76367 | 2001-02-18 11:41:49 +0000 | [diff] [blame] | 1293 | return 0; |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1294 | if (_cmpname(self, v, name, namelen)) { |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1295 | return _check_alias_and_seq(v, code, with_named_seq); |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 1296 | } |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1297 | incr = incr << 1; |
| 1298 | if (incr > mask) |
| 1299 | incr = incr ^ code_poly; |
| 1300 | } |
| 1301 | } |
| 1302 | |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 1303 | static int |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1304 | capi_getcode(const char* name, int namelen, Py_UCS4* code, |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 1305 | int with_named_seq) |
| 1306 | { |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1307 | return _getcode(NULL, name, namelen, code, with_named_seq); |
Victor Stinner | e6b8c52 | 2020-10-15 16:22:19 +0200 | [diff] [blame] | 1308 | |
| 1309 | } |
| 1310 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1311 | static const _PyUnicode_Name_CAPI unicodedata_capi = |
| 1312 | { |
| 1313 | .getname = capi_getucname, |
| 1314 | .getcode = capi_getcode, |
| 1315 | }; |
| 1316 | |
| 1317 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1318 | /* -------------------------------------------------------------------- */ |
| 1319 | /* Python bindings */ |
| 1320 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 1321 | /*[clinic input] |
| 1322 | unicodedata.UCD.name |
| 1323 | |
| 1324 | self: self |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 1325 | chr: int(accept={str}) |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 1326 | default: object=NULL |
| 1327 | / |
| 1328 | |
| 1329 | Returns the name assigned to the character chr as a string. |
| 1330 | |
| 1331 | If no name is defined, default is returned, or, if not given, |
| 1332 | ValueError is raised. |
| 1333 | [clinic start generated code]*/ |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 1334 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1335 | static PyObject * |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 1336 | unicodedata_UCD_name_impl(PyObject *self, int chr, PyObject *default_value) |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 1337 | /*[clinic end generated code: output=6bbb37a326407707 input=3e0367f534de56d9]*/ |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1338 | { |
Christian Heimes | 2f366ca | 2016-09-23 20:20:27 +0200 | [diff] [blame] | 1339 | char name[NAME_MAXLEN+1]; |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 1340 | Py_UCS4 c = (Py_UCS4)chr; |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1341 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1342 | if (!_getucname(self, c, name, NAME_MAXLEN, 0)) { |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 1343 | if (default_value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1344 | PyErr_SetString(PyExc_ValueError, "no such name"); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1345 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | } |
| 1347 | else { |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 1348 | Py_INCREF(default_value); |
| 1349 | return default_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | } |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Walter Dörwald | 4254e76 | 2007-06-05 16:04:09 +0000 | [diff] [blame] | 1353 | return PyUnicode_FromString(name); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 1356 | /*[clinic input] |
| 1357 | unicodedata.UCD.lookup |
| 1358 | |
| 1359 | self: self |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 1360 | name: str(accept={str, robuffer}, zeroes=True) |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 1361 | / |
| 1362 | |
| 1363 | Look up character by name. |
| 1364 | |
| 1365 | If a character with the given name is found, return the |
| 1366 | corresponding character. If not found, KeyError is raised. |
| 1367 | [clinic start generated code]*/ |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 1368 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1369 | static PyObject * |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 1370 | unicodedata_UCD_lookup_impl(PyObject *self, const char *name, |
| 1371 | Py_ssize_clean_t name_length) |
Larry Hastings | 38337d1 | 2015-05-07 23:30:09 -0700 | [diff] [blame] | 1372 | /*[clinic end generated code: output=765cb8186788e6be input=a557be0f8607a0d6]*/ |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1373 | { |
| 1374 | Py_UCS4 code; |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1375 | unsigned int index; |
Christian Heimes | 7ce2013 | 2016-09-14 10:25:46 +0200 | [diff] [blame] | 1376 | if (name_length > NAME_MAXLEN) { |
Victor Stinner | 65a3144 | 2014-07-01 16:45:52 +0200 | [diff] [blame] | 1377 | PyErr_SetString(PyExc_KeyError, "name too long"); |
| 1378 | return NULL; |
| 1379 | } |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1380 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1381 | if (!_getcode(self, name, (int)name_length, &code, 1)) { |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1382 | PyErr_Format(PyExc_KeyError, "undefined character name '%s'", name); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1383 | return NULL; |
| 1384 | } |
Stefan Krah | a4b4dea | 2012-09-23 15:51:16 +0200 | [diff] [blame] | 1385 | /* check if code is in the PUA range that we use for named sequences |
| 1386 | and convert it */ |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 1387 | if (IS_NAMED_SEQ(code)) { |
| 1388 | index = code-named_sequences_start; |
| 1389 | return PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, |
| 1390 | named_sequences[index].seq, |
| 1391 | named_sequences[index].seqlen); |
| 1392 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1393 | return PyUnicode_FromOrdinal(code); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1396 | // List of functions used to define module functions *AND* unicodedata.UCD |
| 1397 | // methods. For module functions, self is the module. For UCD methods, self |
| 1398 | // is an UCD instance. The UCD_Check() macro is used to check if self is |
| 1399 | // an UCD instance. |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 1400 | static PyMethodDef unicodedata_functions[] = { |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 1401 | UNICODEDATA_UCD_DECIMAL_METHODDEF |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 1402 | UNICODEDATA_UCD_DIGIT_METHODDEF |
| 1403 | UNICODEDATA_UCD_NUMERIC_METHODDEF |
| 1404 | UNICODEDATA_UCD_CATEGORY_METHODDEF |
| 1405 | UNICODEDATA_UCD_BIDIRECTIONAL_METHODDEF |
| 1406 | UNICODEDATA_UCD_COMBINING_METHODDEF |
| 1407 | UNICODEDATA_UCD_MIRRORED_METHODDEF |
| 1408 | UNICODEDATA_UCD_EAST_ASIAN_WIDTH_METHODDEF |
| 1409 | UNICODEDATA_UCD_DECOMPOSITION_METHODDEF |
| 1410 | UNICODEDATA_UCD_NAME_METHODDEF |
| 1411 | UNICODEDATA_UCD_LOOKUP_METHODDEF |
Max Bélanger | 2810dd7 | 2018-11-04 15:58:24 -0800 | [diff] [blame] | 1412 | UNICODEDATA_UCD_IS_NORMALIZED_METHODDEF |
Serhiy Storchaka | 6359641 | 2015-04-17 21:18:49 +0300 | [diff] [blame] | 1413 | UNICODEDATA_UCD_NORMALIZE_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 1415 | }; |
| 1416 | |
Victor Stinner | c8c4200 | 2020-10-26 23:19:22 +0100 | [diff] [blame] | 1417 | static void |
| 1418 | ucd_dealloc(PreviousDBVersion *self) |
| 1419 | { |
| 1420 | PyTypeObject *tp = Py_TYPE(self); |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 1421 | PyObject_Free(self); |
Victor Stinner | c8c4200 | 2020-10-26 23:19:22 +0100 | [diff] [blame] | 1422 | Py_DECREF(tp); |
| 1423 | } |
| 1424 | |
| 1425 | static PyType_Slot ucd_type_slots[] = { |
| 1426 | {Py_tp_dealloc, ucd_dealloc}, |
| 1427 | {Py_tp_getattro, PyObject_GenericGetAttr}, |
| 1428 | {Py_tp_methods, unicodedata_functions}, |
| 1429 | {Py_tp_members, DB_members}, |
| 1430 | {0, 0} |
| 1431 | }; |
| 1432 | |
| 1433 | static PyType_Spec ucd_type_spec = { |
| 1434 | .name = "unicodedata.UCD", |
| 1435 | .basicsize = sizeof(PreviousDBVersion), |
| 1436 | .flags = Py_TPFLAGS_DEFAULT, |
| 1437 | .slots = ucd_type_slots |
Martin v. Löwis | 5bd7c02 | 2006-03-10 11:20:04 +0000 | [diff] [blame] | 1438 | }; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 1439 | |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 1440 | PyDoc_STRVAR(unicodedata_docstring, |
| 1441 | "This module provides access to the Unicode Character Database which\n\ |
| 1442 | defines character properties for all Unicode characters. The data in\n\ |
| 1443 | this database is based on the UnicodeData.txt file version\n\ |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 1444 | " UNIDATA_VERSION " which is publicly available from ftp://ftp.unicode.org/.\n\ |
Hye-Shik Chang | cf18a5d | 2005-04-04 16:32:07 +0000 | [diff] [blame] | 1445 | \n\ |
| 1446 | The module uses the same names and symbols as defined by the\n\ |
Benjamin Peterson | 577dd61 | 2013-10-10 20:16:25 -0400 | [diff] [blame] | 1447 | UnicodeData File Format " UNIDATA_VERSION "."); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1448 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1449 | static int |
| 1450 | unicodedata_exec(PyObject *module) |
| 1451 | { |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1452 | if (PyModule_AddStringConstant(module, "unidata_version", UNIDATA_VERSION) < 0) { |
| 1453 | return -1; |
| 1454 | } |
| 1455 | |
Victor Stinner | c8c4200 | 2020-10-26 23:19:22 +0100 | [diff] [blame] | 1456 | PyTypeObject *ucd_type = (PyTypeObject *)PyType_FromSpec(&ucd_type_spec); |
| 1457 | if (ucd_type == NULL) { |
| 1458 | return -1; |
| 1459 | } |
| 1460 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1461 | if (PyModule_AddType(module, ucd_type) < 0) { |
Victor Stinner | c8c4200 | 2020-10-26 23:19:22 +0100 | [diff] [blame] | 1462 | Py_DECREF(ucd_type); |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1463 | return -1; |
| 1464 | } |
| 1465 | |
Victor Stinner | 84f7382 | 2020-10-27 04:36:22 +0100 | [diff] [blame] | 1466 | // Unicode database version 3.2.0 used by the IDNA encoding |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1467 | PyObject *v; |
| 1468 | v = new_previous_version(ucd_type, "3.2.0", |
| 1469 | get_change_3_2_0, normalization_3_2_0); |
Victor Stinner | c8c4200 | 2020-10-26 23:19:22 +0100 | [diff] [blame] | 1470 | Py_DECREF(ucd_type); |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1471 | if (v == NULL) { |
| 1472 | return -1; |
| 1473 | } |
| 1474 | if (PyModule_AddObject(module, "ucd_3_2_0", v) < 0) { |
| 1475 | Py_DECREF(v); |
| 1476 | return -1; |
| 1477 | } |
| 1478 | |
| 1479 | /* Export C API */ |
| 1480 | v = PyCapsule_New((void *)&unicodedata_capi, PyUnicodeData_CAPSULE_NAME, |
| 1481 | NULL); |
| 1482 | if (v == NULL) { |
| 1483 | return -1; |
| 1484 | } |
Victor Stinner | 84f7382 | 2020-10-27 04:36:22 +0100 | [diff] [blame] | 1485 | if (PyModule_AddObject(module, "_ucnhash_CAPI", v) < 0) { |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1486 | Py_DECREF(v); |
| 1487 | return -1; |
| 1488 | } |
| 1489 | return 0; |
| 1490 | } |
| 1491 | |
Victor Stinner | c8c4200 | 2020-10-26 23:19:22 +0100 | [diff] [blame] | 1492 | static PyModuleDef_Slot unicodedata_slots[] = { |
| 1493 | {Py_mod_exec, unicodedata_exec}, |
| 1494 | {0, NULL} |
| 1495 | }; |
| 1496 | |
| 1497 | static struct PyModuleDef unicodedata_module = { |
| 1498 | PyModuleDef_HEAD_INIT, |
| 1499 | .m_name = "unicodedata", |
| 1500 | .m_doc = unicodedata_docstring, |
| 1501 | .m_size = 0, |
| 1502 | .m_methods = unicodedata_functions, |
| 1503 | .m_slots = unicodedata_slots, |
| 1504 | }; |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1505 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1506 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1507 | PyInit_unicodedata(void) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 1508 | { |
Victor Stinner | c8c4200 | 2020-10-26 23:19:22 +0100 | [diff] [blame] | 1509 | return PyModuleDef_Init(&unicodedata_module); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 1510 | } |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 1511 | |
Victor Stinner | 920cb64 | 2020-10-26 19:19:36 +0100 | [diff] [blame] | 1512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | /* |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 1514 | Local variables: |
| 1515 | c-basic-offset: 4 |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 1516 | indent-tabs-mode: nil |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 1517 | End: |
| 1518 | */ |