Add OsStr
methods for testing, stripping, and splitting Unicode prefixes.
#111059
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.