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