summaryrefslogtreecommitdiffstats
path: root/vendor/clap_lex
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:29 +0000
commit631cd5845e8de329d0e227aaa707d7ea228b8f8f (patch)
treea1b87c8f8cad01cf18f7c5f57a08f102771ed303 /vendor/clap_lex
parentAdding debian version 1.69.0+dfsg1-1. (diff)
downloadrustc-631cd5845e8de329d0e227aaa707d7ea228b8f8f.tar.xz
rustc-631cd5845e8de329d0e227aaa707d7ea228b8f8f.zip
Merging upstream version 1.70.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/clap_lex')
-rw-r--r--vendor/clap_lex/.cargo-checksum.json2
-rw-r--r--vendor/clap_lex/Cargo.toml10
-rw-r--r--vendor/clap_lex/README.md8
-rw-r--r--vendor/clap_lex/src/ext.rs354
-rw-r--r--vendor/clap_lex/src/lib.rs128
5 files changed, 415 insertions, 87 deletions
diff --git a/vendor/clap_lex/.cargo-checksum.json b/vendor/clap_lex/.cargo-checksum.json
index a493abc0f..096f62c28 100644
--- a/vendor/clap_lex/.cargo-checksum.json
+++ b/vendor/clap_lex/.cargo-checksum.json
@@ -1 +1 @@
-{"files":{"Cargo.toml":"f2eca81e3cfadfd80956be964ce0d06c7cb1ad735679c38ed86836cd2780e26f","LICENSE-APACHE":"c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4","LICENSE-MIT":"0d687e1f07b58fe68bda74668ff6326125e5e5efa184cce755cd84ac535b7058","README.md":"d8a7c26d4ab47ac8ca387aada755d4dc375e1e85ed52b15575bb67ec118e7cb4","src/lib.rs":"2604cd6a948e0c56da4e8da3f28932476e3243e8c8a9a357dcccf6dddd55ce9c"},"package":"0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8"} \ No newline at end of file
+{"files":{"Cargo.toml":"c9eb0f99077d05483dfe7737502d90522d1d3271c98d1ac53f6e5ae7a5d4f582","LICENSE-APACHE":"c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4","LICENSE-MIT":"0d687e1f07b58fe68bda74668ff6326125e5e5efa184cce755cd84ac535b7058","README.md":"d1f4162770bfbd32c84dc9d914dceeb6e5050ab8be5037e7eb4539b8bbbb8e73","src/ext.rs":"7eae476b1f6919949e2c72331117a34e11eb3c21eebc050410870e7bbd06092c","src/lib.rs":"233a2661c7cfb61d95848e0fc2d37477912c29474a8db0c858ac71bc6af86d57"},"package":"8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1"} \ No newline at end of file
diff --git a/vendor/clap_lex/Cargo.toml b/vendor/clap_lex/Cargo.toml
index d6859256b..5fa2b9292 100644
--- a/vendor/clap_lex/Cargo.toml
+++ b/vendor/clap_lex/Cargo.toml
@@ -11,9 +11,9 @@
[package]
edition = "2021"
-rust-version = "1.60.0"
+rust-version = "1.64.0"
name = "clap_lex"
-version = "0.3.0"
+version = "0.4.1"
include = [
"build.rs",
"src/**/*",
@@ -35,7 +35,6 @@ keywords = [
categories = ["command-line-interface"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/clap-rs/clap/tree/master/clap_lex"
-resolver = "2"
[[package.metadata.release.pre-release-replacements]]
file = "CHANGELOG.md"
@@ -81,8 +80,3 @@ prerelease = true
[lib]
bench = false
-
-[dependencies.os_str_bytes]
-version = "6.0"
-features = ["raw_os_str"]
-default-features = false
diff --git a/vendor/clap_lex/README.md b/vendor/clap_lex/README.md
index eebd4c571..18d856205 100644
--- a/vendor/clap_lex/README.md
+++ b/vendor/clap_lex/README.md
@@ -5,15 +5,15 @@
[![Crates.io](https://img.shields.io/crates/v/clap_lex?style=flat-square)](https://crates.io/crates/clap_lex)
[![Crates.io](https://img.shields.io/crates/d/clap_lex?style=flat-square)](https://crates.io/crates/clap_lex)
-[![License](https://img.shields.io/badge/license-Apache%202.0-blue?style=flat-square)](https://github.com/clap-rs/clap/blob/clap_lex-v0.3.0/LICENSE-APACHE)
-[![License](https://img.shields.io/badge/license-MIT-blue?style=flat-square)](https://github.com/clap-rs/clap/blob/clap_lex-v0.3.0/LICENSE-MIT)
+[![License](https://img.shields.io/badge/license-Apache%202.0-blue?style=flat-square)](https://github.com/clap-rs/clap/blob/clap_lex-v0.4.1/LICENSE-APACHE)
+[![License](https://img.shields.io/badge/license-MIT-blue?style=flat-square)](https://github.com/clap-rs/clap/blob/clap_lex-v0.4.1/LICENSE-MIT)
Dual-licensed under [Apache 2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT).
1. [About](#about)
2. [API Reference](https://docs.rs/clap_lex)
3. [Questions & Discussions](https://github.com/clap-rs/clap/discussions)
-4. [CONTRIBUTING](https://github.com/clap-rs/clap/blob/clap_lex-v0.3.0/clap_lex/CONTRIBUTING.md)
-5. [Sponsors](https://github.com/clap-rs/clap/blob/clap_lex-v0.3.0/README.md#sponsors)
+4. [CONTRIBUTING](https://github.com/clap-rs/clap/blob/clap_lex-v0.4.1/clap_lex/CONTRIBUTING.md)
+5. [Sponsors](https://github.com/clap-rs/clap/blob/clap_lex-v0.4.1/README.md#sponsors)
## About
diff --git a/vendor/clap_lex/src/ext.rs b/vendor/clap_lex/src/ext.rs
new file mode 100644
index 000000000..0e6b33ebb
--- /dev/null
+++ b/vendor/clap_lex/src/ext.rs
@@ -0,0 +1,354 @@
+use std::ffi::OsStr;
+
+pub trait OsStrExt: private::Sealed {
+ /// Converts to a string slice.
+ fn try_str(&self) -> Result<&str, std::str::Utf8Error>;
+ /// Returns `true` if the given pattern matches a sub-slice of
+ /// this string slice.
+ ///
+ /// Returns `false` if it does not.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use clap_lex::OsStrExt as _;
+ /// let bananas = std::ffi::OsStr::new("bananas");
+ ///
+ /// assert!(bananas.contains("nana"));
+ /// assert!(!bananas.contains("apples"));
+ /// ```
+ fn contains(&self, needle: &str) -> bool;
+ /// Returns the byte index of the first character of this string slice that
+ /// matches the pattern.
+ ///
+ /// Returns [`None`] if the pattern doesn't match.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use clap_lex::OsStrExt as _;
+ /// let s = std::ffi::OsStr::new("Löwe 老虎 Léopard Gepardi");
+ ///
+ /// assert_eq!(s.find("L"), Some(0));
+ /// assert_eq!(s.find("é"), Some(14));
+ /// assert_eq!(s.find("par"), Some(17));
+ /// ```
+ ///
+ /// Not finding the pattern:
+ ///
+ /// ```rust
+ /// use clap_lex::OsStrExt as _;
+ /// let s = std::ffi::OsStr::new("Löwe 老虎 Léopard");
+ ///
+ /// assert_eq!(s.find("1"), None);
+ /// ```
+ fn find(&self, needle: &str) -> Option<usize>;
+ /// Returns a string slice with the prefix removed.
+ ///
+ /// If the string starts with the pattern `prefix`, returns substring after the prefix, wrapped
+ /// in `Some`.
+ ///
+ /// If the string does not start with `prefix`, returns `None`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ffi::OsStr;
+ /// use clap_lex::OsStrExt as _;
+ /// assert_eq!(OsStr::new("foo:bar").strip_prefix("foo:"), Some(OsStr::new("bar")));
+ /// assert_eq!(OsStr::new("foo:bar").strip_prefix("bar"), None);
+ /// assert_eq!(OsStr::new("foofoo").strip_prefix("foo"), Some(OsStr::new("foo")));
+ /// ```
+ fn strip_prefix(&self, prefix: &str) -> Option<&OsStr>;
+ /// Returns `true` if the given pattern matches a prefix of this
+ /// string slice.
+ ///
+ /// Returns `false` if it does not.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use clap_lex::OsStrExt as _;
+ /// let bananas = std::ffi::OsStr::new("bananas");
+ ///
+ /// assert!(bananas.starts_with("bana"));
+ /// assert!(!bananas.starts_with("nana"));
+ /// ```
+ fn starts_with(&self, prefix: &str) -> bool;
+ /// An iterator over substrings of this string slice, separated by
+ /// characters matched by a pattern.
+ ///
+ /// # Examples
+ ///
+ /// Simple patterns:
+ ///
+ /// ```
+ /// use std::ffi::OsStr;
+ /// use clap_lex::OsStrExt as _;
+ /// let v: Vec<_> = OsStr::new("Mary had a little lamb").split(" ").collect();
+ /// assert_eq!(v, [OsStr::new("Mary"), OsStr::new("had"), OsStr::new("a"), OsStr::new("little"), OsStr::new("lamb")]);
+ ///
+ /// let v: Vec<_> = OsStr::new("").split("X").collect();
+ /// assert_eq!(v, [OsStr::new("")]);
+ ///
+ /// let v: Vec<_> = OsStr::new("lionXXtigerXleopard").split("X").collect();
+ /// assert_eq!(v, [OsStr::new("lion"), OsStr::new(""), OsStr::new("tiger"), OsStr::new("leopard")]);
+ ///
+ /// let v: Vec<_> = OsStr::new("lion::tiger::leopard").split("::").collect();
+ /// assert_eq!(v, [OsStr::new("lion"), OsStr::new("tiger"), OsStr::new("leopard")]);
+ /// ```
+ ///
+ /// If a string contains multiple contiguous separators, you will end up
+ /// with empty strings in the output:
+ ///
+ /// ```
+ /// use std::ffi::OsStr;
+ /// use clap_lex::OsStrExt as _;
+ /// let x = OsStr::new("||||a||b|c");
+ /// let d: Vec<_> = x.split("|").collect();
+ ///
+ /// assert_eq!(d, &[OsStr::new(""), OsStr::new(""), OsStr::new(""), OsStr::new(""), OsStr::new("a"), OsStr::new(""), OsStr::new("b"), OsStr::new("c")]);
+ /// ```
+ ///
+ /// Contiguous separators are separated by the empty string.
+ ///
+ /// ```
+ /// use std::ffi::OsStr;
+ /// use clap_lex::OsStrExt as _;
+ /// let x = OsStr::new("(///)");
+ /// let d: Vec<_> = x.split("/").collect();
+ ///
+ /// assert_eq!(d, &[OsStr::new("("), OsStr::new(""), OsStr::new(""), OsStr::new(")")]);
+ /// ```
+ ///
+ /// Separators at the start or end of a string are neighbored
+ /// by empty strings.
+ ///
+ /// ```
+ /// use std::ffi::OsStr;
+ /// use clap_lex::OsStrExt as _;
+ /// let d: Vec<_> = OsStr::new("010").split("0").collect();
+ /// assert_eq!(d, &[OsStr::new(""), OsStr::new("1"), OsStr::new("")]);
+ /// ```
+ ///
+ /// When the empty string is used as a separator, it panics
+ ///
+ /// ```should_panic
+ /// use std::ffi::OsStr;
+ /// use clap_lex::OsStrExt as _;
+ /// let f: Vec<_> = OsStr::new("rust").split("").collect();
+ /// assert_eq!(f, &[OsStr::new(""), OsStr::new("r"), OsStr::new("u"), OsStr::new("s"), OsStr::new("t"), OsStr::new("")]);
+ /// ```
+ ///
+ /// Contiguous separators can lead to possibly surprising behavior
+ /// when whitespace is used as the separator. This code is correct:
+ ///
+ /// ```
+ /// use std::ffi::OsStr;
+ /// use clap_lex::OsStrExt as _;
+ /// let x = OsStr::new(" a b c");
+ /// let d: Vec<_> = x.split(" ").collect();
+ ///
+ /// assert_eq!(d, &[OsStr::new(""), OsStr::new(""), OsStr::new(""), OsStr::new(""), OsStr::new("a"), OsStr::new(""), OsStr::new("b"), OsStr::new("c")]);
+ /// ```
+ ///
+ /// It does _not_ give you:
+ ///
+ /// ```,ignore
+ /// assert_eq!(d, &[OsStr::new("a"), OsStr::new("b"), OsStr::new("c")]);
+ /// ```
+ ///
+ /// Use [`split_whitespace`] for this behavior.
+ ///
+ /// [`split_whitespace`]: str::split_whitespace
+ fn split<'s, 'n>(&'s self, needle: &'n str) -> Split<'s, 'n>;
+ /// Divide one string slice into two at an index.
+ ///
+ /// The argument, `mid`, should be a byte offset from the start of the
+ /// string. It must also be on the boundary of a UTF-8 code point.
+ ///
+ /// The two slices returned go from the start of the string slice to `mid`,
+ /// and from `mid` to the end of the string slice.
+ ///
+ /// To get mutable string slices instead, see the [`split_at_mut`]
+ /// method.
+ ///
+ /// [`split_at_mut`]: str::split_at_mut
+ ///
+ /// # Panics
+ ///
+ /// Panics if `mid` is not on a UTF-8 code point boundary, or if it is
+ /// past the end of the last code point of the string slice.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// use clap_lex::OsStrExt as _;
+ /// let s = std::ffi::OsStr::new("Per Martin-Löf");
+ ///
+ /// let (first, last) = s.split_at(3);
+ ///
+ /// assert_eq!("Per", first);
+ /// assert_eq!(" Martin-Löf", last);
+ /// ```
+ #[deprecated(since = "4.1.0", note = "This is not sound for all `index`")]
+ fn split_at(&self, index: usize) -> (&OsStr, &OsStr);
+ /// Splits the string on the first occurrence of the specified delimiter and
+ /// returns prefix before delimiter and suffix after delimiter.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use std::ffi::OsStr;
+ /// use clap_lex::OsStrExt as _;
+ /// assert_eq!(OsStr::new("cfg").split_once("="), None);
+ /// assert_eq!(OsStr::new("cfg=").split_once("="), Some((OsStr::new("cfg"), OsStr::new(""))));
+ /// assert_eq!(OsStr::new("cfg=foo").split_once("="), Some((OsStr::new("cfg"), OsStr::new("foo"))));
+ /// assert_eq!(OsStr::new("cfg=foo=bar").split_once("="), Some((OsStr::new("cfg"), OsStr::new("foo=bar"))));
+ /// ```
+ fn split_once(&self, needle: &'_ str) -> Option<(&OsStr, &OsStr)>;
+}
+
+impl OsStrExt for OsStr {
+ fn try_str(&self) -> Result<&str, std::str::Utf8Error> {
+ let bytes = to_bytes(self);
+ std::str::from_utf8(bytes)
+ }
+
+ fn contains(&self, needle: &str) -> bool {
+ self.find(needle).is_some()
+ }
+
+ fn find(&self, needle: &str) -> Option<usize> {
+ let bytes = to_bytes(self);
+ (0..=self.len().checked_sub(needle.len())?)
+ .find(|&x| bytes[x..].starts_with(needle.as_bytes()))
+ }
+
+ fn strip_prefix(&self, prefix: &str) -> Option<&OsStr> {
+ let bytes = to_bytes(self);
+ bytes.strip_prefix(prefix.as_bytes()).map(|s| {
+ // SAFETY:
+ // - This came from `to_bytes`
+ // - Since `prefix` is `&str`, any split will be along UTF-8 boundarie
+ unsafe { to_os_str_unchecked(s) }
+ })
+ }
+ fn starts_with(&self, prefix: &str) -> bool {
+ let bytes = to_bytes(self);
+ bytes.starts_with(prefix.as_bytes())
+ }
+
+ fn split<'s, 'n>(&'s self, needle: &'n str) -> Split<'s, 'n> {
+ assert_ne!(needle, "");
+ Split {
+ haystack: Some(self),
+ needle,
+ }
+ }
+
+ fn split_at(&self, index: usize) -> (&OsStr, &OsStr) {
+ let bytes = to_bytes(self);
+ unsafe {
+ // BUG: This is unsafe and has been deprecated
+ let (first, second) = bytes.split_at(index);
+ (to_os_str_unchecked(first), to_os_str_unchecked(second))
+ }
+ }
+
+ fn split_once(&self, needle: &'_ str) -> Option<(&OsStr, &OsStr)> {
+ let start = self.find(needle)?;
+ let end = start + needle.len();
+ let haystack = to_bytes(self);
+ let first = &haystack[0..start];
+ let second = &haystack[end..];
+ // SAFETY:
+ // - This came from `to_bytes`
+ // - Since `needle` is `&str`, any split will be along UTF-8 boundarie
+ unsafe { Some((to_os_str_unchecked(first), to_os_str_unchecked(second))) }
+ }
+}
+
+mod private {
+ pub trait Sealed {}
+
+ impl Sealed for std::ffi::OsStr {}
+}
+
+/// Allow access to raw bytes
+///
+/// As the non-UTF8 encoding is not defined, the bytes only make sense when compared with
+/// 7-bit ASCII or `&str`
+///
+/// # Compatibility
+///
+/// There is no guarantee how non-UTF8 bytes will be encoded, even within versions of this crate
+/// (since its dependent on rustc)
+fn to_bytes(s: &OsStr) -> &[u8] {
+ // SAFETY:
+ // - Lifetimes are the same
+ // - Types are compatible (`OsStr` is effectively a transparent wrapper for `[u8]`)
+ // - The primary contract is that the encoding for invalid surrogate code points is not
+ // guaranteed which isn't a problem here
+ //
+ // There is a proposal to support this natively (https://github.com/rust-lang/rust/pull/95290)
+ // but its in limbo
+ unsafe { std::mem::transmute(s) }
+}
+
+/// Restore raw bytes as `OsStr`
+///
+/// # Safety
+///
+/// - `&[u8]` must either by a `&str` or originated with `to_bytes` within the same binary
+/// - Any splits of the original `&[u8]` must be done along UTF-8 boundaries
+unsafe fn to_os_str_unchecked(s: &[u8]) -> &OsStr {
+ // SAFETY:
+ // - Lifetimes are the same
+ // - Types are compatible (`OsStr` is effectively a transparent wrapper for `[u8]`)
+ // - The primary contract is that the encoding for invalid surrogate code points is not
+ // guaranteed which isn't a problem here
+ //
+ // There is a proposal to support this natively (https://github.com/rust-lang/rust/pull/95290)
+ // but its in limbo
+ std::mem::transmute(s)
+}
+
+pub struct Split<'s, 'n> {
+ haystack: Option<&'s OsStr>,
+ needle: &'n str,
+}
+
+impl<'s, 'n> Iterator for Split<'s, 'n> {
+ type Item = &'s OsStr;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let haystack = self.haystack?;
+ match haystack.split_once(self.needle) {
+ Some((first, second)) => {
+ if !haystack.is_empty() {
+ debug_assert_ne!(haystack, second);
+ }
+ self.haystack = Some(second);
+ Some(first)
+ }
+ None => {
+ self.haystack = None;
+ Some(haystack)
+ }
+ }
+ }
+}
+
+/// Split an `OsStr`
+///
+/// # Safety
+///
+/// `index` must be at a valid UTF-8 boundary
+pub(crate) unsafe fn split_at(os: &OsStr, index: usize) -> (&OsStr, &OsStr) {
+ let bytes = to_bytes(os);
+ let (first, second) = bytes.split_at(index);
+ (to_os_str_unchecked(first), to_os_str_unchecked(second))
+}
diff --git a/vendor/clap_lex/src/lib.rs b/vendor/clap_lex/src/lib.rs
index bc4d4ba65..b349fba51 100644
--- a/vendor/clap_lex/src/lib.rs
+++ b/vendor/clap_lex/src/lib.rs
@@ -7,8 +7,11 @@
//! # Examples
//!
//! ```rust
-//! # use std::path::PathBuf;
-//! # type BoxedError = Box<dyn std::error::Error + Send + Sync>;
+//! use std::path::PathBuf;
+//! use std::ffi::OsStr;
+//!
+//! type BoxedError = Box<dyn std::error::Error + Send + Sync>;
+//!
//! #[derive(Debug)]
//! struct Args {
//! paths: Vec<PathBuf>,
@@ -24,7 +27,7 @@
//! }
//!
//! impl Color {
-//! fn parse(s: Option<&clap_lex::RawOsStr>) -> Result<Self, BoxedError> {
+//! fn parse(s: Option<&OsStr>) -> Result<Self, BoxedError> {
//! let s = s.map(|s| s.to_str().ok_or(s));
//! match s {
//! Some(Ok("always")) | Some(Ok("")) | None => {
@@ -91,12 +94,12 @@
//! return Err(format!("Unexpected flag: -{}", c).into());
//! }
//! Err(e) => {
-//! return Err(format!("Unexpected flag: -{}", e.to_str_lossy()).into());
+//! return Err(format!("Unexpected flag: -{}", e.to_string_lossy()).into());
//! }
//! }
//! }
//! } else {
-//! args.paths.push(PathBuf::from(arg.to_value_os().to_os_str().into_owned()));
+//! args.paths.push(PathBuf::from(arg.to_value_os().to_owned()));
//! }
//! }
//!
@@ -107,13 +110,14 @@
//! println!("{:?}", args);
//! ```
+mod ext;
+
use std::ffi::OsStr;
use std::ffi::OsString;
pub use std::io::SeekFrom;
-pub use os_str_bytes::RawOsStr;
-pub use os_str_bytes::RawOsString;
+pub use ext::OsStrExt;
/// Command-line arguments
#[derive(Default, Clone, Debug, PartialEq, Eq)]
@@ -273,30 +277,27 @@ impl ArgCursor {
/// Command-line Argument
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ParsedArg<'s> {
- inner: std::borrow::Cow<'s, RawOsStr>,
- utf8: Option<&'s str>,
+ inner: &'s OsStr,
}
impl<'s> ParsedArg<'s> {
fn new(inner: &'s OsStr) -> Self {
- let utf8 = inner.to_str();
- let inner = RawOsStr::new(inner);
- Self { inner, utf8 }
+ Self { inner }
}
/// Argument is length of 0
pub fn is_empty(&self) -> bool {
- self.inner.as_ref().is_empty()
+ self.inner.is_empty()
}
/// Does the argument look like a stdio argument (`-`)
pub fn is_stdio(&self) -> bool {
- self.inner.as_ref() == "-"
+ self.inner == "-"
}
/// Does the argument look like an argument escape (`--`)
pub fn is_escape(&self) -> bool {
- self.inner.as_ref() == "--"
+ self.inner == "--"
}
/// Does the argument look like a number
@@ -307,56 +308,38 @@ impl<'s> ParsedArg<'s> {
}
/// Treat as a long-flag
- pub fn to_long(&self) -> Option<(Result<&str, &RawOsStr>, Option<&RawOsStr>)> {
- if let Some(raw) = self.utf8 {
- let remainder = raw.strip_prefix("--")?;
- if remainder.is_empty() {
- debug_assert!(self.is_escape());
- return None;
- }
+ pub fn to_long(&self) -> Option<(Result<&str, &OsStr>, Option<&OsStr>)> {
+ let raw = self.inner;
+ let remainder = raw.strip_prefix("--")?;
+ if remainder.is_empty() {
+ debug_assert!(self.is_escape());
+ return None;
+ }
- let (flag, value) = if let Some((p0, p1)) = remainder.split_once('=') {
- (p0, Some(p1))
- } else {
- (remainder, None)
- };
- let flag = Ok(flag);
- let value = value.map(RawOsStr::from_str);
- Some((flag, value))
+ let (flag, value) = if let Some((p0, p1)) = remainder.split_once("=") {
+ (p0, Some(p1))
} else {
- let raw = self.inner.as_ref();
- let remainder = raw.strip_prefix("--")?;
- if remainder.is_empty() {
- debug_assert!(self.is_escape());
- return None;
- }
-
- let (flag, value) = if let Some((p0, p1)) = remainder.split_once('=') {
- (p0, Some(p1))
- } else {
- (remainder, None)
- };
- let flag = flag.to_str().ok_or(flag);
- Some((flag, value))
- }
+ (remainder, None)
+ };
+ let flag = flag.to_str().ok_or(flag);
+ Some((flag, value))
}
/// Can treat as a long-flag
pub fn is_long(&self) -> bool {
- self.inner.as_ref().starts_with("--") && !self.is_escape()
+ self.inner.starts_with("--") && !self.is_escape()
}
/// Treat as a short-flag
pub fn to_short(&self) -> Option<ShortFlags<'_>> {
- if let Some(remainder_os) = self.inner.as_ref().strip_prefix('-') {
- if remainder_os.starts_with('-') {
+ if let Some(remainder_os) = self.inner.strip_prefix("-") {
+ if remainder_os.starts_with("-") {
None
} else if remainder_os.is_empty() {
debug_assert!(self.is_stdio());
None
} else {
- let remainder = self.utf8.map(|s| &s[1..]);
- Some(ShortFlags::new(remainder_os, remainder))
+ Some(ShortFlags::new(remainder_os))
}
} else {
None
@@ -365,48 +348,42 @@ impl<'s> ParsedArg<'s> {
/// Can treat as a short-flag
pub fn is_short(&self) -> bool {
- self.inner.as_ref().starts_with('-')
- && !self.is_stdio()
- && !self.inner.as_ref().starts_with("--")
+ self.inner.starts_with("-") && !self.is_stdio() && !self.inner.starts_with("--")
}
/// Treat as a value
///
/// **NOTE:** May return a flag or an escape.
- pub fn to_value_os(&self) -> &RawOsStr {
- self.inner.as_ref()
+ pub fn to_value_os(&self) -> &OsStr {
+ self.inner
}
/// Treat as a value
///
/// **NOTE:** May return a flag or an escape.
- pub fn to_value(&self) -> Result<&str, &RawOsStr> {
- self.utf8.ok_or_else(|| self.inner.as_ref())
+ pub fn to_value(&self) -> Result<&str, &OsStr> {
+ self.inner.to_str().ok_or(self.inner)
}
/// Safely print an argument that may contain non-UTF8 content
///
/// This may perform lossy conversion, depending on the platform. If you would like an implementation which escapes the path please use Debug instead.
pub fn display(&self) -> impl std::fmt::Display + '_ {
- self.inner.to_str_lossy()
+ self.inner.to_string_lossy()
}
}
/// Walk through short flags within a [`ParsedArg`]
#[derive(Clone, Debug)]
pub struct ShortFlags<'s> {
- inner: &'s RawOsStr,
+ inner: &'s OsStr,
utf8_prefix: std::str::CharIndices<'s>,
- invalid_suffix: Option<&'s RawOsStr>,
+ invalid_suffix: Option<&'s OsStr>,
}
impl<'s> ShortFlags<'s> {
- fn new(inner: &'s RawOsStr, utf8: Option<&'s str>) -> Self {
- let (utf8_prefix, invalid_suffix) = if let Some(utf8) = utf8 {
- (utf8, None)
- } else {
- split_nonutf8_once(inner)
- };
+ fn new(inner: &'s OsStr) -> Self {
+ let (utf8_prefix, invalid_suffix) = split_nonutf8_once(inner);
let utf8_prefix = utf8_prefix.char_indices();
Self {
inner,
@@ -438,7 +415,7 @@ impl<'s> ShortFlags<'s> {
/// Advance the iterator, returning the next short flag on success
///
/// On error, returns the invalid-UTF8 value
- pub fn next_flag(&mut self) -> Option<Result<char, &'s RawOsStr>> {
+ pub fn next_flag(&mut self) -> Option<Result<char, &'s OsStr>> {
if let Some((_, flag)) = self.utf8_prefix.next() {
return Some(Ok(flag));
}
@@ -452,11 +429,13 @@ impl<'s> ShortFlags<'s> {
}
/// Advance the iterator, returning everything left as a value
- pub fn next_value_os(&mut self) -> Option<&'s RawOsStr> {
+ pub fn next_value_os(&mut self) -> Option<&'s OsStr> {
if let Some((index, _)) = self.utf8_prefix.next() {
self.utf8_prefix = "".char_indices();
self.invalid_suffix = None;
- return Some(&self.inner[index..]);
+ // SAFETY: `char_indices` ensures `index` is at a valid UTF-8 boundary
+ let remainder = unsafe { ext::split_at(self.inner, index).1 };
+ return Some(remainder);
}
if let Some(suffix) = self.invalid_suffix {
@@ -469,19 +448,20 @@ impl<'s> ShortFlags<'s> {
}
impl<'s> Iterator for ShortFlags<'s> {
- type Item = Result<char, &'s RawOsStr>;
+ type Item = Result<char, &'s OsStr>;
fn next(&mut self) -> Option<Self::Item> {
self.next_flag()
}
}
-fn split_nonutf8_once(b: &RawOsStr) -> (&str, Option<&RawOsStr>) {
- match std::str::from_utf8(b.as_raw_bytes()) {
+fn split_nonutf8_once(b: &OsStr) -> (&str, Option<&OsStr>) {
+ match b.try_str() {
Ok(s) => (s, None),
Err(err) => {
- let (valid, after_valid) = b.split_at(err.valid_up_to());
- let valid = std::str::from_utf8(valid.as_raw_bytes()).unwrap();
+ // SAFETY: `char_indices` ensures `index` is at a valid UTF-8 boundary
+ let (valid, after_valid) = unsafe { ext::split_at(b, err.valid_up_to()) };
+ let valid = valid.try_str().unwrap();
(valid, Some(after_valid))
}
}