1 /* ------------------------------------------------------------------------
3 unicodedata -- Provides access to the Unicode 3.0 data base.
5 Data was extracted from the Unicode 3.0 UnicodeData.txt file.
7 Written by Marc-Andre Lemburg (mal@lemburg.com).
8 Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com)
10 Copyright (c) Corporation for National Research Initiatives.
12 ------------------------------------------------------------------------ */
15 #include "unicodedatabase.h"
17 /* --- Module API --------------------------------------------------------- */
20 unicodedata_decimal(PyObject
*self
,
24 PyObject
*defobj
= NULL
;
27 if (!PyArg_ParseTuple(args
, "O!|O:decimal",
28 &PyUnicode_Type
, &v
, &defobj
))
30 if (PyUnicode_GET_SIZE(v
) != 1) {
31 PyErr_SetString(PyExc_TypeError
,
32 "need a single Unicode character as parameter");
35 rc
= Py_UNICODE_TODECIMAL(*PyUnicode_AS_UNICODE(v
));
38 PyErr_SetString(PyExc_ValueError
,
47 return PyInt_FromLong(rc
);
54 unicodedata_digit(PyObject
*self
,
58 PyObject
*defobj
= NULL
;
61 if (!PyArg_ParseTuple(args
, "O!|O:digit",
62 &PyUnicode_Type
, &v
, &defobj
))
64 if (PyUnicode_GET_SIZE(v
) != 1) {
65 PyErr_SetString(PyExc_TypeError
,
66 "need a single Unicode character as parameter");
69 rc
= Py_UNICODE_TODIGIT(*PyUnicode_AS_UNICODE(v
));
72 PyErr_SetString(PyExc_ValueError
,
81 return PyInt_FromLong(rc
);
88 unicodedata_numeric(PyObject
*self
,
92 PyObject
*defobj
= NULL
;
95 if (!PyArg_ParseTuple(args
, "O!|O:numeric",
96 &PyUnicode_Type
, &v
, &defobj
))
98 if (PyUnicode_GET_SIZE(v
) != 1) {
99 PyErr_SetString(PyExc_TypeError
,
100 "need a single Unicode character as parameter");
103 rc
= Py_UNICODE_TONUMERIC(*PyUnicode_AS_UNICODE(v
));
105 if (defobj
== NULL
) {
106 PyErr_SetString(PyExc_ValueError
,
107 "not a numeric character");
115 return PyFloat_FromDouble(rc
);
122 unicodedata_category(PyObject
*self
,
128 if (!PyArg_ParseTuple(args
, "O!:category",
129 &PyUnicode_Type
, &v
))
131 if (PyUnicode_GET_SIZE(v
) != 1) {
132 PyErr_SetString(PyExc_TypeError
,
133 "need a single Unicode character as parameter");
136 index
= (int) _PyUnicode_Database_GetRecord(
137 (int) *PyUnicode_AS_UNICODE(v
)
139 return PyString_FromString(_PyUnicode_CategoryNames
[index
]);
146 unicodedata_bidirectional(PyObject
*self
,
152 if (!PyArg_ParseTuple(args
, "O!:bidirectional",
153 &PyUnicode_Type
, &v
))
155 if (PyUnicode_GET_SIZE(v
) != 1) {
156 PyErr_SetString(PyExc_TypeError
,
157 "need a single Unicode character as parameter");
160 index
= (int) _PyUnicode_Database_GetRecord(
161 (int) *PyUnicode_AS_UNICODE(v
)
163 return PyString_FromString(_PyUnicode_BidirectionalNames
[index
]);
170 unicodedata_combining(PyObject
*self
,
176 if (!PyArg_ParseTuple(args
, "O!:combining",
177 &PyUnicode_Type
, &v
))
179 if (PyUnicode_GET_SIZE(v
) != 1) {
180 PyErr_SetString(PyExc_TypeError
,
181 "need a single Unicode character as parameter");
184 value
= (int) _PyUnicode_Database_GetRecord(
185 (int) *PyUnicode_AS_UNICODE(v
)
187 return PyInt_FromLong(value
);
194 unicodedata_mirrored(PyObject
*self
,
200 if (!PyArg_ParseTuple(args
, "O!:mirrored",
201 &PyUnicode_Type
, &v
))
203 if (PyUnicode_GET_SIZE(v
) != 1) {
204 PyErr_SetString(PyExc_TypeError
,
205 "need a single Unicode character as parameter");
208 value
= (int) _PyUnicode_Database_GetRecord(
209 (int) *PyUnicode_AS_UNICODE(v
)
211 return PyInt_FromLong(value
);
218 unicodedata_decomposition(PyObject
*self
,
224 if (!PyArg_ParseTuple(args
, "O!:decomposition",
225 &PyUnicode_Type
, &v
))
227 if (PyUnicode_GET_SIZE(v
) != 1) {
228 PyErr_SetString(PyExc_TypeError
,
229 "need a single Unicode character as parameter");
232 value
= _PyUnicode_Database_GetDecomposition(
233 (int) *PyUnicode_AS_UNICODE(v
)
235 return PyString_FromString(value
);
241 /* XXX Add doc strings. */
243 static PyMethodDef unicodedata_functions
[] = {
244 {"decimal", unicodedata_decimal
, 1},
245 {"digit", unicodedata_digit
, 1},
246 {"numeric", unicodedata_numeric
, 1},
247 {"category", unicodedata_category
, 1},
248 {"bidirectional", unicodedata_bidirectional
, 1},
249 {"combining", unicodedata_combining
, 1},
250 {"mirrored", unicodedata_mirrored
, 1},
251 {"decomposition", unicodedata_decomposition
, 1},
252 {NULL
, NULL
} /* sentinel */
256 initunicodedata(void)
258 Py_InitModule("unicodedata", unicodedata_functions
);