| 1 |
#ifndef MYPYC_STR_EXTRA_OPS_H |
| 2 |
#define MYPYC_STR_EXTRA_OPS_H |
| 3 |
|
| 4 |
#include <Python.h> |
| 5 |
#include <stdint.h> |
| 6 |
#include "CPy.h" |
| 7 |
|
| 8 |
// Optimized str indexing for ord(s[i]) |
| 9 |
|
| 10 |
// If index is negative, convert to non-negative index (no range checking) |
| 11 |
static inline int64_t CPyStr_AdjustIndex(PyObject *obj, int64_t index) { |
| 12 |
if (index < 0) { |
| 13 |
return index + PyUnicode_GET_LENGTH(obj); |
| 14 |
} |
| 15 |
return index; |
| 16 |
} |
| 17 |
|
| 18 |
// Check if index is in valid range [0, len) |
| 19 |
static inline bool CPyStr_RangeCheck(PyObject *obj, int64_t index) { |
| 20 |
return index >= 0 && index < PyUnicode_GET_LENGTH(obj); |
| 21 |
} |
| 22 |
|
| 23 |
// Get character at index as int (ord value) - no bounds checking, returns as CPyTagged |
| 24 |
static inline CPyTagged CPyStr_GetItemUnsafeAsInt(PyObject *obj, int64_t index) { |
| 25 |
int kind = PyUnicode_KIND(obj); |
| 26 |
return PyUnicode_READ(kind, PyUnicode_DATA(obj), index) << 1; |
| 27 |
} |
| 28 |
|
| 29 |
#endif |