use crate::{NL, SPACE};
pub(crate) use ext_impl::*;
use std::ffi::OsStr;
#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
mod ext_impl {
pub(crate) use std::os::unix::ffi::OsStrExt;
}
#[cfg(any(target_os = "windows", target_arch = "wasm32"))]
mod ext_impl {
use super::*;
use crate::INVALID_UTF8;
#[doc(hidden)]
pub trait OsStrExt3 {
fn as_bytes(&self) -> &[u8];
}
impl OsStrExt3 for OsStr {
fn as_bytes(&self) -> &[u8] {
self.to_str().map(|s| s.as_bytes()).expect(INVALID_UTF8)
}
}
}
#[doc(hidden)]
pub trait OsStrExt2 {
fn contains_byte(&self, byte: u8) -> bool;
fn display_string(&self) -> String;
}
impl OsStrExt2 for OsStr {
fn contains_byte(&self, byte: u8) -> bool {
for b in self.as_bytes() {
if b == &byte {
return true;
}
}
false
}
fn display_string(&self) -> String {
let mut has_newline = false;
let mut has_space = false;
for b in self.as_bytes() {
if b == NL {
has_newline = true;
break;
}
if b == SPACE {
has_space = true;
}
}
#[inline]
fn escape_with_newlines(input: &OsStr) -> String {
format!("{input:?}").replace(r"\n", "\n")
}
if has_newline {
escape_with_newlines(self)
} else if has_space {
format!("{self:?}")
} else {
self.to_str().unwrap().to_owned()
}
}
}