run rustfmt on std::path
There was a bunch of manual fixes too... rustfmt isn't quite there yet
This commit is contained in:
parent
01fd4d6227
commit
37735b4d6d
1 changed files with 165 additions and 96 deletions
|
@ -48,7 +48,8 @@
|
||||||
//! The path APIs are built around the notion of "components", which roughly
|
//! The path APIs are built around the notion of "components", which roughly
|
||||||
//! correspond to the substrings between path separators (`/` and, on Windows,
|
//! correspond to the substrings between path separators (`/` and, on Windows,
|
||||||
//! `\`). The APIs for path parsing are largely specified in terms of the path's
|
//! `\`). The APIs for path parsing are largely specified in terms of the path's
|
||||||
//! components, so it's important to clearly understand how those are determined.
|
//! components, so it's important to clearly understand how those are
|
||||||
|
//! determined.
|
||||||
//!
|
//!
|
||||||
//! A path can always be reconstructed into an *equivalent* path by
|
//! A path can always be reconstructed into an *equivalent* path by
|
||||||
//! putting together its components via `push`. Syntactically, the
|
//! putting together its components via `push`. Syntactically, the
|
||||||
|
@ -190,10 +191,9 @@ mod platform {
|
||||||
// \\?\UNC\server\share
|
// \\?\UNC\server\share
|
||||||
path = &path[4..];
|
path = &path[4..];
|
||||||
let (server, share) = match parse_two_comps(path, is_verbatim_sep) {
|
let (server, share) = match parse_two_comps(path, is_verbatim_sep) {
|
||||||
Some((server, share)) => (u8_slice_as_os_str(server),
|
Some((server, share)) =>
|
||||||
u8_slice_as_os_str(share)),
|
(u8_slice_as_os_str(server), u8_slice_as_os_str(share)),
|
||||||
None => (u8_slice_as_os_str(path),
|
None => (u8_slice_as_os_str(path), u8_slice_as_os_str(&[])),
|
||||||
u8_slice_as_os_str(&[])),
|
|
||||||
};
|
};
|
||||||
return Some(VerbatimUNC(server, share));
|
return Some(VerbatimUNC(server, share));
|
||||||
} else {
|
} else {
|
||||||
|
@ -206,7 +206,7 @@ mod platform {
|
||||||
return Some(VerbatimDisk(c.to_ascii_uppercase()));
|
return Some(VerbatimDisk(c.to_ascii_uppercase()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let slice = &path[.. idx.unwrap_or(path.len())];
|
let slice = &path[..idx.unwrap_or(path.len())];
|
||||||
return Some(Verbatim(u8_slice_as_os_str(slice)));
|
return Some(Verbatim(u8_slice_as_os_str(slice)));
|
||||||
}
|
}
|
||||||
} else if path.starts_with(b".\\") {
|
} else if path.starts_with(b".\\") {
|
||||||
|
@ -219,10 +219,9 @@ mod platform {
|
||||||
match parse_two_comps(path, is_sep_byte) {
|
match parse_two_comps(path, is_sep_byte) {
|
||||||
Some((server, share)) if !server.is_empty() && !share.is_empty() => {
|
Some((server, share)) if !server.is_empty() && !share.is_empty() => {
|
||||||
// \\server\share
|
// \\server\share
|
||||||
return Some(UNC(u8_slice_as_os_str(server),
|
return Some(UNC(u8_slice_as_os_str(server), u8_slice_as_os_str(share)));
|
||||||
u8_slice_as_os_str(share)));
|
|
||||||
}
|
}
|
||||||
_ => ()
|
_ => (),
|
||||||
}
|
}
|
||||||
} else if path.len() > 1 && path[1] == b':' {
|
} else if path.len() > 1 && path[1] == b':' {
|
||||||
// C:
|
// C:
|
||||||
|
@ -237,11 +236,11 @@ mod platform {
|
||||||
fn parse_two_comps(mut path: &[u8], f: fn(u8) -> bool) -> Option<(&[u8], &[u8])> {
|
fn parse_two_comps(mut path: &[u8], f: fn(u8) -> bool) -> Option<(&[u8], &[u8])> {
|
||||||
let first = match path.iter().position(|x| f(*x)) {
|
let first = match path.iter().position(|x| f(*x)) {
|
||||||
None => return None,
|
None => return None,
|
||||||
Some(x) => &path[.. x]
|
Some(x) => &path[..x],
|
||||||
};
|
};
|
||||||
path = &path[(first.len()+1)..];
|
path = &path[(first.len() + 1)..];
|
||||||
let idx = path.iter().position(|x| f(*x));
|
let idx = path.iter().position(|x| f(*x));
|
||||||
let second = &path[.. idx.unwrap_or(path.len())];
|
let second = &path[..idx.unwrap_or(path.len())];
|
||||||
Some((first, second))
|
Some((first, second))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -298,15 +297,25 @@ impl<'a> Prefix<'a> {
|
||||||
}
|
}
|
||||||
match *self {
|
match *self {
|
||||||
Verbatim(x) => 4 + os_str_len(x),
|
Verbatim(x) => 4 + os_str_len(x),
|
||||||
VerbatimUNC(x,y) => 8 + os_str_len(x) +
|
VerbatimUNC(x, y) => {
|
||||||
if os_str_len(y) > 0 { 1 + os_str_len(y) }
|
8 + os_str_len(x) +
|
||||||
else { 0 },
|
if os_str_len(y) > 0 {
|
||||||
|
1 + os_str_len(y)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
},
|
||||||
VerbatimDisk(_) => 6,
|
VerbatimDisk(_) => 6,
|
||||||
UNC(x,y) => 2 + os_str_len(x) +
|
UNC(x, y) => {
|
||||||
if os_str_len(y) > 0 { 1 + os_str_len(y) }
|
2 + os_str_len(x) +
|
||||||
else { 0 },
|
if os_str_len(y) > 0 {
|
||||||
|
1 + os_str_len(y)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
},
|
||||||
DeviceNS(x) => 4 + os_str_len(x),
|
DeviceNS(x) => 4 + os_str_len(x),
|
||||||
Disk(_) => 2
|
Disk(_) => 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -367,14 +376,18 @@ pub const MAIN_SEPARATOR: char = platform::MAIN_SEP;
|
||||||
// Iterate through `iter` while it matches `prefix`; return `None` if `prefix`
|
// Iterate through `iter` while it matches `prefix`; return `None` if `prefix`
|
||||||
// is not a prefix of `iter`, otherwise return `Some(iter_after_prefix)` giving
|
// is not a prefix of `iter`, otherwise return `Some(iter_after_prefix)` giving
|
||||||
// `iter` after having exhausted `prefix`.
|
// `iter` after having exhausted `prefix`.
|
||||||
fn iter_after<A, I, J>(mut iter: I, mut prefix: J) -> Option<I> where
|
fn iter_after<A, I, J>(mut iter: I, mut prefix: J) -> Option<I>
|
||||||
I: Iterator<Item=A> + Clone, J: Iterator<Item=A>, A: PartialEq
|
where I: Iterator<Item = A> + Clone,
|
||||||
|
J: Iterator<Item = A>,
|
||||||
|
A: PartialEq
|
||||||
{
|
{
|
||||||
loop {
|
loop {
|
||||||
let mut iter_next = iter.clone();
|
let mut iter_next = iter.clone();
|
||||||
match (iter_next.next(), prefix.next()) {
|
match (iter_next.next(), prefix.next()) {
|
||||||
(Some(x), Some(y)) => {
|
(Some(x), Some(y)) => {
|
||||||
if x != y { return None }
|
if x != y {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
(Some(_), None) => return Some(iter),
|
(Some(_), None) => return Some(iter),
|
||||||
(None, None) => return Some(iter),
|
(None, None) => return Some(iter),
|
||||||
|
@ -398,14 +411,20 @@ unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr {
|
||||||
|
|
||||||
/// Says whether the first byte after the prefix is a separator.
|
/// Says whether the first byte after the prefix is a separator.
|
||||||
fn has_physical_root(s: &[u8], prefix: Option<Prefix>) -> bool {
|
fn has_physical_root(s: &[u8], prefix: Option<Prefix>) -> bool {
|
||||||
let path = if let Some(p) = prefix { &s[p.len()..] } else { s };
|
let path = if let Some(p) = prefix {
|
||||||
|
&s[p.len()..]
|
||||||
|
} else {
|
||||||
|
s
|
||||||
|
};
|
||||||
!path.is_empty() && is_sep_byte(path[0])
|
!path.is_empty() && is_sep_byte(path[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
// basic workhorse for splitting stem and extension
|
// basic workhorse for splitting stem and extension
|
||||||
fn split_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
|
fn split_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
|
||||||
unsafe {
|
unsafe {
|
||||||
if os_str_as_u8_slice(file) == b".." { return (Some(file), None) }
|
if os_str_as_u8_slice(file) == b".." {
|
||||||
|
return (Some(file), None);
|
||||||
|
}
|
||||||
|
|
||||||
// The unsafety here stems from converting between &OsStr and &[u8]
|
// The unsafety here stems from converting between &OsStr and &[u8]
|
||||||
// and back. This is safe to do because (1) we only look at ASCII
|
// and back. This is safe to do because (1) we only look at ASCII
|
||||||
|
@ -581,7 +600,7 @@ pub struct Components<'a> {
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub struct Iter<'a> {
|
pub struct Iter<'a> {
|
||||||
inner: Components<'a>
|
inner: Components<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Components<'a> {
|
impl<'a> Components<'a> {
|
||||||
|
@ -609,8 +628,16 @@ impl<'a> Components<'a> {
|
||||||
// Given the iteration so far, how much of the pre-State::Body path is left?
|
// Given the iteration so far, how much of the pre-State::Body path is left?
|
||||||
#[inline]
|
#[inline]
|
||||||
fn len_before_body(&self) -> usize {
|
fn len_before_body(&self) -> usize {
|
||||||
let root = if self.front <= State::StartDir && self.has_physical_root { 1 } else { 0 };
|
let root = if self.front <= State::StartDir && self.has_physical_root {
|
||||||
let cur_dir = if self.front <= State::StartDir && self.include_cur_dir() { 1 } else { 0 };
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
let cur_dir = if self.front <= State::StartDir && self.include_cur_dir() {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
self.prefix_remaining() + root + cur_dir
|
self.prefix_remaining() + root + cur_dir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -645,28 +672,38 @@ impl<'a> Components<'a> {
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn as_path(&self) -> &'a Path {
|
pub fn as_path(&self) -> &'a Path {
|
||||||
let mut comps = self.clone();
|
let mut comps = self.clone();
|
||||||
if comps.front == State::Body { comps.trim_left(); }
|
if comps.front == State::Body {
|
||||||
if comps.back == State::Body { comps.trim_right(); }
|
comps.trim_left();
|
||||||
|
}
|
||||||
|
if comps.back == State::Body {
|
||||||
|
comps.trim_right();
|
||||||
|
}
|
||||||
unsafe { Path::from_u8_slice(comps.path) }
|
unsafe { Path::from_u8_slice(comps.path) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Is the *original* path rooted?
|
/// Is the *original* path rooted?
|
||||||
fn has_root(&self) -> bool {
|
fn has_root(&self) -> bool {
|
||||||
if self.has_physical_root { return true }
|
if self.has_physical_root {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if let Some(p) = self.prefix {
|
if let Some(p) = self.prefix {
|
||||||
if p.has_implicit_root() { return true }
|
if p.has_implicit_root() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Should the normalized path include a leading . ?
|
/// Should the normalized path include a leading . ?
|
||||||
fn include_cur_dir(&self) -> bool {
|
fn include_cur_dir(&self) -> bool {
|
||||||
if self.has_root() { return false }
|
if self.has_root() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
let mut iter = self.path[self.prefix_len()..].iter();
|
let mut iter = self.path[self.prefix_len()..].iter();
|
||||||
match (iter.next(), iter.next()) {
|
match (iter.next(), iter.next()) {
|
||||||
(Some(&b'.'), None) => true,
|
(Some(&b'.'), None) => true,
|
||||||
(Some(&b'.'), Some(&b)) => self.is_sep_byte(b),
|
(Some(&b'.'), Some(&b)) => self.is_sep_byte(b),
|
||||||
_ => false
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -679,7 +716,7 @@ impl<'a> Components<'a> {
|
||||||
// separately via `include_cur_dir`
|
// separately via `include_cur_dir`
|
||||||
b".." => Some(Component::ParentDir),
|
b".." => Some(Component::ParentDir),
|
||||||
b"" => None,
|
b"" => None,
|
||||||
_ => Some(Component::Normal(unsafe { u8_slice_as_os_str(comp) }))
|
_ => Some(Component::Normal(unsafe { u8_slice_as_os_str(comp) })),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -689,7 +726,7 @@ impl<'a> Components<'a> {
|
||||||
debug_assert!(self.front == State::Body);
|
debug_assert!(self.front == State::Body);
|
||||||
let (extra, comp) = match self.path.iter().position(|b| self.is_sep_byte(*b)) {
|
let (extra, comp) = match self.path.iter().position(|b| self.is_sep_byte(*b)) {
|
||||||
None => (0, self.path),
|
None => (0, self.path),
|
||||||
Some(i) => (1, &self.path[.. i]),
|
Some(i) => (1, &self.path[..i]),
|
||||||
};
|
};
|
||||||
(comp.len() + extra, self.parse_single_component(comp))
|
(comp.len() + extra, self.parse_single_component(comp))
|
||||||
}
|
}
|
||||||
|
@ -700,8 +737,8 @@ impl<'a> Components<'a> {
|
||||||
debug_assert!(self.back == State::Body);
|
debug_assert!(self.back == State::Body);
|
||||||
let start = self.len_before_body();
|
let start = self.len_before_body();
|
||||||
let (extra, comp) = match self.path[start..].iter().rposition(|b| self.is_sep_byte(*b)) {
|
let (extra, comp) = match self.path[start..].iter().rposition(|b| self.is_sep_byte(*b)) {
|
||||||
None => (0, &self.path[start ..]),
|
None => (0, &self.path[start..]),
|
||||||
Some(i) => (1, &self.path[start + i + 1 ..]),
|
Some(i) => (1, &self.path[start + i + 1..]),
|
||||||
};
|
};
|
||||||
(comp.len() + extra, self.parse_single_component(comp))
|
(comp.len() + extra, self.parse_single_component(comp))
|
||||||
}
|
}
|
||||||
|
@ -713,7 +750,7 @@ impl<'a> Components<'a> {
|
||||||
if comp.is_some() {
|
if comp.is_some() {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
self.path = &self.path[size ..];
|
self.path = &self.path[size..];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -725,7 +762,7 @@ impl<'a> Components<'a> {
|
||||||
if comp.is_some() {
|
if comp.is_some() {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
self.path = &self.path[.. self.path.len() - size];
|
self.path = &self.path[..self.path.len() - size];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -799,12 +836,12 @@ impl<'a> Iterator for Components<'a> {
|
||||||
State::Prefix if self.prefix_len() > 0 => {
|
State::Prefix if self.prefix_len() > 0 => {
|
||||||
self.front = State::StartDir;
|
self.front = State::StartDir;
|
||||||
debug_assert!(self.prefix_len() <= self.path.len());
|
debug_assert!(self.prefix_len() <= self.path.len());
|
||||||
let raw = &self.path[.. self.prefix_len()];
|
let raw = &self.path[..self.prefix_len()];
|
||||||
self.path = &self.path[self.prefix_len() .. ];
|
self.path = &self.path[self.prefix_len()..];
|
||||||
return Some(Component::Prefix(PrefixComponent {
|
return Some(Component::Prefix(PrefixComponent {
|
||||||
raw: unsafe { u8_slice_as_os_str(raw) },
|
raw: unsafe { u8_slice_as_os_str(raw) },
|
||||||
parsed: self.prefix.unwrap()
|
parsed: self.prefix.unwrap(),
|
||||||
}))
|
}));
|
||||||
}
|
}
|
||||||
State::Prefix => {
|
State::Prefix => {
|
||||||
self.front = State::StartDir;
|
self.front = State::StartDir;
|
||||||
|
@ -814,26 +851,28 @@ impl<'a> Iterator for Components<'a> {
|
||||||
if self.has_physical_root {
|
if self.has_physical_root {
|
||||||
debug_assert!(!self.path.is_empty());
|
debug_assert!(!self.path.is_empty());
|
||||||
self.path = &self.path[1..];
|
self.path = &self.path[1..];
|
||||||
return Some(Component::RootDir)
|
return Some(Component::RootDir);
|
||||||
} else if let Some(p) = self.prefix {
|
} else if let Some(p) = self.prefix {
|
||||||
if p.has_implicit_root() && !p.is_verbatim() {
|
if p.has_implicit_root() && !p.is_verbatim() {
|
||||||
return Some(Component::RootDir)
|
return Some(Component::RootDir);
|
||||||
}
|
}
|
||||||
} else if self.include_cur_dir() {
|
} else if self.include_cur_dir() {
|
||||||
debug_assert!(!self.path.is_empty());
|
debug_assert!(!self.path.is_empty());
|
||||||
self.path = &self.path[1..];
|
self.path = &self.path[1..];
|
||||||
return Some(Component::CurDir)
|
return Some(Component::CurDir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
State::Body if !self.path.is_empty() => {
|
State::Body if !self.path.is_empty() => {
|
||||||
let (size, comp) = self.parse_next_component();
|
let (size, comp) = self.parse_next_component();
|
||||||
self.path = &self.path[size ..];
|
self.path = &self.path[size..];
|
||||||
if comp.is_some() { return comp }
|
if comp.is_some() {
|
||||||
|
return comp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
State::Body => {
|
State::Body => {
|
||||||
self.front = State::Done;
|
self.front = State::Done;
|
||||||
}
|
}
|
||||||
State::Done => unreachable!()
|
State::Done => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
|
@ -847,8 +886,10 @@ impl<'a> DoubleEndedIterator for Components<'a> {
|
||||||
match self.back {
|
match self.back {
|
||||||
State::Body if self.path.len() > self.len_before_body() => {
|
State::Body if self.path.len() > self.len_before_body() => {
|
||||||
let (size, comp) = self.parse_next_component_back();
|
let (size, comp) = self.parse_next_component_back();
|
||||||
self.path = &self.path[.. self.path.len() - size];
|
self.path = &self.path[..self.path.len() - size];
|
||||||
if comp.is_some() { return comp }
|
if comp.is_some() {
|
||||||
|
return comp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
State::Body => {
|
State::Body => {
|
||||||
self.back = State::StartDir;
|
self.back = State::StartDir;
|
||||||
|
@ -856,29 +897,29 @@ impl<'a> DoubleEndedIterator for Components<'a> {
|
||||||
State::StartDir => {
|
State::StartDir => {
|
||||||
self.back = State::Prefix;
|
self.back = State::Prefix;
|
||||||
if self.has_physical_root {
|
if self.has_physical_root {
|
||||||
self.path = &self.path[.. self.path.len() - 1];
|
self.path = &self.path[..self.path.len() - 1];
|
||||||
return Some(Component::RootDir)
|
return Some(Component::RootDir);
|
||||||
} else if let Some(p) = self.prefix {
|
} else if let Some(p) = self.prefix {
|
||||||
if p.has_implicit_root() && !p.is_verbatim() {
|
if p.has_implicit_root() && !p.is_verbatim() {
|
||||||
return Some(Component::RootDir)
|
return Some(Component::RootDir);
|
||||||
}
|
}
|
||||||
} else if self.include_cur_dir() {
|
} else if self.include_cur_dir() {
|
||||||
self.path = &self.path[.. self.path.len() - 1];
|
self.path = &self.path[..self.path.len() - 1];
|
||||||
return Some(Component::CurDir)
|
return Some(Component::CurDir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
State::Prefix if self.prefix_len() > 0 => {
|
State::Prefix if self.prefix_len() > 0 => {
|
||||||
self.back = State::Done;
|
self.back = State::Done;
|
||||||
return Some(Component::Prefix(PrefixComponent {
|
return Some(Component::Prefix(PrefixComponent {
|
||||||
raw: unsafe { u8_slice_as_os_str(self.path) },
|
raw: unsafe { u8_slice_as_os_str(self.path) },
|
||||||
parsed: self.prefix.unwrap()
|
parsed: self.prefix.unwrap(),
|
||||||
}))
|
}));
|
||||||
}
|
}
|
||||||
State::Prefix => {
|
State::Prefix => {
|
||||||
self.back = State::Done;
|
self.back = State::Done;
|
||||||
return None
|
return None;
|
||||||
}
|
}
|
||||||
State::Done => unreachable!()
|
State::Done => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
|
@ -935,7 +976,7 @@ impl<'a> cmp::Ord for Components<'a> {
|
||||||
#[derive(Clone, Hash)]
|
#[derive(Clone, Hash)]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub struct PathBuf {
|
pub struct PathBuf {
|
||||||
inner: OsString
|
inner: OsString,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PathBuf {
|
impl PathBuf {
|
||||||
|
@ -976,10 +1017,8 @@ impl PathBuf {
|
||||||
// in the special case of `C:` on Windows, do *not* add a separator
|
// in the special case of `C:` on Windows, do *not* add a separator
|
||||||
{
|
{
|
||||||
let comps = self.components();
|
let comps = self.components();
|
||||||
if comps.prefix_len() > 0 &&
|
if comps.prefix_len() > 0 && comps.prefix_len() == comps.path.len() &&
|
||||||
comps.prefix_len() == comps.path.len() &&
|
comps.prefix.unwrap().is_drive() {
|
||||||
comps.prefix.unwrap().is_drive()
|
|
||||||
{
|
|
||||||
need_sep = false
|
need_sep = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1012,7 +1051,7 @@ impl PathBuf {
|
||||||
self.as_mut_vec().truncate(len);
|
self.as_mut_vec().truncate(len);
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
None => false
|
None => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1059,7 +1098,9 @@ impl PathBuf {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _set_extension(&mut self, extension: &OsStr) -> bool {
|
fn _set_extension(&mut self, extension: &OsStr) -> bool {
|
||||||
if self.file_name().is_none() { return false; }
|
if self.file_name().is_none() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
let mut stem = match self.file_stem() {
|
let mut stem = match self.file_stem() {
|
||||||
Some(stem) => stem.to_os_string(),
|
Some(stem) => stem.to_os_string(),
|
||||||
|
@ -1161,7 +1202,9 @@ impl<'a> IntoCow<'a, Path> for &'a Path {
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl ToOwned for Path {
|
impl ToOwned for Path {
|
||||||
type Owned = PathBuf;
|
type Owned = PathBuf;
|
||||||
fn to_owned(&self) -> PathBuf { self.to_path_buf() }
|
fn to_owned(&self) -> PathBuf {
|
||||||
|
self.to_path_buf()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
@ -1227,7 +1270,7 @@ impl Into<OsString> for PathBuf {
|
||||||
#[derive(Hash)]
|
#[derive(Hash)]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub struct Path {
|
pub struct Path {
|
||||||
inner: OsStr
|
inner: OsStr,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Path {
|
impl Path {
|
||||||
|
@ -1350,8 +1393,7 @@ impl Path {
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn is_absolute(&self) -> bool {
|
pub fn is_absolute(&self) -> bool {
|
||||||
self.has_root() &&
|
self.has_root() && (cfg!(unix) || self.prefix().is_some())
|
||||||
(cfg!(unix) || self.prefix().is_some())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A path is *relative* if it is not absolute.
|
/// A path is *relative* if it is not absolute.
|
||||||
|
@ -1398,7 +1440,7 @@ impl Path {
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn has_root(&self) -> bool {
|
pub fn has_root(&self) -> bool {
|
||||||
self.components().has_root()
|
self.components().has_root()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The path without its final component, if any.
|
/// The path without its final component, if any.
|
||||||
|
@ -1422,11 +1464,13 @@ impl Path {
|
||||||
pub fn parent(&self) -> Option<&Path> {
|
pub fn parent(&self) -> Option<&Path> {
|
||||||
let mut comps = self.components();
|
let mut comps = self.components();
|
||||||
let comp = comps.next_back();
|
let comp = comps.next_back();
|
||||||
comp.and_then(|p| match p {
|
comp.and_then(|p| {
|
||||||
Component::Normal(_) |
|
match p {
|
||||||
Component::CurDir |
|
Component::Normal(_) |
|
||||||
Component::ParentDir => Some(comps.as_path()),
|
Component::CurDir |
|
||||||
_ => None
|
Component::ParentDir => Some(comps.as_path()),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1448,9 +1492,11 @@ impl Path {
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub fn file_name(&self) -> Option<&OsStr> {
|
pub fn file_name(&self) -> Option<&OsStr> {
|
||||||
self.components().next_back().and_then(|p| match p {
|
self.components().next_back().and_then(|p| {
|
||||||
Component::Normal(p) => Some(p.as_ref()),
|
match p {
|
||||||
_ => None
|
Component::Normal(p) => Some(p.as_ref()),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1460,8 +1506,7 @@ impl Path {
|
||||||
/// returns false), then `relative_from` returns `None`.
|
/// returns false), then `relative_from` returns `None`.
|
||||||
#[unstable(feature = "path_relative_from", reason = "see #23284",
|
#[unstable(feature = "path_relative_from", reason = "see #23284",
|
||||||
issue = "23284")]
|
issue = "23284")]
|
||||||
pub fn relative_from<'a, P: ?Sized + AsRef<Path>>(&'a self, base: &'a P) -> Option<&Path>
|
pub fn relative_from<'a, P: ?Sized + AsRef<Path>>(&'a self, base: &'a P) -> Option<&Path> {
|
||||||
{
|
|
||||||
self._relative_from(base.as_ref())
|
self._relative_from(base.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1785,7 +1830,7 @@ impl fmt::Debug for Path {
|
||||||
/// Helper struct for safely printing paths with `format!()` and `{}`
|
/// Helper struct for safely printing paths with `format!()` and `{}`
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub struct Display<'a> {
|
pub struct Display<'a> {
|
||||||
path: &'a Path
|
path: &'a Path,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
@ -1828,32 +1873,44 @@ impl cmp::Ord for Path {
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl AsRef<Path> for Path {
|
impl AsRef<Path> for Path {
|
||||||
fn as_ref(&self) -> &Path { self }
|
fn as_ref(&self) -> &Path {
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl AsRef<Path> for OsStr {
|
impl AsRef<Path> for OsStr {
|
||||||
fn as_ref(&self) -> &Path { Path::new(self) }
|
fn as_ref(&self) -> &Path {
|
||||||
|
Path::new(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl AsRef<Path> for OsString {
|
impl AsRef<Path> for OsString {
|
||||||
fn as_ref(&self) -> &Path { Path::new(self) }
|
fn as_ref(&self) -> &Path {
|
||||||
|
Path::new(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl AsRef<Path> for str {
|
impl AsRef<Path> for str {
|
||||||
fn as_ref(&self) -> &Path { Path::new(self) }
|
fn as_ref(&self) -> &Path {
|
||||||
|
Path::new(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl AsRef<Path> for String {
|
impl AsRef<Path> for String {
|
||||||
fn as_ref(&self) -> &Path { Path::new(self) }
|
fn as_ref(&self) -> &Path {
|
||||||
|
Path::new(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl AsRef<Path> for PathBuf {
|
impl AsRef<Path> for PathBuf {
|
||||||
fn as_ref(&self) -> &Path { self }
|
fn as_ref(&self) -> &Path {
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -2894,20 +2951,26 @@ mod tests {
|
||||||
tp!("C:a\\b\\c", "C:d", "C:d");
|
tp!("C:a\\b\\c", "C:d", "C:d");
|
||||||
tp!("C:", r"a\b\c", r"C:a\b\c");
|
tp!("C:", r"a\b\c", r"C:a\b\c");
|
||||||
tp!("C:", r"..\a", r"C:..\a");
|
tp!("C:", r"..\a", r"C:..\a");
|
||||||
tp!("\\\\server\\share\\foo", "bar", "\\\\server\\share\\foo\\bar");
|
tp!("\\\\server\\share\\foo",
|
||||||
|
"bar",
|
||||||
|
"\\\\server\\share\\foo\\bar");
|
||||||
tp!("\\\\server\\share\\foo", "C:baz", "C:baz");
|
tp!("\\\\server\\share\\foo", "C:baz", "C:baz");
|
||||||
tp!("\\\\?\\C:\\a\\b", "C:c\\d", "C:c\\d");
|
tp!("\\\\?\\C:\\a\\b", "C:c\\d", "C:c\\d");
|
||||||
tp!("\\\\?\\C:a\\b", "C:c\\d", "C:c\\d");
|
tp!("\\\\?\\C:a\\b", "C:c\\d", "C:c\\d");
|
||||||
tp!("\\\\?\\C:\\a\\b", "C:\\c\\d", "C:\\c\\d");
|
tp!("\\\\?\\C:\\a\\b", "C:\\c\\d", "C:\\c\\d");
|
||||||
tp!("\\\\?\\foo\\bar", "baz", "\\\\?\\foo\\bar\\baz");
|
tp!("\\\\?\\foo\\bar", "baz", "\\\\?\\foo\\bar\\baz");
|
||||||
tp!("\\\\?\\UNC\\server\\share\\foo", "bar", "\\\\?\\UNC\\server\\share\\foo\\bar");
|
tp!("\\\\?\\UNC\\server\\share\\foo",
|
||||||
|
"bar",
|
||||||
|
"\\\\?\\UNC\\server\\share\\foo\\bar");
|
||||||
tp!("\\\\?\\UNC\\server\\share", "C:\\a", "C:\\a");
|
tp!("\\\\?\\UNC\\server\\share", "C:\\a", "C:\\a");
|
||||||
tp!("\\\\?\\UNC\\server\\share", "C:a", "C:a");
|
tp!("\\\\?\\UNC\\server\\share", "C:a", "C:a");
|
||||||
|
|
||||||
// Note: modified from old path API
|
// Note: modified from old path API
|
||||||
tp!("\\\\?\\UNC\\server", "foo", "\\\\?\\UNC\\server\\foo");
|
tp!("\\\\?\\UNC\\server", "foo", "\\\\?\\UNC\\server\\foo");
|
||||||
|
|
||||||
tp!("C:\\a", "\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share");
|
tp!("C:\\a",
|
||||||
|
"\\\\?\\UNC\\server\\share",
|
||||||
|
"\\\\?\\UNC\\server\\share");
|
||||||
tp!("\\\\.\\foo\\bar", "baz", "\\\\.\\foo\\bar\\baz");
|
tp!("\\\\.\\foo\\bar", "baz", "\\\\.\\foo\\bar\\baz");
|
||||||
tp!("\\\\.\\foo\\bar", "C:a", "C:a");
|
tp!("\\\\.\\foo\\bar", "C:a", "C:a");
|
||||||
// again, not sure about the following, but I'm assuming \\.\ should be verbatim
|
// again, not sure about the following, but I'm assuming \\.\ should be verbatim
|
||||||
|
@ -2960,9 +3023,15 @@ mod tests {
|
||||||
tp!("\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", true);
|
tp!("\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", true);
|
||||||
tp!("\\\\?\\C:\\a", "\\\\?\\C:\\", true);
|
tp!("\\\\?\\C:\\a", "\\\\?\\C:\\", true);
|
||||||
tp!("\\\\?\\C:\\", "\\\\?\\C:\\", false);
|
tp!("\\\\?\\C:\\", "\\\\?\\C:\\", false);
|
||||||
tp!("\\\\?\\UNC\\server\\share\\a\\b", "\\\\?\\UNC\\server\\share\\a", true);
|
tp!("\\\\?\\UNC\\server\\share\\a\\b",
|
||||||
tp!("\\\\?\\UNC\\server\\share\\a", "\\\\?\\UNC\\server\\share\\", true);
|
"\\\\?\\UNC\\server\\share\\a",
|
||||||
tp!("\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share", false);
|
true);
|
||||||
|
tp!("\\\\?\\UNC\\server\\share\\a",
|
||||||
|
"\\\\?\\UNC\\server\\share\\",
|
||||||
|
true);
|
||||||
|
tp!("\\\\?\\UNC\\server\\share",
|
||||||
|
"\\\\?\\UNC\\server\\share",
|
||||||
|
false);
|
||||||
tp!("\\\\.\\a\\b\\c", "\\\\.\\a\\b", true);
|
tp!("\\\\.\\a\\b\\c", "\\\\.\\a\\b", true);
|
||||||
tp!("\\\\.\\a\\b", "\\\\.\\a\\", true);
|
tp!("\\\\.\\a\\b", "\\\\.\\a\\", true);
|
||||||
tp!("\\\\.\\a", "\\\\.\\a", false);
|
tp!("\\\\.\\a", "\\\\.\\a", false);
|
||||||
|
@ -3028,7 +3097,7 @@ mod tests {
|
||||||
tfe!(".", "foo", ".", false);
|
tfe!(".", "foo", ".", false);
|
||||||
tfe!("foo/", "bar", "foo.bar", true);
|
tfe!("foo/", "bar", "foo.bar", true);
|
||||||
tfe!("foo/.", "bar", "foo.bar", true);
|
tfe!("foo/.", "bar", "foo.bar", true);
|
||||||
tfe!("..", "foo", "..", false);
|
tfe!("..", "foo", "..", false);
|
||||||
tfe!("foo/..", "bar", "foo/..", false);
|
tfe!("foo/..", "bar", "foo/..", false);
|
||||||
tfe!("/", "foo", "/", false);
|
tfe!("/", "foo", "/", false);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue