bytestring.c: Cache empty and singleton strings.
[catacomb-python] / bytestring.c
CommitLineData
d7ab1bab 1/* -*-c-*-
2 *
d7ab1bab 3 * Byte strings
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
b2687a0a 8/*----- Licensing notice --------------------------------------------------*
d7ab1bab 9 *
10 * This file is part of the Python interface to Catacomb.
11 *
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.
b2687a0a 16 *
d7ab1bab 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.
b2687a0a 21 *
d7ab1bab 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.
25 */
26
27/*----- Header files ------------------------------------------------------*/
28
29#include "catacomb-python.h"
30
31/*----- Main code ---------------------------------------------------------*/
32
33PyTypeObject *bytestring_pytype;
34
4d09d07e
MW
35static PyObject *empty, *bytev[256];
36
438b1639 37static PyObject *allocate(PyTypeObject *ty, size_t n)
d7ab1bab 38{
438b1639
MW
39 PyStringObject *x;
40 x = (PyStringObject *)ty->tp_alloc(ty, n);
d7ab1bab 41 x->ob_sval[n] = 0;
69e13768 42#if defined(CACHE_HASH) || PY_VERSION_HEX >= 0x02030000
d7ab1bab 43 x->ob_shash = -1;
44#endif
3aa33042 45 x->ob_sstate = SSTATE_NOT_INTERNED;
d7ab1bab 46 return ((PyObject *)x);
47}
48
438b1639
MW
49static PyObject *dowrap(PyTypeObject *ty, const void *p, size_t n)
50{
51 PyObject *x;
4d09d07e
MW
52 int ch;
53
54 if (p && ty == bytestring_pytype) {
55 if (!n) {
56 if (!empty) empty = allocate(ty, 0);
57 Py_INCREF(empty); return (empty);
58 } else if (n == 1 && (ch = *(unsigned char *)p) < sizeof(bytev)) {
59 if (!bytev[ch])
60 { bytev[ch] = allocate(ty, 1); *PyString_AS_STRING(bytev[ch]) = ch; }
61 Py_INCREF(bytev[ch]); return (bytev[ch]);
62 }
63 }
438b1639
MW
64
65 x = allocate(ty, n);
66 if (p) memcpy(PyString_AS_STRING(x), p, n);
67 return (x);
68}
69
46e6ad89 70PyObject *bytestring_pywrap(const void *p, size_t n)
71 { return (dowrap(bytestring_pytype, p, n)); }
72
d7ab1bab 73PyObject *bytestring_pywrapbuf(buf *b)
46e6ad89 74 { return (dowrap(bytestring_pytype, BCUR(b), BLEFT(b))); }
75
76static PyObject *bytestring_pynew(PyTypeObject *ty,
77 PyObject *arg, PyObject *kw)
78{
79 const char *p;
6b54260d 80 Py_ssize_t n;
827f89d7
MW
81 static const char *const kwlist[] = { "data", 0 };
82 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &p, &n))
46e6ad89 83 return (0);
84 return (dowrap(ty, p, n));
85}
d7ab1bab 86
2a21aec7 87static PyObject *meth_ctstreq(PyObject *me, PyObject *arg)
bfb450cc
MW
88{
89 char *p, *q;
90 Py_ssize_t psz, qsz;
2a21aec7 91 if (!PyArg_ParseTuple(arg, "s#s#:ctstreq", &p, &psz, &q, &qsz))
bfb450cc
MW
92 goto end;
93 if (psz == qsz && ct_memeq(p, q, psz)) RETURN_TRUE;
94 else RETURN_FALSE;
95end:
96 return (0);
97}
98
5a8b43b3
MW
99static PyObject *meth__ByteString_zero(PyObject *me, PyObject *arg)
100{
101 size_t sz;
102 PyObject *rc = 0;
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);
106end:
107 return (rc);
108}
109
bfb450cc
MW
110static PyObject *bytestring_pyrichcompare(PyObject *me,
111 PyObject *you, int op)
112{
113 int b;
114 void *mystr, *yourstr;
115 Py_ssize_t mylen, yourlen, minlen;
116
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);
120
121 switch (op) {
122 case Py_EQ:
123 b = mylen == yourlen && ct_memeq(mystr, yourstr, mylen);
124 break;
125 case Py_NE:
126 b = mylen != yourlen || !ct_memeq(mystr, yourstr, mylen);
127 break;
128 default:
129 minlen = mylen < yourlen ? mylen : yourlen;
130 b = memcmp(mystr, yourstr, minlen);
131 if (!b) b = mylen < yourlen ? -1 : mylen > yourlen ? +1 : 0;
132 switch (op) {
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;
137 default: abort();
138 }
139 }
140 if (b) RETURN_TRUE;
141 else RETURN_FALSE;
142}
143
d7ab1bab 144#define BINOP(name, op) \
145 static PyObject *bytestring_py##name(PyObject *x, PyObject *y) { \
146 const void *xv, *yv; \
147 const unsigned char *xp, *yp; \
148 unsigned char *zp; \
87d705a8 149 Py_ssize_t xsz, ysz; \
d7ab1bab 150 int i; \
151 PyObject *rc = 0; \
152 if (PyObject_AsReadBuffer(x, &xv, &xsz) || \
153 PyObject_AsReadBuffer(y, &yv, &ysz)) \
154 goto end; \
155 if (xsz != ysz) VALERR("length mismatch"); \
156 rc = bytestring_pywrap(0, xsz); \
157 xp = xv; yp = yv; zp = (unsigned char *)PyString_AS_STRING(rc); \
158 for (i = xsz; i > 0; i--) *zp++ = *xp++ op *yp++; \
159 end: \
160 return (rc); \
161 }
162BINOP(and, &)
163BINOP(or, |)
164BINOP(xor, ^)
165
166#define UNOP(name, op) \
167 static PyObject *bytestring_py##name(PyObject *x) { \
168 const void *xv; \
169 const unsigned char *xp; \
170 unsigned char *zp; \
87d705a8 171 Py_ssize_t xsz; \
d7ab1bab 172 int i; \
173 PyObject *rc = 0; \
174 if (PyObject_AsReadBuffer(x, &xv, &xsz)) goto end; \
175 rc = bytestring_pywrap(0, xsz); \
176 xp = xv; zp = (unsigned char *)PyString_AS_STRING(rc); \
177 for (i = xsz; i > 0; i--) *zp++ = op *xp++; \
178 end: \
179 return (rc); \
180 }
181UNOP(not, ~)
182
183static PyNumberMethods bytestring_pynumber = {
184 0, /* @nb_add@ */
185 0, /* @nb_subtract@ */
186 0, /* @nb_multiply@ */
187 0, /* @nb_divide@ */
188 0, /* @nb_remainder@ */
189 0, /* @nb_divmod@ */
190 0, /* @nb_power@ */
191 0, /* @nb_negative@ */
192 0, /* @nb_positive@ */
193 0, /* @nb_absolute@ */
194 0, /* @nb_nonzero@ */
195 bytestring_pynot, /* @nb_invert@ */
196 0, /* @nb_lshift@ */
197 0, /* @nb_rshift@ */
198 bytestring_pyand, /* @nb_and@ */
199 bytestring_pyxor, /* @nb_xor@ */
200 bytestring_pyor, /* @nb_or@ */
201 0, /* @nb_coerce@ */
202 0, /* @nb_int@ */
203 0, /* @nb_long@ */
204 0, /* @nb_float@ */
205 0, /* @nb_oct@ */
206 0, /* @nb_hex@ */
207};
208
209static PyBufferProcs bytestring_pybuffer;
210
211static PyTypeObject bytestring_pytype_skel = {
6d4db0bf 212 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 213 "ByteString", /* @tp_name@ */
d7ab1bab 214 0, /* @tp_basicsize@ */
215 0, /* @tp_itemsize@ */
216
217 0, /* @tp_dealloc@ */
218 0, /* @tp_print@ */
219 0, /* @tp_getattr@ */
220 0, /* @tp_setattr@ */
221 0, /* @tp_compare@ */
222 0, /* @tp_repr@ */
223 &bytestring_pynumber, /* @tp_as_number@ */
224 0, /* @tp_as_sequence@ */
225 0, /* @tp_as_mapping@ */
226 0, /* @tp_hash@ */
227 0, /* @tp_call@ */
228 0, /* @tp_str@ */
229 0, /* @tp_getattro@ */
230 0, /* @tp_setattro@ */
231 &bytestring_pybuffer, /* @tp_as_buffer@ */
232 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
233 Py_TPFLAGS_CHECKTYPES |
234 Py_TPFLAGS_BASETYPE,
235
236 /* @tp_doc@ */
06cd26e8 237"ByteString(STR): byte string class.",
d7ab1bab 238
239 0, /* @tp_traverse@ */
240 0, /* @tp_clear@ */
bfb450cc 241 bytestring_pyrichcompare, /* @tp_richcompare@ */
d7ab1bab 242 0, /* @tp_weaklistoffset@ */
243 0, /* @tp_iter@ */
963a6148 244 0, /* @tp_iternext@ */
d7ab1bab 245 0, /* @tp_methods@ */
246 0, /* @tp_members@ */
247 0, /* @tp_getset@ */
248 0, /* @tp_base@ */
249 0, /* @tp_dict@ */
250 0, /* @tp_descr_get@ */
251 0, /* @tp_descr_set@ */
252 0, /* @tp_dictoffset@ */
253 0, /* @tp_init@ */
254 PyType_GenericAlloc, /* @tp_alloc@ */
46e6ad89 255 bytestring_pynew, /* @tp_new@ */
3aa33042 256 0, /* @tp_free@ */
d7ab1bab 257 0 /* @tp_is_gc@ */
258};
259
260/*----- Initialization ----------------------------------------------------*/
261
bfb450cc
MW
262static PyMethodDef methods[] = {
263#define METHNAME(func) meth_##func
264 METH (ctstreq, "ctstreq(S, T) -> BOOL")
5a8b43b3 265 METH (_ByteString_zero, "zero(N) -> 0000...00")
bfb450cc
MW
266#undef METHNAME
267 { 0 }
268};
269
d7ab1bab 270#define string_pytype &PyString_Type
271void bytestring_pyinit(void)
272{
273 INITTYPE(bytestring, string);
bfb450cc 274 addmethods(methods);
d7ab1bab 275}
276
277void bytestring_pyinsert(PyObject *mod)
278{
279 INSERT("ByteString", bytestring_pytype);
280}
281
282/*----- That's all, folks -------------------------------------------------*/