-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjchar.cpp
More file actions
240 lines (229 loc) · 7.98 KB
/
jchar.cpp
File metadata and controls
240 lines (229 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "jchar.hpp"
static PyObject* jchar_desc(jcharObject *self) {
jchar x = jchar_Val(self);
Py_ssize_t newsize = 1;
switch(x) {
case '\0': case '\t': case '\n': case '\r':
newsize = 2;
break;
default:
if (x < ' ' || x >= 0x7f)
newsize = (x <= 0xff) ? 4 : 6;
}
PyObject *result = PyUnicode_New(newsize, 0xffff);
if (result != NULL) {
Py_UCS2 *data = PyUnicode_2BYTE_DATA(result);
switch(x) {
case '\t': case '\n': case '\r':
*data++ = '\\';
*data++ = x;
break;
case '\0':
*data++ = '\\';
*data++ = '0';
break;
default:
if (x >= ' ' && x < 0x7f) {
*data ++ = x;
} else if (x <= 0xff) {
*data++ = '\\';
*data++ = 'x';
*data++ = Py_hexdigits[(x & 0xf0) >> 4];
*data++ = Py_hexdigits[x & 0xf];
} else {
*data++ = '\\';
*data++ = 'u';
*data++ = Py_hexdigits[(x & 0xf000) >> 12];
*data++ = Py_hexdigits[(x & 0xf00) >> 8];
*data++ = Py_hexdigits[(x & 0xf0) >> 4];
*data++ = Py_hexdigits[x & 0xf];
}
}
}
return result;
}
static bool convert_to_jchar(PyObject *obj, jchar *val) {
if (jchar_Check(obj)) {
*val = jchar_Val(obj);
} else if (PyLong_Check(obj)) {
*val = PyLong_AsLong(obj);
} else {
return false;
}
return true;
}
static jcharObject* jchar_add(PyObject *self, PyObject *other) {
jchar a, b;
if (convert_to_jchar(self, &a) && convert_to_jchar(other, &b))
return jchar_FromValue(a + b);
return (jcharObject*)Py_NotImplemented;
}
static jcharObject* jchar_sub(PyObject *self, PyObject *other) {
jchar a, b;
if (convert_to_jchar(self, &a) && convert_to_jchar(other, &b))
return jchar_FromValue(a - b);
return (jcharObject*)Py_NotImplemented;
}
static jcharObject* jchar_mul(PyObject *self, PyObject *other) {
jchar a, b;
if (convert_to_jchar(self, &a) && convert_to_jchar(other, &b))
return jchar_FromValue(a * b);
return (jcharObject*)Py_NotImplemented;
}
static jcharObject* jchar_mod(PyObject *self, PyObject *other) {
jchar a, b;
if (convert_to_jchar(self, &a) && convert_to_jchar(other, &b))
return jchar_FromValue(a % b);
INC_RETURN_NOTIMPLEMENTED(jcharObject*);
}
static PyObject* jchar_divmod(PyObject *self, PyObject *other) {
PyObject *result = PyTuple_New(2);
jcharObject *div, *mod;
if (result) {
jchar a, b;
if (!convert_to_jchar(self, &a) || !convert_to_jchar(other, &b)) {
Py_DECREF(result);
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
div = jchar_FromValue(a / b);
mod = jchar_FromValue(a % b);
PyTuple_SetItem(result, 0, div);
PyTuple_SetItem(result, 1, mod);
}
return result;
}
static jcharObject* jchar_pow(PyObject *base, PyObject *exp, PyObject *mod) {
jchar a, b, c;
jcharObject *result;
if (mod == Py_None) {
if (convert_to_jchar(base, &a) && convert_to_jchar(exp, &b))
return jchar_FromValue(pow(a, b));
} else {
if (convert_to_jchar(base, &a) && convert_to_jchar(exp, &b), convert_to_jchar(mod, &c))
return jchar_FromValue((jchar)pow(a, b) % c);
}
INC_RETURN_NOTIMPLEMENTED(jcharObject*);
}
static bool jchar_bool(jcharObject *self) {
return jchar_Val(self);
}
static jcharObject* jchar_invert(jcharObject *self) {
return jchar_FromValue(~jchar_Val(self));
}
static jcharObject* jchar_lshift(PyObject *self, PyObject *other) {
jchar a, b;
if (convert_to_jchar(self, &a) && convert_to_jchar(other, &b))
return jchar_FromValue(a << b);
INC_RETURN_NOTIMPLEMENTED(jcharObject*);
}
static jcharObject* jchar_rshift(PyObject *self, PyObject *other) {
jchar a, b;
if (convert_to_jchar(self, &a) && convert_to_jchar(other, &b))
return jchar_FromValue(a >> b);
INC_RETURN_NOTIMPLEMENTED(jcharObject*);
}
static jcharObject* jchar_and(PyObject *self, PyObject *other) {
jchar a, b;
if (convert_to_jchar(self, &a) && convert_to_jchar(other, &b))
return jchar_FromValue(a & b);
INC_RETURN_NOTIMPLEMENTED(jcharObject*);
}
static jcharObject* jchar_xor(PyObject *self, PyObject *other) {
jchar a, b;
if (convert_to_jchar(self, &a) && convert_to_jchar(other, &b))
return jchar_FromValue(a ^ b);
INC_RETURN_NOTIMPLEMENTED(jcharObject*);
}
static jcharObject* jchar_or(PyObject *self, PyObject *other) {
jchar a, b;
if (convert_to_jchar(self, &a) && convert_to_jchar(other, &b))
return jchar_FromValue(a | b);
INC_RETURN_NOTIMPLEMENTED(jcharObject*);
}
static PyObject* jchar_long(jcharObject *self) {
return PyLong_FromLong(jchar_Val(self));
}
static PyObject* jchar_float(jcharObject *self) {
return PyFloat_FromDouble(jchar_Val(self));
}
static jcharObject* jchar_div(PyObject *self, PyObject *other) {
jchar a, b;
if (convert_to_jchar(self, &a) && convert_to_jchar(other, &b))
return jchar_FromValue(a / b);
INC_RETURN_NOTIMPLEMENTED(jcharObject*);
}
static PyObject* jchar_richcompare(jcharObject* self, PyObject *other, int op) {
jchar a, b;
if (convert_to_jchar(self, &a) && convert_to_jchar(other, &b))
Py_RETURN_RICHCOMPARE(a, b, op);
INC_RETURN_NOTIMPLEMENTED(jcharObject*);
}
static jcharObject* jchar_alloc(PyTypeObject *type, Py_ssize_t nitems) {
jcharObject *obj = (jcharObject*)PyType_GenericAlloc(type, nitems);
for (Py_ssize_t i=0; i<nitems+1; i++)
obj[i].desc = (descfunc)jchar_desc;
return obj;
}
static jcharObject* jchar_jchar(PyObject *x) {
if (jchar_Check(x)) {
INC_RETURN_TYPE(x, jcharObject*);
} else if (PyLong_Check(x)) {
return jchar_FromValue(PyLong_AsLong(x));
} else if (PyUnicode_Check(x)) {
Py_ssize_t length = PyBytes_Size(x);
if (length != 1) {
PyErr_Format(PyExc_TypeError, "argument length must be 1, not %zd", length);
return NULL;
}
return jchar_FromValue(PyBytes_AsString(x)[0]);
}
_BadArgument("jchar", "argument", "int or one-character-bytes", x);
return NULL;
}
static jcharObject* jchar_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
PyObject *x = NULL;
if (!PyArg_UnpackTuple(args, "jchar", 0, 1, &x))
return NULL;
if (x == NULL)
return jchar_FromValue(0);
return jchar_jchar(x);
}
static PyNumberMethods jchar_as_number = {
.nb_add = (binaryfunc)jchar_add,
.nb_subtract = (binaryfunc)jchar_sub,
.nb_multiply = (binaryfunc)jchar_mul,
.nb_remainder = (binaryfunc)jchar_mod,
.nb_divmod = (binaryfunc)jchar_divmod,
.nb_power = (ternaryfunc)jchar_pow,
.nb_bool = (inquiry)jchar_bool,
.nb_invert = (unaryfunc)jchar_invert,
.nb_lshift = (binaryfunc)jchar_lshift,
.nb_rshift = (binaryfunc)jchar_rshift,
.nb_and = (binaryfunc)jchar_and,
.nb_xor = (binaryfunc)jchar_xor,
.nb_or = (binaryfunc)jchar_or,
.nb_int = (unaryfunc)jchar_long,
.nb_float = (unaryfunc)jchar_float,
.nb_floor_divide = (binaryfunc)jchar_div,
.nb_true_divide = (binaryfunc)jchar_div,
.nb_index = (unaryfunc)jchar_long
};
PyTypeObject jchar_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "jchar",
.tp_as_number = &jchar_as_number,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = "jchar type",
.tp_richcompare = (richcmpfunc)jchar_richcompare,
.tp_base = &jvalue_Type,
.tp_alloc = (allocfunc)jchar_alloc,
.tp_new = (newfunc)jchar_new,
};
jcharObject *jchar_FromValue(jchar value) {
PyTypeObject *type = &jchar_Type;
jcharObject *self = (jcharObject*)type->tp_alloc(type, 0);
if (self != NULL)
jchar_Val(self) = value;
return self;
}