-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
String methods for replacing a sub-string with another one in place #2462
Comments
I have had this need often when having to replace some fixed with date/time format or a fixed width (zero-padded) serial code in some string. It always feels really bad to allocate for that, because Rust makes it explicit and you know it would be possible since you know the exact length of your string. |
@gnzlbg So sortof a combination |
I feel that this is niche enough that it can be developed on crates.io, at least at first. |
@scottmcm kind of but doing this without allocating if the replacement is smaller, and allocating only once if it is larger, and in one pass, is tricky. |
I have a sorta similar issue for non-allocating string pattern removal: rust-lang/rust#50206 |
here's the thing, you can convert String to Vec via into then operate on bytes directly and convert it back to |
I wasn't suggesting that it was impossible to achieve this by building something on top of Because it is not "easily" available it's often better to say "let's do an allocation and call it a day" than trying to implement this yourself unless this is your bottle neck. But often I have to replace patterns of equal length (one letter with another) or larger patterns with shorter patters, and if this would be available, I'll definitely prefer it over doing an allocation. |
I agree that this problem, like many problems, is better by having a shared solution that is well thought-out and that people can reuse, so that every potential user doesn’t need to solve it again every time. My point is only that crates.io is a fine way to do such reuse, not everything needs to be in the standard library. My subjective feeling that this use case has enough combined constraints that it’s not that common, but maybe I’m wrong. Once an implementation exists and gets some use, we can see if it becomes popular and consider inclusion then. |
I forget: Why is In any case, this sounds largely like
Anyways, there are several issues that need fixing here: We've no
We've no
We need a method to smartly borrow a I think allocating only once if the result is larger sounds kinda tricky, maybe you even want xi-rope |
I don’t see a reason not to add However you cannot change the length of |
Oops, I'd imagined changing the length of the inner Appropriate generality for array and string operations that avoid reallocations might resemble:
|
I've often needed to replace a sub-string within a
String
in place with a new sub-string. If the new sub-string is smaller than the sub-string its replacing, that can be done in place without any allocations.I don't know if this is the right place to fill in a "feature request", but there is only one correct efficient way to implement such a method, and an RFC feels overkill for that.
The only thing I don't know is how to name it. We could name it
String::replace_in_place
andString::replacen_in_place
but... if the new string is larger than the one it is replacing then an allocation will happen, so "in place" isn't 100% accurate, unless we consider "in place" to mean that it happens on thisString
, independently of whether it is reallocated or not.The text was updated successfully, but these errors were encountered: