algorithms.c: Add bindings for STROBE.
[catacomb-python] / bytestring.c
index fdc6e50..89f3e4a 100644 (file)
@@ -36,13 +36,15 @@ static PyObject *empty, *bytev[256];
 
 static PyObject *allocate(PyTypeObject *ty, size_t n)
 {
-  PyStringObject *x;
-  x = (PyStringObject *)ty->tp_alloc(ty, n);
+  BINOBJ *x;
+  x = (BINOBJ *)ty->tp_alloc(ty, n);
   x->ob_sval[n] = 0;
 #if defined(CACHE_HASH) || PY_VERSION_HEX >= 0x02030000
   x->ob_shash = -1;
 #endif
+#ifdef PY2
   x->ob_sstate = SSTATE_NOT_INTERNED;
+#endif
   return ((PyObject *)x);
 }
 
@@ -57,13 +59,13 @@ static PyObject *dowrap(PyTypeObject *ty, const void *p, size_t n)
       Py_INCREF(empty); return (empty);
     } else if (n == 1 && (ch = *(unsigned char *)p) < sizeof(bytev)) {
       if (!bytev[ch])
-       { bytev[ch] = allocate(ty, 1); *PyString_AS_STRING(bytev[ch]) = ch; }
+       { bytev[ch] = allocate(ty, 1); *BIN_PTR(bytev[ch]) = ch; }
       Py_INCREF(bytev[ch]); return (bytev[ch]);
     }
   }
 
   x = allocate(ty, n);
-  if (p) memcpy(PyString_AS_STRING(x), p, n);
+  if (p) memcpy(BIN_PTR(x), p, n);
   return (x);
 }
 
@@ -100,7 +102,7 @@ static PyObject *bymeth_zero(PyObject *me, PyObject *arg)
   PyObject *rc = 0;
   if (!PyArg_ParseTuple(arg, "O&:zero", convszt, &sz)) goto end;
   rc = bytestring_pywrap(0, sz);
-  memset(PyString_AS_STRING(rc), 0, sz);
+  memset(BIN_PTR(rc), 0, sz);
 end:
   return (rc);
 }
@@ -112,7 +114,7 @@ static PyObject *bytestring_pyrichcompare(PyObject *me,
   int b;
   Py_ssize_t minlen;
 
-  s0.p = PyString_AS_STRING(me); s0.sz = PyString_GET_SIZE(me);
+  s0.p = BIN_PTR(me); s0.sz = BIN_LEN(me);
   if (!convbin(you, &s1)) { PyErr_Clear(); RETURN_NOTIMPL; }
 
   switch (op) {
@@ -146,7 +148,7 @@ static PyObject *bytestring_pyconcat(PyObject *x, PyObject *y)
   if (!convbin(x, &xx) || !convbin(y, &yy)) goto end;
   zsz = (size_t)xx.sz + (size_t)yy.sz;
   if (xx.sz < 0 || yy.sz < 0 || zsz < xx.sz) VALERR("too long");
-  z = bytestring_pywrap(0, zsz); zp = PyString_AS_STRING(z);
+  z = bytestring_pywrap(0, zsz); zp = BIN_PTR(z);
   memcpy(zp, xx.p, xx.sz); memcpy(zp + xx.sz, yy.p, yy.sz);
 end:
   return (z);
@@ -157,10 +159,10 @@ static PyObject *bytestring_pyrepeat(PyObject *me, Py_ssize_t n)
   const unsigned char *xp; size_t xsz;
   PyObject *z = 0; char *zp; size_t zsz;
 
-  xp = (const unsigned char *)PyString_AS_STRING(me);
-  xsz = PyString_GET_SIZE(me);
+  xp = (const unsigned char *)BIN_PTR(me);
+  xsz = BIN_LEN(me);
   if (n < 0 || (n && xsz >= (size_t)-1/n)) VALERR("too long");
-  zsz = n*xsz; z = bytestring_pywrap(0, zsz); zp = PyString_AS_STRING(z);
+  zsz = n*xsz; z = bytestring_pywrap(0, zsz); zp = BIN_PTR(z);
   if (xsz == 1) memset(zp, *xp, zsz);
   else while (zsz) { memcpy(zp, xp, xsz); zp += xsz; zsz -= xsz; }
 end:
@@ -171,8 +173,12 @@ static PyObject *bytestring_pyitem(PyObject *me, Py_ssize_t i)
 {
   PyObject *rc = 0;
 
-  if (i < 0 || i >= PyString_GET_SIZE(me)) IXERR("out of range");
-  rc = bytestring_pywrap(PyString_AS_STRING(me) + i, 1);
+  if (i < 0 || i >= BIN_LEN(me)) IXERR("out of range");
+#ifdef PY3
+  rc = getulong(BIN_PTR(me)[i]&0xff);
+#else
+  rc = bytestring_pywrap(BIN_PTR(me) + i, 1);
+#endif
 end:
   return (rc);
 }
@@ -180,7 +186,7 @@ end:
 static PyObject *bytestring_pyslice(PyObject *me, Py_ssize_t i, Py_ssize_t j)
 {
   PyObject *rc = 0;
-  size_t n = PyString_GET_SIZE(me);
+  size_t n = BIN_LEN(me);
 
   if (i < 0) i = 0;
   if (j < 0) j = 0;
@@ -188,7 +194,7 @@ static PyObject *bytestring_pyslice(PyObject *me, Py_ssize_t i, Py_ssize_t j)
   if (j < i) i = j = 0;
   if (i == 0 && j == n && Py_TYPE(me) == bytestring_pytype)
     { Py_INCREF(me); rc = me; goto end; }
-  rc = bytestring_pywrap(PyString_AS_STRING(me) + i, j - i);
+  rc = bytestring_pywrap(BIN_PTR(me) + i, j - i);
 end:
   return (rc);
 }
@@ -203,16 +209,16 @@ static PyObject *bytestring_pysubscript(PyObject *me, PyObject *ix)
   if (PyIndex_Check(ix)) {
     i = PyNumber_AsSsize_t(ix, PyExc_IndexError);
     if (i == -1 && PyErr_Occurred()) return (0);
-    if (i < 0) i += PyString_GET_SIZE(me);
+    if (i < 0) i += BIN_LEN(me);
     rc = bytestring_pyitem(me, i);
   } else if (PySlice_Check(ix)) {
-    if (PySlice_GetIndicesEx((PySliceObject *)ix, PyString_GET_SIZE(me),
-                            &i, &j, &k, &n))
+    if (PySlice_GetIndicesEx(PY23((PySliceObject *), NOTHING)ix,
+                            BIN_LEN(me), &i, &j, &k, &n))
       return (0);
     if (k == 1) return bytestring_pyslice(me, i, j);
     rc = bytestring_pywrap(0, n);
-    p = (unsigned char *)PyString_AS_STRING(me) + i;
-    q = (unsigned char *)PyString_AS_STRING(rc);
+    p = (unsigned char *)BIN_PTR(me) + i;
+    q = (unsigned char *)BIN_PTR(rc);
     while (n--) { *q++ = *p; p += k; }
   } else
     TYERR("wanted integer or slice");
@@ -230,7 +236,7 @@ end:
     if (!convbin(x, &xx) || !convbin(y, &yy)) goto end;                        \
     if (xx.sz != yy.sz) VALERR("length mismatch");                     \
     rc = bytestring_pywrap(0, xx.sz);                                  \
-    xp = xx.p; yp = yy.p; zp = (unsigned char *)PyString_AS_STRING(rc);        \
+    xp = xx.p; yp = yy.p; zp = (unsigned char *)BIN_PTR(rc);           \
     for (i = xx.sz; i > 0; i--) *zp++ = *xp++ op *yp++;                        \
   end:                                                                 \
     return (rc);                                                       \
@@ -248,7 +254,7 @@ BINOP(xor, ^)
     PyObject *rc = 0;                                                  \
     if (!convbin(x, &xx)) goto end;                                    \
     rc = bytestring_pywrap(0, xx.sz);                                  \
-    xp = xx.p; zp = (unsigned char *)PyString_AS_STRING(rc);           \
+    xp = xx.p; zp = (unsigned char *)BIN_PTR(rc);                      \
     for (i = xx.sz; i > 0; i--) *zp++ = op *xp++;                      \
   end:                                                                 \
     return (rc);                                                       \
@@ -266,7 +272,9 @@ static const PyNumberMethods bytestring_pynumber = {
   0,                                   /* @nb_add@ */
   0,                                   /* @nb_subtract@ */
   0,                                   /* @nb_multiply@ */
+#ifdef PY2
   0,                                   /* @nb_divide@ */
+#endif
   0,                                   /* @nb_remainder@ */
   0,                                   /* @nb_divmod@ */
   0,                                   /* @nb_power@ */
@@ -365,7 +373,7 @@ static const PyMethodDef methods[] = {
   { 0 }
 };
 
-#define string_pytype &PyString_Type
+#define string_pytype &BIN_TYPE
 void bytestring_pyinit(void)
 {
   INITTYPE(bytestring, string);