5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
12 * Catacomb/Python is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Catacomb/Python is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Catacomb/Python; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- Main code ---------------------------------------------------------*/
33 PyTypeObject *bytestring_pytype;
35 static PyObject *empty, *bytev[256];
37 static PyObject *allocate(PyTypeObject *ty, size_t n)
40 x = (PyStringObject *)ty->tp_alloc(ty, n);
42 #if defined(CACHE_HASH) || PY_VERSION_HEX >= 0x02030000
45 x->ob_sstate = SSTATE_NOT_INTERNED;
46 return ((PyObject *)x);
49 static PyObject *dowrap(PyTypeObject *ty, const void *p, size_t n)
54 if (p && ty == bytestring_pytype) {
56 if (!empty) empty = allocate(ty, 0);
57 Py_INCREF(empty); return (empty);
58 } else if (n == 1 && (ch = *(unsigned char *)p) < sizeof(bytev)) {
60 { bytev[ch] = allocate(ty, 1); *PyString_AS_STRING(bytev[ch]) = ch; }
61 Py_INCREF(bytev[ch]); return (bytev[ch]);
66 if (p) memcpy(PyString_AS_STRING(x), p, n);
70 PyObject *bytestring_pywrap(const void *p, size_t n)
71 { return (dowrap(bytestring_pytype, p, n)); }
73 PyObject *bytestring_pywrapbuf(buf *b)
74 { return (dowrap(bytestring_pytype, BCUR(b), BLEFT(b))); }
76 static PyObject *bytestring_pynew(PyTypeObject *ty,
77 PyObject *arg, PyObject *kw)
81 static const char *const kwlist[] = { "data", 0 };
82 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &p, &n))
84 return (dowrap(ty, p, n));
87 static PyObject *meth_ctstreq(PyObject *me, PyObject *arg)
91 if (!PyArg_ParseTuple(arg, "s#s#:ctstreq", &p, &psz, &q, &qsz))
93 if (psz == qsz && ct_memeq(p, q, psz)) RETURN_TRUE;
99 static PyObject *meth__ByteString_zero(PyObject *me, PyObject *arg)
103 if (!PyArg_ParseTuple(arg, "OO&:zero", &me, convszt, &sz)) goto end;
104 rc = bytestring_pywrap(0, sz);
105 memset(PyString_AS_STRING(rc), 0, sz);
110 static PyObject *bytestring_pyrichcompare(PyObject *me,
111 PyObject *you, int op)
114 void *mystr, *yourstr;
115 Py_ssize_t mylen, yourlen, minlen;
117 if (!PyString_Check(me) || !PyString_Check(you)) RETURN_NOTIMPL;
118 mystr = PyString_AS_STRING(me); mylen = PyString_GET_SIZE(me);
119 yourstr = PyString_AS_STRING(you); yourlen = PyString_GET_SIZE(you);
123 b = mylen == yourlen && ct_memeq(mystr, yourstr, mylen);
126 b = mylen != yourlen || !ct_memeq(mystr, yourstr, mylen);
129 minlen = mylen < yourlen ? mylen : yourlen;
130 b = memcmp(mystr, yourstr, minlen);
131 if (!b) b = mylen < yourlen ? -1 : mylen > yourlen ? +1 : 0;
133 case Py_LT: b = b < 0; break;
134 case Py_LE: b = b <= 0; break;
135 case Py_GE: b = b >= 0; break;
136 case Py_GT: b = b > 0; break;
144 static PyObject *bytestring_pyconcat(PyObject *x, PyObject *y)
146 const void *xv; Py_ssize_t xsz;
147 const void *yv; Py_ssize_t ysz;
148 PyObject *z = 0; char *zp; size_t zsz;
150 if (PyObject_AsReadBuffer(x, &xv, &xsz) ||
151 PyObject_AsReadBuffer(y, &yv, &ysz))
153 zsz = (size_t)xsz + (size_t)ysz;
154 if (xsz < 0 || ysz < 0 || zsz < xsz) VALERR("too long");
155 z = bytestring_pywrap(0, zsz); zp = PyString_AS_STRING(z);
156 memcpy(zp, xv, xsz); memcpy(zp + xsz, yv, ysz);
161 static PyObject *bytestring_pyrepeat(PyObject *me, Py_ssize_t n)
163 const unsigned char *xp; size_t xsz;
164 PyObject *z = 0; char *zp; size_t zsz;
166 xp = (const unsigned char *)PyString_AS_STRING(me);
167 xsz = PyString_GET_SIZE(me);
168 if (n < 0 || (n && xsz >= (size_t)-1/n)) VALERR("too long");
169 zsz = n*xsz; z = bytestring_pywrap(0, zsz); zp = PyString_AS_STRING(z);
170 if (xsz == 1) memset(zp, *xp, zsz);
171 else while (zsz) { memcpy(zp, xp, xsz); zp += xsz; zsz -= xsz; }
176 static PyObject *bytestring_pyitem(PyObject *me, Py_ssize_t i)
180 if (i < 0 || i >= PyString_GET_SIZE(me)) IXERR("out of range");
181 rc = bytestring_pywrap(PyString_AS_STRING(me) + i, 1);
186 static PyObject *bytestring_pyslice(PyObject *me, Py_ssize_t i, Py_ssize_t j)
189 size_t n = PyString_GET_SIZE(me);
193 else if (j > n) j = n;
194 if (j < i) i = j = 0;
195 if (i == 0 && j == n && me->ob_type == bytestring_pytype)
196 { Py_INCREF(me); rc = me; goto end; }
197 rc = bytestring_pywrap(PyString_AS_STRING(me) + i, j - i);
202 static PyObject *bytestring_pysubscript(PyObject *me, PyObject *ix)
204 Py_ssize_t i, j, k, n;
205 const unsigned char *p;
209 if (PyIndex_Check(ix)) {
210 i = PyNumber_AsSsize_t(ix, PyExc_IndexError);
211 if (i == -1 && PyErr_Occurred()) return (0);
212 if (i < 0) i += PyString_GET_SIZE(me);
213 rc = bytestring_pyitem(me, i);
214 } else if (PySlice_Check(ix)) {
215 if (PySlice_GetIndicesEx((PySliceObject *)ix, PyString_GET_SIZE(me),
218 if (k == 1) return bytestring_pyslice(me, i, j);
219 rc = bytestring_pywrap(0, n);
220 p = (unsigned char *)PyString_AS_STRING(me) + i;
221 q = (unsigned char *)PyString_AS_STRING(rc);
222 while (n--) { *q++ = *p; p += k; }
224 TYERR("wanted integer or slice");
229 #define BINOP(name, op) \
230 static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \
231 const void *xv, *yv; \
232 const unsigned char *xp, *yp; \
234 Py_ssize_t xsz, ysz; \
237 if (PyObject_AsReadBuffer(x, &xv, &xsz) || \
238 PyObject_AsReadBuffer(y, &yv, &ysz)) \
240 if (xsz != ysz) VALERR("length mismatch"); \
241 rc = bytestring_pywrap(0, xsz); \
242 xp = xv; yp = yv; zp = (unsigned char *)PyString_AS_STRING(rc); \
243 for (i = xsz; i > 0; i--) *zp++ = *xp++ op *yp++; \
251 #define UNOP(name, op) \
252 static PyObject *bytestring_py##name(PyObject *x) { \
254 const unsigned char *xp; \
259 if (PyObject_AsReadBuffer(x, &xv, &xsz)) goto end; \
260 rc = bytestring_pywrap(0, xsz); \
261 xp = xv; zp = (unsigned char *)PyString_AS_STRING(rc); \
262 for (i = xsz; i > 0; i--) *zp++ = op *xp++; \
268 static PyNumberMethods bytestring_pynumber = {
270 0, /* @nb_subtract@ */
271 0, /* @nb_multiply@ */
273 0, /* @nb_remainder@ */
276 0, /* @nb_negative@ */
277 0, /* @nb_positive@ */
278 0, /* @nb_absolute@ */
279 0, /* @nb_nonzero@ */
280 bytestring_pynot, /* @nb_invert@ */
283 bytestring_pyand, /* @nb_and@ */
284 bytestring_pyxor, /* @nb_xor@ */
285 bytestring_pyor, /* @nb_or@ */
294 static PySequenceMethods bytestring_pysequence = {
296 bytestring_pyconcat, /* @sq_concat@ */
297 bytestring_pyrepeat, /* @sq_repeat@ */
298 bytestring_pyitem, /* @sq_item@ */
299 bytestring_pyslice, /* @sq_slice@ */
300 0, /* @sq_ass_item@ */
301 0, /* @sq_ass_slice@ */
302 0, /* @sq_contains@ */
303 0, /* @sq_inplace_concat@ */
304 0, /* @sq_inplace_repeat@ */
307 static PyMappingMethods bytestring_pymapping = {
309 bytestring_pysubscript, /* @mp_subscript@ */
310 0, /* @mp_ass_subscript@ */
313 static PyBufferProcs bytestring_pybuffer;
315 static PyTypeObject bytestring_pytype_skel = {
316 PyObject_HEAD_INIT(0) 0, /* Header */
317 "ByteString", /* @tp_name@ */
318 0, /* @tp_basicsize@ */
319 0, /* @tp_itemsize@ */
321 0, /* @tp_dealloc@ */
323 0, /* @tp_getattr@ */
324 0, /* @tp_setattr@ */
325 0, /* @tp_compare@ */
327 &bytestring_pynumber, /* @tp_as_number@ */
328 &bytestring_pysequence, /* @tp_as_sequence@ */
329 &bytestring_pymapping, /* @tp_as_mapping@ */
333 0, /* @tp_getattro@ */
334 0, /* @tp_setattro@ */
335 &bytestring_pybuffer, /* @tp_as_buffer@ */
336 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
337 Py_TPFLAGS_CHECKTYPES |
341 "ByteString(STR): byte string class.",
343 0, /* @tp_traverse@ */
345 bytestring_pyrichcompare, /* @tp_richcompare@ */
346 0, /* @tp_weaklistoffset@ */
348 0, /* @tp_iternext@ */
349 0, /* @tp_methods@ */
350 0, /* @tp_members@ */
354 0, /* @tp_descr_get@ */
355 0, /* @tp_descr_set@ */
356 0, /* @tp_dictoffset@ */
358 PyType_GenericAlloc, /* @tp_alloc@ */
359 bytestring_pynew, /* @tp_new@ */
364 /*----- Initialization ----------------------------------------------------*/
366 static PyMethodDef methods[] = {
367 #define METHNAME(func) meth_##func
368 METH (ctstreq, "ctstreq(S, T) -> BOOL")
369 METH (_ByteString_zero, "zero(N) -> 0000...00")
374 #define string_pytype &PyString_Type
375 void bytestring_pyinit(void)
377 INITTYPE(bytestring, string);
381 void bytestring_pyinsert(PyObject *mod)
383 INSERT("ByteString", bytestring_pytype);
386 /*----- That's all, folks -------------------------------------------------*/