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 | |
| 9 | (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
| 10 | |
| 11 | ------------------------------------------------------------------------ */ |
| 12 | |
| 13 | #include "Python.h" |
| 14 | #include "unicodedatabase.h" |
| 15 | |
| 16 | /* --- Module API --------------------------------------------------------- */ |
| 17 | |
| 18 | static PyObject * |
| 19 | unicodedata_decimal(PyObject *self, |
| 20 | PyObject *args) |
| 21 | { |
| 22 | PyUnicodeObject *v; |
| 23 | PyObject *defobj = NULL; |
| 24 | long rc; |
| 25 | |
| 26 | if (!PyArg_ParseTuple(args, "O!|O:decimal", |
| 27 | &PyUnicode_Type, &v, &defobj)) |
| 28 | goto onError; |
| 29 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 30 | PyErr_SetString(PyExc_TypeError, |
| 31 | "need a single Unicode character as parameter"); |
| 32 | goto onError; |
| 33 | } |
| 34 | rc = Py_UNICODE_TODECIMAL(*PyUnicode_AS_UNICODE(v)); |
| 35 | if (rc < 0) { |
| 36 | if (defobj == NULL) { |
| 37 | PyErr_SetString(PyExc_ValueError, |
| 38 | "not a decimal"); |
| 39 | goto onError; |
| 40 | } |
| 41 | else { |
| 42 | Py_INCREF(defobj); |
| 43 | return defobj; |
| 44 | } |
| 45 | } |
| 46 | return PyInt_FromLong(rc); |
| 47 | |
| 48 | onError: |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | static PyObject * |
| 53 | unicodedata_digit(PyObject *self, |
| 54 | PyObject *args) |
| 55 | { |
| 56 | PyUnicodeObject *v; |
| 57 | PyObject *defobj = NULL; |
| 58 | long rc; |
| 59 | |
| 60 | if (!PyArg_ParseTuple(args, "O!|O:digit", |
| 61 | &PyUnicode_Type, &v, &defobj)) |
| 62 | goto onError; |
| 63 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 64 | PyErr_SetString(PyExc_TypeError, |
| 65 | "need a single Unicode character as parameter"); |
| 66 | goto onError; |
| 67 | } |
| 68 | rc = Py_UNICODE_TODIGIT(*PyUnicode_AS_UNICODE(v)); |
| 69 | if (rc < 0) { |
| 70 | if (defobj == NULL) { |
| 71 | PyErr_SetString(PyExc_ValueError, |
| 72 | "not a digit"); |
| 73 | goto onError; |
| 74 | } |
| 75 | else { |
| 76 | Py_INCREF(defobj); |
| 77 | return defobj; |
| 78 | } |
| 79 | } |
| 80 | return PyInt_FromLong(rc); |
| 81 | |
| 82 | onError: |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | static PyObject * |
| 87 | unicodedata_numeric(PyObject *self, |
| 88 | PyObject *args) |
| 89 | { |
| 90 | PyUnicodeObject *v; |
| 91 | PyObject *defobj = NULL; |
| 92 | double rc; |
| 93 | |
| 94 | if (!PyArg_ParseTuple(args, "O!|O:numeric", |
| 95 | &PyUnicode_Type, &v, &defobj)) |
| 96 | goto onError; |
| 97 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 98 | PyErr_SetString(PyExc_TypeError, |
| 99 | "need a single Unicode character as parameter"); |
| 100 | goto onError; |
| 101 | } |
| 102 | rc = Py_UNICODE_TONUMERIC(*PyUnicode_AS_UNICODE(v)); |
| 103 | if (rc < 0) { |
| 104 | if (defobj == NULL) { |
| 105 | PyErr_SetString(PyExc_ValueError, |
| 106 | "not a numeric character"); |
| 107 | goto onError; |
| 108 | } |
| 109 | else { |
| 110 | Py_INCREF(defobj); |
| 111 | return defobj; |
| 112 | } |
| 113 | } |
| 114 | return PyFloat_FromDouble(rc); |
| 115 | |
| 116 | onError: |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | static PyObject * |
| 121 | unicodedata_category(PyObject *self, |
| 122 | PyObject *args) |
| 123 | { |
| 124 | PyUnicodeObject *v; |
| 125 | int index; |
| 126 | |
| 127 | if (!PyArg_ParseTuple(args, "O!:category", |
| 128 | &PyUnicode_Type, &v)) |
| 129 | goto onError; |
| 130 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 131 | PyErr_SetString(PyExc_TypeError, |
| 132 | "need a single Unicode character as parameter"); |
| 133 | goto onError; |
| 134 | } |
| 135 | index = (int)_PyUnicode_Database[(int)*PyUnicode_AS_UNICODE(v)].category; |
| 136 | if (index < 0 || |
| 137 | index > sizeof(_PyUnicode_CategoryNames) / |
| 138 | sizeof(_PyUnicode_CategoryNames[0])) { |
| 139 | PyErr_Format(PyExc_SystemError, |
| 140 | "category index out of range: %i", |
| 141 | index); |
| 142 | goto onError; |
| 143 | } |
| 144 | return PyString_FromString(_PyUnicode_CategoryNames[index]); |
| 145 | |
| 146 | onError: |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | static PyObject * |
| 151 | unicodedata_bidirectional(PyObject *self, |
| 152 | PyObject *args) |
| 153 | { |
| 154 | PyUnicodeObject *v; |
| 155 | int index; |
| 156 | |
| 157 | if (!PyArg_ParseTuple(args, "O!:bidirectional", |
| 158 | &PyUnicode_Type, &v)) |
| 159 | goto onError; |
| 160 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 161 | PyErr_SetString(PyExc_TypeError, |
| 162 | "need a single Unicode character as parameter"); |
| 163 | goto onError; |
| 164 | } |
| 165 | index = (int)_PyUnicode_Database[ |
| 166 | (int)*PyUnicode_AS_UNICODE(v)].bidirectional; |
| 167 | if (index < 0 || |
| 168 | index > sizeof(_PyUnicode_CategoryNames) / |
| 169 | sizeof(_PyUnicode_CategoryNames[0])) { |
| 170 | PyErr_Format(PyExc_SystemError, |
| 171 | "bidirectional index out of range: %i", |
| 172 | index); |
| 173 | goto onError; |
| 174 | } |
| 175 | return PyString_FromString(_PyUnicode_BidirectionalNames[index]); |
| 176 | |
| 177 | onError: |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | static PyObject * |
| 182 | unicodedata_combining(PyObject *self, |
| 183 | PyObject *args) |
| 184 | { |
| 185 | PyUnicodeObject *v; |
| 186 | int value; |
| 187 | |
| 188 | if (!PyArg_ParseTuple(args, "O!:combining", |
| 189 | &PyUnicode_Type, &v)) |
| 190 | goto onError; |
| 191 | if (PyUnicode_GET_SIZE(v) != 1) { |
| 192 | PyErr_SetString(PyExc_TypeError, |
| 193 | "need a single Unicode character as parameter"); |
| 194 | goto onError; |
| 195 | } |
| 196 | value = (int)_PyUnicode_Database[ |
| 197 | (int)*PyUnicode_AS_UNICODE(v)].combining; |
| 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 | } |
| 219 | value = (int)_PyUnicode_Database[(int)*PyUnicode_AS_UNICODE(v)].mirrored; |
| 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 | } |
| 241 | value = _PyUnicode_Database[(int)*PyUnicode_AS_UNICODE(v)].decomposition; |
| 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) |
| 266 | initunicodedata() |
| 267 | { |
| 268 | Py_InitModule("unicodedata", unicodedata_functions); |
| 269 | } |