We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 555bfae commit 8fc8d26Copy full SHA for 8fc8d26
mypyc/doc/str_operations.rst
@@ -32,6 +32,8 @@ Methods
32
* ``s.encode(encoding: str, errors: str)``
33
* ``s1.endswith(s2: str)``
34
* ``s.join(x: Iterable)``
35
+* ``s.removeprefix(prefix: str)``
36
+* ``s.removesuffix(suffix: str)``
37
* ``s.replace(old: str, new: str)``
38
* ``s.replace(old: str, new: str, count: int)``
39
* ``s.split()``
mypyc/lib-rt/CPy.h
@@ -726,6 +726,8 @@ PyObject *CPyStr_Append(PyObject *o1, PyObject *o2);
726
PyObject *CPyStr_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end);
727
bool CPyStr_Startswith(PyObject *self, PyObject *subobj);
728
bool CPyStr_Endswith(PyObject *self, PyObject *subobj);
729
+PyObject *CPyStr_Removeprefix(PyObject *self, PyObject *prefix);
730
+PyObject *CPyStr_Removesuffix(PyObject *self, PyObject *suffix);
731
bool CPyStr_IsTrue(PyObject *obj);
732
Py_ssize_t CPyStr_Size_size_t(PyObject *str);
733
PyObject *CPy_Decode(PyObject *obj, PyObject *encoding, PyObject *errors);
mypyc/lib-rt/str_ops.c
@@ -164,6 +164,26 @@ bool CPyStr_Endswith(PyObject *self, PyObject *subobj) {
164
return PyUnicode_Tailmatch(self, subobj, start, end, 1);
165
}
166
167
+PyObject *CPyStr_Removeprefix(PyObject *self, PyObject *prefix) {
168
+ Py_ssize_t end = PyUnicode_GET_LENGTH(self);
169
+ int match = PyUnicode_Tailmatch(self, prefix, 0, end, -1);
170
+ if (match) {
171
+ Py_ssize_t prefix_end = PyUnicode_GET_LENGTH(prefix);
172
+ return PyUnicode_Substring(self, prefix_end, end);
173
+ }
174
+ return Py_NewRef(self);
175
+}
176
+
177
+PyObject *CPyStr_Removesuffix(PyObject *self, PyObject *suffix) {
178
179
+ int match = PyUnicode_Tailmatch(self, suffix, 0, end, 1);
180
181
+ Py_ssize_t suffix_end = PyUnicode_GET_LENGTH(suffix);
182
+ return PyUnicode_Substring(self, 0, end - suffix_end);
183
184
185
186
187
/* This does a dodgy attempt to append in place */
188
PyObject *CPyStr_Append(PyObject *o1, PyObject *o2) {
189
PyUnicode_Append(&o1, o2);
mypyc/primitives/str_ops.py
@@ -118,6 +118,24 @@
118
error_kind=ERR_NEVER,
119
)
120
121
+# str.removeprefix(str)
122
+method_op(
123
+ name="removeprefix",
124
+ arg_types=[str_rprimitive, str_rprimitive],
125
+ return_type=str_rprimitive,
126
+ c_function_name="CPyStr_Removeprefix",
127
+ error_kind=ERR_NEVER,
128
+)
129
130
+# str.removesuffix(str)
131
132
+ name="removesuffix",
133
134
135
+ c_function_name="CPyStr_Removesuffix",
136
137
138
139
# str.split(...)
140
str_split_types: list[RType] = [str_rprimitive, str_rprimitive, int_rprimitive]
141
str_split_functions = ["PyUnicode_Split", "PyUnicode_Split", "CPyStr_Split"]
mypyc/test-data/fixtures/ir.py
@@ -111,6 +111,8 @@ def startswith(self, x: str, start: int=..., end: int=...) -> bool: ...
111
def endswith(self, x: str, start: int=..., end: int=...) -> bool: ...
112
def replace(self, old: str, new: str, maxcount: int=...) -> str: ...
113
def encode(self, encoding: str=..., errors: str=...) -> bytes: ...
114
+ def removeprefix(self, prefix: str, /) -> str: ...
115
+ def removesuffix(self, suffix: str, /) -> str: ...
116
117
class float:
def __init__(self, x: object) -> None: pass
mypyc/test-data/run-strings.test
@@ -20,9 +20,11 @@ def eq(x: str) -> int:
20
return 2
21
def match(x: str, y: str) -> Tuple[bool, bool]:
22
return (x.startswith(y), x.endswith(y))
23
+def remove_prefix_suffix(x: str, y: str) -> Tuple[str, str]:
24
+ return (x.removeprefix(y), x.removesuffix(y))
25
26
[file driver.py]
-from native import f, g, tostr, booltostr, concat, eq, match
27
+from native import f, g, tostr, booltostr, concat, eq, match, remove_prefix_suffix
28
import sys
29
30
assert f() == 'some string'
@@ -44,6 +46,10 @@ assert match('abc', 'a') == (True, False)
44
46
assert match('abc', 'c') == (False, True)
45
47
assert match('', 'abc') == (False, False)
48
49
+assert remove_prefix_suffix('', '') == ('', '')
50
+assert remove_prefix_suffix('abc', 'a') == ('bc', 'abc')
51
+assert remove_prefix_suffix('abc', 'c') == ('abc', 'ab')
52
53
[case testStringOps]
54
from typing import List, Optional
55
0 commit comments