-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add str.substr() and str.substr_until() methods #31140
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
#[inline] | ||
fn substr_until(&self, start_index: usize, length: usize) -> Option<&str> { | ||
if length == 0 || self.is_empty() { return None; } | ||
if start_index == 0 && length == self.len() { return Some(self); } |
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.
that optimization is unnecessary
What's the motivation behind this change? What's the advantage over using the slicing syntax directly? |
@oli-obk Using the slicing syntax directly can be confusing. For example, someone wanting to retrieve a substring (or, a slice) with length of 2 characters could think this would do it: let string = "Hello, world!"
let substring = string[7, 2]; // we want to retrieve the 'wo' part However, this gives a compile error, because the last parameter of the range syntax works like let substring = string[7, 9]; It also allows you to substring from That's where I got the motivation to submit this Pull Request. EDIT: the above snippet implemented with my code would look like: let substring = string.substr_until(7, 2); |
While the range syntax doesn't support this directly, the following works: |
We do want some kind of Option-enabled slicing api, but the naming and api needs some adjustment. It should use a range as the argument, just like Index does, and like |
@bluss Are you suggesting me to remove the two methods and just go with one instead, that takes a range as an argument? |
Yes. I don't know if we have or need an RFC for this, but it's in fact something we want. But not to have an alternative argument style, but to provide careful slicing (which returns None on out of bounds / indexing error). |
str.substr(start_index: usize) returns a substring of str starting from the specified point (Hello.substr(1) = ello) str.substr_until(start_index: usize, length: usize) returns a substring of str starting from the specified point, with the specified length (Hello.substr_until(0, 2) = He)
As of now, as @bluss suggested, I decided to go with a let string = String::from("Hello, world!");
assert_eq!(Some("world!"), string.substr(7..)); |
let end = *range.end().unwrap_or(&len); | ||
|
||
if self.is_char_boundary(start) && | ||
self.is_char_boundary(end) { return Some(&self[start .. end]); } |
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 needs the condition start <= end
too, to be correct (and never panic).
Also updated documentation of the method
Yeah, I looked over the PR's in more detail again and may habe been a bit hasty in coming to my conclusion when posting the previous comment. I mainly wanted create a link between both PR's (since they both moved |
Thanks for the PR @RockyTV! This definitely seems related to rust-lang/rfcs#1325 which @bluss mentioned in terms of an Otherwise, it looks like (as others have commented here), this API is subsumed by |
☔ The latest upstream changes (presumably #31120) made this pull request unmergeable. Please resolve the merge conflicts. |
Closing due to inactivity, but feel free to resubmit with a rebase! |
str.substr(start_index: usize)
returns a substring of str starting from the specified point(Hello.substr(1) = ello)
str.substr_until(start_index: usize, length: usize)
returns a substring of str starting from the specified point, with the specified length(Hello.substr_until(0, 2) = He)