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* |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 33 | _getrecord(PyUnicodeObject* v) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 34 | { |
| 35 | int code; |
| 36 | int index; |
| 37 | |
| 38 | code = (int) *PyUnicode_AS_UNICODE(v); |
| 39 | |
Martin v. Löwis | 9def6a3 | 2002-10-18 16:11:54 +0000 | [diff] [blame] | 40 | if (code < 0 || code >= 0x110000) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 41 | index = 0; |
| 42 | else { |
| 43 | index = index1[(code>>SHIFT)]; |
| 44 | index = index2[(index<<SHIFT)+(code&((1<<SHIFT)-1))]; |
| 45 | } |
| 46 | |
| 47 | return &_PyUnicode_Database_Records[index]; |
| 48 | } |
| 49 | |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 50 | /* --- Module API --------------------------------------------------------- */ |
| 51 | |
| 52 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 53 | unicodedata_decimal(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 54 | { |
| 55 | PyUnicodeObject *v; |
| 56 | PyObject *defobj = NULL; |
| 57 | long rc; |
| 58 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 59 | if (!PyArg_ParseTuple(args, "O!|O:decimal", &PyUnicode_Type, &v, &defobj)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 60 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 61 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 62 | PyErr_SetString(PyExc_TypeError, |
| 63 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 64 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 65 | } |
| 66 | rc = Py_UNICODE_TODECIMAL(*PyUnicode_AS_UNICODE(v)); |
| 67 | if (rc < 0) { |
| 68 | if (defobj == NULL) { |
| 69 | PyErr_SetString(PyExc_ValueError, |
| 70 | "not a decimal"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 71 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 72 | } |
| 73 | else { |
| 74 | Py_INCREF(defobj); |
| 75 | return defobj; |
| 76 | } |
| 77 | } |
| 78 | return PyInt_FromLong(rc); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 82 | unicodedata_digit(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 83 | { |
| 84 | PyUnicodeObject *v; |
| 85 | PyObject *defobj = NULL; |
| 86 | long rc; |
| 87 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 88 | if (!PyArg_ParseTuple(args, "O!|O:digit", &PyUnicode_Type, &v, &defobj)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 89 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 90 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 91 | PyErr_SetString(PyExc_TypeError, |
| 92 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 93 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 94 | } |
| 95 | rc = Py_UNICODE_TODIGIT(*PyUnicode_AS_UNICODE(v)); |
| 96 | if (rc < 0) { |
| 97 | if (defobj == NULL) { |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 98 | PyErr_SetString(PyExc_ValueError, "not a digit"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 99 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 100 | } |
| 101 | else { |
| 102 | Py_INCREF(defobj); |
| 103 | return defobj; |
| 104 | } |
| 105 | } |
| 106 | return PyInt_FromLong(rc); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 110 | unicodedata_numeric(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 111 | { |
| 112 | PyUnicodeObject *v; |
| 113 | PyObject *defobj = NULL; |
| 114 | double rc; |
| 115 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 116 | if (!PyArg_ParseTuple(args, "O!|O:numeric", &PyUnicode_Type, &v, &defobj)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 117 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 118 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 119 | PyErr_SetString(PyExc_TypeError, |
| 120 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 121 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 122 | } |
| 123 | rc = Py_UNICODE_TONUMERIC(*PyUnicode_AS_UNICODE(v)); |
| 124 | if (rc < 0) { |
| 125 | if (defobj == NULL) { |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 126 | PyErr_SetString(PyExc_ValueError, "not a numeric character"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 127 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 128 | } |
| 129 | else { |
| 130 | Py_INCREF(defobj); |
| 131 | return defobj; |
| 132 | } |
| 133 | } |
| 134 | return PyFloat_FromDouble(rc); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 138 | unicodedata_category(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 139 | { |
| 140 | PyUnicodeObject *v; |
| 141 | int index; |
| 142 | |
| 143 | if (!PyArg_ParseTuple(args, "O!:category", |
| 144 | &PyUnicode_Type, &v)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 145 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 146 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 147 | PyErr_SetString(PyExc_TypeError, |
| 148 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 149 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 150 | } |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 151 | index = (int) _getrecord(v)->category; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 152 | return PyString_FromString(_PyUnicode_CategoryNames[index]); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 156 | unicodedata_bidirectional(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 157 | { |
| 158 | PyUnicodeObject *v; |
| 159 | int index; |
| 160 | |
| 161 | if (!PyArg_ParseTuple(args, "O!:bidirectional", |
| 162 | &PyUnicode_Type, &v)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 163 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 164 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 165 | PyErr_SetString(PyExc_TypeError, |
| 166 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 167 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 168 | } |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 169 | index = (int) _getrecord(v)->bidirectional; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 170 | return PyString_FromString(_PyUnicode_BidirectionalNames[index]); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 174 | unicodedata_combining(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 175 | { |
| 176 | PyUnicodeObject *v; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 177 | |
| 178 | if (!PyArg_ParseTuple(args, "O!:combining", |
| 179 | &PyUnicode_Type, &v)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 180 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 181 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 182 | PyErr_SetString(PyExc_TypeError, |
| 183 | "need a single Unicode character as parameter"); |
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 | } |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 186 | return PyInt_FromLong((int) _getrecord(v)->combining); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 190 | unicodedata_mirrored(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 191 | { |
| 192 | PyUnicodeObject *v; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 193 | |
| 194 | if (!PyArg_ParseTuple(args, "O!:mirrored", |
| 195 | &PyUnicode_Type, &v)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 196 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 197 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 198 | PyErr_SetString(PyExc_TypeError, |
| 199 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 200 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 201 | } |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 202 | return PyInt_FromLong((int) _getrecord(v)->mirrored); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static PyObject * |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 206 | unicodedata_decomposition(PyObject *self, PyObject *args) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 207 | { |
| 208 | PyUnicodeObject *v; |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 209 | char decomp[256]; |
| 210 | int code, index, count, i; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 211 | |
| 212 | if (!PyArg_ParseTuple(args, "O!:decomposition", |
| 213 | &PyUnicode_Type, &v)) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 214 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 215 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 216 | PyErr_SetString(PyExc_TypeError, |
| 217 | "need a single Unicode character as parameter"); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 218 | return NULL; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 219 | } |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 220 | |
| 221 | code = (int) *PyUnicode_AS_UNICODE(v); |
| 222 | |
Martin v. Löwis | 9def6a3 | 2002-10-18 16:11:54 +0000 | [diff] [blame] | 223 | if (code < 0 || code >= 0x110000) |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 224 | index = 0; |
| 225 | else { |
| 226 | index = decomp_index1[(code>>DECOMP_SHIFT)]; |
| 227 | index = decomp_index2[(index<<DECOMP_SHIFT)+ |
| 228 | (code&((1<<DECOMP_SHIFT)-1))]; |
| 229 | } |
| 230 | |
Tim Peters | 69b83b1 | 2001-11-30 07:23:05 +0000 | [diff] [blame] | 231 | /* 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] | 232 | is prefix code (from*/ |
| 233 | count = decomp_data[index] >> 8; |
| 234 | |
| 235 | /* XXX: could allocate the PyString up front instead |
| 236 | (strlen(prefix) + 5 * count + 1 bytes) */ |
| 237 | |
| 238 | /* copy prefix */ |
| 239 | i = strlen(decomp_prefix[decomp_data[index] & 255]); |
| 240 | memcpy(decomp, decomp_prefix[decomp_data[index] & 255], i); |
| 241 | |
| 242 | while (count-- > 0) { |
| 243 | if (i) |
| 244 | decomp[i++] = ' '; |
Tim Peters | 69b83b1 | 2001-11-30 07:23:05 +0000 | [diff] [blame] | 245 | assert((size_t)i < sizeof(decomp)); |
| 246 | PyOS_snprintf(decomp + i, sizeof(decomp) - i, "%04X", |
| 247 | decomp_data[++index]); |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 248 | i += strlen(decomp + i); |
| 249 | } |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 250 | |
Fredrik Lundh | 7b7dd10 | 2001-01-21 22:41:08 +0000 | [diff] [blame] | 251 | decomp[i] = '\0'; |
| 252 | |
| 253 | return PyString_FromString(decomp); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 256 | /* -------------------------------------------------------------------- */ |
| 257 | /* unicode character name tables */ |
| 258 | |
| 259 | /* data file generated by Tools/unicode/makeunicodedata.py */ |
| 260 | #include "unicodename_db.h" |
| 261 | |
| 262 | /* -------------------------------------------------------------------- */ |
| 263 | /* database code (cut and pasted from the unidb package) */ |
| 264 | |
| 265 | static unsigned long |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 266 | _gethash(const char *s, int len, int scale) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 267 | { |
| 268 | int i; |
| 269 | unsigned long h = 0; |
| 270 | unsigned long ix; |
| 271 | for (i = 0; i < len; i++) { |
| 272 | h = (h * scale) + (unsigned char) toupper(s[i]); |
| 273 | ix = h & 0xff000000; |
| 274 | if (ix) |
| 275 | h = (h ^ ((ix>>24) & 0xff)) & 0x00ffffff; |
| 276 | } |
| 277 | return h; |
| 278 | } |
| 279 | |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 280 | #define SBase 0xAC00 |
| 281 | #define LBase 0x1100 |
| 282 | #define VBase 0x1161 |
| 283 | #define TBase 0x11A7 |
| 284 | #define LCount 19 |
| 285 | #define VCount 21 |
| 286 | #define TCount 28 |
| 287 | #define NCount (VCount*TCount) |
| 288 | #define SCount (LCount*NCount) |
| 289 | |
| 290 | static char *hangul_syllables[][3] = { |
| 291 | { "G", "A", "" }, |
| 292 | { "GG", "AE", "G" }, |
| 293 | { "N", "YA", "GG" }, |
| 294 | { "D", "YAE", "GS" }, |
| 295 | { "DD", "EO", "N", }, |
| 296 | { "R", "E", "NJ" }, |
| 297 | { "M", "YEO", "NH" }, |
| 298 | { "B", "YE", "D" }, |
| 299 | { "BB", "O", "L" }, |
| 300 | { "S", "WA", "LG" }, |
| 301 | { "SS", "WAE", "LM" }, |
| 302 | { "", "OE", "LB" }, |
| 303 | { "J", "YO", "LS" }, |
| 304 | { "JJ", "U", "LT" }, |
| 305 | { "C", "WEO", "LP" }, |
| 306 | { "K", "WE", "LH" }, |
| 307 | { "T", "WI", "M" }, |
| 308 | { "P", "YU", "B" }, |
| 309 | { "H", "EU", "BS" }, |
| 310 | { 0, "YI", "S" }, |
| 311 | { 0, "I", "SS" }, |
| 312 | { 0, 0, "NG" }, |
| 313 | { 0, 0, "J" }, |
| 314 | { 0, 0, "C" }, |
| 315 | { 0, 0, "K" }, |
| 316 | { 0, 0, "T" }, |
| 317 | { 0, 0, "P" }, |
| 318 | { 0, 0, "H" } |
| 319 | }; |
| 320 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 321 | static int |
Andrew MacIntyre | 74a3bec | 2002-06-13 11:55:14 +0000 | [diff] [blame] | 322 | _getucname(Py_UCS4 code, char* buffer, int buflen) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 323 | { |
| 324 | int offset; |
| 325 | int i; |
| 326 | int word; |
| 327 | unsigned char* w; |
| 328 | |
Martin v. Löwis | 2f4be4e | 2002-11-23 17:11:06 +0000 | [diff] [blame] | 329 | if (SBase <= code && code < SBase+SCount) { |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 330 | /* Hangul syllable. */ |
| 331 | int SIndex = code - SBase; |
| 332 | int L = SIndex / NCount; |
| 333 | int V = (SIndex % NCount) / TCount; |
| 334 | int T = SIndex % TCount; |
| 335 | |
| 336 | if (buflen < 27) |
| 337 | /* Worst case: HANGUL SYLLABLE <10chars>. */ |
| 338 | return 0; |
| 339 | strcpy(buffer, "HANGUL SYLLABLE "); |
| 340 | buffer += 16; |
| 341 | strcpy(buffer, hangul_syllables[L][0]); |
| 342 | buffer += strlen(hangul_syllables[L][0]); |
| 343 | strcpy(buffer, hangul_syllables[V][1]); |
| 344 | buffer += strlen(hangul_syllables[V][1]); |
| 345 | strcpy(buffer, hangul_syllables[T][2]); |
| 346 | buffer += strlen(hangul_syllables[T][2]); |
| 347 | *buffer = '\0'; |
| 348 | return 1; |
| 349 | } |
| 350 | |
Martin v. Löwis | ef7fe2e | 2002-11-23 18:01:32 +0000 | [diff] [blame^] | 351 | if ((0x3400 <= code && code <= 0x4DB5) || /* CJK Ideograph Extension A */ |
| 352 | (0x4E00 <= code && code <= 0x9FA5) || /* CJK Ideograph */ |
| 353 | (0x20000 <= code && code <= 0x2A6D6)) {/* CJK Ideograph Extension B */ |
| 354 | if (buflen < 28) |
| 355 | /* Worst case: CJK UNIFIED IDEOGRAPH-20000 */ |
| 356 | return 0; |
| 357 | sprintf(buffer, "CJK UNIFIED IDEOGRAPH-%X", code); |
| 358 | return 1; |
| 359 | } |
| 360 | |
Martin v. Löwis | 9def6a3 | 2002-10-18 16:11:54 +0000 | [diff] [blame] | 361 | if (code >= 0x110000) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 362 | return 0; |
| 363 | |
| 364 | /* get offset into phrasebook */ |
| 365 | offset = phrasebook_offset1[(code>>phrasebook_shift)]; |
| 366 | offset = phrasebook_offset2[(offset<<phrasebook_shift) + |
| 367 | (code&((1<<phrasebook_shift)-1))]; |
| 368 | if (!offset) |
| 369 | return 0; |
| 370 | |
| 371 | i = 0; |
| 372 | |
| 373 | for (;;) { |
| 374 | /* get word index */ |
| 375 | word = phrasebook[offset] - phrasebook_short; |
| 376 | if (word >= 0) { |
| 377 | word = (word << 8) + phrasebook[offset+1]; |
| 378 | offset += 2; |
| 379 | } else |
| 380 | word = phrasebook[offset++]; |
| 381 | if (i) { |
| 382 | if (i > buflen) |
| 383 | return 0; /* buffer overflow */ |
| 384 | buffer[i++] = ' '; |
| 385 | } |
| 386 | /* copy word string from lexicon. the last character in the |
| 387 | word has bit 7 set. the last word in a string ends with |
| 388 | 0x80 */ |
| 389 | w = lexicon + lexicon_offset[word]; |
| 390 | while (*w < 128) { |
| 391 | if (i >= buflen) |
| 392 | return 0; /* buffer overflow */ |
| 393 | buffer[i++] = *w++; |
| 394 | } |
| 395 | if (i >= buflen) |
| 396 | return 0; /* buffer overflow */ |
| 397 | buffer[i++] = *w & 127; |
| 398 | if (*w == 128) |
| 399 | break; /* end of word */ |
| 400 | } |
| 401 | |
| 402 | return 1; |
| 403 | } |
| 404 | |
| 405 | static int |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 406 | _cmpname(int code, const char* name, int namelen) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 407 | { |
| 408 | /* check if code corresponds to the given name */ |
| 409 | int i; |
| 410 | char buffer[NAME_MAXLEN]; |
Andrew MacIntyre | 74a3bec | 2002-06-13 11:55:14 +0000 | [diff] [blame] | 411 | if (!_getucname(code, buffer, sizeof(buffer))) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 412 | return 0; |
| 413 | for (i = 0; i < namelen; i++) { |
| 414 | if (toupper(name[i]) != buffer[i]) |
| 415 | return 0; |
| 416 | } |
| 417 | return buffer[namelen] == '\0'; |
| 418 | } |
| 419 | |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 420 | static void |
| 421 | find_syllable(const char *str, int *len, int *pos, int count, int column) |
| 422 | { |
| 423 | int i, len1; |
| 424 | *len = -1; |
| 425 | for (i = 0; i < count; i++) { |
| 426 | char *s = hangul_syllables[i][column]; |
| 427 | len1 = strlen(s); |
| 428 | if (len1 <= *len) |
| 429 | continue; |
| 430 | if (strncmp(str, s, len1) == 0) { |
| 431 | *len = len1; |
| 432 | *pos = i; |
| 433 | } |
| 434 | } |
| 435 | if (*len == -1) { |
| 436 | *len = 0; |
| 437 | *pos = -1; |
| 438 | } |
| 439 | } |
| 440 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 441 | static int |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 442 | _getcode(const char* name, int namelen, Py_UCS4* code) |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 443 | { |
| 444 | unsigned int h, v; |
| 445 | unsigned int mask = code_size-1; |
| 446 | unsigned int i, incr; |
| 447 | |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 448 | /* Check for hangul syllables. */ |
| 449 | if (strncmp(name, "HANGUL SYLLABLE ", 16) == 0) { |
| 450 | int L, V, T, len; |
| 451 | const char *pos = name + 16; |
| 452 | find_syllable(pos, &len, &L, LCount, 0); |
| 453 | pos += len; |
| 454 | find_syllable(pos, &len, &V, VCount, 1); |
| 455 | pos += len; |
| 456 | find_syllable(pos, &len, &T, TCount, 2); |
| 457 | pos += len; |
| 458 | if (V != -1 && V != -1 && T != -1 && pos-name == namelen) { |
| 459 | *code = SBase + (L*VCount+V)*TCount + T; |
| 460 | return 1; |
| 461 | } |
Martin v. Löwis | ef7fe2e | 2002-11-23 18:01:32 +0000 | [diff] [blame^] | 462 | /* Otherwise, it's an illegal syllable name. */ |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | /* Check for unified ideographs. */ |
| 467 | if (strncmp(name, "CJK UNIFIED IDEOGRAPH-", 22) == 0) { |
| 468 | /* Four or five hexdigits must follow. */ |
| 469 | v = 0; |
| 470 | name += 22; |
| 471 | namelen -= 22; |
| 472 | if (namelen != 4 && namelen != 5) |
| 473 | return 0; |
| 474 | while (namelen--) { |
| 475 | v *= 16; |
| 476 | if (*name >= '0' && *name <= '9') |
| 477 | v += *name - '0'; |
| 478 | else if (*name >= 'A' && *name <= 'F') |
| 479 | v += *name - 'A' + 10; |
| 480 | else |
| 481 | return 0; |
| 482 | name++; |
| 483 | } |
| 484 | *code = v; |
| 485 | return 1; |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 488 | /* the following is the same as python's dictionary lookup, with |
| 489 | only minor changes. see the makeunicodedata script for more |
| 490 | details */ |
| 491 | |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 492 | h = (unsigned int) _gethash(name, namelen, code_magic); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 493 | i = (~h) & mask; |
| 494 | v = code_hash[i]; |
| 495 | if (!v) |
| 496 | return 0; |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 497 | if (_cmpname(v, name, namelen)) { |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 498 | *code = v; |
| 499 | return 1; |
| 500 | } |
| 501 | incr = (h ^ (h >> 3)) & mask; |
| 502 | if (!incr) |
| 503 | incr = mask; |
| 504 | for (;;) { |
| 505 | i = (i + incr) & mask; |
| 506 | v = code_hash[i]; |
| 507 | if (!v) |
Fredrik Lundh | ae76367 | 2001-02-18 11:41:49 +0000 | [diff] [blame] | 508 | return 0; |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 509 | if (_cmpname(v, name, namelen)) { |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 510 | *code = v; |
| 511 | return 1; |
| 512 | } |
| 513 | incr = incr << 1; |
| 514 | if (incr > mask) |
| 515 | incr = incr ^ code_poly; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | static const _PyUnicode_Name_CAPI hashAPI = |
| 520 | { |
| 521 | sizeof(_PyUnicode_Name_CAPI), |
Andrew MacIntyre | 74a3bec | 2002-06-13 11:55:14 +0000 | [diff] [blame] | 522 | _getucname, |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 523 | _getcode |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 524 | }; |
| 525 | |
| 526 | /* -------------------------------------------------------------------- */ |
| 527 | /* Python bindings */ |
| 528 | |
| 529 | static PyObject * |
| 530 | unicodedata_name(PyObject* self, PyObject* args) |
| 531 | { |
| 532 | char name[NAME_MAXLEN]; |
| 533 | |
| 534 | PyUnicodeObject* v; |
| 535 | PyObject* defobj = NULL; |
| 536 | if (!PyArg_ParseTuple(args, "O!|O:name", &PyUnicode_Type, &v, &defobj)) |
| 537 | return NULL; |
| 538 | |
| 539 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 540 | PyErr_SetString(PyExc_TypeError, |
| 541 | "need a single Unicode character as parameter"); |
| 542 | return NULL; |
| 543 | } |
| 544 | |
Andrew MacIntyre | 74a3bec | 2002-06-13 11:55:14 +0000 | [diff] [blame] | 545 | if (!_getucname((Py_UCS4) *PyUnicode_AS_UNICODE(v), |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 546 | name, sizeof(name))) { |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 547 | if (defobj == NULL) { |
| 548 | PyErr_SetString(PyExc_ValueError, "no such name"); |
| 549 | return NULL; |
| 550 | } |
| 551 | else { |
| 552 | Py_INCREF(defobj); |
| 553 | return defobj; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | return Py_BuildValue("s", name); |
| 558 | } |
| 559 | |
| 560 | static PyObject * |
| 561 | unicodedata_lookup(PyObject* self, PyObject* args) |
| 562 | { |
| 563 | Py_UCS4 code; |
| 564 | Py_UNICODE str[1]; |
| 565 | |
| 566 | char* name; |
| 567 | int namelen; |
| 568 | if (!PyArg_ParseTuple(args, "s#:lookup", &name, &namelen)) |
| 569 | return NULL; |
| 570 | |
Fredrik Lundh | b95896b | 2001-02-18 22:06:17 +0000 | [diff] [blame] | 571 | if (!_getcode(name, namelen, &code)) { |
Martin v. Löwis | ef7fe2e | 2002-11-23 18:01:32 +0000 | [diff] [blame^] | 572 | char fmt[] = "undefined character name '%s'"; |
| 573 | char *buf = PyMem_MALLOC(sizeof(fmt) + namelen); |
| 574 | sprintf(buf, fmt, name); |
| 575 | PyErr_SetString(PyExc_KeyError, buf); |
| 576 | PyMem_FREE(buf); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 577 | return NULL; |
| 578 | } |
| 579 | |
| 580 | str[0] = (Py_UNICODE) code; |
| 581 | return PyUnicode_FromUnicode(str, 1); |
| 582 | } |
| 583 | |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 584 | /* XXX Add doc strings. */ |
| 585 | |
| 586 | static PyMethodDef unicodedata_functions[] = { |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 587 | {"decimal", unicodedata_decimal, METH_VARARGS}, |
| 588 | {"digit", unicodedata_digit, METH_VARARGS}, |
| 589 | {"numeric", unicodedata_numeric, METH_VARARGS}, |
| 590 | {"category", unicodedata_category, METH_VARARGS}, |
| 591 | {"bidirectional", unicodedata_bidirectional, METH_VARARGS}, |
| 592 | {"combining", unicodedata_combining, METH_VARARGS}, |
| 593 | {"mirrored", unicodedata_mirrored, METH_VARARGS}, |
| 594 | {"decomposition",unicodedata_decomposition, METH_VARARGS}, |
| 595 | {"name", unicodedata_name, METH_VARARGS}, |
| 596 | {"lookup", unicodedata_lookup, METH_VARARGS}, |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 597 | {NULL, NULL} /* sentinel */ |
| 598 | }; |
| 599 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 600 | PyDoc_STRVAR(unicodedata_docstring, "unicode character database"); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 601 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 602 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 603 | initunicodedata(void) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 604 | { |
Fred Drake | a2bd8d3 | 2002-04-03 21:39:26 +0000 | [diff] [blame] | 605 | PyObject *m, *v; |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 606 | |
Fred Drake | f585bef | 2001-03-03 19:41:55 +0000 | [diff] [blame] | 607 | m = Py_InitModule3( |
| 608 | "unicodedata", unicodedata_functions, unicodedata_docstring); |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 609 | if (!m) |
| 610 | return; |
| 611 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 612 | /* Export C API */ |
| 613 | v = PyCObject_FromVoidPtr((void *) &hashAPI, NULL); |
Fred Drake | a2bd8d3 | 2002-04-03 21:39:26 +0000 | [diff] [blame] | 614 | if (v != NULL) |
| 615 | PyModule_AddObject(m, "ucnhash_CAPI", v); |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 616 | } |
Martin v. Löwis | 7d41e29 | 2002-11-23 12:22:32 +0000 | [diff] [blame] | 617 | |
| 618 | /* |
| 619 | Local variables: |
| 620 | c-basic-offset: 4 |
| 621 | End: |
| 622 | */ |