Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 1 | /* ------------------------------------------------------------------------ |
| 2 | |
| 3 | unicodedata -- Provides access to the Unicode 3.0 data base. |
| 4 | |
| 5 | Data was extracted from the Unicode 3.0 UnicodeData.txt file. |
| 6 | |
| 7 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 8 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 9 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 10 | |
| 11 | ------------------------------------------------------------------------ */ |
| 12 | |
| 13 | #include "Python.h" |
| 14 | #include "unicodedatabase.h" |
| 15 | |
Fredrik Lundh | a4287c2 | 2000-09-24 21:45:34 +0000 | [diff] [blame^] | 16 | #define unicode_db _PyUnicode_Database_GetRecord |
Guido van Rossum | 8a16054 | 2000-03-31 17:26:12 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 18 | /* --- Module API --------------------------------------------------------- */ |
| 19 | |
| 20 | static PyObject * |
| 21 | unicodedata_decimal(PyObject *self, |
| 22 | PyObject *args) |
| 23 | { |
| 24 | PyUnicodeObject *v; |
| 25 | PyObject *defobj = NULL; |
| 26 | long rc; |
| 27 | |
| 28 | if (!PyArg_ParseTuple(args, "O!|O:decimal", |
| 29 | &PyUnicode_Type, &v, &defobj)) |
| 30 | goto onError; |
| 31 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 32 | PyErr_SetString(PyExc_TypeError, |
| 33 | "need a single Unicode character as parameter"); |
| 34 | goto onError; |
| 35 | } |
| 36 | rc = Py_UNICODE_TODECIMAL(*PyUnicode_AS_UNICODE(v)); |
| 37 | if (rc < 0) { |
| 38 | if (defobj == NULL) { |
| 39 | PyErr_SetString(PyExc_ValueError, |
| 40 | "not a decimal"); |
| 41 | goto onError; |
| 42 | } |
| 43 | else { |
| 44 | Py_INCREF(defobj); |
| 45 | return defobj; |
| 46 | } |
| 47 | } |
| 48 | return PyInt_FromLong(rc); |
| 49 | |
| 50 | onError: |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | static PyObject * |
| 55 | unicodedata_digit(PyObject *self, |
| 56 | PyObject *args) |
| 57 | { |
| 58 | PyUnicodeObject *v; |
| 59 | PyObject *defobj = NULL; |
| 60 | long rc; |
| 61 | |
| 62 | if (!PyArg_ParseTuple(args, "O!|O:digit", |
| 63 | &PyUnicode_Type, &v, &defobj)) |
| 64 | goto onError; |
| 65 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 66 | PyErr_SetString(PyExc_TypeError, |
| 67 | "need a single Unicode character as parameter"); |
| 68 | goto onError; |
| 69 | } |
| 70 | rc = Py_UNICODE_TODIGIT(*PyUnicode_AS_UNICODE(v)); |
| 71 | if (rc < 0) { |
| 72 | if (defobj == NULL) { |
| 73 | PyErr_SetString(PyExc_ValueError, |
| 74 | "not a digit"); |
| 75 | goto onError; |
| 76 | } |
| 77 | else { |
| 78 | Py_INCREF(defobj); |
| 79 | return defobj; |
| 80 | } |
| 81 | } |
| 82 | return PyInt_FromLong(rc); |
| 83 | |
| 84 | onError: |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | static PyObject * |
| 89 | unicodedata_numeric(PyObject *self, |
| 90 | PyObject *args) |
| 91 | { |
| 92 | PyUnicodeObject *v; |
| 93 | PyObject *defobj = NULL; |
| 94 | double rc; |
| 95 | |
| 96 | if (!PyArg_ParseTuple(args, "O!|O:numeric", |
| 97 | &PyUnicode_Type, &v, &defobj)) |
| 98 | goto onError; |
| 99 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 100 | PyErr_SetString(PyExc_TypeError, |
| 101 | "need a single Unicode character as parameter"); |
| 102 | goto onError; |
| 103 | } |
| 104 | rc = Py_UNICODE_TONUMERIC(*PyUnicode_AS_UNICODE(v)); |
| 105 | if (rc < 0) { |
| 106 | if (defobj == NULL) { |
| 107 | PyErr_SetString(PyExc_ValueError, |
| 108 | "not a numeric character"); |
| 109 | goto onError; |
| 110 | } |
| 111 | else { |
| 112 | Py_INCREF(defobj); |
| 113 | return defobj; |
| 114 | } |
| 115 | } |
| 116 | return PyFloat_FromDouble(rc); |
| 117 | |
| 118 | onError: |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | static PyObject * |
| 123 | unicodedata_category(PyObject *self, |
| 124 | PyObject *args) |
| 125 | { |
| 126 | PyUnicodeObject *v; |
| 127 | int index; |
| 128 | |
| 129 | if (!PyArg_ParseTuple(args, "O!:category", |
| 130 | &PyUnicode_Type, &v)) |
| 131 | goto onError; |
| 132 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 133 | PyErr_SetString(PyExc_TypeError, |
| 134 | "need a single Unicode character as parameter"); |
| 135 | goto onError; |
| 136 | } |
Guido van Rossum | 8a16054 | 2000-03-31 17:26:12 +0000 | [diff] [blame] | 137 | index = (int)unicode_db((int)*PyUnicode_AS_UNICODE(v))->category; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 138 | if (index < 0 || |
| 139 | index > sizeof(_PyUnicode_CategoryNames) / |
| 140 | sizeof(_PyUnicode_CategoryNames[0])) { |
| 141 | PyErr_Format(PyExc_SystemError, |
| 142 | "category index out of range: %i", |
| 143 | index); |
| 144 | goto onError; |
| 145 | } |
| 146 | return PyString_FromString(_PyUnicode_CategoryNames[index]); |
| 147 | |
| 148 | onError: |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | static PyObject * |
| 153 | unicodedata_bidirectional(PyObject *self, |
| 154 | PyObject *args) |
| 155 | { |
| 156 | PyUnicodeObject *v; |
| 157 | int index; |
| 158 | |
| 159 | if (!PyArg_ParseTuple(args, "O!:bidirectional", |
| 160 | &PyUnicode_Type, &v)) |
| 161 | goto onError; |
| 162 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 163 | PyErr_SetString(PyExc_TypeError, |
| 164 | "need a single Unicode character as parameter"); |
| 165 | goto onError; |
| 166 | } |
Guido van Rossum | 8a16054 | 2000-03-31 17:26:12 +0000 | [diff] [blame] | 167 | index = (int)unicode_db((int)*PyUnicode_AS_UNICODE(v))->bidirectional; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 168 | if (index < 0 || |
| 169 | index > sizeof(_PyUnicode_CategoryNames) / |
| 170 | sizeof(_PyUnicode_CategoryNames[0])) { |
| 171 | PyErr_Format(PyExc_SystemError, |
| 172 | "bidirectional index out of range: %i", |
| 173 | index); |
| 174 | goto onError; |
| 175 | } |
| 176 | return PyString_FromString(_PyUnicode_BidirectionalNames[index]); |
| 177 | |
| 178 | onError: |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | static PyObject * |
| 183 | unicodedata_combining(PyObject *self, |
| 184 | PyObject *args) |
| 185 | { |
| 186 | PyUnicodeObject *v; |
| 187 | int value; |
| 188 | |
| 189 | if (!PyArg_ParseTuple(args, "O!:combining", |
| 190 | &PyUnicode_Type, &v)) |
| 191 | goto onError; |
| 192 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 193 | PyErr_SetString(PyExc_TypeError, |
| 194 | "need a single Unicode character as parameter"); |
| 195 | goto onError; |
| 196 | } |
Guido van Rossum | 8a16054 | 2000-03-31 17:26:12 +0000 | [diff] [blame] | 197 | value = (int)unicode_db((int)*PyUnicode_AS_UNICODE(v))->combining; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 198 | return PyInt_FromLong(value); |
| 199 | |
| 200 | onError: |
| 201 | return NULL; |
| 202 | } |
| 203 | |
| 204 | static PyObject * |
| 205 | unicodedata_mirrored(PyObject *self, |
| 206 | PyObject *args) |
| 207 | { |
| 208 | PyUnicodeObject *v; |
| 209 | int value; |
| 210 | |
| 211 | if (!PyArg_ParseTuple(args, "O!:mirrored", |
| 212 | &PyUnicode_Type, &v)) |
| 213 | goto onError; |
| 214 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 215 | PyErr_SetString(PyExc_TypeError, |
| 216 | "need a single Unicode character as parameter"); |
| 217 | goto onError; |
| 218 | } |
Guido van Rossum | 8a16054 | 2000-03-31 17:26:12 +0000 | [diff] [blame] | 219 | value = (int)unicode_db((int)*PyUnicode_AS_UNICODE(v))->mirrored; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 220 | return PyInt_FromLong(value); |
| 221 | |
| 222 | onError: |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | static PyObject * |
| 227 | unicodedata_decomposition(PyObject *self, |
| 228 | PyObject *args) |
| 229 | { |
| 230 | PyUnicodeObject *v; |
| 231 | const char *value; |
| 232 | |
| 233 | if (!PyArg_ParseTuple(args, "O!:decomposition", |
| 234 | &PyUnicode_Type, &v)) |
| 235 | goto onError; |
| 236 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 237 | PyErr_SetString(PyExc_TypeError, |
| 238 | "need a single Unicode character as parameter"); |
| 239 | goto onError; |
| 240 | } |
Guido van Rossum | 8a16054 | 2000-03-31 17:26:12 +0000 | [diff] [blame] | 241 | value = unicode_db((int)*PyUnicode_AS_UNICODE(v))->decomposition; |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 242 | if (value == NULL) |
| 243 | return PyString_FromString(""); |
| 244 | else |
| 245 | return PyString_FromString(value); |
| 246 | |
| 247 | onError: |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | /* XXX Add doc strings. */ |
| 252 | |
| 253 | static PyMethodDef unicodedata_functions[] = { |
| 254 | {"decimal", unicodedata_decimal, 1}, |
| 255 | {"digit", unicodedata_digit, 1}, |
| 256 | {"numeric", unicodedata_numeric, 1}, |
| 257 | {"category", unicodedata_category, 1}, |
| 258 | {"bidirectional", unicodedata_bidirectional, 1}, |
| 259 | {"combining", unicodedata_combining, 1}, |
| 260 | {"mirrored", unicodedata_mirrored, 1}, |
| 261 | {"decomposition", unicodedata_decomposition, 1}, |
| 262 | {NULL, NULL} /* sentinel */ |
| 263 | }; |
| 264 | |
| 265 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 266 | initunicodedata(void) |
Guido van Rossum | 2a70a3a | 2000-03-10 23:10:21 +0000 | [diff] [blame] | 267 | { |
| 268 | Py_InitModule("unicodedata", unicodedata_functions); |
| 269 | } |