2018-03-21 03:49:38 +00:00
|
|
|
#[cfg(any(target_os = "windows", target_arch = "wasm32"))]
|
2016-09-05 21:03:45 +00:00
|
|
|
use INVALID_UTF8;
|
|
|
|
use std::ffi::OsStr;
|
2018-03-21 03:49:38 +00:00
|
|
|
#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
|
2016-01-11 08:59:56 +00:00
|
|
|
use std::os::unix::ffi::OsStrExt;
|
|
|
|
|
2018-03-21 03:49:38 +00:00
|
|
|
#[cfg(any(target_os = "windows", target_arch = "wasm32"))]
|
2017-02-21 01:31:57 +00:00
|
|
|
pub trait OsStrExt3 {
|
2016-01-27 22:36:42 +00:00
|
|
|
fn from_bytes(b: &[u8]) -> &Self;
|
2016-01-26 07:03:30 +00:00
|
|
|
fn as_bytes(&self) -> &[u8];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2016-01-11 08:59:56 +00:00
|
|
|
pub trait OsStrExt2 {
|
|
|
|
fn starts_with(&self, s: &[u8]) -> bool;
|
|
|
|
fn split_at_byte(&self, b: u8) -> (&OsStr, &OsStr);
|
|
|
|
fn split_at(&self, i: usize) -> (&OsStr, &OsStr);
|
|
|
|
fn trim_left_matches(&self, b: u8) -> &OsStr;
|
2016-02-21 16:18:26 +00:00
|
|
|
fn len_(&self) -> usize;
|
2016-01-11 08:59:56 +00:00
|
|
|
fn contains_byte(&self, b: u8) -> bool;
|
2016-02-21 16:18:26 +00:00
|
|
|
fn is_empty_(&self) -> bool;
|
2016-01-26 07:03:30 +00:00
|
|
|
fn split(&self, b: u8) -> OsSplit;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
impl OsStrExt3 for OsStr {
|
2016-01-27 22:36:42 +00:00
|
|
|
fn from_bytes(b: &[u8]) -> &Self {
|
2016-05-06 21:35:53 +00:00
|
|
|
use std::mem;
|
2016-01-26 07:03:30 +00:00
|
|
|
unsafe { mem::transmute(b) }
|
|
|
|
}
|
2016-11-20 19:41:15 +00:00
|
|
|
fn as_bytes(&self) -> &[u8] { self.to_str().map(|s| s.as_bytes()).expect(INVALID_UTF8) }
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OsStrExt2 for OsStr {
|
2016-11-20 19:41:15 +00:00
|
|
|
fn starts_with(&self, s: &[u8]) -> bool { self.as_bytes().starts_with(s) }
|
2016-01-11 08:59:56 +00:00
|
|
|
|
2016-11-20 19:41:15 +00:00
|
|
|
fn is_empty_(&self) -> bool { self.as_bytes().is_empty() }
|
2016-01-22 17:58:56 +00:00
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
fn contains_byte(&self, byte: u8) -> bool {
|
|
|
|
for b in self.as_bytes() {
|
2016-05-06 21:35:53 +00:00
|
|
|
if b == &byte {
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn split_at_byte(&self, byte: u8) -> (&OsStr, &OsStr) {
|
2016-01-23 11:46:16 +00:00
|
|
|
for (i, b) in self.as_bytes().iter().enumerate() {
|
2016-05-06 21:35:53 +00:00
|
|
|
if b == &byte {
|
2017-11-28 12:30:06 +00:00
|
|
|
return (
|
|
|
|
OsStr::from_bytes(&self.as_bytes()[..i]),
|
|
|
|
OsStr::from_bytes(&self.as_bytes()[i + 1..]),
|
|
|
|
);
|
2016-05-06 21:35:53 +00:00
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
2017-11-28 12:30:06 +00:00
|
|
|
(
|
|
|
|
&*self,
|
|
|
|
OsStr::from_bytes(&self.as_bytes()[self.len_()..self.len_()]),
|
|
|
|
)
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn trim_left_matches(&self, byte: u8) -> &OsStr {
|
2017-11-13 22:02:50 +00:00
|
|
|
let mut found = false;
|
2016-01-23 11:46:16 +00:00
|
|
|
for (i, b) in self.as_bytes().iter().enumerate() {
|
2016-05-06 21:35:53 +00:00
|
|
|
if b != &byte {
|
2016-06-08 04:10:56 +00:00
|
|
|
return OsStr::from_bytes(&self.as_bytes()[i..]);
|
2017-11-13 22:02:50 +00:00
|
|
|
} else {
|
|
|
|
found = true;
|
2016-05-06 21:35:53 +00:00
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
2017-11-13 22:02:50 +00:00
|
|
|
if found {
|
|
|
|
return OsStr::from_bytes(&self.as_bytes()[self.len_()..]);
|
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
&*self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn split_at(&self, i: usize) -> (&OsStr, &OsStr) {
|
2017-11-28 12:30:06 +00:00
|
|
|
(
|
|
|
|
OsStr::from_bytes(&self.as_bytes()[..i]),
|
|
|
|
OsStr::from_bytes(&self.as_bytes()[i..]),
|
|
|
|
)
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-20 19:41:15 +00:00
|
|
|
fn len_(&self) -> usize { self.as_bytes().len() }
|
2016-01-26 07:03:30 +00:00
|
|
|
|
|
|
|
fn split(&self, b: u8) -> OsSplit {
|
2016-05-06 21:35:53 +00:00
|
|
|
OsSplit {
|
|
|
|
sep: b,
|
|
|
|
val: self.as_bytes(),
|
|
|
|
pos: 0,
|
|
|
|
}
|
2016-01-26 07:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 19:02:10 +00:00
|
|
|
#[doc(hidden)]
|
2016-01-26 07:03:30 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct OsSplit<'a> {
|
|
|
|
sep: u8,
|
|
|
|
val: &'a [u8],
|
|
|
|
pos: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for OsSplit<'a> {
|
|
|
|
type Item = &'a OsStr;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<&'a OsStr> {
|
2016-12-29 19:07:04 +00:00
|
|
|
debugln!("OsSplit::next: self={:?}", self);
|
2016-05-06 21:35:53 +00:00
|
|
|
if self.pos == self.val.len() {
|
|
|
|
return None;
|
|
|
|
}
|
2016-01-26 07:03:30 +00:00
|
|
|
let start = self.pos;
|
|
|
|
for b in &self.val[start..] {
|
|
|
|
self.pos += 1;
|
|
|
|
if *b == self.sep {
|
2016-06-08 04:10:56 +00:00
|
|
|
return Some(OsStr::from_bytes(&self.val[start..self.pos - 1]));
|
2016-01-26 07:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-08 04:10:56 +00:00
|
|
|
Some(OsStr::from_bytes(&self.val[start..]))
|
2016-01-26 07:03:30 +00:00
|
|
|
}
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
let mut count = 0;
|
|
|
|
for b in &self.val[self.pos..] {
|
2016-05-06 21:35:53 +00:00
|
|
|
if *b == self.sep {
|
|
|
|
count += 1;
|
|
|
|
}
|
2016-01-26 07:03:30 +00:00
|
|
|
}
|
|
|
|
if count > 0 {
|
|
|
|
return (count, Some(count));
|
|
|
|
}
|
|
|
|
(0, None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> DoubleEndedIterator for OsSplit<'a> {
|
|
|
|
fn next_back(&mut self) -> Option<&'a OsStr> {
|
2016-05-06 21:35:53 +00:00
|
|
|
if self.pos == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
2016-01-26 07:03:30 +00:00
|
|
|
let start = self.pos;
|
|
|
|
for b in self.val[..self.pos].iter().rev() {
|
|
|
|
self.pos -= 1;
|
|
|
|
if *b == self.sep {
|
2016-06-08 04:10:56 +00:00
|
|
|
return Some(OsStr::from_bytes(&self.val[self.pos + 1..start]));
|
2016-01-26 07:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-08 04:10:56 +00:00
|
|
|
Some(OsStr::from_bytes(&self.val[..start]))
|
2016-01-26 07:03:30 +00:00
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|