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