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