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
46 changes: 21 additions & 25 deletions mypyc/lib-rt/str_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,22 +236,30 @@ PyObject *CPyStr_Build(Py_ssize_t len, ...) {
return res;
}

// Convert a start or end index to Py_ssize_t. Like CPython, clamp values that don't fit
// in Py_ssize_t (these are out of range for any string) instead of raising an error.
static Py_ssize_t CPyStr_AsSliceIndex(CPyTagged x) {
if (likely(CPyTagged_CheckShort(x))) {
return CPyTagged_ShortAsSsize_t(x);
}
PyObject *obj = CPyTagged_LongAsObject(x);
Py_ssize_t n = PyLong_AsSsize_t(obj);
if (n == -1 && PyErr_Occurred()) {
// The only possible error is an overflow
PyErr_Clear();
return PyLong_IsNegative(obj) ? PY_SSIZE_T_MIN : PY_SSIZE_T_MAX;
}
return n;
}

CPyTagged CPyStr_Find(PyObject *str, PyObject *substr, CPyTagged start, int direction) {
CPyTagged end = PyUnicode_GET_LENGTH(str) << 1;
return CPyStr_FindWithEnd(str, substr, start, end, direction);
}

CPyTagged CPyStr_FindWithEnd(PyObject *str, PyObject *substr, CPyTagged start, CPyTagged end, int direction) {
Py_ssize_t temp_start = CPyTagged_AsSsize_t(start);
if (temp_start == -1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
return CPY_INT_TAG;
}
Py_ssize_t temp_end = CPyTagged_AsSsize_t(end);
if (temp_end == -1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
return CPY_INT_TAG;
}
Py_ssize_t temp_start = CPyStr_AsSliceIndex(start);
Py_ssize_t temp_end = CPyStr_AsSliceIndex(end);
Py_ssize_t index = PyUnicode_Find(str, substr, temp_start, temp_end, direction);
if (unlikely(index == -2)) {
return CPY_INT_TAG;
Expand Down Expand Up @@ -605,26 +613,14 @@ PyObject *CPy_Encode(PyObject *obj, PyObject *encoding, PyObject *errors) {
}

Py_ssize_t CPyStr_Count(PyObject *unicode, PyObject *substring, CPyTagged start) {
Py_ssize_t temp_start = CPyTagged_AsSsize_t(start);
if (temp_start == -1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
return -1;
}
Py_ssize_t temp_start = CPyStr_AsSliceIndex(start);
Py_ssize_t end = PyUnicode_GET_LENGTH(unicode);
return PyUnicode_Count(unicode, substring, temp_start, end);
}

Py_ssize_t CPyStr_CountFull(PyObject *unicode, PyObject *substring, CPyTagged start, CPyTagged end) {
Py_ssize_t temp_start = CPyTagged_AsSsize_t(start);
if (temp_start == -1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
return -1;
}
Py_ssize_t temp_end = CPyTagged_AsSsize_t(end);
if (temp_end == -1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
return -1;
}
Py_ssize_t temp_start = CPyStr_AsSliceIndex(start);
Py_ssize_t temp_end = CPyStr_AsSliceIndex(end);
return PyUnicode_Count(unicode, substring, temp_start, temp_end);
}

Expand Down
25 changes: 25 additions & 0 deletions mypyc/test-data/run-strings.test
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,21 @@ def test_find() -> None:
assert rfind(s, "b", 3, 5) == 4
assert rfind(s, "b", 3, 4) == -1

def test_find_huge_indices() -> None:
# Indices that don't fit in Py_ssize_t are clamped, like in CPython
s = "abcab"
assert find(s, "b", 2**62) == -1
assert find(s, "b", 2**63) == -1
assert find(s, "b", -2**63 - 1) == 1
assert find(s, "b", 0, 2**100) == 1
assert find(s, "b", 0, -2**100) == -1
assert find(s, "", -2**100) == 0
assert find("", "", 2**63) == -1
assert rfind(s, "b", -2**100) == 4
assert rfind(s, "b", 0, 2**100) == 4
assert rfind(s, "", 0, 2**100) == 5
assert rfind(s, "b", 2**100, -2**100) == -1

def str_to_int(s: str, base: Optional[int] = None) -> int:
if base:
return int(s, base)
Expand Down Expand Up @@ -1214,6 +1229,16 @@ def test_count_multi_start_end_emoji() -> None:
assert string.count("😴😴😴", 0, 12) == 1, string.count("😴😴😴", 0, 12)
assert string.count("🚀🚀🚀", 0, 12) == 2, string.count("🚀🚀🚀", 0, 12)
assert string.count("ñññ", 0, 12) == 1, string.count("ñññ", 0, 12)
def test_count_huge_indices() -> None:
# Indices that don't fit in Py_ssize_t are clamped, like in CPython
string = "abcbcb"
assert string.count("b", 2**62) == 0
assert string.count("b", 2**63) == 0
assert string.count("b", -2**100) == 3
assert string.count("b", 0, 2**100) == 3
assert string.count("b", -2**100, -2**100) == 0
assert string.count("", 2**62) == 0
assert string.count("", -2**100, 2**100) == 7

[case testIsInstance]
from copysubclass import subc
Expand Down
Loading