-
Couldn't load subscription status.
- Fork 24
Description
Proposal
Problem statement
str and [u8] share a bunch of *ascii* methods. There is (at least?) one exception: str::split_ascii_whitespace was stabilized in 1.34, but does not exist in [u8], which seems like an oversight. It (and split_whitespace) is similarly missing from ByteStr, and could be added there as well.
Motivating examples or use cases
This method is useful for ad-hoc parsers of the multitude of textual file formats made of fields separated by ASCII whitespace.
Solution sketch
Add to core::slice:
impl [u8] {
pub fn split_ascii_whitespace(&self) -> SplitAsciiWhitespace<'_> {
let inner = self
.split(core::str::IsAsciiWhitespace)
.filter(core::str::BytesIsNotEmpty)
SplitAsciiWhitespace { inner }
}
}
pub struct SplitAsciiWhitespace<'a> {
pub(super) inner:
Filter<SliceSplit<'a, u8, IsAsciiWhitespace>, BytesIsNotEmpty>,
}The existing str::split_ascii_whitespace already works on bytes internally. Change it to:
pub fn split_ascii_whitespace(&self) -> core::str::SplitAsciiWhitespace<'_> {
let inner = self.as_bytes()
.split_ascii_whitespace()
.inner
.map(UnsafeBytesToStr)
SplitAsciiWhitespace { inner }
}Alternatives
It's not difficult to work around with split(is_ascii_whitespace), but there's the gotcha that you have to remember to filter out empty subslices to get the same semantics.
Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.