diff --git a/mypyc/doc/str_operations.rst b/mypyc/doc/str_operations.rst index b5d488486e37..b2d7309e1ce5 100644 --- a/mypyc/doc/str_operations.rst +++ b/mypyc/doc/str_operations.rst @@ -44,6 +44,9 @@ Methods * ``s1.find(s2: str)`` * ``s1.find(s2: str, start: int)`` * ``s1.find(s2: str, start: int, end: int)`` +* ``s1.index(s2: str)`` +* ``s1.index(s2: str, start: int)`` +* ``s1.index(s2: str, start: int, end: int)`` * ``s.isalnum()`` * ``s.isalpha()`` * ``s.isascii()`` @@ -64,6 +67,9 @@ Methods * ``s1.rfind(s2: str)`` * ``s1.rfind(s2: str, start: int)`` * ``s1.rfind(s2: str, start: int, end: int)`` +* ``s1.rindex(s2: str)`` +* ``s1.rindex(s2: str, start: int)`` +* ``s1.rindex(s2: str, start: int, end: int)`` * ``s.rpartition(sep: str)`` * ``s.rsplit()`` * ``s.rsplit(sep: str)`` diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 0518ab034df3..c207b3c5806a 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -1073,6 +1073,8 @@ PyObject *CPyStr_GetItem(PyObject *str, CPyTagged index); PyObject *CPyStr_GetItemUnsafe(PyObject *str, Py_ssize_t index); CPyTagged CPyStr_Find(PyObject *str, PyObject *substr, CPyTagged start, int direction); CPyTagged CPyStr_FindWithEnd(PyObject *str, PyObject *substr, CPyTagged start, CPyTagged end, int direction); +CPyTagged CPyStr_Index(PyObject *str, PyObject *substr, CPyTagged start, int direction); +CPyTagged CPyStr_IndexWithEnd(PyObject *str, PyObject *substr, CPyTagged start, CPyTagged end, int direction); PyObject *CPyStr_Split(PyObject *str, PyObject *sep, CPyTagged max_split); PyObject *CPyStr_RSplit(PyObject *str, PyObject *sep, CPyTagged max_split); PyObject *_CPyStr_Strip(PyObject *self, int strip_type, PyObject *sep); diff --git a/mypyc/lib-rt/str_ops.c b/mypyc/lib-rt/str_ops.c index 0fcdac6a3815..6262fb10264b 100644 --- a/mypyc/lib-rt/str_ops.c +++ b/mypyc/lib-rt/str_ops.c @@ -267,6 +267,21 @@ CPyTagged CPyStr_FindWithEnd(PyObject *str, PyObject *substr, CPyTagged start, C return index << 1; } +CPyTagged CPyStr_Index(PyObject *str, PyObject *substr, CPyTagged start, int direction) { + CPyTagged end = PyUnicode_GET_LENGTH(str) << 1; + return CPyStr_IndexWithEnd(str, substr, start, end, direction); +} + +CPyTagged CPyStr_IndexWithEnd(PyObject *str, PyObject *substr, CPyTagged start, CPyTagged end, + int direction) { + CPyTagged index = CPyStr_FindWithEnd(str, substr, start, end, direction); + if (unlikely(index == CPyTagged_ShortFromSsize_t(-1))) { + PyErr_SetString(PyExc_ValueError, "substring not found"); + return CPY_INT_TAG; + } + return index; +} + PyObject *CPyStr_Split(PyObject *str, PyObject *sep, CPyTagged max_split) { Py_ssize_t temp_max_split = CPyTagged_AsSsize_t(max_split); if (temp_max_split == -1 && PyErr_Occurred()) { diff --git a/mypyc/primitives/str_ops.py b/mypyc/primitives/str_ops.py index ea968cd2837f..4215422707a8 100644 --- a/mypyc/primitives/str_ops.py +++ b/mypyc/primitives/str_ops.py @@ -185,6 +185,26 @@ error_kind=ERR_MAGIC, ) +# str.index(...) and str.rindex(...) +str_index_functions = ["CPyStr_Index", "CPyStr_Index", "CPyStr_IndexWithEnd"] +for i in range(len(str_find_types) - 1): + method_op( + name="index", + arg_types=str_find_types[0 : i + 2], + return_type=int_rprimitive, + c_function_name=str_index_functions[i], + extra_int_constants=str_find_constants[i] + [(1, c_int_rprimitive)], + error_kind=ERR_MAGIC, + ) + method_op( + name="rindex", + arg_types=str_find_types[0 : i + 2], + return_type=int_rprimitive, + c_function_name=str_index_functions[i], + extra_int_constants=str_rfind_constants[i] + [(-1, c_int_rprimitive)], + error_kind=ERR_MAGIC, + ) + # str.join(obj) method_op( name="join", diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index 8e2d63ae317a..3f470dae4879 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -117,6 +117,8 @@ def __contains__(self, item: str) -> bool: pass def __iter__(self) -> Iterator[str]: ... def find(self, sub: str, start: Optional[int] = None, end: Optional[int] = None, /) -> int: ... def rfind(self, sub: str, start: Optional[int] = None, end: Optional[int] = None, /) -> int: ... + def index(self, sub: str, start: Optional[int] = None, end: Optional[int] = None, /) -> int: ... + def rindex(self, sub: str, start: Optional[int] = None, end: Optional[int] = None, /) -> int: ... def split(self, sep: Optional[str] = None, maxsplit: int = -1) -> List[str]: pass def rsplit(self, sep: Optional[str] = None, maxsplit: int = -1) -> List[str]: pass def splitlines(self, keepends: bool = False) -> List[str]: ... diff --git a/mypyc/test-data/irbuild-str.test b/mypyc/test-data/irbuild-str.test index 1a9580af54f3..e114e940af00 100644 --- a/mypyc/test-data/irbuild-str.test +++ b/mypyc/test-data/irbuild-str.test @@ -995,6 +995,46 @@ L0: r0 = CPyStr_Multiply(s, n) return r0 +[case testStrIndex] +def index(s: str, sub: str, start: int, end: int) -> int: + a = s.index(sub) + b = s.index(sub, start) + c = s.index(sub, start, end) + return a + b + c + +def rindex(s: str, sub: str, start: int, end: int) -> int: + a = s.rindex(sub) + b = s.rindex(sub, start) + c = s.rindex(sub, start, end) + return a + b + c +[out] +def index(s, sub, start, end): + s, sub :: str + start, end, r0, a, r1, b, r2, c, r3, r4 :: int +L0: + r0 = CPyStr_Index(s, sub, 0, 1) + a = r0 + r1 = CPyStr_Index(s, sub, start, 1) + b = r1 + r2 = CPyStr_IndexWithEnd(s, sub, start, end, 1) + c = r2 + r3 = CPyTagged_Add(a, b) + r4 = CPyTagged_Add(r3, c) + return r4 +def rindex(s, sub, start, end): + s, sub :: str + start, end, r0, a, r1, b, r2, c, r3, r4 :: int +L0: + r0 = CPyStr_Index(s, sub, 0, -1) + a = r0 + r1 = CPyStr_Index(s, sub, start, -1) + b = r1 + r2 = CPyStr_IndexWithEnd(s, sub, start, end, -1) + c = r2 + r3 = CPyTagged_Add(a, b) + r4 = CPyTagged_Add(r3, c) + return r4 + [case testStrLower] def do_lower(s: str) -> str: return s.lower() diff --git a/mypyc/test-data/run-strings.test b/mypyc/test-data/run-strings.test index 46baca6341bd..0457f460585d 100644 --- a/mypyc/test-data/run-strings.test +++ b/mypyc/test-data/run-strings.test @@ -157,7 +157,7 @@ def test_unicode() -> None: assert ne("\U0001f4a9foo", "\U0001f4a8foo" + str()) [case testStringOps] -from typing import List, Optional, Tuple +from typing import Any, List, Optional, Tuple from testutil import assertRaises def do_split(s: str, sep: Optional[str] = None, max_split: Optional[int] = None) -> List[str]: @@ -246,6 +246,20 @@ def rfind(s: str, substr: str, start: Optional[int] = None, end: Optional[int] = return s.rfind(substr, start) return s.rfind(substr) +def index(s: str, substr: str, start: Optional[int] = None, end: Optional[int] = None) -> int: + if start is not None: + if end is not None: + return s.index(substr, start, end) + return s.index(substr, start) + return s.index(substr) + +def rindex(s: str, substr: str, start: Optional[int] = None, end: Optional[int] = None) -> int: + if start is not None: + if end is not None: + return s.rindex(substr, start, end) + return s.rindex(substr, start) + return s.rindex(substr) + s = "abc" def test_contains() -> None: @@ -339,6 +353,77 @@ def test_find_huge_indices() -> None: assert rfind(s, "", 0, 2**100) == 5 assert rfind(s, "b", 2**100, -2**100) == -1 +def test_index() -> None: + s = "abcab" + assert index(s, "abc") == 0 + assert index(s, "b") == 1 + assert index(s, "b", 1) == 1 + assert index(s, "b", 1, 2) == 1 + assert index(s, "b", 3) == 4 + assert index(s, "b", 3, 5) == 4 + assert index(s, "b", -2) == 4 + assert index(s, "b", -5, -3) == 1 + assert index(s, "") == 0 + assert index(s, "", 5) == 5 + assert index("\u4e2d\U0001f600a\U0001f600", "\U0001f600a") == 1 + + assert rindex(s, "abc") == 0 + assert rindex(s, "b") == 4 + assert rindex(s, "b", 1) == 4 + assert rindex(s, "b", 1, 2) == 1 + assert rindex(s, "b", 3) == 4 + assert rindex(s, "b", 3, 5) == 4 + assert rindex(s, "b", -5, -3) == 1 + assert rindex(s, "") == 5 + assert rindex(s, "", 0, 2) == 2 + assert rindex("\u4e2d\U0001f600a\U0001f600", "\U0001f600") == 3 + + for f in (index, rindex): + with assertRaises(ValueError, "substring not found"): + f(s, "Hello") + with assertRaises(ValueError, "substring not found"): + f(s, "b", 3, 4) + with assertRaises(ValueError, "substring not found"): + f(s, "", 6) + with assertRaises(ValueError, "substring not found"): + f("", "a") + + for sub in ["a", "b", "c", "ab", "ca", "x", ""]: + for start in range(-7, 8): + for end in range(-7, 8): + a: Any = s + try: + expected = a.index(sub, start, end) + except ValueError: + expected = -1 + try: + got = index(s, sub, start, end) + except ValueError: + got = -1 + assert got == expected + try: + expected = a.rindex(sub, start, end) + except ValueError: + expected = -1 + try: + got = rindex(s, sub, start, end) + except ValueError: + got = -1 + assert got == expected + +def test_index_huge_indices() -> None: + # Indices that don't fit in Py_ssize_t are clamped, like in CPython + s = "abcab" + assert index(s, "b", 0, 2**100) == 1 + assert index(s, "b", -2**100) == 1 + assert rindex(s, "b", -2**100) == 4 + assert rindex(s, "b", 0, 2**100) == 4 + for f in (index, rindex): + with assertRaises(ValueError, "substring not found"): + f(s, "b", 2**63) + with assertRaises(ValueError, "substring not found"): + f(s, "b", 0, -2**100) + def str_to_int(s: str, base: Optional[int] = None) -> int: if base: return int(s, base)