mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
Returns new string when (str|bytes).replace get empty original string
This commit is contained in:
1 parent
3fde8f3b27
commit
f7eb51204a
3 files changed
+10
-3
No files matched your search
Vendored
+3
@@ -1202,6 +1202,9 @@ class MixinStrUnicodeUserStringTest:
|
||||
self.checkequal(False, 'asd', '__contains__', 'asdf')
|
||||
self.checkequal(False, '', '__contains__', 'asdf')
|
||||
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_subscript(self):
|
||||
self.checkequal('a', 'abc', '__getitem__', 0)
|
||||
self.checkequal('c', 'abc', '__getitem__', -1)
|
||||
|
||||
@@ -826,9 +826,11 @@ impl PyStr {
|
||||
let s = self.as_str();
|
||||
match count {
|
||||
OptionalArg::Present(max_count) if max_count >= 0 => {
|
||||
if max_count == 0 || s.is_empty() {
|
||||
if max_count == 0 || (s.is_empty() && !old.is_empty()) {
|
||||
// nothing to do; return the original bytes
|
||||
s.into()
|
||||
s.to_owned()
|
||||
} else if s.is_empty() && old.is_empty() {
|
||||
new.as_str().to_owned()
|
||||
} else {
|
||||
s.replacen(old.as_str(), new.as_str(), max_count as usize)
|
||||
}
|
||||
|
||||
@@ -847,9 +847,11 @@ impl PyBytesInner {
|
||||
// stringlib_replace in CPython
|
||||
let maxcount = match maxcount {
|
||||
OptionalArg::Present(maxcount) if maxcount >= 0 => {
|
||||
if maxcount == 0 || self.elements.is_empty() {
|
||||
if maxcount == 0 || (self.elements.is_empty() && !from.is_empty()) {
|
||||
// nothing to do; return the original bytes
|
||||
return Ok(self.elements.clone());
|
||||
} else if self.elements.is_empty() && from.is_empty() {
|
||||
return Ok(to.elements);
|
||||
}
|
||||
Some(maxcount as usize)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user