1 #[cfg(target_os = "windows")]
4 #[cfg(not(target_os = "windows"))]
5 use std
::os
::unix
::ffi
::OsStrExt
;
7 #[cfg(target_os = "windows")]
9 fn from_bytes(b
: &[u8]) -> &Self;
10 fn as_bytes(&self) -> &[u8];
15 fn starts_with(&self, s
: &[u8]) -> bool
;
16 fn split_at_byte(&self, b
: u8) -> (&OsStr
, &OsStr
);
17 fn split_at(&self, i
: usize) -> (&OsStr
, &OsStr
);
18 fn trim_left_matches(&self, b
: u8) -> &OsStr
;
19 fn len_(&self) -> usize;
20 fn contains_byte(&self, b
: u8) -> bool
;
21 fn is_empty_(&self) -> bool
;
22 fn split(&self, b
: u8) -> OsSplit
;
25 #[cfg(target_os = "windows")]
26 impl OsStrExt3
for OsStr
{
27 fn from_bytes(b
: &[u8]) -> &Self {
29 unsafe { mem::transmute(b) }
31 fn as_bytes(&self) -> &[u8] { self.to_str().map(|s| s.as_bytes()).expect(INVALID_UTF8) }
34 impl OsStrExt2
for OsStr
{
35 fn starts_with(&self, s
: &[u8]) -> bool { self.as_bytes().starts_with(s) }
37 fn is_empty_(&self) -> bool { self.as_bytes().is_empty() }
39 fn contains_byte(&self, byte
: u8) -> bool
{
40 for b
in self.as_bytes() {
48 fn split_at_byte(&self, byte
: u8) -> (&OsStr
, &OsStr
) {
49 for (i
, b
) in self.as_bytes().iter().enumerate() {
51 return (OsStr
::from_bytes(&self.as_bytes()[..i
]),
52 OsStr
::from_bytes(&self.as_bytes()[i
+ 1..]));
55 (&*self, OsStr
::from_bytes(&self.as_bytes()[self.len_()..self.len_()]))
58 fn trim_left_matches(&self, byte
: u8) -> &OsStr
{
59 for (i
, b
) in self.as_bytes().iter().enumerate() {
61 return OsStr
::from_bytes(&self.as_bytes()[i
..]);
67 fn split_at(&self, i
: usize) -> (&OsStr
, &OsStr
) {
68 (OsStr
::from_bytes(&self.as_bytes()[..i
]), OsStr
::from_bytes(&self.as_bytes()[i
..]))
71 fn len_(&self) -> usize { self.as_bytes().len() }
73 fn split(&self, b
: u8) -> OsSplit
{
83 #[derive(Clone, Debug)]
84 pub struct OsSplit
<'a
> {
90 impl<'a
> Iterator
for OsSplit
<'a
> {
91 type Item
= &'a OsStr
;
93 fn next(&mut self) -> Option
<&'a OsStr
> {
94 debugln
!("fn=OsSplit::next;");
95 debugln
!("OsSplit: {:?}", self);
96 if self.pos
== self.val
.len() {
100 for b
in &self.val
[start
..] {
103 return Some(OsStr
::from_bytes(&self.val
[start
..self.pos
- 1]));
106 Some(OsStr
::from_bytes(&self.val
[start
..]))
108 fn size_hint(&self) -> (usize, Option
<usize>) {
110 for b
in &self.val
[self.pos
..] {
116 return (count
, Some(count
));
122 impl<'a
> DoubleEndedIterator
for OsSplit
<'a
> {
123 fn next_back(&mut self) -> Option
<&'a OsStr
> {
127 let start
= self.pos
;
128 for b
in self.val
[..self.pos
].iter().rev() {
131 return Some(OsStr
::from_bytes(&self.val
[self.pos
+ 1..start
]));
134 Some(OsStr
::from_bytes(&self.val
[..start
]))