blob: e15ff27183f5613bd7e067ddf70c5e5e6872ca6d [file] [log] [blame]
Guido van Rossum2a70a3a2000-03-10 23:10:21 +00001/* ------------------------------------------------------------------------
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
7Written by Marc-Andre Lemburg (mal@lemburg.com).
8
Guido van Rossum16b1ad92000-08-03 16:24:25 +00009Copyright (c) Corporation for National Research Initiatives.
Guido van Rossum2a70a3a2000-03-10 23:10:21 +000010
11 ------------------------------------------------------------------------ */
12
13#include "Python.h"
14#include "unicodedatabase.h"
15
Fredrik Lundha4287c22000-09-24 21:45:34 +000016#define unicode_db _PyUnicode_Database_GetRecord
Guido van Rossum8a160542000-03-31 17:26:12 +000017
Guido van Rossum2a70a3a2000-03-10 23:10:21 +000018/* --- Module API --------------------------------------------------------- */
19
20static PyObject *
21unicodedata_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
54static PyObject *
55unicodedata_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
88static PyObject *
89unicodedata_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
122static PyObject *
123unicodedata_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 Rossum8a160542000-03-31 17:26:12 +0000137 index = (int)unicode_db((int)*PyUnicode_AS_UNICODE(v))->category;
Guido van Rossum2a70a3a2000-03-10 23:10:21 +0000138 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
152static PyObject *
153unicodedata_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 Rossum8a160542000-03-31 17:26:12 +0000167 index = (int)unicode_db((int)*PyUnicode_AS_UNICODE(v))->bidirectional;
Guido van Rossum2a70a3a2000-03-10 23:10:21 +0000168 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
182static PyObject *
183unicodedata_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 Rossum8a160542000-03-31 17:26:12 +0000197 value = (int)unicode_db((int)*PyUnicode_AS_UNICODE(v))->combining;
Guido van Rossum2a70a3a2000-03-10 23:10:21 +0000198 return PyInt_FromLong(value);
199
200 onError:
201 return NULL;
202}
203
204static PyObject *
205unicodedata_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 Rossum8a160542000-03-31 17:26:12 +0000219 value = (int)unicode_db((int)*PyUnicode_AS_UNICODE(v))->mirrored;
Guido van Rossum2a70a3a2000-03-10 23:10:21 +0000220 return PyInt_FromLong(value);
221
222 onError:
223 return NULL;
224}
225
226static PyObject *
227unicodedata_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 Rossum8a160542000-03-31 17:26:12 +0000241 value = unicode_db((int)*PyUnicode_AS_UNICODE(v))->decomposition;
Guido van Rossum2a70a3a2000-03-10 23:10:21 +0000242 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
253static 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
265DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000266initunicodedata(void)
Guido van Rossum2a70a3a2000-03-10 23:10:21 +0000267{
268 Py_InitModule("unicodedata", unicodedata_functions);
269}