Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions mypyc/doc/str_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Operators
* Slicing (``s[n:m]``, ``s[n:]``, ``s[:m]``)
* Comparisons (``==``, ``!=``)
* Augmented assignment (``s1 += s2``)
* Containment (``s1 in s2``)

.. _str-methods:

Expand Down
11 changes: 11 additions & 0 deletions mypyc/primitives/str_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@
error_kind=ERR_MAGIC,
)

# item in str
set_in_op = binary_op(
name="in",
arg_types=[str_rprimitive, str_rprimitive],
return_type=c_int_rprimitive,
c_function_name="PyUnicode_Contains",
error_kind=ERR_NEG_INT,
truncated_type=bool_rprimitive,
ordering=[1, 0],
)

# str.join(obj)
method_op(
name="join",
Expand Down
10 changes: 10 additions & 0 deletions mypyc/test-data/run-strings.test
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,21 @@ def test_partition() -> None:
with assertRaises(ValueError, "empty separator"):
rpartition(s_partition, "")

def contains(s: str, o: str) -> bool:
return o in s

def getitem(s: str, index: int) -> str:
return s[index]

s = "abc"

def test_contains() -> None:
assert contains(s, "a") is True
assert contains(s, "abc") is True
assert contains(s, "Hello") is False
assert contains(s, "") is True
assert contains(s, " ") is False

def test_getitem() -> None:
assert getitem(s, 0) == "a"
assert getitem(s, 1) == "b"
Expand Down