Skip to content

Commit 5202c98

Browse files
authored
1 parent eb2b509 commit 5202c98

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

mypyc/doc/str_operations.rst

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Methods
4242
* ``s.split()``
4343
* ``s.split(sep: str)``
4444
* ``s.split(sep: str, maxsplit: int)``
45+
* ``s.splitlines()``
46+
* ``s.splitlines(keepends: bool)``
4547
* ``s1.startswith(s2: str)``
4648

4749
.. note::

mypyc/primitives/str_ops.py

+13
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@
163163
error_kind=ERR_MAGIC,
164164
)
165165

166+
# str.splitlines(...)
167+
str_splitlines_types: list[RType] = [str_rprimitive, bool_rprimitive]
168+
str_splitlines_constants: list[list[tuple[int, RType]]] = [[(0, c_int_rprimitive)], []]
169+
for i in range(2):
170+
method_op(
171+
name="splitlines",
172+
arg_types=str_splitlines_types[0 : i + 1],
173+
return_type=list_rprimitive,
174+
c_function_name="PyUnicode_Splitlines",
175+
extra_int_constants=str_splitlines_constants[i],
176+
error_kind=ERR_NEVER,
177+
)
178+
166179
# str.replace(old, new)
167180
method_op(
168181
name="replace",

mypyc/test-data/fixtures/ir.py

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def __contains__(self, item: str) -> bool: pass
104104
def __iter__(self) -> Iterator[str]: ...
105105
def split(self, sep: Optional[str] = None, maxsplit: int = -1) -> List[str]: pass
106106
def rsplit(self, sep: Optional[str] = None, maxsplit: int = -1) -> List[str]: pass
107+
def splitlines(self, keepends: bool = False) -> List[str]: ...
107108
def strip (self, item: str) -> str: pass
108109
def join(self, x: Iterable[str]) -> str: pass
109110
def format(self, *args: Any, **kwargs: Any) -> str: ...

mypyc/test-data/run-strings.test

+12
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ def test_rsplit() -> None:
8989
assert do_rsplit(ss, " ", 1) == ["abc abcd abcde", "abcdef"] # different to do_split
9090
assert do_rsplit(ss, " ", 2) == ["abc abcd", "abcde", "abcdef"] # different to do_split
9191

92+
def splitlines(s: str, keepends: Optional[bool] = None) -> List[str]:
93+
if keepends is not None:
94+
return s.splitlines(keepends)
95+
return s.splitlines()
96+
97+
s_text = "This\nis\n\nsome\nlong\ntext.\n"
98+
99+
def test_splitlines() -> None:
100+
assert splitlines(s_text) == ["This", "is", "", "some", "long", "text."]
101+
assert splitlines(s_text, False) == ["This", "is", "", "some", "long", "text."]
102+
assert splitlines(s_text, True) == ["This\n", "is\n", "\n", "some\n", "long\n", "text.\n"]
103+
92104
def getitem(s: str, index: int) -> str:
93105
return s[index]
94106

0 commit comments

Comments
 (0)