FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

optimize str.(l|r)strip by youknowone · Pull Request #4496 · RustPython/RustPython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rs  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 0 additions & 10 deletions Lib/test/test_bigmem.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -765,16 +765,6 @@ def test_translate(self, size):
self.assertEqual(s.count(_('!')), repeats * 2)
self.assertEqual(s.count(_('z')), repeats * 3)

# TODO: RUSTPYTHON
Comment thread
DimitrisJim marked this conversation as resolved.
@unittest.expectedFailure
def test_lstrip(self, size):
super().test_lstrip(size)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_rstrip(self, size):
super().test_rstrip(size)


class BytesTest(unittest.TestCase, BaseStrTest):

Expand Down
48 changes: 32 additions & 16 deletions vm/src/builtins/str.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -592,25 +592,41 @@ impl PyStr {
}

#[pymethod]
fn lstrip(&self, chars: OptionalOption<PyStrRef>) -> String {
self.as_str()
.py_strip(
chars,
|s, chars| s.trim_start_matches(|c| chars.contains(c)),
|s| s.trim_start(),
)
.to_owned()
fn lstrip(
zelf: PyRef<Self>,
chars: OptionalOption<PyStrRef>,
vm: &VirtualMachine,
) -> PyRef<Self> {
let s = zelf.as_str();
let stripped = s.py_strip(
chars,
|s, chars| s.trim_start_matches(|c| chars.contains(c)),
|s| s.trim_start(),
);
if s == stripped {
zelf
} else {
stripped.into_pystr_ref(vm)
}
}

#[pymethod]
fn rstrip(&self, chars: OptionalOption<PyStrRef>) -> String {
self.as_str()
.py_strip(
chars,
|s, chars| s.trim_end_matches(|c| chars.contains(c)),
|s| s.trim_end(),
)
.to_owned()
fn rstrip(
zelf: PyRef<Self>,
chars: OptionalOption<PyStrRef>,
vm: &VirtualMachine,
) -> PyRef<Self> {
let s = zelf.as_str();
let stripped = s.py_strip(
chars,
|s, chars| s.trim_end_matches(|c| chars.contains(c)),
|s| s.trim_end(),
);
if s == stripped {
zelf
} else {
stripped.into_pystr_ref(vm)
}
}

#[pymethod]
Expand Down

Back | FazBrowse Home | New Git URL