-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix bytes.replace #1847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix bytes.replace #1847
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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 |
---|---|---|
|
@@ -1127,41 +1127,166 @@ impl PyByteInner { | |
bytes_zfill(&self.elements, width.to_usize().unwrap_or(0)) | ||
} | ||
|
||
pub fn replace( | ||
// len(self)>=1, from="", len(to)>=1, maxcount>=1 | ||
fn replace_interleave(&self, to: PyByteInner, maxcount: Option<usize>) -> Vec<u8> { | ||
let place_count = self.elements.len() + 1; | ||
let count = maxcount.map_or(place_count, |v| std::cmp::min(v, place_count)) - 1; | ||
let capacity = self.elements.len() + count * to.len(); | ||
let mut result = Vec::with_capacity(capacity); | ||
let to_slice = to.elements.as_slice(); | ||
result.extend_from_slice(to_slice); | ||
for c in &self.elements[..count] { | ||
result.push(*c); | ||
result.extend_from_slice(to_slice); | ||
} | ||
result.extend_from_slice(&self.elements[count..]); | ||
result | ||
} | ||
|
||
fn replace_delete(&self, from: PyByteInner, maxcount: Option<usize>) -> Vec<u8> { | ||
let count = count_substring(self.elements.as_slice(), from.elements.as_slice(), maxcount); | ||
if count == 0 { | ||
// no matches | ||
return self.elements.clone(); | ||
} | ||
|
||
let result_len = self.len() - (count * from.len()); | ||
debug_assert!(self.len() >= count * from.len()); | ||
|
||
let mut result = Vec::with_capacity(result_len); | ||
let mut last_end = 0; | ||
let mut count = count; | ||
for offset in self.elements.find_iter(&from.elements) { | ||
result.extend_from_slice(&self.elements[last_end..offset]); | ||
last_end = offset + from.len(); | ||
count -= 1; | ||
if count == 0 { | ||
break; | ||
} | ||
} | ||
result.extend_from_slice(&self.elements[last_end..]); | ||
result | ||
} | ||
|
||
pub fn replace_in_place( | ||
&self, | ||
old: PyByteInner, | ||
new: PyByteInner, | ||
count: OptionalArg<PyIntRef>, | ||
) -> PyResult<Vec<u8>> { | ||
let count = match count.into_option() { | ||
Some(int) => int | ||
.as_bigint() | ||
.to_u32() | ||
.unwrap_or(self.elements.len() as u32), | ||
None => self.elements.len() as u32, | ||
from: PyByteInner, | ||
to: PyByteInner, | ||
maxcount: Option<usize>, | ||
) -> Vec<u8> { | ||
let len = from.len(); | ||
let mut iter = self.elements.find_iter(&from.elements); | ||
|
||
let mut new = if let Some(offset) = iter.next() { | ||
let mut new = self.elements.clone(); | ||
new[offset..offset + len].clone_from_slice(to.elements.as_slice()); | ||
if maxcount == Some(1) { | ||
youknowone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return new; | ||
} else { | ||
new | ||
} | ||
} else { | ||
return self.elements.clone(); | ||
}; | ||
|
||
let mut res = vec![]; | ||
let mut index = 0; | ||
let mut done = 0; | ||
let mut count = maxcount.unwrap_or(std::usize::MAX) - 1; | ||
for offset in iter { | ||
new[offset..offset + len].clone_from_slice(to.elements.as_slice()); | ||
count -= 1; | ||
if count == 0 { | ||
break; | ||
} | ||
} | ||
new | ||
} | ||
|
||
let slice = &self.elements; | ||
loop { | ||
if done == count || index > slice.len() - old.len() { | ||
res.extend_from_slice(&slice[index..]); | ||
fn replace_general( | ||
&self, | ||
from: PyByteInner, | ||
to: PyByteInner, | ||
maxcount: Option<usize>, | ||
vm: &VirtualMachine, | ||
) -> PyResult<Vec<u8>> { | ||
let count = count_substring(self.elements.as_slice(), from.elements.as_slice(), maxcount); | ||
if count == 0 { | ||
// no matches, return unchanged | ||
return Ok(self.elements.clone()); | ||
} | ||
|
||
// Check for overflow | ||
// result_len = self_len + count * (to_len-from_len) | ||
debug_assert!(count > 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this? |
||
if to.len() as isize - from.len() as isize | ||
> (std::isize::MAX - self.elements.len() as isize) / count as isize | ||
{ | ||
return Err(vm.new_overflow_error("replace bytes is too long".to_owned())); | ||
} | ||
let result_len = self.elements.len() + count * (to.len() - from.len()); | ||
|
||
let mut result = Vec::with_capacity(result_len); | ||
let mut last_end = 0; | ||
let mut count = count; | ||
for offset in self.elements.find_iter(&from.elements) { | ||
result.extend_from_slice(&self.elements[last_end..offset]); | ||
result.extend_from_slice(to.elements.as_slice()); | ||
last_end = offset + from.len(); | ||
count -= 1; | ||
if count == 0 { | ||
break; | ||
} | ||
if &slice[index..index + old.len()] == old.elements.as_slice() { | ||
res.extend_from_slice(&new.elements); | ||
index += old.len(); | ||
done += 1; | ||
} else { | ||
res.push(slice[index]); | ||
index += 1 | ||
} | ||
result.extend_from_slice(&self.elements[last_end..]); | ||
Ok(result) | ||
} | ||
|
||
pub fn replace( | ||
&self, | ||
from: PyByteInner, | ||
to: PyByteInner, | ||
maxcount: OptionalArg<isize>, | ||
vm: &VirtualMachine, | ||
) -> PyResult<Vec<u8>> { | ||
// stringlib_replace in CPython | ||
let maxcount = match maxcount { | ||
OptionalArg::Present(maxcount) if maxcount >= 0 => { | ||
if maxcount == 0 || self.elements.is_empty() { | ||
// nothing to do; return the original bytes | ||
return Ok(self.elements.clone()); | ||
} | ||
Some(maxcount as usize) | ||
} | ||
_ => None, | ||
}; | ||
|
||
// Handle zero-length special cases | ||
if from.elements.is_empty() { | ||
if to.elements.is_empty() { | ||
// nothing to do; return the original bytes | ||
return Ok(self.elements.clone()); | ||
} | ||
// insert the 'to' bytes everywhere. | ||
// >>> b"Python".replace(b"", b".") | ||
// b'.P.y.t.h.o.n.' | ||
return Ok(self.replace_interleave(to, maxcount)); | ||
} | ||
|
||
Ok(res) | ||
// Except for b"".replace(b"", b"A") == b"A" there is no way beyond this | ||
// point for an empty self bytes to generate a non-empty bytes | ||
// Special case so the remaining code always gets a non-empty bytes | ||
if self.elements.is_empty() { | ||
return Ok(self.elements.clone()); | ||
} | ||
|
||
if to.elements.is_empty() { | ||
// delete all occurrences of 'from' bytes | ||
Ok(self.replace_delete(from, maxcount)) | ||
} else if from.len() == to.len() { | ||
// Handle special case where both bytes have the same length | ||
Ok(self.replace_in_place(from, to, maxcount)) | ||
} else { | ||
// Otherwise use the more generic algorithms | ||
self.replace_general(from, to, maxcount, vm) | ||
} | ||
} | ||
|
||
pub fn title(&self) -> Vec<u8> { | ||
|
@@ -1233,6 +1358,16 @@ pub fn try_as_byte(obj: &PyObjectRef) -> Option<Vec<u8>> { | |
}) | ||
} | ||
|
||
#[inline] | ||
fn count_substring(haystack: &[u8], needle: &[u8], maxcount: Option<usize>) -> usize { | ||
let substrings = haystack.find_iter(needle); | ||
if let Some(maxcount) = maxcount { | ||
std::cmp::min(substrings.take(maxcount).count(), maxcount) | ||
} else { | ||
substrings.count() | ||
} | ||
} | ||
|
||
pub trait ByteOr: ToPrimitive { | ||
fn byte_or(&self, vm: &VirtualMachine) -> PyResult<u8> { | ||
match self.to_u8() { | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe remove this? I know is is only in debug but it does not provide additional data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is line by line port of CPython implementation. Do you still want to remove this regardless of it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am OK with you keeping this. Just not sure if that is necessary. Your call.