-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathbytes_extra_ops.h
More file actions
31 lines (24 loc) · 896 Bytes
/
bytes_extra_ops.h
File metadata and controls
31 lines (24 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef MYPYC_BYTES_EXTRA_OPS_H
#define MYPYC_BYTES_EXTRA_OPS_H
#include <Python.h>
#include <stdint.h>
#include "CPy.h"
// Optimized bytes translate operation
PyObject *CPyBytes_Translate(PyObject *bytes, PyObject *table);
// Optimized bytes.__getitem__ operations
// If index is negative, convert to non-negative index (no range checking)
static inline int64_t CPyBytes_AdjustIndex(PyObject *obj, int64_t index) {
if (index < 0) {
return index + Py_SIZE(obj);
}
return index;
}
// Check if index is in valid range [0, len)
static inline bool CPyBytes_RangeCheck(PyObject *obj, int64_t index) {
return index >= 0 && index < Py_SIZE(obj);
}
// Get byte at index (no bounds checking) - returns as CPyTagged
static inline CPyTagged CPyBytes_GetItemUnsafe(PyObject *obj, int64_t index) {
return ((CPyTagged)(uint8_t)(PyBytes_AS_STRING(obj))[index]) << 1;
}
#endif