Fix str.replace negative value

This commit is contained in:
Jeong YunWon
2020-04-11 10:19:32 +09:00
parent c7467e444d
commit 62febeb80c
2 changed files with 11 additions and 6 deletions

View File

@@ -488,8 +488,6 @@ class UnicodeTest(string_tests.CommonTest,
seq = ('A' * size,) * size
self.assertRaises(OverflowError, ''.join, seq)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_replace(self):
string_tests.CommonTest.test_replace(self)

View File

@@ -790,10 +790,17 @@ impl PyString {
}
#[pymethod]
fn replace(&self, old: PyStringRef, new: PyStringRef, num: OptionalArg<usize>) -> String {
match num.into_option() {
Some(num) => self.value.replacen(&old.value, &new.value, num),
None => self.value.replace(&old.value, &new.value),
fn replace(&self, old: PyStringRef, new: PyStringRef, count: OptionalArg<isize>) -> String {
match count {
OptionalArg::Present(maxcount) if maxcount >= 0 => {
if maxcount == 0 || self.value.is_empty() {
// nothing to do; return the original bytes
return self.value.clone();
}
self.value
.replacen(&old.value, &new.value, maxcount as usize)
}
_ => self.value.replace(&old.value, &new.value),
}
}