use std::borrow::Borrow;
use std::borrow::Cow;
use std::borrow::ToOwned;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::mem;
use std::ops::Deref;
use std::ops::Index;
use std::ops::Range;
use std::ops::RangeFrom;
use std::ops::RangeFull;
use std::ops::RangeInclusive;
use std::ops::RangeTo;
use std::ops::RangeToInclusive;
use std::result;
use std::str;
#[cfg(feature = "memchr")]
use memchr::memmem::find;
#[cfg(feature = "memchr")]
use memchr::memmem::rfind;
use super::imp;
use super::imp::raw;
use super::iter::Split;
use super::pattern::Encoded as EncodedPattern;
use super::private;
use super::Pattern;
if_checked_conversions! {
use super::EncodingError;
use super::Result;
}
#[cfg(not(feature = "memchr"))]
fn find(string: &[u8], pat: &[u8]) -> Option<usize> {
(0..=string.len().checked_sub(pat.len())?)
.find(|&x| string[x..].starts_with(pat))
}
#[cfg(not(feature = "memchr"))]
fn rfind(string: &[u8], pat: &[u8]) -> Option<usize> {
(pat.len()..=string.len())
.rfind(|&x| string[..x].ends_with(pat))
.map(|x| x - pat.len())
}
#[allow(clippy::missing_safety_doc)]
unsafe trait TransmuteBox {
fn transmute_box<R>(self: Box<Self>) -> Box<R>
where
R: ?Sized + TransmuteBox,
{
let value = Box::into_raw(self);
unsafe { Box::from_raw(mem::transmute_copy(&value)) }
}
}
unsafe impl TransmuteBox for RawOsStr {}
unsafe impl TransmuteBox for [u8] {}
#[derive(Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(os_str_bytes_docs_rs, doc(cfg(feature = "raw_os_str")))]
#[repr(transparent)]
pub struct RawOsStr([u8]);
impl RawOsStr {
const fn from_inner(string: &[u8]) -> &Self {
unsafe { mem::transmute(string) }
}
#[inline]
#[must_use]
pub fn new(string: &OsStr) -> Cow<'_, Self> {
match imp::os_str_to_bytes(string) {
Cow::Borrowed(string) => Cow::Borrowed(Self::from_inner(string)),
Cow::Owned(string) => Cow::Owned(RawOsString(string)),
}
}
#[allow(clippy::should_implement_trait)]
#[inline]
#[must_use]
pub fn from_str(string: &str) -> &Self {
Self::from_inner(string.as_bytes())
}
#[inline]
#[must_use = "method should not be used for validation"]
#[track_caller]
pub fn assert_from_raw_bytes(string: &[u8]) -> &Self {
expect_encoded!(raw::validate_bytes(string));
Self::from_inner(string)
}
if_checked_conversions! {
#[cfg_attr(
os_str_bytes_docs_rs,
doc(cfg(feature = "checked_conversions"))
)]
#[inline]
pub fn from_raw_bytes(string: &[u8]) -> Result<&Self> {
raw::validate_bytes(string)
.map(|()| Self::from_inner(string))
.map_err(EncodingError)
}
}
#[inline]
#[must_use]
#[track_caller]
pub unsafe fn from_raw_bytes_unchecked(string: &[u8]) -> &Self {
if cfg!(debug_assertions) {
expect_encoded!(raw::validate_bytes(string));
}
Self::from_inner(string)
}
#[inline]
#[must_use]
pub fn as_raw_bytes(&self) -> &[u8] {
&self.0
}
#[inline]
#[must_use]
pub fn contains<P>(&self, pat: P) -> bool
where
P: Pattern,
{
self.find(pat).is_some()
}
#[inline]
#[must_use]
pub fn ends_with<P>(&self, pat: P) -> bool
where
P: Pattern,
{
let pat = pat.__encode();
let pat = pat.__get();
self.0.ends_with(pat)
}
#[inline]
#[must_use]
pub fn ends_with_os(&self, pat: &Self) -> bool {
raw::ends_with(&self.0, &pat.0)
}
#[inline]
#[must_use]
pub fn find<P>(&self, pat: P) -> Option<usize>
where
P: Pattern,
{
let pat = pat.__encode();
let pat = pat.__get();
find(&self.0, pat)
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
#[must_use]
pub fn raw_len(&self) -> usize {
self.0.len()
}
#[inline]
#[must_use]
pub fn rfind<P>(&self, pat: P) -> Option<usize>
where
P: Pattern,
{
let pat = pat.__encode();
let pat = pat.__get();
rfind(&self.0, pat)
}
fn split_once_raw_with<P, F>(
&self,
pat: &P,
find_fn: F,
) -> Option<(&Self, &Self)>
where
F: FnOnce(&[u8], &[u8]) -> Option<usize>,
P: EncodedPattern,
{
let pat = pat.__get();
let index = find_fn(&self.0, pat)?;
let prefix = &self.0[..index];
let suffix = &self.0[index + pat.len()..];
Some((Self::from_inner(prefix), Self::from_inner(suffix)))
}
pub(super) fn rsplit_once_raw<P>(&self, pat: &P) -> Option<(&Self, &Self)>
where
P: EncodedPattern,
{
self.split_once_raw_with(pat, rfind)
}
#[inline]
#[must_use]
pub fn rsplit_once<P>(&self, pat: P) -> Option<(&Self, &Self)>
where
P: Pattern,
{
self.rsplit_once_raw(&pat.__encode())
}
#[cold]
#[inline(never)]
#[track_caller]
fn index_boundary_error(&self, index: usize) -> ! {
debug_assert!(raw::is_continuation(self.0[index]));
let start = expect_encoded!(self.0[..index]
.iter()
.rposition(|&x| !raw::is_continuation(x)));
let mut end = index + 1;
end += self.0[end..]
.iter()
.take_while(|&&x| raw::is_continuation(x))
.count();
let code_point = raw::decode_code_point(&self.0[start..end]);
panic!(
"byte index {} is not a valid boundary; it is inside U+{:04X} \
(bytes {}..{})",
index, code_point, start, end,
);
}
#[track_caller]
fn check_bound(&self, index: usize) {
if let Some(&byte) = self.0.get(index) {
if raw::is_continuation(byte) {
self.index_boundary_error(index);
}
}
}
#[inline]
#[must_use]
#[track_caller]
pub fn split<P>(&self, pat: P) -> Split<'_, P>
where
P: Pattern,
{
Split::new(self, pat)
}
#[inline]
#[must_use]
#[track_caller]
pub fn split_at(&self, mid: usize) -> (&Self, &Self) {
self.check_bound(mid);
let (prefix, suffix) = self.0.split_at(mid);
(Self::from_inner(prefix), Self::from_inner(suffix))
}
pub(super) fn split_once_raw<P>(&self, pat: &P) -> Option<(&Self, &Self)>
where
P: EncodedPattern,
{
self.split_once_raw_with(pat, find)
}
#[inline]
#[must_use]
pub fn split_once<P>(&self, pat: P) -> Option<(&Self, &Self)>
where
P: Pattern,
{
self.split_once_raw(&pat.__encode())
}
#[inline]
#[must_use]
pub fn starts_with<P>(&self, pat: P) -> bool
where
P: Pattern,
{
let pat = pat.__encode();
let pat = pat.__get();
self.0.starts_with(pat)
}
#[inline]
#[must_use]
pub fn starts_with_os(&self, pat: &Self) -> bool {
raw::starts_with(&self.0, &pat.0)
}
#[inline]
#[must_use]
pub fn strip_prefix<P>(&self, pat: P) -> Option<&Self>
where
P: Pattern,
{
let pat = pat.__encode();
let pat = pat.__get();
self.0.strip_prefix(pat).map(Self::from_inner)
}
#[inline]
#[must_use]
pub fn strip_suffix<P>(&self, pat: P) -> Option<&Self>
where
P: Pattern,
{
let pat = pat.__encode();
let pat = pat.__get();
self.0.strip_suffix(pat).map(Self::from_inner)
}
#[inline]
#[must_use]
pub fn to_os_str(&self) -> Cow<'_, OsStr> {
expect_encoded!(imp::os_str_from_bytes(&self.0))
}
#[inline]
#[must_use]
pub fn to_str(&self) -> Option<&str> {
str::from_utf8(&self.0).ok()
}
#[inline]
#[must_use]
pub fn to_str_lossy(&self) -> Cow<'_, str> {
String::from_utf8_lossy(&self.0)
}
fn trim_matches_raw_with<P, F>(&self, pat: &P, strip_fn: F) -> &Self
where
F: for<'a> Fn(&'a [u8], &[u8]) -> Option<&'a [u8]>,
P: EncodedPattern,
{
let pat = pat.__get();
if pat.is_empty() {
return self;
}
let mut string = &self.0;
while let Some(substring) = strip_fn(string, pat) {
string = substring;
}
Self::from_inner(string)
}
fn trim_end_matches_raw<P>(&self, pat: &P) -> &Self
where
P: EncodedPattern,
{
self.trim_matches_raw_with(pat, <[_]>::strip_suffix)
}
#[inline]
#[must_use]
pub fn trim_end_matches<P>(&self, pat: P) -> &Self
where
P: Pattern,
{
self.trim_end_matches_raw(&pat.__encode())
}
#[inline]
#[must_use]
pub fn trim_matches<P>(&self, pat: P) -> &Self
where
P: Pattern,
{
let pat = pat.__encode();
self.trim_start_matches_raw(&pat).trim_end_matches_raw(&pat)
}
fn trim_start_matches_raw<P>(&self, pat: &P) -> &Self
where
P: EncodedPattern,
{
self.trim_matches_raw_with(pat, <[_]>::strip_prefix)
}
#[inline]
#[must_use]
pub fn trim_start_matches<P>(&self, pat: P) -> &Self
where
P: Pattern,
{
self.trim_start_matches_raw(&pat.__encode())
}
}
impl AsRef<Self> for RawOsStr {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}
impl AsRef<RawOsStr> for str {
#[inline]
fn as_ref(&self) -> &RawOsStr {
RawOsStr::from_str(self)
}
}
impl AsRef<RawOsStr> for String {
#[inline]
fn as_ref(&self) -> &RawOsStr {
(**self).as_ref()
}
}
impl Default for &RawOsStr {
#[inline]
fn default() -> Self {
RawOsStr::from_str("")
}
}
impl<'a> From<&'a RawOsStr> for Cow<'a, RawOsStr> {
#[inline]
fn from(value: &'a RawOsStr) -> Self {
Cow::Borrowed(value)
}
}
impl From<Box<str>> for Box<RawOsStr> {
#[inline]
fn from(value: Box<str>) -> Self {
value.into_boxed_bytes().transmute_box()
}
}
impl ToOwned for RawOsStr {
type Owned = RawOsString;
#[inline]
fn to_owned(&self) -> Self::Owned {
RawOsString(self.0.to_owned())
}
}
#[cfg_attr(os_str_bytes_docs_rs, doc(cfg(feature = "raw_os_str")))]
pub trait RawOsStrCow<'a>: private::Sealed {
#[must_use]
fn into_os_str(self) -> Cow<'a, OsStr>;
#[must_use]
fn into_raw_bytes(self) -> Cow<'a, [u8]>;
}
impl<'a> RawOsStrCow<'a> for Cow<'a, RawOsStr> {
#[inline]
fn into_os_str(self) -> Cow<'a, OsStr> {
match self {
Cow::Borrowed(string) => string.to_os_str(),
Cow::Owned(string) => Cow::Owned(string.into_os_string()),
}
}
#[inline]
fn into_raw_bytes(self) -> Cow<'a, [u8]> {
match self {
Cow::Borrowed(string) => Cow::Borrowed(&string.0),
Cow::Owned(string) => Cow::Owned(string.0),
}
}
}
#[derive(Clone, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(os_str_bytes_docs_rs, doc(cfg(feature = "raw_os_str")))]
pub struct RawOsString(Vec<u8>);
impl RawOsString {
#[inline]
#[must_use]
pub fn new(string: OsString) -> Self {
Self(imp::os_string_into_vec(string))
}
#[inline]
#[must_use]
pub fn from_string(string: String) -> Self {
Self(string.into_bytes())
}
#[inline]
#[must_use = "method should not be used for validation"]
#[track_caller]
pub fn assert_from_raw_vec(string: Vec<u8>) -> Self {
expect_encoded!(raw::validate_bytes(&string));
Self(string)
}
if_checked_conversions! {
#[cfg_attr(
os_str_bytes_docs_rs,
doc(cfg(feature = "checked_conversions"))
)]
#[inline]
pub fn from_raw_vec(string: Vec<u8>) -> Result<Self> {
raw::validate_bytes(&string)
.map(|()| Self(string))
.map_err(EncodingError)
}
}
#[inline]
#[must_use]
#[track_caller]
pub unsafe fn from_raw_vec_unchecked(string: Vec<u8>) -> Self {
if cfg!(debug_assertions) {
expect_encoded!(raw::validate_bytes(&string));
}
Self(string)
}
#[inline]
pub fn clear(&mut self) {
self.0.clear();
}
#[inline]
#[must_use]
pub fn into_box(self) -> Box<RawOsStr> {
self.0.into_boxed_slice().transmute_box()
}
#[inline]
#[must_use]
pub fn into_os_string(self) -> OsString {
expect_encoded!(imp::os_string_from_vec(self.0))
}
#[inline]
#[must_use]
pub fn into_raw_vec(self) -> Vec<u8> {
self.0
}
#[inline]
pub fn into_string(self) -> result::Result<String, Self> {
String::from_utf8(self.0).map_err(|x| Self(x.into_bytes()))
}
#[inline]
pub fn shrink_to_fit(&mut self) {
self.0.shrink_to_fit();
}
#[inline]
#[must_use]
#[track_caller]
pub fn split_off(&mut self, at: usize) -> Self {
self.check_bound(at);
Self(self.0.split_off(at))
}
#[inline]
#[track_caller]
pub fn truncate(&mut self, new_len: usize) {
self.check_bound(new_len);
self.0.truncate(new_len);
}
}
impl AsRef<RawOsStr> for RawOsString {
#[inline]
fn as_ref(&self) -> &RawOsStr {
self
}
}
impl Borrow<RawOsStr> for RawOsString {
#[inline]
fn borrow(&self) -> &RawOsStr {
self
}
}
impl Deref for RawOsString {
type Target = RawOsStr;
#[inline]
fn deref(&self) -> &Self::Target {
RawOsStr::from_inner(&self.0)
}
}
impl From<RawOsString> for Box<RawOsStr> {
#[inline]
fn from(value: RawOsString) -> Self {
value.into_box()
}
}
impl From<Box<RawOsStr>> for RawOsString {
#[inline]
fn from(value: Box<RawOsStr>) -> Self {
Self(value.transmute_box::<[_]>().into_vec())
}
}
impl From<RawOsString> for Cow<'_, RawOsStr> {
#[inline]
fn from(value: RawOsString) -> Self {
Cow::Owned(value)
}
}
impl From<String> for RawOsString {
#[inline]
fn from(value: String) -> Self {
Self::from_string(value)
}
}
struct DebugBuffer<'a>(&'a [u8]);
impl Debug for DebugBuffer<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("\"")?;
let mut string = self.0;
let mut invalid_length = 0;
while !string.is_empty() {
let (invalid, substring) = string.split_at(invalid_length);
let valid = match str::from_utf8(substring) {
Ok(valid) => {
string = &[];
valid
}
Err(error) => {
let (valid, substring) =
substring.split_at(error.valid_up_to());
let invalid_char_length =
error.error_len().unwrap_or_else(|| substring.len());
if valid.is_empty() {
invalid_length += invalid_char_length;
continue;
}
string = substring;
invalid_length = invalid_char_length;
unsafe { str::from_utf8_unchecked(valid) }
}
};
raw::debug(invalid, f)?;
Display::fmt(&valid.escape_debug(), f)?;
}
f.write_str("\"")
}
}
macro_rules! r#impl {
( $type:ty ) => {
impl Debug for $type {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple(stringify!($type))
.field(&DebugBuffer(&self.0))
.finish()
}
}
};
}
r#impl!(RawOsStr);
r#impl!(RawOsString);
macro_rules! r#impl {
( $index_type:ty $(, $index_var:ident , $($bound:expr),+)? ) => {
impl Index<$index_type> for RawOsStr {
type Output = Self;
#[inline]
fn index(&self, idx: $index_type) -> &Self::Output {
$(
let $index_var = &idx;
$(self.check_bound($bound);)+
)?
Self::from_inner(&self.0[idx])
}
}
impl Index<$index_type> for RawOsString {
type Output = RawOsStr;
#[allow(clippy::indexing_slicing)]
#[inline]
fn index(&self, idx: $index_type) -> &Self::Output {
&(**self)[idx]
}
}
};
}
r#impl!(Range<usize>, x, x.start, x.end);
r#impl!(RangeFrom<usize>, x, x.start);
r#impl!(RangeFull);
#[rustfmt::skip]
r#impl!(RangeInclusive<usize>, x, *x.start(), x.end().wrapping_add(1));
r#impl!(RangeTo<usize>, x, x.end);
r#impl!(RangeToInclusive<usize>, x, x.end.wrapping_add(1));
macro_rules! r#impl {
( $type:ty , $other_type:ty ) => {
impl PartialEq<$other_type> for $type {
#[inline]
fn eq(&self, other: &$other_type) -> bool {
let raw: &RawOsStr = self;
let other: &RawOsStr = other.as_ref();
raw == other
}
}
impl PartialEq<$type> for $other_type {
#[inline]
fn eq(&self, other: &$type) -> bool {
other == self
}
}
};
}
r#impl!(RawOsStr, RawOsString);
r#impl!(&RawOsStr, RawOsString);
r#impl!(RawOsStr, str);
r#impl!(RawOsStr, String);
r#impl!(&RawOsStr, String);
r#impl!(RawOsString, str);
r#impl!(RawOsString, &str);
r#impl!(RawOsString, String);
#[cfg(feature = "print_bytes")]
#[cfg_attr(os_str_bytes_docs_rs, doc(cfg(feature = "print_bytes")))]
mod print_bytes {
use print_bytes::ByteStr;
use print_bytes::ToBytes;
#[cfg(windows)]
use print_bytes::WideStr;
#[cfg(windows)]
use crate::imp::raw;
use super::RawOsStr;
use super::RawOsString;
impl ToBytes for RawOsStr {
#[inline]
fn to_bytes(&self) -> ByteStr<'_> {
self.0.to_bytes()
}
#[cfg(windows)]
#[inline]
fn to_wide(&self) -> Option<WideStr> {
Some(WideStr::new(raw::encode_wide_unchecked(&self.0).collect()))
}
}
impl ToBytes for RawOsString {
#[inline]
fn to_bytes(&self) -> ByteStr<'_> {
(**self).to_bytes()
}
#[cfg(windows)]
#[inline]
fn to_wide(&self) -> Option<WideStr> {
(**self).to_wide()
}
}
}
#[cfg(feature = "uniquote")]
#[cfg_attr(os_str_bytes_docs_rs, doc(cfg(feature = "uniquote")))]
mod uniquote {
use uniquote::Formatter;
use uniquote::Quote;
use uniquote::Result;
use crate::imp::raw;
use super::RawOsStr;
use super::RawOsString;
impl Quote for RawOsStr {
#[inline]
fn escape(&self, f: &mut Formatter<'_>) -> Result {
raw::uniquote::escape(&self.0, f)
}
}
impl Quote for RawOsString {
#[inline]
fn escape(&self, f: &mut Formatter<'_>) -> Result {
(**self).escape(f)
}
}
}