Skip to content

Add str::{Split,RSplit,SplitN,RSplitN,SplitTerminator,RSplitTerminator,SplitInclusive}::as_str methods - #75265

Merged
bors merged 4 commits into
rust-lang:masterfrom
WaffleLapkin:str_split_as_str
Oct 16, 2020
Merged

bors merged 4 commits into
rust-lang:masterfrom
WaffleLapkin:str_split_as_str

Conversation

@WaffleLapkin

Copy link
Copy Markdown
Member

tl;dr this allows viewing unyelded part of str-split-iterators, like so:

let mut split = "Mary had a little lamb".split(' '); 
assert_eq!(split.as_str(), "Mary had a little lamb");
split.next();
assert_eq!(split.as_str(), "had a little lamb");
split.by_ref().for_each(drop);
assert_eq!(split.as_str(), "");

This PR adds semi-identical as_str methods to most str-split-iterators with signatures like &'_ Split<'a, P: Pattern<'a>> -> &'a str (Note: output &str lifetime is bound to the 'a, not the '_). The methods are similar to Chars::as_str

SplitInclusive::as_str is 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 into String to emulate as_str.

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @kennytm

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 7, 2020
@LeSeulArtichaut

Copy link
Copy Markdown
Contributor

Ping from triage: @kennytm do you want to review this? I'd expect someone from T-libs to be assigned here -- maybe cc @sfackler?

Comment thread library/core/src/str/mod.rs Outdated

ghost Sep 12, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause surprising result for those who want to calculate ptr offsets of this &str from the whole &str

ghost Sep 12, 2020

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a use case when you need to do those kinds of tricks?

ghost Sep 30, 2020

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean the fix is trivial, but for me, it's not clear if we should provide such guarantees.

ghost Jan 15, 2021 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

ghost Jan 15, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

ghost Jun 23, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be related to the semantics of raw pointer comparison.
rust-lang/unsafe-code-guidelines#239

@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 12, 2020
@Dylan-DPC-zz

ghost commented Sep 30, 2020

Copy link
Copy Markdown

r? @dtolnay

@rust-highfive rust-highfive assigned dtolnay and unassigned kennytm Sep 30, 2020
@bors

ghost commented Oct 1, 2020

Copy link
Copy Markdown
Contributor

☔ 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:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

Waffle added 2 commits October 1, 2020 22:53
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".
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).
@WaffleLapkin

ghost commented Oct 1, 2020

Copy link
Copy Markdown
Member Author

Hm. I've fixed the conflict, but is it normal that bors didn't change the labels (S-waiting-on-review->S-waiting-on-author)? It said to run a command after resolving conflicts, but labels are already as they should be after resolving conflicts.

@Dylan-DPC-zz

ghost commented Oct 2, 2020

Copy link
Copy Markdown

@WaffleLapkin not sure why, the change has been reported last week as well, will check it accordingly :)

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good!

Please make a tracking issue and update the stability attributes in the PR.

@WaffleLapkin

ghost commented Oct 15, 2020

Copy link
Copy Markdown
Member Author

@dtolnay done :)

@leonardo-m

ghost commented Oct 15, 2020

Copy link
Copy Markdown

What's an use case of this as_str method?

@Dylan-DPC-zz

ghost commented Oct 15, 2020

Copy link
Copy Markdown

@bors r=dtolnay

@bors

ghost commented Oct 15, 2020

Copy link
Copy Markdown
Contributor

📌 Commit 7bd6403 has been approved by dtolnay

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 15, 2020
@jyn514 jyn514 added the T-libs-api [DEPRECATED; DO NOT USE] label Oct 16, 2020
@bors
bors merged commit 977df43 into rust-lang:master Oct 16, 2020
@rustbot rustbot added this to the 1.49.0 milestone Oct 16, 2020
@WaffleLapkin
WaffleLapkin deleted the str_split_as_str branch October 16, 2020 08:45
@mikevoronov

ghost commented Oct 17, 2020

Copy link
Copy Markdown

@leonardo-m I needed this feature while implementing very simple argument parser.

Let's imagine that I want to parse the following command:
call <module_name> <func_name> [args in json]

So in call after <func_name> extracting, I want to get a remainder of string as &str to parse it like a json with serde. In particular, here I had to use join or collect that implies some overhead.

@crumblingstatue

ghost commented Oct 23, 2020

Copy link
Copy Markdown
Contributor

Would it be possible to also implement this for SplitWhitespace?

@WaffleLapkin

ghost commented Oct 27, 2020

Copy link
Copy Markdown
Member Author

@crumblingstatue this should be possible, but it's not trivial. SplitWhitespace uses Filter iterator adaptor, making it impossible to reach inner Split and get the remainder. The only solution I can think of - making Filter's iter field pub(crate).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api [DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.