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