use crate::string::String;
use crate::vec::Vec;
pub type OsStr = str;
pub type OsString = String;
#[derive(Debug)]
pub struct NulError;
impl core::fmt::Display for NulError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("interior null byte in string")
}
}
impl core::error::Error for NulError {}
pub struct CString(Vec<u8>);
impl CString {
pub fn new(s: &str) -> core::result::Result<Self, NulError> {
if s.bytes().any(|b| b == 0) {
return Err(NulError);
}
let mut v = Vec::with_capacity(s.len() + 1);
v.extend_from_slice(s.as_bytes());
v.push(0);
Ok(Self(v))
}
#[inline]
pub fn as_ptr(&self) -> *const u8 {
self.0.as_ptr()
}
}
pub trait OsStrExt {
fn as_bytes(&self) -> &[u8];
fn encode_wide(&self) -> EncodeWide<'_>;
}
impl OsStrExt for str {
#[inline]
fn as_bytes(&self) -> &[u8] {
str::as_bytes(self)
}
#[inline]
fn encode_wide(&self) -> EncodeWide<'_> {
EncodeWide {
inner: self.encode_utf16(),
}
}
}
pub struct EncodeWide<'a> {
inner: core::str::EncodeUtf16<'a>,
}
impl Iterator for EncodeWide<'_> {
type Item = u16;
#[inline]
fn next(&mut self) -> Option<u16> {
self.inner.next()
}
}