Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 1 | /* ------------------------------------------------------------------------ |
| 2 | |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 3 | unicodedata -- Provides access to the Unicode 3.2 data base. |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 4 | |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 5 | Data was extracted from the Unicode 3.2 UnicodeData.txt file. |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 6 | |
Fredrik Lundh | cfcea49 | 2000-09-25 08:07:06 +0000 | [diff] [blame] | 7 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 8 | Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com) |
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" |
| 17 | |
| 18 | /* character properties */ |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 19 | |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 20 | typedef struct { |
| 21 | const unsigned char category; /* index into |
| 22 | _PyUnicode_CategoryNames */ |
| 23 | const unsigned char combining; /* combining class value 0 - 255 */ |
| 24 | const unsigned char bidirectional; /* index into |
| 25 | _PyUnicode_BidirectionalNames */ |
| 26 | const unsigned char mirrored; /* true if mirrored in bidir mode */ |
| 27 | } _PyUnicode_DatabaseRecord; |
| 28 | |
| 29 | /* data file generated by Tools/unicode/makeunicodedata.py */ |
| 30 | #include "unicodedata_db.h" |
| 31 | |
| 32 | static const _PyUnicode_DatabaseRecord* |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 33 | _getrecord_ex(Py_UCS4 code) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 34 | { |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 35 | int index; |
Neal Norwitz | e9c571f | 2003-02-28 03:14:37 +0000 | [diff] [blame] | 36 | if (code >= 0x110000) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 37 | index = 0; |
| 38 | else { |
| 39 | index = index1[(code>>SHIFT)]; |
| 40 | index = index2[(index<<SHIFT)+(code&((1<<SHIFT)-1))]; |
| 41 | } |
| 42 | |
| 43 | return &_PyUnicode_Database_Records[index]; |
| 44 | } |
| 45 | |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 46 | static const _PyUnicode_DatabaseRecord* |
| 47 | _getrecord(PyUnicodeObject* v) |
| 48 | { |
| 49 | return _getrecord_ex(*PyUnicode_AS_UNICODE(v)); |
| 50 | } |
| 51 | |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 52 | /* --- Module API --------------------------------------------------------- */ |
| 53 | |
| 54 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 55 | unicodedata_decimal(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 56 | { |
| 57 | PyUnicodeObject *v; |
| 58 | PyObject *defobj = NULL; |
| 59 | long rc; |
| 60 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 61 | if (!PyArg_ParseTuple(args, "O!|O:decimal", &PyUnicode_Type, &v, &defobj)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 62 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 63 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 64 | PyErr_SetString(PyExc_TypeError, |
| 65 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 66 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 67 | } |
| 68 | rc = Py_UNICODE_TODECIMAL(*PyUnicode_AS_UNICODE(v)); |
| 69 | if (rc < 0) { |
| 70 | if (defobj == NULL) { |
| 71 | PyErr_SetString(PyExc_ValueError, |
| 72 | "not a decimal"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 73 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 74 | } |
| 75 | else { |
| 76 | Py_INCREF(defobj); |
| 77 | return defobj; |
| 78 | } |
| 79 | } |
| 80 | return PyInt_FromLong(rc); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 84 | unicodedata_digit(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 85 | { |
| 86 | PyUnicodeObject *v; |
| 87 | PyObject *defobj = NULL; |
| 88 | long rc; |
| 89 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 90 | if (!PyArg_ParseTuple(args, "O!|O:digit", &PyUnicode_Type, &v, &defobj)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 91 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 92 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 93 | PyErr_SetString(PyExc_TypeError, |
| 94 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 95 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 96 | } |
| 97 | rc = Py_UNICODE_TODIGIT(*PyUnicode_AS_UNICODE(v)); |
| 98 | if (rc < 0) { |
| 99 | if (defobj == NULL) { |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 100 | PyErr_SetString(PyExc_ValueError, "not a digit"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 101 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 102 | } |
| 103 | else { |
| 104 | Py_INCREF(defobj); |
| 105 | return defobj; |
| 106 | } |
| 107 | } |
| 108 | return PyInt_FromLong(rc); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 112 | unicodedata_numeric(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 113 | { |
| 114 | PyUnicodeObject *v; |
| 115 | PyObject *defobj = NULL; |
| 116 | double rc; |
| 117 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 118 | if (!PyArg_ParseTuple(args, "O!|O:numeric", &PyUnicode_Type, &v, &defobj)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 119 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 120 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 121 | PyErr_SetString(PyExc_TypeError, |
| 122 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 123 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 124 | } |
| 125 | rc = Py_UNICODE_TONUMERIC(*PyUnicode_AS_UNICODE(v)); |
| 126 | if (rc < 0) { |
| 127 | if (defobj == NULL) { |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 128 | PyErr_SetString(PyExc_ValueError, "not a numeric character"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 129 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 130 | } |
| 131 | else { |
| 132 | Py_INCREF(defobj); |
| 133 | return defobj; |
| 134 | } |
| 135 | } |
| 136 | return PyFloat_FromDouble(rc); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 140 | unicodedata_category(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 141 | { |
| 142 | PyUnicodeObject *v; |
| 143 | int index; |
| 144 | |
| 145 | if (!PyArg_ParseTuple(args, "O!:category", |
| 146 | &PyUnicode_Type, &v)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 147 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 148 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 149 | PyErr_SetString(PyExc_TypeError, |
| 150 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 151 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 152 | } |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 153 | index = (int) _getrecord(v)->category; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 154 | return PyString_FromString(_PyUnicode_CategoryNames[index]); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 158 | unicodedata_bidirectional(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 159 | { |
| 160 | PyUnicodeObject *v; |
| 161 | int index; |
| 162 | |
| 163 | if (!PyArg_ParseTuple(args, "O!:bidirectional", |
| 164 | &PyUnicode_Type, &v)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 165 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 166 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 167 | PyErr_SetString(PyExc_TypeError, |
| 168 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 169 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 170 | } |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 171 | index = (int) _getrecord(v)->bidirectional; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 172 | return PyString_FromString(_PyUnicode_BidirectionalNames[index]); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 176 | unicodedata_combining(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 177 | { |
| 178 | PyUnicodeObject *v; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 179 | |
| 180 | if (!PyArg_ParseTuple(args, "O!:combining", |
| 181 | &PyUnicode_Type, &v)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 182 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 183 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 184 | PyErr_SetString(PyExc_TypeError, |
| 185 | "need a single Unicode character as parameter"); |
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 | } |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 188 | return PyInt_FromLong((int) _getrecord(v)->combining); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 192 | unicodedata_mirrored(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 193 | { |
| 194 | PyUnicodeObject *v; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 195 | |
| 196 | if (!PyArg_ParseTuple(args, "O!:mirrored", |
| 197 | &PyUnicode_Type, &v)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 198 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 199 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 200 | PyErr_SetString(PyExc_TypeError, |
| 201 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 202 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 203 | } |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 204 | return PyInt_FromLong((int) _getrecord(v)->mirrored); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 208 | unicodedata_decomposition(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 209 | { |
| 210 | PyUnicodeObject *v; |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 211 | char decomp[256]; |
| 212 | int code, index, count, i; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 213 | |
| 214 | if (!PyArg_ParseTuple(args, "O!:decomposition", |
| 215 | &PyUnicode_Type, &v)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 216 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 217 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 218 | PyErr_SetString(PyExc_TypeError, |
| 219 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 220 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 221 | } |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 222 | |
| 223 | code = (int) *PyUnicode_AS_UNICODE(v); |
| 224 | |
Martin v. Löwis | 9def6a3 | 2002-10-18 16:11:54 +0000 | [diff] [blame] | 225 | if (code < 0 || code >= 0x110000) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 226 | index = 0; |
| 227 | else { |
| 228 | index = decomp_index1[(code>>DECOMP_SHIFT)]; |
| 229 | index = decomp_index2[(index<<DECOMP_SHIFT)+ |
| 230 | (code&((1<<DECOMP_SHIFT)-1))]; |
| 231 | } |
| 232 | |
Tim Peters | 69b83b1 | 2001-11-30 07:23:05 +0000 | [diff] [blame] | 233 | /* 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] | 234 | is prefix code (from*/ |
| 235 | count = decomp_data[index] >> 8; |
| 236 | |
| 237 | /* XXX: could allocate the PyString up front instead |
| 238 | (strlen(prefix) + 5 * count + 1 bytes) */ |
| 239 | |
| 240 | /* copy prefix */ |
| 241 | i = strlen(decomp_prefix[decomp_data[index] & 255]); |
| 242 | memcpy(decomp, decomp_prefix[decomp_data[index] & 255], i); |
| 243 | |
| 244 | while (count-- > 0) { |
| 245 | if (i) |
| 246 | decomp[i++] = ' '; |
Tim Peters | 69b83b1 | 2001-11-30 07:23:05 +0000 | [diff] [blame] | 247 | assert((size_t)i < sizeof(decomp)); |
| 248 | PyOS_snprintf(decomp + i, sizeof(decomp) - i, "%04X", |
| 249 | decomp_data[++index]); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 250 | i += strlen(decomp + i); |
| 251 | } |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 252 | |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 253 | decomp[i] = '\0'; |
| 254 | |
| 255 | return PyString_FromString(decomp); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 258 | void |
| 259 | get_decomp_record(Py_UCS4 code, int *index, int *prefix, int *count) |
| 260 | { |
Neal Norwitz | e9c571f | 2003-02-28 03:14:37 +0000 | [diff] [blame] | 261 | if (code >= 0x110000) { |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 262 | *index = 0; |
| 263 | } |
| 264 | else { |
| 265 | *index = decomp_index1[(code>>DECOMP_SHIFT)]; |
| 266 | *index = decomp_index2[(*index<<DECOMP_SHIFT)+ |
| 267 | (code&((1<<DECOMP_SHIFT)-1))]; |
| 268 | } |
| 269 | |
| 270 | /* high byte is number of hex bytes (usually one or two), low byte |
| 271 | is prefix code (from*/ |
| 272 | *count = decomp_data[*index] >> 8; |
| 273 | *prefix = decomp_data[*index] & 255; |
| 274 | |
| 275 | (*index)++; |
| 276 | } |
| 277 | |
| 278 | #define SBase 0xAC00 |
| 279 | #define LBase 0x1100 |
| 280 | #define VBase 0x1161 |
| 281 | #define TBase 0x11A7 |
| 282 | #define LCount 19 |
| 283 | #define VCount 21 |
| 284 | #define TCount 28 |
| 285 | #define NCount (VCount*TCount) |
| 286 | #define SCount (LCount*NCount) |
| 287 | |
| 288 | static PyObject* |
| 289 | nfd_nfkd(PyObject *input, int k) |
| 290 | { |
| 291 | PyObject *result; |
| 292 | Py_UNICODE *i, *end, *o; |
| 293 | /* Longest decomposition in Unicode 3.2: U+FDFA */ |
| 294 | Py_UNICODE stack[20]; |
| 295 | int space, stackptr, isize; |
| 296 | int index, prefix, count; |
| 297 | unsigned char prev, cur; |
| 298 | |
| 299 | stackptr = 0; |
| 300 | isize = PyUnicode_GET_SIZE(input); |
| 301 | /* Overallocate atmost 10 characters. */ |
| 302 | space = (isize > 10 ? 10 : isize) + isize; |
| 303 | result = PyUnicode_FromUnicode(NULL, space); |
| 304 | if (!result) |
| 305 | return NULL; |
| 306 | i = PyUnicode_AS_UNICODE(input); |
| 307 | end = i + isize; |
| 308 | o = PyUnicode_AS_UNICODE(result); |
| 309 | |
| 310 | while (i < end) { |
| 311 | stack[stackptr++] = *i++; |
| 312 | while(stackptr) { |
| 313 | Py_UNICODE code = stack[--stackptr]; |
Martin v. Löwis | d2171d2 | 2003-11-06 20:47:57 +0000 | [diff] [blame] | 314 | /* Hangul Decomposition adds three characters in |
| 315 | a single step, so we need atleast that much room. */ |
| 316 | if (space < 3) { |
| 317 | int newsize = PyString_GET_SIZE(result) + 10; |
| 318 | space += 10; |
| 319 | if (PyUnicode_Resize(&result, newsize) == -1) |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 320 | return NULL; |
Martin v. Löwis | d2171d2 | 2003-11-06 20:47:57 +0000 | [diff] [blame] | 321 | o = PyUnicode_AS_UNICODE(result) + newsize - space; |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 322 | } |
| 323 | /* Hangul Decomposition. */ |
| 324 | if (SBase <= code && code < (SBase+SCount)) { |
| 325 | int SIndex = code - SBase; |
| 326 | int L = LBase + SIndex / NCount; |
| 327 | int V = VBase + (SIndex % NCount) / TCount; |
| 328 | int T = TBase + SIndex % TCount; |
| 329 | *o++ = L; |
| 330 | *o++ = V; |
| 331 | space -= 2; |
| 332 | if (T != TBase) { |
| 333 | *o++ = T; |
| 334 | space --; |
| 335 | } |
| 336 | continue; |
| 337 | } |
| 338 | /* Other decompoistions. */ |
| 339 | get_decomp_record(code, &index, &prefix, &count); |
| 340 | |
| 341 | /* Copy character if it is not decomposable, or has a |
| 342 | compatibility decomposition, but we do NFD. */ |
| 343 | if (!count || (prefix && !k)) { |
| 344 | *o++ = code; |
| 345 | space--; |
| 346 | continue; |
| 347 | } |
| 348 | /* Copy decomposition onto the stack, in reverse |
| 349 | order. */ |
| 350 | while(count) { |
| 351 | code = decomp_data[index + (--count)]; |
| 352 | stack[stackptr++] = code; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /* Drop overallocation. Cannot fail. */ |
| 358 | PyUnicode_Resize(&result, PyUnicode_GET_SIZE(result) - space); |
| 359 | |
| 360 | /* Sort canonically. */ |
| 361 | i = PyUnicode_AS_UNICODE(result); |
| 362 | prev = _getrecord_ex(*i)->combining; |
| 363 | end = i + PyUnicode_GET_SIZE(result); |
| 364 | for (i++; i < end; i++) { |
| 365 | cur = _getrecord_ex(*i)->combining; |
| 366 | if (prev == 0 || cur == 0 || prev <= cur) { |
| 367 | prev = cur; |
| 368 | continue; |
| 369 | } |
| 370 | /* Non-canonical order. Need to switch *i with previous. */ |
| 371 | o = i - 1; |
| 372 | while (1) { |
| 373 | Py_UNICODE tmp = o[1]; |
| 374 | o[1] = o[0]; |
| 375 | o[0] = tmp; |
| 376 | o--; |
| 377 | if (o < PyUnicode_AS_UNICODE(result)) |
| 378 | break; |
| 379 | prev = _getrecord_ex(*o)->combining; |
| 380 | if (prev == 0 || prev <= cur) |
| 381 | break; |
| 382 | } |
| 383 | prev = _getrecord_ex(*i)->combining; |
| 384 | } |
| 385 | return result; |
| 386 | } |
| 387 | |
| 388 | static int |
| 389 | find_nfc_index(struct reindex* nfc, Py_UNICODE code) |
| 390 | { |
| 391 | int index; |
| 392 | for (index = 0; nfc[index].start; index++) { |
| 393 | int start = nfc[index].start; |
| 394 | if (code < start) |
| 395 | return -1; |
| 396 | if (code <= start + nfc[index].count) { |
| 397 | int delta = code - start; |
| 398 | return nfc[index].index + delta; |
| 399 | } |
| 400 | } |
| 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | static PyObject* |
| 405 | nfc_nfkc(PyObject *input, int k) |
| 406 | { |
| 407 | PyObject *result; |
| 408 | Py_UNICODE *i, *i1, *o, *end; |
| 409 | int f,l,index,index1,comb; |
| 410 | Py_UNICODE code; |
| 411 | Py_UNICODE *skipped[20]; |
| 412 | int cskipped = 0; |
| 413 | |
| 414 | result = nfd_nfkd(input, k); |
| 415 | if (!result) |
| 416 | return NULL; |
| 417 | |
| 418 | /* We are going to modify result in-place. |
| 419 | If nfd_nfkd is changed to sometimes return the input, |
| 420 | this code needs to be reviewed. */ |
| 421 | assert(result != input); |
| 422 | |
| 423 | i = PyUnicode_AS_UNICODE(result); |
| 424 | end = i + PyUnicode_GET_SIZE(result); |
| 425 | o = PyUnicode_AS_UNICODE(result); |
| 426 | |
| 427 | again: |
| 428 | while (i < end) { |
| 429 | for (index = 0; index < cskipped; index++) { |
| 430 | if (skipped[index] == i) { |
| 431 | /* *i character is skipped. |
| 432 | Remove from list. */ |
| 433 | skipped[index] = skipped[cskipped-1]; |
| 434 | cskipped--; |
| 435 | i++; |
Martin v. Löwis | 2fb661f | 2002-12-07 14:56:36 +0000 | [diff] [blame] | 436 | goto again; /* continue while */ |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | /* Hangul Composition. We don't need to check for <LV,T> |
| 440 | pairs, since we always have decomposed data. */ |
| 441 | if (LBase <= *i && *i < (LBase+LCount) && |
| 442 | i + 1 < end && |
| 443 | VBase <= i[1] && i[1] <= (VBase+VCount)) { |
| 444 | int LIndex, VIndex; |
| 445 | LIndex = i[0] - LBase; |
| 446 | VIndex = i[1] - VBase; |
| 447 | code = SBase + (LIndex*VCount+VIndex)*TCount; |
| 448 | i+=2; |
| 449 | if (i < end && |
| 450 | TBase <= *i && *i <= (TBase+TCount)) { |
| 451 | code += *i-TBase; |
| 452 | i++; |
| 453 | } |
| 454 | *o++ = code; |
| 455 | continue; |
| 456 | } |
| 457 | |
| 458 | f = find_nfc_index(nfc_first, *i); |
| 459 | if (f == -1) { |
| 460 | *o++ = *i++; |
| 461 | continue; |
| 462 | } |
| 463 | /* Find next unblocked character. */ |
| 464 | i1 = i+1; |
| 465 | comb = 0; |
| 466 | while (i1 < end) { |
| 467 | int comb1 = _getrecord_ex(*i1)->combining; |
| 468 | if (comb1 && comb == comb1) { |
| 469 | /* Character is blocked. */ |
| 470 | i1++; |
| 471 | continue; |
| 472 | } |
| 473 | l = find_nfc_index(nfc_last, *i1); |
| 474 | /* *i1 cannot be combined with *i. If *i1 |
| 475 | is a starter, we don't need to look further. |
| 476 | Otherwise, record the combining class. */ |
| 477 | if (l == -1) { |
| 478 | not_combinable: |
| 479 | if (comb1 == 0) |
| 480 | break; |
| 481 | comb = comb1; |
| 482 | i1++; |
| 483 | continue; |
| 484 | } |
| 485 | index = f*TOTAL_LAST + l; |
| 486 | index1 = comp_index[index >> COMP_SHIFT]; |
| 487 | code = comp_data[(index1<<COMP_SHIFT)+ |
| 488 | (index&((1<<COMP_SHIFT)-1))]; |
| 489 | if (code == 0) |
| 490 | goto not_combinable; |
| 491 | |
| 492 | /* Replace the original character. */ |
| 493 | *i = code; |
| 494 | /* Mark the second character unused. */ |
| 495 | skipped[cskipped++] = i1; |
| 496 | i1++; |
| 497 | f = find_nfc_index(nfc_first, *i); |
| 498 | if (f == -1) |
| 499 | break; |
| 500 | } |
| 501 | *o++ = *i++; |
| 502 | } |
| 503 | if (o != end) |
| 504 | PyUnicode_Resize(&result, o - PyUnicode_AS_UNICODE(result)); |
| 505 | return result; |
| 506 | } |
| 507 | |
| 508 | static PyObject* |
| 509 | unicodedata_normalize(PyObject *self, PyObject *args) |
| 510 | { |
| 511 | char *form; |
| 512 | PyObject *input; |
| 513 | |
Hye-Shik Chang | 69dc1c8 | 2004-07-15 04:30:25 +0000 | [diff] [blame] | 514 | if(!PyArg_ParseTuple(args, "sO!:normalize", |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 515 | &form, &PyUnicode_Type, &input)) |
| 516 | return NULL; |
| 517 | |
Martin v. Löwis | 61e40bd | 2004-04-17 19:36:48 +0000 | [diff] [blame] | 518 | if (PyUnicode_GetSize(input) == 0) { |
| 519 | /* Special case empty input strings, since resizing |
| 520 | them later would cause internal errors. */ |
| 521 | Py_INCREF(input); |
| 522 | return input; |
| 523 | } |
| 524 | |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 525 | if (strcmp(form, "NFC") == 0) |
| 526 | return nfc_nfkc(input, 0); |
| 527 | if (strcmp(form, "NFKC") == 0) |
| 528 | return nfc_nfkc(input, 1); |
| 529 | if (strcmp(form, "NFD") == 0) |
| 530 | return nfd_nfkd(input, 0); |
| 531 | if (strcmp(form, "NFKD") == 0) |
| 532 | return nfd_nfkd(input, 1); |
| 533 | PyErr_SetString(PyExc_ValueError, "invalid normalization form"); |
| 534 | return NULL; |
| 535 | } |
| 536 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 537 | /* -------------------------------------------------------------------- */ |
| 538 | /* unicode character name tables */ |
| 539 | |
| 540 | /* data file generated by Tools/unicode/makeunicodedata.py */ |
| 541 | #include "unicodename_db.h" |
| 542 | |
| 543 | /* -------------------------------------------------------------------- */ |
| 544 | /* database code (cut and pasted from the unidb package) */ |
| 545 | |
| 546 | static unsigned long |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 547 | _gethash(const char *s, int len, int scale) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 548 | { |
| 549 | int i; |
| 550 | unsigned long h = 0; |
| 551 | unsigned long ix; |
| 552 | for (i = 0; i < len; i++) { |
| 553 | h = (h * scale) + (unsigned char) toupper(s[i]); |
| 554 | ix = h & 0xff000000; |
| 555 | if (ix) |
| 556 | h = (h ^ ((ix>>24) & 0xff)) & 0x00ffffff; |
| 557 | } |
| 558 | return h; |
| 559 | } |
| 560 | |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 561 | static char *hangul_syllables[][3] = { |
| 562 | { "G", "A", "" }, |
| 563 | { "GG", "AE", "G" }, |
| 564 | { "N", "YA", "GG" }, |
| 565 | { "D", "YAE", "GS" }, |
| 566 | { "DD", "EO", "N", }, |
| 567 | { "R", "E", "NJ" }, |
| 568 | { "M", "YEO", "NH" }, |
| 569 | { "B", "YE", "D" }, |
| 570 | { "BB", "O", "L" }, |
| 571 | { "S", "WA", "LG" }, |
| 572 | { "SS", "WAE", "LM" }, |
| 573 | { "", "OE", "LB" }, |
| 574 | { "J", "YO", "LS" }, |
| 575 | { "JJ", "U", "LT" }, |
| 576 | { "C", "WEO", "LP" }, |
| 577 | { "K", "WE", "LH" }, |
| 578 | { "T", "WI", "M" }, |
| 579 | { "P", "YU", "B" }, |
| 580 | { "H", "EU", "BS" }, |
| 581 | { 0, "YI", "S" }, |
| 582 | { 0, "I", "SS" }, |
| 583 | { 0, 0, "NG" }, |
| 584 | { 0, 0, "J" }, |
| 585 | { 0, 0, "C" }, |
| 586 | { 0, 0, "K" }, |
| 587 | { 0, 0, "T" }, |
| 588 | { 0, 0, "P" }, |
| 589 | { 0, 0, "H" } |
| 590 | }; |
| 591 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 592 | static int |
Martin v. Löwis | 8d93ca1 | 2002-11-23 22:10:29 +0000 | [diff] [blame] | 593 | is_unified_ideograph(Py_UCS4 code) |
| 594 | { |
| 595 | return ( |
| 596 | (0x3400 <= code && code <= 0x4DB5) || /* CJK Ideograph Extension A */ |
| 597 | (0x4E00 <= code && code <= 0x9FA5) || /* CJK Ideograph */ |
| 598 | (0x20000 <= code && code <= 0x2A6D6));/* CJK Ideograph Extension B */ |
| 599 | } |
| 600 | |
| 601 | static int |
Andrew MacIntyre | 74a3bec | 2002-06-13 11:55:14 +0000 | [diff] [blame] | 602 | _getucname(Py_UCS4 code, char* buffer, int buflen) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 603 | { |
| 604 | int offset; |
| 605 | int i; |
| 606 | int word; |
| 607 | unsigned char* w; |
| 608 | |
Martin v. Löwis | 2f4be4e | 2002-11-23 17:11:06 +0000 | [diff] [blame] | 609 | if (SBase <= code && code < SBase+SCount) { |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 610 | /* Hangul syllable. */ |
| 611 | int SIndex = code - SBase; |
| 612 | int L = SIndex / NCount; |
| 613 | int V = (SIndex % NCount) / TCount; |
| 614 | int T = SIndex % TCount; |
| 615 | |
| 616 | if (buflen < 27) |
| 617 | /* Worst case: HANGUL SYLLABLE <10chars>. */ |
| 618 | return 0; |
| 619 | strcpy(buffer, "HANGUL SYLLABLE "); |
| 620 | buffer += 16; |
| 621 | strcpy(buffer, hangul_syllables[L][0]); |
| 622 | buffer += strlen(hangul_syllables[L][0]); |
| 623 | strcpy(buffer, hangul_syllables[V][1]); |
| 624 | buffer += strlen(hangul_syllables[V][1]); |
| 625 | strcpy(buffer, hangul_syllables[T][2]); |
| 626 | buffer += strlen(hangul_syllables[T][2]); |
| 627 | *buffer = '\0'; |
| 628 | return 1; |
| 629 | } |
| 630 | |
Martin v. Löwis | 8d93ca1 | 2002-11-23 22:10:29 +0000 | [diff] [blame] | 631 | if (is_unified_ideograph(code)) { |
Martin v. Löwis | ef7fe2e | 2002-11-23 18:01:32 +0000 | [diff] [blame] | 632 | if (buflen < 28) |
| 633 | /* Worst case: CJK UNIFIED IDEOGRAPH-20000 */ |
| 634 | return 0; |
| 635 | sprintf(buffer, "CJK UNIFIED IDEOGRAPH-%X", code); |
| 636 | return 1; |
| 637 | } |
| 638 | |
Martin v. Löwis | 9def6a3 | 2002-10-18 16:11:54 +0000 | [diff] [blame] | 639 | if (code >= 0x110000) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 640 | return 0; |
| 641 | |
| 642 | /* get offset into phrasebook */ |
| 643 | offset = phrasebook_offset1[(code>>phrasebook_shift)]; |
| 644 | offset = phrasebook_offset2[(offset<<phrasebook_shift) + |
| 645 | (code&((1<<phrasebook_shift)-1))]; |
| 646 | if (!offset) |
| 647 | return 0; |
| 648 | |
| 649 | i = 0; |
| 650 | |
| 651 | for (;;) { |
| 652 | /* get word index */ |
| 653 | word = phrasebook[offset] - phrasebook_short; |
| 654 | if (word >= 0) { |
| 655 | word = (word << 8) + phrasebook[offset+1]; |
| 656 | offset += 2; |
| 657 | } else |
| 658 | word = phrasebook[offset++]; |
| 659 | if (i) { |
| 660 | if (i > buflen) |
| 661 | return 0; /* buffer overflow */ |
| 662 | buffer[i++] = ' '; |
| 663 | } |
| 664 | /* copy word string from lexicon. the last character in the |
| 665 | word has bit 7 set. the last word in a string ends with |
| 666 | 0x80 */ |
| 667 | w = lexicon + lexicon_offset[word]; |
| 668 | while (*w < 128) { |
| 669 | if (i >= buflen) |
| 670 | return 0; /* buffer overflow */ |
| 671 | buffer[i++] = *w++; |
| 672 | } |
| 673 | if (i >= buflen) |
| 674 | return 0; /* buffer overflow */ |
| 675 | buffer[i++] = *w & 127; |
| 676 | if (*w == 128) |
| 677 | break; /* end of word */ |
| 678 | } |
| 679 | |
| 680 | return 1; |
| 681 | } |
| 682 | |
| 683 | static int |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 684 | _cmpname(int code, const char* name, int namelen) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 685 | { |
| 686 | /* check if code corresponds to the given name */ |
| 687 | int i; |
| 688 | char buffer[NAME_MAXLEN]; |
Andrew MacIntyre | 74a3bec | 2002-06-13 11:55:14 +0000 | [diff] [blame] | 689 | if (!_getucname(code, buffer, sizeof(buffer))) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 690 | return 0; |
| 691 | for (i = 0; i < namelen; i++) { |
| 692 | if (toupper(name[i]) != buffer[i]) |
| 693 | return 0; |
| 694 | } |
| 695 | return buffer[namelen] == '\0'; |
| 696 | } |
| 697 | |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 698 | static void |
| 699 | find_syllable(const char *str, int *len, int *pos, int count, int column) |
| 700 | { |
| 701 | int i, len1; |
| 702 | *len = -1; |
| 703 | for (i = 0; i < count; i++) { |
| 704 | char *s = hangul_syllables[i][column]; |
| 705 | len1 = strlen(s); |
| 706 | if (len1 <= *len) |
| 707 | continue; |
| 708 | if (strncmp(str, s, len1) == 0) { |
| 709 | *len = len1; |
| 710 | *pos = i; |
| 711 | } |
| 712 | } |
| 713 | if (*len == -1) { |
| 714 | *len = 0; |
| 715 | *pos = -1; |
| 716 | } |
| 717 | } |
| 718 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 719 | static int |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 720 | _getcode(const char* name, int namelen, Py_UCS4* code) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 721 | { |
| 722 | unsigned int h, v; |
| 723 | unsigned int mask = code_size-1; |
| 724 | unsigned int i, incr; |
| 725 | |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 726 | /* Check for hangul syllables. */ |
| 727 | if (strncmp(name, "HANGUL SYLLABLE ", 16) == 0) { |
| 728 | int L, V, T, len; |
| 729 | const char *pos = name + 16; |
| 730 | find_syllable(pos, &len, &L, LCount, 0); |
| 731 | pos += len; |
| 732 | find_syllable(pos, &len, &V, VCount, 1); |
| 733 | pos += len; |
| 734 | find_syllable(pos, &len, &T, TCount, 2); |
| 735 | pos += len; |
| 736 | if (V != -1 && V != -1 && T != -1 && pos-name == namelen) { |
| 737 | *code = SBase + (L*VCount+V)*TCount + T; |
| 738 | return 1; |
| 739 | } |
Martin v. Löwis | ef7fe2e | 2002-11-23 18:01:32 +0000 | [diff] [blame] | 740 | /* Otherwise, it's an illegal syllable name. */ |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | /* Check for unified ideographs. */ |
| 745 | if (strncmp(name, "CJK UNIFIED IDEOGRAPH-", 22) == 0) { |
| 746 | /* Four or five hexdigits must follow. */ |
| 747 | v = 0; |
| 748 | name += 22; |
| 749 | namelen -= 22; |
| 750 | if (namelen != 4 && namelen != 5) |
| 751 | return 0; |
| 752 | while (namelen--) { |
| 753 | v *= 16; |
| 754 | if (*name >= '0' && *name <= '9') |
| 755 | v += *name - '0'; |
| 756 | else if (*name >= 'A' && *name <= 'F') |
| 757 | v += *name - 'A' + 10; |
| 758 | else |
| 759 | return 0; |
| 760 | name++; |
| 761 | } |
Martin v. Löwis | 8d93ca1 | 2002-11-23 22:10:29 +0000 | [diff] [blame] | 762 | if (!is_unified_ideograph(v)) |
| 763 | return 0; |
Martin v. Löwis | ef7fe2e | 2002-11-23 18:01:32 +0000 | [diff] [blame] | 764 | *code = v; |
| 765 | return 1; |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 768 | /* the following is the same as python's dictionary lookup, with |
| 769 | only minor changes. see the makeunicodedata script for more |
| 770 | details */ |
| 771 | |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 772 | h = (unsigned int) _gethash(name, namelen, code_magic); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 773 | i = (~h) & mask; |
| 774 | v = code_hash[i]; |
| 775 | if (!v) |
| 776 | return 0; |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 777 | if (_cmpname(v, name, namelen)) { |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 778 | *code = v; |
| 779 | return 1; |
| 780 | } |
| 781 | incr = (h ^ (h >> 3)) & mask; |
| 782 | if (!incr) |
| 783 | incr = mask; |
| 784 | for (;;) { |
| 785 | i = (i + incr) & mask; |
| 786 | v = code_hash[i]; |
| 787 | if (!v) |
Fredrik Lundh | ae76367 | 2001-02-18 11:41:49 +0000 | [diff] [blame] | 788 | return 0; |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 789 | if (_cmpname(v, name, namelen)) { |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 790 | *code = v; |
| 791 | return 1; |
| 792 | } |
| 793 | incr = incr << 1; |
| 794 | if (incr > mask) |
| 795 | incr = incr ^ code_poly; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | static const _PyUnicode_Name_CAPI hashAPI = |
| 800 | { |
| 801 | sizeof(_PyUnicode_Name_CAPI), |
Andrew MacIntyre | 74a3bec | 2002-06-13 11:55:14 +0000 | [diff] [blame] | 802 | _getucname, |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 803 | _getcode |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 804 | }; |
| 805 | |
| 806 | /* -------------------------------------------------------------------- */ |
| 807 | /* Python bindings */ |
| 808 | |
| 809 | static PyObject * |
| 810 | unicodedata_name(PyObject* self, PyObject* args) |
| 811 | { |
| 812 | char name[NAME_MAXLEN]; |
| 813 | |
| 814 | PyUnicodeObject* v; |
| 815 | PyObject* defobj = NULL; |
| 816 | if (!PyArg_ParseTuple(args, "O!|O:name", &PyUnicode_Type, &v, &defobj)) |
| 817 | return NULL; |
| 818 | |
| 819 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 820 | PyErr_SetString(PyExc_TypeError, |
| 821 | "need a single Unicode character as parameter"); |
| 822 | return NULL; |
| 823 | } |
| 824 | |
Andrew MacIntyre | 74a3bec | 2002-06-13 11:55:14 +0000 | [diff] [blame] | 825 | if (!_getucname((Py_UCS4) *PyUnicode_AS_UNICODE(v), |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 826 | name, sizeof(name))) { |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 827 | if (defobj == NULL) { |
| 828 | PyErr_SetString(PyExc_ValueError, "no such name"); |
| 829 | return NULL; |
| 830 | } |
| 831 | else { |
| 832 | Py_INCREF(defobj); |
| 833 | return defobj; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | return Py_BuildValue("s", name); |
| 838 | } |
| 839 | |
| 840 | static PyObject * |
| 841 | unicodedata_lookup(PyObject* self, PyObject* args) |
| 842 | { |
| 843 | Py_UCS4 code; |
| 844 | Py_UNICODE str[1]; |
| 845 | |
| 846 | char* name; |
| 847 | int namelen; |
| 848 | if (!PyArg_ParseTuple(args, "s#:lookup", &name, &namelen)) |
| 849 | return NULL; |
| 850 | |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 851 | if (!_getcode(name, namelen, &code)) { |
Martin v. Löwis | ef7fe2e | 2002-11-23 18:01:32 +0000 | [diff] [blame] | 852 | char fmt[] = "undefined character name '%s'"; |
| 853 | char *buf = PyMem_MALLOC(sizeof(fmt) + namelen); |
| 854 | sprintf(buf, fmt, name); |
| 855 | PyErr_SetString(PyExc_KeyError, buf); |
| 856 | PyMem_FREE(buf); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 857 | return NULL; |
| 858 | } |
| 859 | |
| 860 | str[0] = (Py_UNICODE) code; |
| 861 | return PyUnicode_FromUnicode(str, 1); |
| 862 | } |
| 863 | |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 864 | /* XXX Add doc strings. */ |
| 865 | |
| 866 | static PyMethodDef unicodedata_functions[] = { |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 867 | {"decimal", unicodedata_decimal, METH_VARARGS}, |
| 868 | {"digit", unicodedata_digit, METH_VARARGS}, |
| 869 | {"numeric", unicodedata_numeric, METH_VARARGS}, |
| 870 | {"category", unicodedata_category, METH_VARARGS}, |
| 871 | {"bidirectional", unicodedata_bidirectional, METH_VARARGS}, |
| 872 | {"combining", unicodedata_combining, METH_VARARGS}, |
| 873 | {"mirrored", unicodedata_mirrored, METH_VARARGS}, |
| 874 | {"decomposition",unicodedata_decomposition, METH_VARARGS}, |
| 875 | {"name", unicodedata_name, METH_VARARGS}, |
| 876 | {"lookup", unicodedata_lookup, METH_VARARGS}, |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 877 | {"normalize", unicodedata_normalize, METH_VARARGS}, |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 878 | {NULL, NULL} /* sentinel */ |
| 879 | }; |
| 880 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 881 | PyDoc_STRVAR(unicodedata_docstring, "unicode character database"); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 882 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 883 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 884 | initunicodedata(void) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 885 | { |
Fred Drake | a2bd8d3 | 2002-04-03 21:39:26 +0000 | [diff] [blame] | 886 | PyObject *m, *v; |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 887 | |
Fred Drake | f585bef | 2001-03-03 19:41:55 +0000 | [diff] [blame] | 888 | m = Py_InitModule3( |
| 889 | "unicodedata", unicodedata_functions, unicodedata_docstring); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 890 | if (!m) |
| 891 | return; |
| 892 | |
Martin v. Löwis | b5c980b | 2002-11-25 09:13:37 +0000 | [diff] [blame] | 893 | PyModule_AddStringConstant(m, "unidata_version", UNIDATA_VERSION); |
| 894 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 895 | /* Export C API */ |
| 896 | v = PyCObject_FromVoidPtr((void *) &hashAPI, NULL); |
Fred Drake | a2bd8d3 | 2002-04-03 21:39:26 +0000 | [diff] [blame] | 897 | if (v != NULL) |
| 898 | PyModule_AddObject(m, "ucnhash_CAPI", v); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 899 | } |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 900 | |
| 901 | /* |
| 902 | Local variables: |
| 903 | c-basic-offset: 4 |
Martin v. Löwis | 677bde2 | 2002-11-23 22:08:15 +0000 | [diff] [blame] | 904 | indent-tabs-mode: nil |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 905 | End: |
| 906 | */ |