mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
optimize str.(l|r)strip
This commit is contained in:
1 parent
443835096b
commit
317802a26e
2 files changed
+32
-26
No files matched your search
Vendored
-10
@@ -765,16 +765,6 @@ class StrTest(unittest.TestCase, BaseStrTest):
|
||||
self.assertEqual(s.count(_('!')), repeats * 2)
|
||||
self.assertEqual(s.count(_('z')), repeats * 3)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@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):
|
||||
|
||||
|
||||
+32
-16
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user