| 1 |
// Bytes primitive operations |
| 2 |
// |
| 3 |
// These are registered in mypyc.primitives.bytes_ops. |
| 4 |
|
| 5 |
#include <Python.h> |
| 6 |
#include "CPy.h" |
| 7 |
|
| 8 |
// Returns -1 on error, 0 on inequality, 1 on equality. |
| 9 |
// |
| 10 |
// Falls back to PyObject_RichCompareBool. |
| 11 |
int CPyBytes_Compare(PyObject *left, PyObject *right) { |
| 12 |
if (PyBytes_CheckExact(left) && PyBytes_CheckExact(right)) { |
| 13 |
if (left == right) { |
| 14 |
return 1; |
| 15 |
} |
| 16 |
|
| 17 |
// Adapted from cpython internal implementation of bytes_compare. |
| 18 |
Py_ssize_t len = Py_SIZE(left); |
| 19 |
if (Py_SIZE(right) != len) { |
| 20 |
return 0; |
| 21 |
} |
| 22 |
PyBytesObject *left_b = (PyBytesObject *)left; |
| 23 |
PyBytesObject *right_b = (PyBytesObject *)right; |
| 24 |
if (left_b->ob_sval[0] != right_b->ob_sval[0]) { |
| 25 |
return 0; |
| 26 |
} |
| 27 |
|
| 28 |
return memcmp(left_b->ob_sval, right_b->ob_sval, len) == 0; |
| 29 |
} |
| 30 |
return PyObject_RichCompareBool(left, right, Py_EQ); |
| 31 |
} |
| 32 |
|
| 33 |
CPyTagged CPyBytes_GetItem(PyObject *o, CPyTagged index) { |
| 34 |
if (CPyTagged_CheckShort(index)) { |
| 35 |
Py_ssize_t n = CPyTagged_ShortAsSsize_t(index); |
| 36 |
Py_ssize_t size = ((PyVarObject *)o)->ob_size; |
| 37 |
if (n < 0) |
| 38 |
n += size; |
| 39 |
if (n < 0 || n >= size) { |
| 40 |
PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 41 |
return CPY_INT_TAG; |
| 42 |
} |
| 43 |
unsigned char num = PyBytes_Check(o) ? ((PyBytesObject *)o)->ob_sval[n] |
| 44 |
: ((PyByteArrayObject *)o)->ob_bytes[n]; |
| 45 |
return num << 1; |
| 46 |
} else { |
| 47 |
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG); |
| 48 |
return CPY_INT_TAG; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
PyObject *CPyBytes_Concat(PyObject *a, PyObject *b) { |
| 53 |
Py_ssize_t a_len = ((PyVarObject *)a)->ob_size; |
| 54 |
Py_ssize_t b_len = ((PyVarObject *)b)->ob_size; |
| 55 |
PyBytesObject *ret = (PyBytesObject *)PyBytes_FromStringAndSize(NULL, a_len + b_len); |
| 56 |
if (ret != NULL) { |
| 57 |
memcpy(ret->ob_sval, ((PyBytesObject *)a)->ob_sval, a_len); |
| 58 |
memcpy(ret->ob_sval + a_len, ((PyBytesObject *)b)->ob_sval, b_len); |
| 59 |
} |
| 60 |
return (PyObject *)ret; |
| 61 |
} |
| 62 |
|
| 63 |
static inline Py_ssize_t Clamp(Py_ssize_t a, Py_ssize_t b, Py_ssize_t c) { |
| 64 |
return a < b ? b : (a >= c ? c : a); |
| 65 |
} |
| 66 |
|
| 67 |
PyObject *CPyBytes_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end) { |
| 68 |
if (CPyTagged_CheckShort(start) && CPyTagged_CheckShort(end)) { |
| 69 |
Py_ssize_t startn = CPyTagged_ShortAsSsize_t(start); |
| 70 |
Py_ssize_t endn = CPyTagged_ShortAsSsize_t(end); |
| 71 |
Py_ssize_t len = ((PyVarObject *)obj)->ob_size; |
| 72 |
if (startn < 0) { |
| 73 |
startn += len; |
| 74 |
} |
| 75 |
if (endn < 0) { |
| 76 |
endn += len; |
| 77 |
} |
| 78 |
startn = Clamp(startn, 0, len); |
| 79 |
endn = Clamp(endn, 0, len); |
| 80 |
Py_ssize_t slice_len = endn - startn; |
| 81 |
if (PyBytes_Check(obj)) { |
| 82 |
return PyBytes_FromStringAndSize(PyBytes_AS_STRING(obj) + startn, slice_len); |
| 83 |
} else { |
| 84 |
return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(obj) + startn, slice_len); |
| 85 |
} |
| 86 |
} |
| 87 |
return CPyObject_GetSlice(obj, start, end); |
| 88 |
} |
| 89 |
|
| 90 |
// Like _PyBytes_Join but fallback to dynamic call if 'sep' is not bytes |
| 91 |
// (mostly commonly, for bytearrays) |
| 92 |
PyObject *CPyBytes_Join(PyObject *sep, PyObject *iter) { |
| 93 |
if (PyBytes_CheckExact(sep)) { |
| 94 |
return PyBytes_Join(sep, iter); |
| 95 |
} else { |
| 96 |
return PyObject_CallMethodOneArg(sep, mypyc_interned_str.join, iter); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
PyObject *CPyBytes_Build(Py_ssize_t len, ...) { |
| 101 |
Py_ssize_t i; |
| 102 |
Py_ssize_t sz = 0; |
| 103 |
|
| 104 |
va_list args; |
| 105 |
va_start(args, len); |
| 106 |
for (i = 0; i < len; i++) { |
| 107 |
PyObject *item = va_arg(args, PyObject *); |
| 108 |
size_t add_sz = ((PyVarObject *)item)->ob_size; |
| 109 |
// Using size_t to avoid overflow during arithmetic calculation |
| 110 |
if (add_sz > (size_t)(PY_SSIZE_T_MAX - sz)) { |
| 111 |
PyErr_SetString(PyExc_OverflowError, |
| 112 |
"join() result is too long for a Python bytes"); |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
sz += add_sz; |
| 116 |
} |
| 117 |
va_end(args); |
| 118 |
|
| 119 |
PyBytesObject *ret = (PyBytesObject *)PyBytes_FromStringAndSize(NULL, sz); |
| 120 |
if (ret != NULL) { |
| 121 |
char *res_data = ret->ob_sval; |
| 122 |
va_start(args, len); |
| 123 |
for (i = 0; i < len; i++) { |
| 124 |
PyObject *item = va_arg(args, PyObject *); |
| 125 |
Py_ssize_t item_sz = ((PyVarObject *)item)->ob_size; |
| 126 |
memcpy(res_data, ((PyBytesObject *)item)->ob_sval, item_sz); |
| 127 |
res_data += item_sz; |
| 128 |
} |
| 129 |
va_end(args); |
| 130 |
assert(res_data == ret->ob_sval + ((PyVarObject *)ret)->ob_size); |
| 131 |
} |
| 132 |
|
| 133 |
return (PyObject *)ret; |
| 134 |
} |
| 135 |
|
| 136 |
CPyTagged CPyBytes_Ord(PyObject *obj) { |
| 137 |
Py_ssize_t s = PyBytes_GET_SIZE(obj); |
| 138 |
if (s == 1) { |
| 139 |
return (unsigned char)(PyBytes_AS_STRING(obj)[0]) << 1; |
| 140 |
} |
| 141 |
PyErr_SetString(PyExc_TypeError, "ord() expects a character"); |
| 142 |
return CPY_INT_TAG; |
| 143 |
} |
| 144 |
|
| 145 |
PyObject *CPyBytes_Multiply(PyObject *bytes, CPyTagged count) { |
| 146 |
Py_ssize_t temp_count = CPyTagged_AsSsize_t(count); |
| 147 |
if (temp_count == -1 && PyErr_Occurred()) { |
| 148 |
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG); |
| 149 |
return NULL; |
| 150 |
} |
| 151 |
return PySequence_Repeat(bytes, temp_count); |
| 152 |
} |
| 153 |
|
| 154 |
int CPyBytes_Startswith(PyObject *self, PyObject *subobj) { |
| 155 |
if (PyBytes_CheckExact(self) && PyBytes_CheckExact(subobj)) { |
| 156 |
if (self == subobj) { |
| 157 |
return 1; |
| 158 |
} |
| 159 |
|
| 160 |
Py_ssize_t subobj_len = PyBytes_GET_SIZE(subobj); |
| 161 |
if (subobj_len == 0) { |
| 162 |
return 1; |
| 163 |
} |
| 164 |
|
| 165 |
Py_ssize_t self_len = PyBytes_GET_SIZE(self); |
| 166 |
if (subobj_len > self_len) { |
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
const char *self_buf = PyBytes_AS_STRING(self); |
| 171 |
const char *subobj_buf = PyBytes_AS_STRING(subobj); |
| 172 |
|
| 173 |
return memcmp(self_buf, subobj_buf, (size_t)subobj_len) == 0 ? 1 : 0; |
| 174 |
} |
| 175 |
PyObject *result = PyObject_CallMethodOneArg(self, mypyc_interned_str.startswith, subobj); |
| 176 |
if (result == NULL) { |
| 177 |
return 2; |
| 178 |
} |
| 179 |
int ret = PyObject_IsTrue(result); |
| 180 |
Py_DECREF(result); |
| 181 |
if (ret < 0) { |
| 182 |
return 2; |
| 183 |
} |
| 184 |
return ret; |
| 185 |
} |
| 186 |
|
| 187 |
int CPyBytes_Endswith(PyObject *self, PyObject *subobj) { |
| 188 |
if (PyBytes_CheckExact(self) && PyBytes_CheckExact(subobj)) { |
| 189 |
if (self == subobj) { |
| 190 |
return 1; |
| 191 |
} |
| 192 |
|
| 193 |
Py_ssize_t subobj_len = PyBytes_GET_SIZE(subobj); |
| 194 |
if (subobj_len == 0) { |
| 195 |
return 1; |
| 196 |
} |
| 197 |
|
| 198 |
Py_ssize_t self_len = PyBytes_GET_SIZE(self); |
| 199 |
if (subobj_len > self_len) { |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
const char *self_buf = PyBytes_AS_STRING(self); |
| 204 |
const char *subobj_buf = PyBytes_AS_STRING(subobj); |
| 205 |
|
| 206 |
return memcmp(self_buf + (self_len - subobj_len), subobj_buf, (size_t)subobj_len) == 0 ? 1 : 0; |
| 207 |
} |
| 208 |
PyObject *result = PyObject_CallMethodOneArg(self, mypyc_interned_str.endswith, subobj); |
| 209 |
if (result == NULL) { |
| 210 |
return 2; |
| 211 |
} |
| 212 |
int ret = PyObject_IsTrue(result); |
| 213 |
Py_DECREF(result); |
| 214 |
if (ret < 0) { |
| 215 |
return 2; |
| 216 |
} |
| 217 |
return ret; |
| 218 |
} |