Returns new string when (str|bytes).replace get empty original string

This commit is contained in:
Yongjin authored and Jeong YunWon committed 2023-03-27 15:05:45 +09:00
1 parent 3fde8f3b27
commit f7eb51204a
3 files changed
+10 -3

No files matched your search

+3
View File
@@ -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)
+4 -2
View File
@@ -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)
}
+3 -1
View File
@@ -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)
}