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