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