implement replace_with and replacen_with for unstable feature replace_with - #147741
implement replace_with and replacen_with for unstable feature replace_with#147741alpaylan wants to merge 3 commits into
replace_with and replacen_with for unstable feature replace_with#147741Conversation
| if let Some(from_byte) = match from.as_utf8_pattern() { | ||
| Some(Utf8Pattern::StringPattern([from_byte])) => Some(*from_byte), | ||
| Some(Utf8Pattern::CharPattern(c)) => c.as_ascii().map(|ascii_char| ascii_char.to_u8()), | ||
| _ => None, | ||
| } { | ||
| if let Some(sref) = from_byte.as_ascii().map(|ascii_char| f(ascii_char.as_str())) { | ||
| if let [to_byte] = sref.as_ref().as_bytes() { | ||
| return unsafe { replace_ascii(self.as_bytes(), from_byte, *to_byte) }; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Would it be possible to use let chains here?
There was a problem hiding this comment.
Updated with an if-let chain, did you have something else in mind or is this fine? (f70b5e1)
This comment has been minimized.
This comment has been minimized.
|
Hm, that's weird because alloc is 2024 edition Line 11 in f524236 |
|
It does compile with edition 2021 anyway tho Well, I guess that's fine to revert this then :) If this ever will get 2024 edition I will cleanup this accordingly |
| from: P, | ||
| mut f: impl FnMut(&str) -> S, | ||
| ) -> String { | ||
| // Fast path for replacing a single ASCII character with another. |
There was a problem hiding this comment.
f might not be side-effect-free, so this optimisation is incorrect.
There was a problem hiding this comment.
Ah, that is correct. I'm assuming I should just remove it, or are there any other potential optimization?
|
Hi, ping from triage team. This PR has been inactive for a while. Are there any updates on this? I assume this PR should now be waiting for review? @rustbot ready |
| result | ||
| } | ||
|
|
||
| /// Replaces first N matches of a pattern with another string. |
There was a problem hiding this comment.
| /// Replaces first N matches of a pattern with another string. | |
| /// Replaces the first N matches of a pattern with another string. |
There was a problem hiding this comment.
This is also an issue that's similarly present in replacen (https://doc.rust-lang.org/src/alloc/str.rs.html#296)
I assume I should fix both.
|
|
||
| /// Replaces first N matches of a pattern with another string. | ||
| /// | ||
| /// `replacen` creates a new [`String`], and copies the data from this string slice into it. |
There was a problem hiding this comment.
This paragraph still needs updating.
| // Hope to reduce the times of re-allocation | ||
| let mut result = String::with_capacity(32); |
There was a problem hiding this comment.
This is a bit inconsistent, replace_with doesn't do this preallocation. I'd do it in replace_with, too.
There was a problem hiding this comment.
This is again replicating a pattern in replacen. I can do it of course but I had assumed there was a concrete reasoning behind it based on the usage pattern of replace vs replacen
|
Reminder, once the PR becomes ready for a review, use |
This comment has been minimized.
This comment has been minimized.
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This is the PR for the tracking issue #147731 for implementation of
replace_with.