Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[mypyc] Optimize str.rsplit#18673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JukkaL merged 1 commit intopython:masterfromcdce8p:mypyc-str-rsplit
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
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
[mypyc] Optimize str.rsplit
  • Loading branch information
@cdce8p
cdce8p committedFeb 14, 2025
commitb6f0d47b05e9d89045b7cc7cd32ead351ded3503
3 changes: 3 additions & 0 deletionsmypyc/doc/str_operations.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,6 +36,9 @@ Methods
* ``s.removesuffix(suffix: str)``
* ``s.replace(old: str, new: str)``
* ``s.replace(old: str, new: str, count: int)``
* ``s.rsplit()``
* ``s.rsplit(sep: str)``
* ``s.rsplit(sep: str, maxsplit: int)``
* ``s.split()``
* ``s.split(sep: str)``
* ``s.split(sep: str, maxsplit: int)``
Expand Down
1 change: 1 addition & 0 deletionsmypyc/lib-rt/CPy.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -721,6 +721,7 @@ static inline char CPyDict_CheckSize(PyObject *dict, CPyTagged size) {
PyObject*CPyStr_Build(Py_ssize_tlen, ...);
PyObject*CPyStr_GetItem(PyObject*str,CPyTaggedindex);
PyObject*CPyStr_Split(PyObject*str,PyObject*sep,CPyTaggedmax_split);
PyObject*CPyStr_RSplit(PyObject*str,PyObject*sep,CPyTaggedmax_split);
PyObject*CPyStr_Replace(PyObject*str,PyObject*old_substr,PyObject*new_substr,CPyTaggedmax_replace);
PyObject*CPyStr_Append(PyObject*o1,PyObject*o2);
PyObject*CPyStr_GetSlice(PyObject*obj,CPyTaggedstart,CPyTaggedend);
Expand Down
9 changes: 9 additions & 0 deletionsmypyc/lib-rt/str_ops.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -142,6 +142,15 @@ PyObject *CPyStr_Split(PyObject *str, PyObject *sep, CPyTagged max_split) {
returnPyUnicode_Split(str,sep,temp_max_split);
}

PyObject*CPyStr_RSplit(PyObject*str,PyObject*sep,CPyTaggedmax_split) {
Py_ssize_ttemp_max_split=CPyTagged_AsSsize_t(max_split);
if (temp_max_split==-1&&PyErr_Occurred()) {
PyErr_SetString(PyExc_OverflowError,CPYTHON_LARGE_INT_ERRMSG);
returnNULL;
}
returnPyUnicode_RSplit(str,sep,temp_max_split);
}

PyObject*CPyStr_Replace(PyObject*str,PyObject*old_substr,
PyObject*new_substr,CPyTaggedmax_replace) {
Py_ssize_ttemp_max_replace=CPyTagged_AsSsize_t(max_replace);
Expand Down
11 changes: 10 additions & 1 deletionmypyc/primitives/str_ops.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -136,9 +136,10 @@
error_kind=ERR_NEVER,
)

# str.split(...)
# str.split(...) and str.rsplit(...)
str_split_types: list[RType] = [str_rprimitive, str_rprimitive, int_rprimitive]
str_split_functions = ["PyUnicode_Split", "PyUnicode_Split", "CPyStr_Split"]
str_rsplit_functions = ["PyUnicode_RSplit", "PyUnicode_RSplit", "CPyStr_RSplit"]
str_split_constants: list[list[tuple[int, RType]]] = [
[(0, pointer_rprimitive), (-1, c_int_rprimitive)],
[(-1, c_int_rprimitive)],
Expand All@@ -153,6 +154,14 @@
extra_int_constants=str_split_constants[i],
error_kind=ERR_MAGIC,
)
method_op(
name="rsplit",
arg_types=str_split_types[0 : i + 1],
return_type=list_rprimitive,
c_function_name=str_rsplit_functions[i],
extra_int_constants=str_split_constants[i],
error_kind=ERR_MAGIC,
)

# str.replace(old, new)
method_op(
Expand Down
3 changes: 2 additions & 1 deletionmypyc/test-data/fixtures/ir.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,7 +102,8 @@ def __getitem__(self, i: int) -> str: pass
def __getitem__(self, i: slice) -> str: pass
def __contains__(self, item: str) -> bool: pass
def __iter__(self) -> Iterator[str]: ...
def split(self, sep: Optional[str] = None, max: Optional[int] = None) -> List[str]: pass
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 strip (self, item: str) -> str: pass
def join(self, x: Iterable[str]) -> str: pass
def format(self, *args: Any, **kwargs: Any) -> str: ...
Expand Down
17 changes: 17 additions & 0 deletionsmypyc/test-data/run-strings.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,14 @@ def do_split(s: str, sep: Optional[str] = None, max_split: Optional[int] = None)
return s.split(sep)
return s.split()

def do_rsplit(s: str, sep: Optional[str] = None, max_split: Optional[int] = None) -> List[str]:
if sep is not None:
if max_split is not None:
return s.rsplit(sep, max_split)
else:
return s.rsplit(sep)
return s.rsplit()

ss = "abc abcd abcde abcdef"

def test_split() -> None:
Expand All@@ -72,6 +80,15 @@ def test_split() -> None:
assert do_split(ss, " ", 1) == ["abc", "abcd abcde abcdef"]
assert do_split(ss, " ", 2) == ["abc", "abcd", "abcde abcdef"]

def test_rsplit() -> None:
assert do_rsplit(ss) == ["abc", "abcd", "abcde", "abcdef"]
assert do_rsplit(ss, " ") == ["abc", "abcd", "abcde", "abcdef"]
assert do_rsplit(ss, "-") == ["abc abcd abcde abcdef"]
assert do_rsplit(ss, " ", -1) == ["abc", "abcd", "abcde", "abcdef"]
assert do_rsplit(ss, " ", 0) == ["abc abcd abcde abcdef"]
assert do_rsplit(ss, " ", 1) == ["abc abcd abcde", "abcdef"] # different to do_split
assert do_rsplit(ss, " ", 2) == ["abc abcd", "abcde", "abcdef"] # different to do_split

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

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp