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