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