#![crate_type="lib"]
#![crate_name="cmdline_words_parser"]
#![cfg_attr(not(feature="std"), no_std)]
#[cfg(feature="alloc")]
extern crate alloc;
#[cfg(not(feature="std"))]
mod std {
pub use core::marker;
pub use core::mem;
pub use core::str;
}
pub use crate::posix::PosixShellWords;
mod posix;
pub fn parse_posix<T: ?Sized + ByteString>(string: &mut T) -> PosixShellWords<T::OutSlice> {
PosixShellWords::new(unsafe { string.as_mut_bytes() })
}
pub trait ByteString
{
type OutSlice: ?Sized + ByteStringSlice;
unsafe fn as_mut_bytes(&mut self) -> &mut [u8];
}
impl ByteString for str
{
type OutSlice = str;
#[doc(hidden)]
unsafe fn as_mut_bytes(&mut self) -> &mut [u8] {
::std::mem::transmute(self)
}
}
impl ByteString for [u8]
{
type OutSlice = [u8];
#[doc(hidden)]
unsafe fn as_mut_bytes(&mut self) -> &mut [u8] {
self
}
}
#[cfg(feature="std")]
impl ByteString for ::std::ffi::OsStr
{
type OutSlice = ::std::ffi::OsStr;
#[doc(hidden)]
unsafe fn as_mut_bytes(&mut self) -> &mut [u8] {
#[cfg(unix)]
fn _type_assert(v: &::std::ffi::OsStr) {
let _: &[u8] = <_ as ::std::os::unix::ffi::OsStrExt>::as_bytes(v);
}
::std::mem::transmute(self)
}
}
#[cfg(feature="alloc")]
impl ByteString for ::alloc::string::String {
type OutSlice = str;
#[doc(hidden)]
unsafe fn as_mut_bytes(&mut self) -> &mut [u8] {
&mut **self.as_mut_vec()
}
}
#[doc(hidden)]
pub trait ByteStringSlice {
fn from_bytes(bytes: &[u8]) -> Option<&Self>;
}
impl ByteStringSlice for str {
fn from_bytes(bytes: &[u8]) -> Option<&Self> {
::std::str::from_utf8(bytes).ok()
}
}
impl ByteStringSlice for [u8] {
fn from_bytes(bytes: &[u8]) -> Option<&Self> {
Some(bytes)
}
}
#[cfg(feature="std")]
impl ByteStringSlice for ::std::ffi::OsStr {
fn from_bytes(bytes: &[u8]) -> Option<&Self> {
Some( unsafe { ::std::mem::transmute(bytes) } )
}
}
fn split_off_front_inplace_mut<'a, T>(slice: &mut &'a mut [T], idx: usize) -> &'a mut [T] {
let (ret, tail) = ::std::mem::replace(slice, &mut []).split_at_mut(idx);
*slice = tail;
ret
}