-
Notifications
You must be signed in to change notification settings - Fork 13k
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 OsStr
methods for testing, stripping, and splitting Unicode prefixes.
#111059
Conversation
(rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
@rustbot label +T-libs-api -T-libs |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
c581eec
to
432f15d
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
* `OsStr::starts_with()` tests whether an `OsStr` has a prefix matching the given `Pattern`. * `OsStr::strip_prefix()` returns the `OsStr` after removing a prefix matching the given `Pattern`. * `OsStr::split_once()` splits an `OsStr` into a `(&str, &OsStr)` pair, where the delimiter matches a given `Pattern`. * `OsStr::starts_with_str()` and `OsStr::strip_prefix_str()` are specialized variants that are implemented more efficiently than the `Pattern` cases. In all cases, the prefix must be Unicode because the current `Pattern` trait is built around the `&str` type.
☔ The latest upstream changes (presumably #112624) made this pull request unmergeable. Please resolve the merge conflicts. |
ACP: rust-lang/libs-team#114
Discussion on that ACP seems to have quieted down without a clear consensus, so I'm going to throw this PR out for consideration. The general idea is that it's difficult to write prefix/suffix manipulations for
OsStr
that accept non-Unicode patterns[0], but the most pressing need (inspecting contents ofargs_os
) are all based around Unicode patterns, and a big chunk of those operate exclusively on prefixes.The first commit in this PR adds the
OsStr::to_str_split()
andOsString::into_string_split()
methods, which extract the longest prefix from anOsStr
/OsString
that is valid Unicode. This prefix can then be parsed like normal, in platform-independent logic.The second commit is based on discussion in the above ACP. I had considered this to be separate functionality, but there was some concern that extracting a Unicode prefix wasn't useful on its own, so I added some helper functions:
OsStr::starts_with()
tests whether anOsStr
has a prefix matching the givenPattern
.if arg.starts_with("--") {
OsStr::strip_prefix()
returns theOsStr
after removing a prefix matching the givenPattern
.if let Some(arg_value) = arg.strip_prefix("--some-flag=") {
OsStr::split_once()
splits anOsStr
into a(&str, &OsStr)
pair, where the delimiter matches a givenPattern
.let Some((flag_name, flag_value)) = arg.split_once("=") {
OsStr::starts_with_str()
andOsStr::strip_prefix_str()
are specialized variants that are implemented as&[u8]
comparisons, which I expect to be more efficient than thePattern
versions since they don't need to validate UTF-8.[0] See extensive prior discussions, for example rfcs/2295.