Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions mypyc/lib-rt/bytes_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,22 @@ CPyTagged CPyBytes_GetItem(PyObject *o, CPyTagged index) {
}

PyObject *CPyBytes_Concat(PyObject *a, PyObject *b) {
if (PyBytes_Check(a) && PyBytes_Check(b)) {
Py_ssize_t a_len = ((PyVarObject *)a)->ob_size;
Py_ssize_t b_len = ((PyVarObject *)b)->ob_size;
PyBytesObject *ret = (PyBytesObject *)PyBytes_FromStringAndSize(NULL, a_len + b_len);
if (ret != NULL) {
memcpy(ret->ob_sval, ((PyBytesObject *)a)->ob_sval, a_len);
memcpy(ret->ob_sval + a_len, ((PyBytesObject *)b)->ob_sval, b_len);
}
return (PyObject *)ret;
} else if (PyByteArray_Check(a)) {
return PyByteArray_Concat(a, b);
} else {
PyBytes_Concat(&a, b);
return a;
Py_ssize_t a_len = ((PyVarObject *)a)->ob_size;
Py_ssize_t b_len = ((PyVarObject *)b)->ob_size;
PyBytesObject *ret = (PyBytesObject *)PyBytes_FromStringAndSize(NULL, a_len + b_len);
if (ret != NULL) {
memcpy(ret->ob_sval, ((PyBytesObject *)a)->ob_sval, a_len);
memcpy(ret->ob_sval + a_len, ((PyBytesObject *)b)->ob_sval, b_len);
}
return (PyObject *)ret;
}

static inline Py_ssize_t Clamp(Py_ssize_t a, Py_ssize_t b, Py_ssize_t c) {
return a < b ? b : (a >= c ? c : a);
}

PyObject *CPyBytes_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end) {
if ((PyBytes_Check(obj) || PyByteArray_Check(obj))
&& CPyTagged_CheckShort(start) && CPyTagged_CheckShort(end)) {
if (CPyTagged_CheckShort(start) && CPyTagged_CheckShort(end)) {
Py_ssize_t startn = CPyTagged_ShortAsSsize_t(start);
Py_ssize_t endn = CPyTagged_ShortAsSsize_t(end);
Py_ssize_t len = ((PyVarObject *)obj)->ob_size;
Expand Down Expand Up @@ -141,18 +133,10 @@ PyObject *CPyBytes_Build(Py_ssize_t len, ...) {
return (PyObject *)ret;
}


CPyTagged CPyBytes_Ord(PyObject *obj) {
if (PyBytes_Check(obj)) {
Py_ssize_t s = PyBytes_GET_SIZE(obj);
if (s == 1) {
return (unsigned char)(PyBytes_AS_STRING(obj)[0]) << 1;
}
} else if (PyByteArray_Check(obj)) {
Py_ssize_t s = PyByteArray_GET_SIZE(obj);
if (s == 1) {
return (unsigned char)(PyByteArray_AS_STRING(obj)[0]) << 1;
}
Py_ssize_t s = PyBytes_GET_SIZE(obj);
if (s == 1) {
return (unsigned char)(PyBytes_AS_STRING(obj)[0]) << 1;
}
PyErr_SetString(PyExc_TypeError, "ord() expects a character");
return CPY_INT_TAG;
Expand Down
3 changes: 1 addition & 2 deletions mypyc/primitives/bytes_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
function_op(
name="builtins.bytearray",
arg_types=[object_rprimitive],
return_type=bytes_rprimitive,
return_type=object_rprimitive,
c_function_name="PyByteArray_FromObject",
error_kind=ERR_MAGIC,
)
Expand All @@ -74,7 +74,6 @@
)

# bytes + bytes
# bytearray + bytearray
binary_op(
name="+",
arg_types=[bytes_rprimitive, bytes_rprimitive],
Expand Down
6 changes: 1 addition & 5 deletions mypyc/test-data/irbuild-bytes.test
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ def f(s, num):
num :: int
r0 :: object
r1 :: str
r2, r3, a :: object
r4 :: bytes
b, r5 :: object
r6 :: bytes
c :: object
r2, r3, a, r4, b, r5, r6, c :: object
L0:
r0 = builtins :: module
r1 = 'bytearray'
Expand Down