-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
str: common logic for replace & replacen #56464
Conversation
r? @kennytm (rust_highfive has picked a reviewer for you, use r? to override) |
I'm going to rewrite these functions for #56345, would you mind if I just incorporate these changes directly into the new PR? |
Sure, I don't mind at all 👌. |
OK I'll close this when that PR is submitted. |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@@ -622,3 +605,16 @@ impl str { | |||
pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> { | |||
Box::from_raw(Box::into_raw(v) as *mut str) | |||
} | |||
|
|||
fn replace_helper<'a, P: Pattern<'a>>(s: &'a str, pat: P, to: &str, lim: Option<usize>) -> String { |
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.
A comment would be nice... :)
let mut last_end = 0; | ||
let limit = if let Some(limit) = lim { limit } else { s.len() }; | ||
for (start, part) in s.match_indices(pat).take(limit) { | ||
result.push_str(unsafe { s.get_unchecked(last_end..start) }); |
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.
And a comment explaining why this is safe as well...
result.push_str(to); | ||
last_end = start + part.len(); | ||
} | ||
result.push_str(unsafe { s.get_unchecked(last_end..s.len()) }); |
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.
and here...
@ljedrz can you update this PR with the changes requested above? |
@Dylan-DPC actually @kennytm is interested in intercepting this change (see #56464 (comment)); that's why I'm not updating this PR - it will be closed when it becomes a part of #56345. |
Thanks. Marking it as blocked. |
☔ The latest upstream changes (presumably #58051) made this pull request unmergeable. Please resolve the merge conflicts. |
Closing meanwhile. |
The logic of
str::replace
andstr::replacen
is almost identical - we can use a common helper function. The initial capacity of the resulting string was different (0
vs.32
), but I propose a different approach: use the same capacity as in the original string; this will neither reallocate too often (or not at all if the substitute's length is the same as the substituted bit's), nor will it overallocate for short strings. It will also be an exact fit in case of no pattern hits.