Repository navigation
Add str::{Split,RSplit,SplitN,RSplitN,SplitTerminator,RSplitTerminator,SplitInclusive}::as_str methods - #75265
Conversation
|
r? @kennytm (rust_highfive has picked a reviewer for you, use r? to override) |
77f0f05 to
6f119d5
Compare
There was a problem hiding this comment.
This will cause surprising result for those who want to calculate ptr offsets of this &str from the whole &str
There was a problem hiding this comment.
Is there a use case when you need to do those kinds of tricks?
There was a problem hiding this comment.
I mean the fix is trivial, but for me, it's not clear if we should provide such guarantees.
There was a problem hiding this comment.
I would like the return to be a slice of the provided input.
When parsing a http header, I find myself wanting to see how much is remaining in the buffer to determine if I have all the contents (specified by Content-Length).
Example
let http_response_buffer: &str;
let mut header_lines = http_response_buffer.split("\r\n");
let mut content_length = None;
/* read through header and set content_length, header ends on empty header_line */
/* ... */
let unread_buffer_size = unsafe {
header_lines.as_str().as_ptr().offset_from(response_buffer.as_ptr())
};
if content_length.unwrap() < unread_buffer_size {
panic!("http_response_buffer does not contain response body");
}
This would break if http_response_buffer was
Content-Length: 100
granted I am using unsafe but this pattern doesn't seem too unreasonable (I might be biased as it's what I want to do).
Edit
Just realized I could just do header_lines.as_str().len(). Late night for me, my justification isn't really holding water anymore.
There was a problem hiding this comment.
I was thinking about this today and thought that there should be a function (probably in a crate) like
fn substr_bounds(string: &str, sub: &str) -> Option<Range<usize>> { ... }
let string = "aabb";
let sub = &r[1..3];
assert_eq!(substr_bound(string, sub), Some(1..3));
assert_eq!(substr_bound(string, "ab"), None);It feels natural, but are there any cases when it's useful? Anyway, as said before, for these kinds of things returning an empty substr may be a good idea.
If there is code that may benefit from this, I could make a PR with a fix.
There was a problem hiding this comment.
It should be related to the semantics of raw pointer comparison.
rust-lang/unsafe-code-guidelines#239
commented
Sep 30, 2020
|
r? @dtolnay |
commented
Oct 1, 2020
|
☔ The latest upstream changes (presumably #76325) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels: |
This commit introduses 2 methods under "str_split_as_str" gate with common signature of `&Split<'a, _> -> &'a str'`. Both of them work like `Chars::as_str` - return unyield part of the inner string.
…r` methods This commit entroduce 4 methods smililar to `Split::as_str` all under the same gate "str_split_as_str".
6f119d5 to
076514c
Compare
This commit entroduces `core::str::SplitInclusive::as_str` method similar to `core::str::Split::as_str`, but under different gate - "str_split_inclusive_as_str" (this is done so because `SplitInclusive` is itself unstable).
commented
Oct 1, 2020
|
Hm. I've fixed the conflict, but is it normal that bors didn't change the labels ( |
commented
Oct 2, 2020
|
@WaffleLapkin not sure why, the change has been reported last week as well, will check it accordingly :) |
commented
Oct 15, 2020
|
@dtolnay done :) |
commented
Oct 15, 2020
|
What's an use case of this as_str method? |
commented
Oct 15, 2020
|
@bors r=dtolnay |
commented
Oct 15, 2020
|
📌 Commit 7bd6403 has been approved by |
commented
Oct 17, 2020
|
@leonardo-m I needed this feature while implementing very simple argument parser. Let's imagine that I want to parse the following command: So in |
commented
Oct 23, 2020
|
Would it be possible to also implement this for SplitWhitespace? |
commented
Oct 27, 2020
|
@crumblingstatue this should be possible, but it's not trivial. |
tl;dr this allows viewing unyelded part of str-split-iterators, like so:
This PR adds semi-identical
as_strmethods to most str-split-iterators with signatures like&'_ Split<'a, P: Pattern<'a>> -> &'a str(Note: output&strlifetime is bound to the'a, not the'_). The methods are similar toChars::as_strSplitInclusive::as_stris under"str_split_inclusive_as_str"feature gate, all other methods are under"str_split_as_str"feature gate.Before this PR you had to sum
lens of all yielded parts or collect intoStringto emulateas_str.