Skip to content

Commit 49e014a

Browse files
authored
1 parent f66741c commit 49e014a

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

mypyc/doc/str_operations.rst

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Operators
2121
* Slicing (``s[n:m]``, ``s[n:]``, ``s[:m]``)
2222
* Comparisons (``==``, ``!=``)
2323
* Augmented assignment (``s1 += s2``)
24+
* Containment (``s1 in s2``)
2425

2526
.. _str-methods:
2627

mypyc/primitives/str_ops.py

+11
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@
8484
error_kind=ERR_MAGIC,
8585
)
8686

87+
# item in str
88+
binary_op(
89+
name="in",
90+
arg_types=[str_rprimitive, str_rprimitive],
91+
return_type=c_int_rprimitive,
92+
c_function_name="PyUnicode_Contains",
93+
error_kind=ERR_NEG_INT,
94+
truncated_type=bool_rprimitive,
95+
ordering=[1, 0],
96+
)
97+
8798
# str.join(obj)
8899
method_op(
89100
name="join",

mypyc/test-data/run-strings.test

+13
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,24 @@ def test_partition() -> None:
140140
with assertRaises(ValueError, "empty separator"):
141141
rpartition(s_partition, "")
142142

143+
def contains(s: str, o: str) -> bool:
144+
return o in s
145+
143146
def getitem(s: str, index: int) -> str:
144147
return s[index]
145148

146149
s = "abc"
147150

151+
def test_contains() -> None:
152+
assert contains(s, "a") is True
153+
assert contains(s, "abc") is True
154+
assert contains(s, "Hello") is False
155+
assert contains(s, "bc") is True
156+
assert contains(s, "abcd") is False
157+
assert contains(s, "bb") is False
158+
assert contains(s, "") is True
159+
assert contains(s, " ") is False
160+
148161
def test_getitem() -> None:
149162
assert getitem(s, 0) == "a"
150163
assert getitem(s, 1) == "b"

0 commit comments

Comments
 (0)