| 1 |
#ifndef MYPYC_BYTES_EXTRA_OPS_H |
| 2 |
#define MYPYC_BYTES_EXTRA_OPS_H |
| 3 |
|
| 4 |
#include <Python.h> |
| 5 |
#include <stdint.h> |
| 6 |
#include "CPy.h" |
| 7 |
|
| 8 |
// Optimized bytes translate operation |
| 9 |
PyObject *CPyBytes_Translate(PyObject *bytes, PyObject *table); |
| 10 |
|
| 11 |
// Optimized bytes.__getitem__ operations |
| 12 |
|
| 13 |
// If index is negative, convert to non-negative index (no range checking) |
| 14 |
static inline int64_t CPyBytes_AdjustIndex(PyObject *obj, int64_t index) { |
| 15 |
if (index < 0) { |
| 16 |
return index + Py_SIZE(obj); |
| 17 |
} |
| 18 |
return index; |
| 19 |
} |
| 20 |
|
| 21 |
// Check if index is in valid range [0, len) |
| 22 |
static inline bool CPyBytes_RangeCheck(PyObject *obj, int64_t index) { |
| 23 |
return index >= 0 && index < Py_SIZE(obj); |
| 24 |
} |
| 25 |
|
| 26 |
// Get byte at index (no bounds checking) - returns as CPyTagged |
| 27 |
static inline CPyTagged CPyBytes_GetItemUnsafe(PyObject *obj, int64_t index) { |
| 28 |
return ((CPyTagged)(uint8_t)(PyBytes_AS_STRING(obj))[index]) << 1; |
| 29 |
} |
| 30 |
|
| 31 |
#endif |