1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
use std::ffi::OsStr; #[cfg(not(target_os = "windows"))] use std::os::unix::ffi::OsStrExt; #[cfg(target_os = "windows")] use INVALID_UTF8; #[cfg(target_os = "windows")] trait OsStrExt3 { fn from_bytes(b: &[u8]) -> &Self; fn as_bytes(&self) -> &[u8]; } #[doc(hidden)] 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; fn len_(&self) -> usize; fn contains_byte(&self, b: u8) -> bool; fn is_empty_(&self) -> bool; fn split(&self, b: u8) -> OsSplit; } #[cfg(target_os = "windows")] impl OsStrExt3 for OsStr { fn from_bytes(b: &[u8]) -> &Self { use ::std::mem; unsafe { mem::transmute(b) } } fn as_bytes(&self) -> &[u8] { self.to_str().map(|s| s.as_bytes()).expect(INVALID_UTF8) } } impl OsStrExt2 for OsStr { fn starts_with(&self, s: &[u8]) -> bool { self.as_bytes().starts_with(s) } fn is_empty_(&self) -> bool { self.as_bytes().is_empty() } fn contains_byte(&self, byte: u8) -> bool { for b in self.as_bytes() { if b == &byte { return true; } } false } fn split_at_byte(&self, byte: u8) -> (&OsStr, &OsStr) { for (i, b) in self.as_bytes().iter().enumerate() { if b == &byte { return (&OsStr::from_bytes(&self.as_bytes()[..i]), &OsStr::from_bytes(&self.as_bytes()[i+1..])); } } (&*self, &OsStr::from_bytes(&self.as_bytes()[self.len_()..self.len_()])) } fn trim_left_matches(&self, byte: u8) -> &OsStr { for (i, b) in self.as_bytes().iter().enumerate() { if b != &byte { return &OsStr::from_bytes(&self.as_bytes()[i..]); } } &*self } fn split_at(&self, i: usize) -> (&OsStr, &OsStr) { (&OsStr::from_bytes(&self.as_bytes()[..i]), &OsStr::from_bytes(&self.as_bytes()[i..])) } fn len_(&self) -> usize { self.as_bytes().len() } fn split(&self, b: u8) -> OsSplit { OsSplit { sep: b, val: self.as_bytes(), pos: 0 } } } #[doc(hidden)] #[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> { debugln!("fn=OsSplit::next;"); debugln!("OsSplit: {:?}", self); if self.pos == self.val.len() { return None; } let start = self.pos; for b in &self.val[start..] { self.pos += 1; if *b == self.sep { return Some(&OsStr::from_bytes(&self.val[start..self.pos - 1])); } } Some(&OsStr::from_bytes(&self.val[start..])) } fn size_hint(&self) -> (usize, Option<usize>) { let mut count = 0; for b in &self.val[self.pos..] { if *b == self.sep { count += 1; } } if count > 0 { return (count, Some(count)); } (0, None) } } impl<'a> DoubleEndedIterator for OsSplit<'a> { fn next_back(&mut self) -> Option<&'a OsStr> { if self.pos == 0 { return None; } let start = self.pos; for b in self.val[..self.pos].iter().rev() { self.pos -= 1; if *b == self.sep { return Some(&OsStr::from_bytes(&self.val[self.pos + 1..start])); } } Some(&OsStr::from_bytes(&self.val[..start])) } }