Provide an implementation of DoubleEndedIterator for the results of &[T]::split and &[T]::rsplit
This makes the splitting functions in std::slice return DoubleEndedIterators. Unfortunately,
splitn and rsplitn cannot provide such an interface and so must return different types. As a
result, the following changes were made:
* RevSplits was removed in favor of explicitly using Rev
* Splits can no longer bound the number of splits done
* Splits now implements DoubleEndedIterator
* SplitsN was added, taking the role of what both Splits and RevSplits used to be
* rsplit returns Rev<Splits<'a, T>> instead of RevSplits<'a, T>
* splitn returns SplitsN<'a, T> instead of Splits<'a, T>
* rsplitn returns SplitsN<'a, T> instead of RevSplits<'a, T>
All functions that were previously implemented on each return value still are, so outside of changing
of type annotations, existing code should work out of the box. In the rare case that code relied
on the return types of split and splitn or of rsplit and rsplitn being the same, the previous
behavior can be emulated by calling splitn or rsplitn with a bount of uint::MAX.
The value of this change comes in multiple parts:
* Consistency. The splitting code in std::str is structured similarly to the new slice splitting code,
having separate CharSplits and CharSplitsN types.
* Smaller API. Although this commit doesn't implement it, using a DoubleEndedIterator for splitting
means that rsplit, path::RevComponents, path::RevStrComponents, Path::rev_components, and
Path::rev_str_components are no longer needed - they can be emulated simply with .rev().
* Power. DoubleEndedIterators are able to traverse the list from both sides at once instead of only
forwards or backwards.
* Efficiency. For the common case of using split instead of splitn, the iterator is slightly smaller
and slightly faster.
[breaking-change]
2014-04-20 21:28:38 -05:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-09-01 12:44:07 -07:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! POSIX file path handling
|
|
|
|
|
|
|
|
use c_str::{CString, ToCStr};
|
|
|
|
use clone::Clone;
|
2014-07-09 18:16:16 -07:00
|
|
|
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
|
2014-07-11 10:12:38 -07:00
|
|
|
use collections::{Collection, MutableSeq};
|
2013-09-01 12:44:07 -07:00
|
|
|
use from_str::FromStr;
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 18:50:12 -07:00
|
|
|
use hash;
|
2014-02-25 08:03:41 -08:00
|
|
|
use io::Writer;
|
2014-05-22 10:40:07 -07:00
|
|
|
use iter::{DoubleEndedIterator, AdditiveIterator, Extendable, Iterator, Map};
|
2013-09-01 12:44:07 -07:00
|
|
|
use option::{Option, None, Some};
|
|
|
|
use str::Str;
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 18:50:12 -07:00
|
|
|
use str;
|
Provide an implementation of DoubleEndedIterator for the results of &[T]::split and &[T]::rsplit
This makes the splitting functions in std::slice return DoubleEndedIterators. Unfortunately,
splitn and rsplitn cannot provide such an interface and so must return different types. As a
result, the following changes were made:
* RevSplits was removed in favor of explicitly using Rev
* Splits can no longer bound the number of splits done
* Splits now implements DoubleEndedIterator
* SplitsN was added, taking the role of what both Splits and RevSplits used to be
* rsplit returns Rev<Splits<'a, T>> instead of RevSplits<'a, T>
* splitn returns SplitsN<'a, T> instead of Splits<'a, T>
* rsplitn returns SplitsN<'a, T> instead of RevSplits<'a, T>
All functions that were previously implemented on each return value still are, so outside of changing
of type annotations, existing code should work out of the box. In the rare case that code relied
on the return types of split and splitn or of rsplit and rsplitn being the same, the previous
behavior can be emulated by calling splitn or rsplitn with a bount of uint::MAX.
The value of this change comes in multiple parts:
* Consistency. The splitting code in std::str is structured similarly to the new slice splitting code,
having separate CharSplits and CharSplitsN types.
* Smaller API. Although this commit doesn't implement it, using a DoubleEndedIterator for splitting
means that rsplit, path::RevComponents, path::RevStrComponents, Path::rev_components, and
Path::rev_str_components are no longer needed - they can be emulated simply with .rev().
* Power. DoubleEndedIterators are able to traverse the list from both sides at once instead of only
forwards or backwards.
* Efficiency. For the common case of using split instead of splitn, the iterator is slightly smaller
and slightly faster.
[breaking-change]
2014-04-20 21:28:38 -05:00
|
|
|
use slice::{CloneableVector, Splits, Vector, VectorVector,
|
2014-06-06 10:27:49 -07:00
|
|
|
ImmutableEqVector, ImmutableVector};
|
2014-04-12 20:42:17 +10:00
|
|
|
use vec::Vec;
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
use super::{BytesContainer, GenericPath, GenericPathUnsafe};
|
2013-09-01 12:44:07 -07:00
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
/// Iterator that yields successive components of a Path as &[u8]
|
2014-01-14 22:32:24 -05:00
|
|
|
pub type Components<'a> = Splits<'a, u8>;
|
2013-09-26 17:21:59 -07:00
|
|
|
|
|
|
|
/// Iterator that yields successive components of a Path as Option<&str>
|
2014-01-14 22:32:24 -05:00
|
|
|
pub type StrComponents<'a> = Map<'a, &'a [u8], Option<&'a str>,
|
|
|
|
Components<'a>>;
|
2013-09-01 12:44:07 -07:00
|
|
|
|
|
|
|
/// Represents a POSIX file path
|
2014-03-05 01:19:14 -05:00
|
|
|
#[deriving(Clone)]
|
2013-09-01 12:44:07 -07:00
|
|
|
pub struct Path {
|
2014-04-12 20:42:17 +10:00
|
|
|
repr: Vec<u8>, // assumed to never be empty or contain NULs
|
2014-03-27 15:09:47 -07:00
|
|
|
sepidx: Option<uint> // index of the final separator in repr
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The standard path separator character
|
2014-01-18 09:17:44 -08:00
|
|
|
pub static SEP: char = '/';
|
|
|
|
|
|
|
|
/// The standard path separator byte
|
|
|
|
pub static SEP_BYTE: u8 = SEP as u8;
|
2013-09-01 12:44:07 -07:00
|
|
|
|
|
|
|
/// Returns whether the given byte is a path separator
|
|
|
|
#[inline]
|
2013-10-07 19:16:58 -07:00
|
|
|
pub fn is_sep_byte(u: &u8) -> bool {
|
2014-01-18 09:17:44 -08:00
|
|
|
*u as char == SEP
|
2013-10-07 19:16:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether the given char is a path separator
|
|
|
|
#[inline]
|
|
|
|
pub fn is_sep(c: char) -> bool {
|
2014-01-18 09:17:44 -08:00
|
|
|
c == SEP
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
2014-05-29 17:45:07 -07:00
|
|
|
impl PartialEq for Path {
|
2013-08-27 00:51:08 -07:00
|
|
|
#[inline]
|
2013-09-01 12:44:07 -07:00
|
|
|
fn eq(&self, other: &Path) -> bool {
|
|
|
|
self.repr == other.repr
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 16:30:45 -04:00
|
|
|
|
2014-05-31 10:43:52 -07:00
|
|
|
impl Eq for Path {}
|
2013-09-01 12:44:07 -07:00
|
|
|
|
2014-07-09 18:16:16 -07:00
|
|
|
impl PartialOrd for Path {
|
|
|
|
fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for Path {
|
|
|
|
fn cmp(&self, other: &Path) -> Ordering {
|
|
|
|
self.repr.cmp(&other.repr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
impl FromStr for Path {
|
|
|
|
fn from_str(s: &str) -> Option<Path> {
|
2013-12-03 19:15:12 -08:00
|
|
|
Path::new_opt(s)
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 14:15:11 -07:00
|
|
|
// FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
|
|
|
|
// we cannot usefully take ToCStr arguments by reference (without forcing an
|
|
|
|
// additional & around &str). So we are instead temporarily adding an instance
|
|
|
|
// for &Path, so that we can take ToCStr as owned. When DST lands, the &Path
|
|
|
|
// instance should be removed, and arguments bound by ToCStr should be passed by
|
|
|
|
// reference.
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
impl ToCStr for Path {
|
|
|
|
#[inline]
|
|
|
|
fn to_c_str(&self) -> CString {
|
|
|
|
// The Path impl guarantees no internal NUL
|
2014-05-05 14:15:11 -07:00
|
|
|
unsafe { self.to_c_str_unchecked() }
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn to_c_str_unchecked(&self) -> CString {
|
|
|
|
self.as_vec().to_c_str_unchecked()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 14:15:11 -07:00
|
|
|
impl<'a> ToCStr for &'a Path {
|
|
|
|
#[inline]
|
|
|
|
fn to_c_str(&self) -> CString {
|
|
|
|
(*self).to_c_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn to_c_str_unchecked(&self) -> CString {
|
|
|
|
(*self).to_c_str_unchecked()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 18:50:12 -07:00
|
|
|
impl<S: hash::Writer> hash::Hash<S> for Path {
|
2013-09-26 12:58:56 -07:00
|
|
|
#[inline]
|
2014-03-10 20:47:25 -07:00
|
|
|
fn hash(&self, state: &mut S) {
|
|
|
|
self.repr.hash(state)
|
2013-09-26 12:58:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-07 19:16:58 -07:00
|
|
|
impl BytesContainer for Path {
|
|
|
|
#[inline]
|
|
|
|
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
|
|
|
|
self.as_vec()
|
|
|
|
}
|
|
|
|
#[inline]
|
2014-04-12 20:42:17 +10:00
|
|
|
fn container_into_owned_bytes(self) -> Vec<u8> {
|
2013-10-07 19:16:58 -07:00
|
|
|
self.into_vec()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
impl<'a> BytesContainer for &'a Path {
|
2013-10-07 19:16:58 -07:00
|
|
|
#[inline]
|
|
|
|
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
|
|
|
|
self.as_vec()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
impl GenericPathUnsafe for Path {
|
2013-12-03 19:15:12 -08:00
|
|
|
unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::normalize(path.container_as_bytes());
|
2013-09-01 12:44:07 -07:00
|
|
|
assert!(!path.is_empty());
|
2014-04-12 20:42:17 +10:00
|
|
|
let idx = path.as_slice().rposition_elem(&SEP_BYTE);
|
2013-09-01 12:44:07 -07:00
|
|
|
Path{ repr: path, sepidx: idx }
|
|
|
|
}
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
|
|
|
|
let filename = filename.container_as_bytes();
|
2013-09-01 12:44:07 -07:00
|
|
|
match self.sepidx {
|
2014-06-18 20:25:36 +02:00
|
|
|
None if b".." == self.repr.as_slice() => {
|
2014-04-12 20:42:17 +10:00
|
|
|
let mut v = Vec::with_capacity(3 + filename.len());
|
2013-09-01 12:44:07 -07:00
|
|
|
v.push_all(dot_dot_static);
|
2014-01-18 09:17:44 -08:00
|
|
|
v.push(SEP_BYTE);
|
2013-09-01 12:44:07 -07:00
|
|
|
v.push_all(filename);
|
2014-04-12 20:42:17 +10:00
|
|
|
// FIXME: this is slow
|
|
|
|
self.repr = Path::normalize(v.as_slice());
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.repr = Path::normalize(filename);
|
|
|
|
}
|
2014-06-18 20:25:36 +02:00
|
|
|
Some(idx) if self.repr.slice_from(idx+1) == b".." => {
|
2014-04-12 20:42:17 +10:00
|
|
|
let mut v = Vec::with_capacity(self.repr.len() + 1 + filename.len());
|
|
|
|
v.push_all(self.repr.as_slice());
|
2014-01-18 09:17:44 -08:00
|
|
|
v.push(SEP_BYTE);
|
2013-09-01 12:44:07 -07:00
|
|
|
v.push_all(filename);
|
2014-04-12 20:42:17 +10:00
|
|
|
// FIXME: this is slow
|
|
|
|
self.repr = Path::normalize(v.as_slice());
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
Some(idx) => {
|
2014-04-12 20:42:17 +10:00
|
|
|
let mut v = Vec::with_capacity(idx + 1 + filename.len());
|
2013-09-01 12:44:07 -07:00
|
|
|
v.push_all(self.repr.slice_to(idx+1));
|
|
|
|
v.push_all(filename);
|
2014-04-12 20:42:17 +10:00
|
|
|
// FIXME: this is slow
|
|
|
|
self.repr = Path::normalize(v.as_slice());
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
}
|
2014-04-12 20:42:17 +10:00
|
|
|
self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
|
|
|
|
let path = path.container_as_bytes();
|
2013-09-01 12:44:07 -07:00
|
|
|
if !path.is_empty() {
|
2014-01-18 09:17:44 -08:00
|
|
|
if path[0] == SEP_BYTE {
|
2013-09-01 12:44:07 -07:00
|
|
|
self.repr = Path::normalize(path);
|
|
|
|
} else {
|
2014-04-12 20:42:17 +10:00
|
|
|
let mut v = Vec::with_capacity(self.repr.len() + path.len() + 1);
|
|
|
|
v.push_all(self.repr.as_slice());
|
2014-01-18 09:17:44 -08:00
|
|
|
v.push(SEP_BYTE);
|
2013-09-01 12:44:07 -07:00
|
|
|
v.push_all(path);
|
2014-04-12 20:42:17 +10:00
|
|
|
// FIXME: this is slow
|
|
|
|
self.repr = Path::normalize(v.as_slice());
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
2014-04-12 20:42:17 +10:00
|
|
|
self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericPath for Path {
|
|
|
|
#[inline]
|
|
|
|
fn as_vec<'a>(&'a self) -> &'a [u8] {
|
|
|
|
self.repr.as_slice()
|
|
|
|
}
|
|
|
|
|
2014-04-12 20:42:17 +10:00
|
|
|
fn into_vec(self) -> Vec<u8> {
|
2013-10-05 19:49:32 -07:00
|
|
|
self.repr
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
fn dirname<'a>(&'a self) -> &'a [u8] {
|
|
|
|
match self.sepidx {
|
2014-06-18 20:25:36 +02:00
|
|
|
None if b".." == self.repr.as_slice() => self.repr.as_slice(),
|
2013-09-01 12:44:07 -07:00
|
|
|
None => dot_static,
|
|
|
|
Some(0) => self.repr.slice_to(1),
|
2014-06-18 20:25:36 +02:00
|
|
|
Some(idx) if self.repr.slice_from(idx+1) == b".." => self.repr.as_slice(),
|
2013-09-01 12:44:07 -07:00
|
|
|
Some(idx) => self.repr.slice_to(idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
fn filename<'a>(&'a self) -> Option<&'a [u8]> {
|
2013-09-01 12:44:07 -07:00
|
|
|
match self.sepidx {
|
2014-06-18 20:25:36 +02:00
|
|
|
None if b"." == self.repr.as_slice() ||
|
|
|
|
b".." == self.repr.as_slice() => None,
|
2013-09-26 17:21:59 -07:00
|
|
|
None => Some(self.repr.as_slice()),
|
2014-06-18 20:25:36 +02:00
|
|
|
Some(idx) if self.repr.slice_from(idx+1) == b".." => None,
|
2013-09-26 17:21:59 -07:00
|
|
|
Some(0) if self.repr.slice_from(1).is_empty() => None,
|
|
|
|
Some(idx) => Some(self.repr.slice_from(idx+1))
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 22:05:14 -07:00
|
|
|
fn pop(&mut self) -> bool {
|
2013-09-01 12:44:07 -07:00
|
|
|
match self.sepidx {
|
2014-06-18 20:25:36 +02:00
|
|
|
None if b"." == self.repr.as_slice() => false,
|
2013-09-01 12:44:07 -07:00
|
|
|
None => {
|
2014-04-12 20:42:17 +10:00
|
|
|
self.repr = vec!['.' as u8];
|
2013-09-01 12:44:07 -07:00
|
|
|
self.sepidx = None;
|
2013-10-09 22:05:14 -07:00
|
|
|
true
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
2014-06-18 20:25:36 +02:00
|
|
|
Some(0) if b"/" == self.repr.as_slice() => false,
|
2013-09-01 12:44:07 -07:00
|
|
|
Some(idx) => {
|
|
|
|
if idx == 0 {
|
|
|
|
self.repr.truncate(idx+1);
|
|
|
|
} else {
|
|
|
|
self.repr.truncate(idx);
|
|
|
|
}
|
2014-04-12 20:42:17 +10:00
|
|
|
self.sepidx = self.repr.as_slice().rposition_elem(&SEP_BYTE);
|
2013-10-09 22:05:14 -07:00
|
|
|
true
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
fn root_path(&self) -> Option<Path> {
|
|
|
|
if self.is_absolute() {
|
2013-12-03 19:15:12 -08:00
|
|
|
Some(Path::new("/"))
|
2013-09-26 17:21:59 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[inline]
|
|
|
|
fn is_absolute(&self) -> bool {
|
2014-04-12 20:42:17 +10:00
|
|
|
*self.repr.get(0) == SEP_BYTE
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_ancestor_of(&self, other: &Path) -> bool {
|
|
|
|
if self.is_absolute() != other.is_absolute() {
|
|
|
|
false
|
|
|
|
} else {
|
2013-11-23 11:18:51 +01:00
|
|
|
let mut ita = self.components();
|
|
|
|
let mut itb = other.components();
|
2014-06-18 20:25:36 +02:00
|
|
|
if b"." == self.repr.as_slice() {
|
2014-01-15 14:39:08 -05:00
|
|
|
return match itb.next() {
|
|
|
|
None => true,
|
2014-06-18 20:25:36 +02:00
|
|
|
Some(b) => b != b".."
|
2014-01-15 14:39:08 -05:00
|
|
|
};
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
loop {
|
|
|
|
match (ita.next(), itb.next()) {
|
|
|
|
(None, _) => break,
|
2013-10-02 20:26:28 -07:00
|
|
|
(Some(a), Some(b)) if a == b => { continue },
|
2014-06-18 20:25:36 +02:00
|
|
|
(Some(a), _) if a == b".." => {
|
2013-09-01 12:44:07 -07:00
|
|
|
// if ita contains only .. components, it's an ancestor
|
2014-06-18 20:25:36 +02:00
|
|
|
return ita.all(|x| x == b"..");
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
_ => return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path_relative_from(&self, base: &Path) -> Option<Path> {
|
|
|
|
if self.is_absolute() != base.is_absolute() {
|
|
|
|
if self.is_absolute() {
|
|
|
|
Some(self.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
2013-11-23 11:18:51 +01:00
|
|
|
let mut ita = self.components();
|
|
|
|
let mut itb = base.components();
|
2014-04-12 20:42:17 +10:00
|
|
|
let mut comps = vec![];
|
2013-09-01 12:44:07 -07:00
|
|
|
loop {
|
|
|
|
match (ita.next(), itb.next()) {
|
|
|
|
(None, None) => break,
|
|
|
|
(Some(a), None) => {
|
|
|
|
comps.push(a);
|
2014-03-20 14:12:56 +01:00
|
|
|
comps.extend(ita.by_ref());
|
2013-09-01 12:44:07 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
(None, _) => comps.push(dot_dot_static),
|
|
|
|
(Some(a), Some(b)) if comps.is_empty() && a == b => (),
|
2014-06-18 20:25:36 +02:00
|
|
|
(Some(a), Some(b)) if b == b"." => comps.push(a),
|
|
|
|
(Some(_), Some(b)) if b == b".." => return None,
|
2013-09-01 12:44:07 -07:00
|
|
|
(Some(a), Some(_)) => {
|
|
|
|
comps.push(dot_dot_static);
|
|
|
|
for _ in itb {
|
|
|
|
comps.push(dot_dot_static);
|
|
|
|
}
|
|
|
|
comps.push(a);
|
2014-03-20 14:12:56 +01:00
|
|
|
comps.extend(ita.by_ref());
|
2013-09-01 12:44:07 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-12 20:42:17 +10:00
|
|
|
Some(Path::new(comps.as_slice().connect_vec(&SEP_BYTE)))
|
2013-10-05 19:49:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ends_with_path(&self, child: &Path) -> bool {
|
|
|
|
if !child.is_relative() { return false; }
|
Deprecate the rev_iter pattern in all places where a DoubleEndedIterator is provided (everywhere but treemap)
This commit deprecates rev_iter, mut_rev_iter, move_rev_iter everywhere (except treemap) and also
deprecates related functions like rsplit, rev_components, and rev_str_components. In every case,
these functions can be replaced with the non-reversed form followed by a call to .rev(). To make this
more concrete, a translation table for all functional changes necessary follows:
* container.rev_iter() -> container.iter().rev()
* container.mut_rev_iter() -> container.mut_iter().rev()
* container.move_rev_iter() -> container.move_iter().rev()
* sliceorstr.rsplit(sep) -> sliceorstr.split(sep).rev()
* path.rev_components() -> path.components().rev()
* path.rev_str_components() -> path.str_components().rev()
In terms of the type system, this change also deprecates any specialized reversed iterator types (except
in treemap), opting instead to use Rev directly if any type annotations are needed. However, since
methods directly returning reversed iterators are now discouraged, the need for such annotations should
be small. However, in those cases, the general pattern for conversion is to take whatever follows Rev in
the original reversed name and surround it with Rev<>:
* RevComponents<'a> -> Rev<Components<'a>>
* RevStrComponents<'a> -> Rev<StrComponents<'a>>
* RevItems<'a, T> -> Rev<Items<'a, T>>
* etc.
The reasoning behind this change is that it makes the standard API much simpler without reducing readability,
performance, or power. The presence of functions such as rev_iter adds more boilerplate code to libraries
(all of which simply call .iter().rev()), clutters up the documentation, and only helps code by saving two
characters. Additionally, the numerous type synonyms that were used to make the type signatures look nice
like RevItems add even more boilerplate and clutter up the docs even more. With this change, all that cruft
goes away.
[breaking-change]
2014-04-20 23:59:12 -05:00
|
|
|
let mut selfit = self.components().rev();
|
|
|
|
let mut childit = child.components().rev();
|
2013-10-05 19:49:32 -07:00
|
|
|
loop {
|
|
|
|
match (selfit.next(), childit.next()) {
|
|
|
|
(Some(a), Some(b)) => if a != b { return false; },
|
|
|
|
(Some(_), None) => break,
|
|
|
|
(None, Some(_)) => return false,
|
|
|
|
(None, None) => break
|
|
|
|
}
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
2013-10-05 19:49:32 -07:00
|
|
|
true
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Path {
|
2013-10-05 19:49:32 -07:00
|
|
|
/// Returns a new Path from a byte vector or string
|
2013-09-01 12:44:07 -07:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
Remove std::condition
This has been a long time coming. Conditions in rust were initially envisioned
as being a good alternative to error code return pattern. The idea is that all
errors are fatal-by-default, and you can opt-in to handling the error by
registering an error handler.
While sounding nice, conditions ended up having some unforseen shortcomings:
* Actually handling an error has some very awkward syntax:
let mut result = None;
let mut answer = None;
io::io_error::cond.trap(|e| { result = Some(e) }).inside(|| {
answer = Some(some_io_operation());
});
match result {
Some(err) => { /* hit an I/O error */ }
None => {
let answer = answer.unwrap();
/* deal with the result of I/O */
}
}
This pattern can certainly use functions like io::result, but at its core
actually handling conditions is fairly difficult
* The "zero value" of a function is often confusing. One of the main ideas
behind using conditions was to change the signature of I/O functions. Instead
of read_be_u32() returning a result, it returned a u32. Errors were notified
via a condition, and if you caught the condition you understood that the "zero
value" returned is actually a garbage value. These zero values are often
difficult to understand, however.
One case of this is the read_bytes() function. The function takes an integer
length of the amount of bytes to read, and returns an array of that size. The
array may actually be shorter, however, if an error occurred.
Another case is fs::stat(). The theoretical "zero value" is a blank stat
struct, but it's a little awkward to create and return a zero'd out stat
struct on a call to stat().
In general, the return value of functions that can raise error are much more
natural when using a Result as opposed to an always-usable zero-value.
* Conditions impose a necessary runtime requirement on *all* I/O. In theory I/O
is as simple as calling read() and write(), but using conditions imposed the
restriction that a rust local task was required if you wanted to catch errors
with I/O. While certainly an surmountable difficulty, this was always a bit of
a thorn in the side of conditions.
* Functions raising conditions are not always clear that they are raising
conditions. This suffers a similar problem to exceptions where you don't
actually know whether a function raises a condition or not. The documentation
likely explains, but if someone retroactively adds a condition to a function
there's nothing forcing upstream users to acknowledge a new point of task
failure.
* Libaries using I/O are not guaranteed to correctly raise on conditions when an
error occurs. In developing various I/O libraries, it's much easier to just
return `None` from a read rather than raising an error. The silent contract of
"don't raise on EOF" was a little difficult to understand and threw a wrench
into the answer of the question "when do I raise a condition?"
Many of these difficulties can be overcome through documentation, examples, and
general practice. In the end, all of these difficulties added together ended up
being too overwhelming and improving various aspects didn't end up helping that
much.
A result-based I/O error handling strategy also has shortcomings, but the
cognitive burden is much smaller. The tooling necessary to make this strategy as
usable as conditions were is much smaller than the tooling necessary for
conditions.
Perhaps conditions may manifest themselves as a future entity, but for now
we're going to remove them from the standard library.
Closes #9795
Closes #8968
2014-02-04 19:02:10 -08:00
|
|
|
/// Fails the task if the vector contains a NUL.
|
2013-09-01 12:44:07 -07:00
|
|
|
#[inline]
|
2013-12-03 19:15:12 -08:00
|
|
|
pub fn new<T: BytesContainer>(path: T) -> Path {
|
|
|
|
GenericPath::new(path)
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
/// Returns a new Path from a byte vector or string, if possible
|
2013-09-26 00:54:30 -07:00
|
|
|
#[inline]
|
2013-12-03 19:15:12 -08:00
|
|
|
pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
|
|
|
|
GenericPath::new_opt(path)
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a normalized byte vector representation of a path, by removing all empty
|
|
|
|
/// components, and unnecessary . and .. components.
|
2014-04-12 20:42:17 +10:00
|
|
|
fn normalize<V: Vector<u8>+CloneableVector<u8>>(v: V) -> Vec<u8> {
|
2013-09-01 12:44:07 -07:00
|
|
|
// borrowck is being very picky
|
|
|
|
let val = {
|
2014-01-18 09:17:44 -08:00
|
|
|
let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;
|
2013-09-01 12:44:07 -07:00
|
|
|
let v_ = if is_abs { v.as_slice().slice_from(1) } else { v.as_slice() };
|
2013-08-27 00:51:08 -07:00
|
|
|
let comps = normalize_helper(v_, is_abs);
|
2013-09-01 12:44:07 -07:00
|
|
|
match comps {
|
|
|
|
None => None,
|
|
|
|
Some(comps) => {
|
|
|
|
if is_abs && comps.is_empty() {
|
2014-04-12 20:42:17 +10:00
|
|
|
Some(vec![SEP_BYTE])
|
2013-09-01 12:44:07 -07:00
|
|
|
} else {
|
|
|
|
let n = if is_abs { comps.len() } else { comps.len() - 1} +
|
|
|
|
comps.iter().map(|v| v.len()).sum();
|
2014-04-12 20:42:17 +10:00
|
|
|
let mut v = Vec::with_capacity(n);
|
2013-09-01 12:44:07 -07:00
|
|
|
let mut it = comps.move_iter();
|
|
|
|
if !is_abs {
|
|
|
|
match it.next() {
|
|
|
|
None => (),
|
|
|
|
Some(comp) => v.push_all(comp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for comp in it {
|
2014-01-18 09:17:44 -08:00
|
|
|
v.push(SEP_BYTE);
|
2013-09-01 12:44:07 -07:00
|
|
|
v.push_all(comp);
|
|
|
|
}
|
|
|
|
Some(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match val {
|
2014-04-12 20:42:17 +10:00
|
|
|
None => Vec::from_slice(v.as_slice()),
|
2013-09-01 12:44:07 -07:00
|
|
|
Some(val) => val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator that yields each component of the path in turn.
|
|
|
|
/// Does not distinguish between absolute and relative paths, e.g.
|
|
|
|
/// /a/b/c and a/b/c yield the same set of components.
|
|
|
|
/// A path of "/" yields no components. A path of "." yields one component.
|
2014-01-14 22:32:24 -05:00
|
|
|
pub fn components<'a>(&'a self) -> Components<'a> {
|
2014-04-12 20:42:17 +10:00
|
|
|
let v = if *self.repr.get(0) == SEP_BYTE {
|
2013-09-01 12:44:07 -07:00
|
|
|
self.repr.slice_from(1)
|
|
|
|
} else { self.repr.as_slice() };
|
2013-11-23 11:18:51 +01:00
|
|
|
let mut ret = v.split(is_sep_byte);
|
2013-09-01 12:44:07 -07:00
|
|
|
if v.is_empty() {
|
|
|
|
// consume the empty "" component
|
|
|
|
ret.next();
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
2013-09-26 17:21:59 -07:00
|
|
|
|
|
|
|
/// Returns an iterator that yields each component of the path as Option<&str>.
|
2013-11-23 11:18:51 +01:00
|
|
|
/// See components() for details.
|
2014-01-14 22:32:24 -05:00
|
|
|
pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
|
2013-12-23 17:30:49 +01:00
|
|
|
self.components().map(str::from_utf8)
|
2013-09-26 17:21:59 -07:00
|
|
|
}
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// None result means the byte vector didn't need normalizing
|
2014-04-12 20:42:17 +10:00
|
|
|
fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<Vec<&'a [u8]>> {
|
2013-09-01 12:44:07 -07:00
|
|
|
if is_abs && v.as_slice().is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
2014-04-12 20:42:17 +10:00
|
|
|
let mut comps: Vec<&'a [u8]> = vec![];
|
2013-09-01 12:44:07 -07:00
|
|
|
let mut n_up = 0u;
|
|
|
|
let mut changed = false;
|
2013-11-23 11:18:51 +01:00
|
|
|
for comp in v.split(is_sep_byte) {
|
2013-09-01 12:44:07 -07:00
|
|
|
if comp.is_empty() { changed = true }
|
2014-06-18 20:25:36 +02:00
|
|
|
else if comp == b"." { changed = true }
|
|
|
|
else if comp == b".." {
|
2013-09-01 12:44:07 -07:00
|
|
|
if is_abs && comps.is_empty() { changed = true }
|
|
|
|
else if comps.len() == n_up { comps.push(dot_dot_static); n_up += 1 }
|
2013-12-23 16:20:52 +01:00
|
|
|
else { comps.pop().unwrap(); changed = true }
|
2013-09-01 12:44:07 -07:00
|
|
|
} else { comps.push(comp) }
|
|
|
|
}
|
|
|
|
if changed {
|
|
|
|
if comps.is_empty() && !is_abs {
|
2014-06-18 20:25:36 +02:00
|
|
|
if v == b"." {
|
2013-09-01 12:44:07 -07:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
comps.push(dot_static);
|
|
|
|
}
|
|
|
|
Some(comps)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
static dot_static: &'static [u8] = b".";
|
|
|
|
static dot_dot_static: &'static [u8] = b"..";
|
2013-09-01 12:44:07 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2014-01-06 22:33:37 -08:00
|
|
|
use prelude::*;
|
2013-09-01 12:44:07 -07:00
|
|
|
use super::*;
|
|
|
|
use str;
|
2014-04-15 18:17:48 -07:00
|
|
|
use str::StrSlice;
|
2013-09-01 12:44:07 -07:00
|
|
|
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $exp:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(path.as_str() == Some($exp));
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $exp:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(path.as_vec() == $exp);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_paths() {
|
2013-10-05 19:49:32 -07:00
|
|
|
let empty: &[u8] = [];
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: Path::new(empty), b".");
|
|
|
|
t!(v: Path::new(b"/"), b"/");
|
|
|
|
t!(v: Path::new(b"a/b/c"), b"a/b/c");
|
|
|
|
t!(v: Path::new(b"a/b/c\xFF"), b"a/b/c\xFF");
|
|
|
|
t!(v: Path::new(b"\xFF/../foo\x80"), b"foo\x80");
|
|
|
|
let p = Path::new(b"a/b/c\xFF");
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(p.as_str() == None);
|
2013-09-01 12:44:07 -07:00
|
|
|
|
2013-12-03 19:15:12 -08:00
|
|
|
t!(s: Path::new(""), ".");
|
|
|
|
t!(s: Path::new("/"), "/");
|
|
|
|
t!(s: Path::new("hi"), "hi");
|
|
|
|
t!(s: Path::new("hi/"), "hi");
|
|
|
|
t!(s: Path::new("/lib"), "/lib");
|
|
|
|
t!(s: Path::new("/lib/"), "/lib");
|
|
|
|
t!(s: Path::new("hi/there"), "hi/there");
|
|
|
|
t!(s: Path::new("hi/there.txt"), "hi/there.txt");
|
|
|
|
|
|
|
|
t!(s: Path::new("hi/there/"), "hi/there");
|
|
|
|
t!(s: Path::new("hi/../there"), "there");
|
|
|
|
t!(s: Path::new("../hi/there"), "../hi/there");
|
|
|
|
t!(s: Path::new("/../hi/there"), "/hi/there");
|
|
|
|
t!(s: Path::new("foo/.."), ".");
|
|
|
|
t!(s: Path::new("/foo/.."), "/");
|
|
|
|
t!(s: Path::new("/foo/../.."), "/");
|
|
|
|
t!(s: Path::new("/foo/../../bar"), "/bar");
|
|
|
|
t!(s: Path::new("/./hi/./there/."), "/hi/there");
|
|
|
|
t!(s: Path::new("/./hi/./there/./.."), "/hi");
|
|
|
|
t!(s: Path::new("foo/../.."), "..");
|
|
|
|
t!(s: Path::new("foo/../../.."), "../..");
|
|
|
|
t!(s: Path::new("foo/../../bar"), "../bar");
|
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
assert_eq!(Path::new(b"foo/bar").into_vec().as_slice(), b"foo/bar");
|
|
|
|
assert_eq!(Path::new(b"/foo/../../bar").into_vec().as_slice(),
|
|
|
|
b"/bar");
|
2013-09-01 12:44:07 -07:00
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
let p = Path::new(b"foo/bar\x80");
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(p.as_str() == None);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
2013-09-26 00:54:30 -07:00
|
|
|
#[test]
|
|
|
|
fn test_opt_paths() {
|
2014-06-18 20:25:36 +02:00
|
|
|
assert!(Path::new_opt(b"foo/bar\0") == None);
|
|
|
|
t!(v: Path::new_opt(b"foo/bar").unwrap(), b"foo/bar");
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(Path::new_opt("foo/bar\0") == None);
|
2013-12-03 19:15:12 -08:00
|
|
|
t!(s: Path::new_opt("foo/bar").unwrap(), "foo/bar");
|
2013-09-26 00:54:30 -07:00
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_null_byte() {
|
Remove std::condition
This has been a long time coming. Conditions in rust were initially envisioned
as being a good alternative to error code return pattern. The idea is that all
errors are fatal-by-default, and you can opt-in to handling the error by
registering an error handler.
While sounding nice, conditions ended up having some unforseen shortcomings:
* Actually handling an error has some very awkward syntax:
let mut result = None;
let mut answer = None;
io::io_error::cond.trap(|e| { result = Some(e) }).inside(|| {
answer = Some(some_io_operation());
});
match result {
Some(err) => { /* hit an I/O error */ }
None => {
let answer = answer.unwrap();
/* deal with the result of I/O */
}
}
This pattern can certainly use functions like io::result, but at its core
actually handling conditions is fairly difficult
* The "zero value" of a function is often confusing. One of the main ideas
behind using conditions was to change the signature of I/O functions. Instead
of read_be_u32() returning a result, it returned a u32. Errors were notified
via a condition, and if you caught the condition you understood that the "zero
value" returned is actually a garbage value. These zero values are often
difficult to understand, however.
One case of this is the read_bytes() function. The function takes an integer
length of the amount of bytes to read, and returns an array of that size. The
array may actually be shorter, however, if an error occurred.
Another case is fs::stat(). The theoretical "zero value" is a blank stat
struct, but it's a little awkward to create and return a zero'd out stat
struct on a call to stat().
In general, the return value of functions that can raise error are much more
natural when using a Result as opposed to an always-usable zero-value.
* Conditions impose a necessary runtime requirement on *all* I/O. In theory I/O
is as simple as calling read() and write(), but using conditions imposed the
restriction that a rust local task was required if you wanted to catch errors
with I/O. While certainly an surmountable difficulty, this was always a bit of
a thorn in the side of conditions.
* Functions raising conditions are not always clear that they are raising
conditions. This suffers a similar problem to exceptions where you don't
actually know whether a function raises a condition or not. The documentation
likely explains, but if someone retroactively adds a condition to a function
there's nothing forcing upstream users to acknowledge a new point of task
failure.
* Libaries using I/O are not guaranteed to correctly raise on conditions when an
error occurs. In developing various I/O libraries, it's much easier to just
return `None` from a read rather than raising an error. The silent contract of
"don't raise on EOF" was a little difficult to understand and threw a wrench
into the answer of the question "when do I raise a condition?"
Many of these difficulties can be overcome through documentation, examples, and
general practice. In the end, all of these difficulties added together ended up
being too overwhelming and improving various aspects didn't end up helping that
much.
A result-based I/O error handling strategy also has shortcomings, but the
cognitive burden is much smaller. The tooling necessary to make this strategy as
usable as conditions were is much smaller than the tooling necessary for
conditions.
Perhaps conditions may manifest themselves as a future entity, but for now
we're going to remove them from the standard library.
Closes #9795
Closes #8968
2014-02-04 19:02:10 -08:00
|
|
|
use task;
|
|
|
|
let result = task::try(proc() {
|
2014-06-18 20:25:36 +02:00
|
|
|
Path::new(b"foo/bar\0")
|
2013-11-21 17:23:21 -08:00
|
|
|
});
|
Remove std::condition
This has been a long time coming. Conditions in rust were initially envisioned
as being a good alternative to error code return pattern. The idea is that all
errors are fatal-by-default, and you can opt-in to handling the error by
registering an error handler.
While sounding nice, conditions ended up having some unforseen shortcomings:
* Actually handling an error has some very awkward syntax:
let mut result = None;
let mut answer = None;
io::io_error::cond.trap(|e| { result = Some(e) }).inside(|| {
answer = Some(some_io_operation());
});
match result {
Some(err) => { /* hit an I/O error */ }
None => {
let answer = answer.unwrap();
/* deal with the result of I/O */
}
}
This pattern can certainly use functions like io::result, but at its core
actually handling conditions is fairly difficult
* The "zero value" of a function is often confusing. One of the main ideas
behind using conditions was to change the signature of I/O functions. Instead
of read_be_u32() returning a result, it returned a u32. Errors were notified
via a condition, and if you caught the condition you understood that the "zero
value" returned is actually a garbage value. These zero values are often
difficult to understand, however.
One case of this is the read_bytes() function. The function takes an integer
length of the amount of bytes to read, and returns an array of that size. The
array may actually be shorter, however, if an error occurred.
Another case is fs::stat(). The theoretical "zero value" is a blank stat
struct, but it's a little awkward to create and return a zero'd out stat
struct on a call to stat().
In general, the return value of functions that can raise error are much more
natural when using a Result as opposed to an always-usable zero-value.
* Conditions impose a necessary runtime requirement on *all* I/O. In theory I/O
is as simple as calling read() and write(), but using conditions imposed the
restriction that a rust local task was required if you wanted to catch errors
with I/O. While certainly an surmountable difficulty, this was always a bit of
a thorn in the side of conditions.
* Functions raising conditions are not always clear that they are raising
conditions. This suffers a similar problem to exceptions where you don't
actually know whether a function raises a condition or not. The documentation
likely explains, but if someone retroactively adds a condition to a function
there's nothing forcing upstream users to acknowledge a new point of task
failure.
* Libaries using I/O are not guaranteed to correctly raise on conditions when an
error occurs. In developing various I/O libraries, it's much easier to just
return `None` from a read rather than raising an error. The silent contract of
"don't raise on EOF" was a little difficult to understand and threw a wrench
into the answer of the question "when do I raise a condition?"
Many of these difficulties can be overcome through documentation, examples, and
general practice. In the end, all of these difficulties added together ended up
being too overwhelming and improving various aspects didn't end up helping that
much.
A result-based I/O error handling strategy also has shortcomings, but the
cognitive burden is much smaller. The tooling necessary to make this strategy as
usable as conditions were is much smaller than the tooling necessary for
conditions.
Perhaps conditions may manifest themselves as a future entity, but for now
we're going to remove them from the standard library.
Closes #9795
Closes #8968
2014-02-04 19:02:10 -08:00
|
|
|
assert!(result.is_err());
|
2013-09-01 12:44:07 -07:00
|
|
|
|
Remove std::condition
This has been a long time coming. Conditions in rust were initially envisioned
as being a good alternative to error code return pattern. The idea is that all
errors are fatal-by-default, and you can opt-in to handling the error by
registering an error handler.
While sounding nice, conditions ended up having some unforseen shortcomings:
* Actually handling an error has some very awkward syntax:
let mut result = None;
let mut answer = None;
io::io_error::cond.trap(|e| { result = Some(e) }).inside(|| {
answer = Some(some_io_operation());
});
match result {
Some(err) => { /* hit an I/O error */ }
None => {
let answer = answer.unwrap();
/* deal with the result of I/O */
}
}
This pattern can certainly use functions like io::result, but at its core
actually handling conditions is fairly difficult
* The "zero value" of a function is often confusing. One of the main ideas
behind using conditions was to change the signature of I/O functions. Instead
of read_be_u32() returning a result, it returned a u32. Errors were notified
via a condition, and if you caught the condition you understood that the "zero
value" returned is actually a garbage value. These zero values are often
difficult to understand, however.
One case of this is the read_bytes() function. The function takes an integer
length of the amount of bytes to read, and returns an array of that size. The
array may actually be shorter, however, if an error occurred.
Another case is fs::stat(). The theoretical "zero value" is a blank stat
struct, but it's a little awkward to create and return a zero'd out stat
struct on a call to stat().
In general, the return value of functions that can raise error are much more
natural when using a Result as opposed to an always-usable zero-value.
* Conditions impose a necessary runtime requirement on *all* I/O. In theory I/O
is as simple as calling read() and write(), but using conditions imposed the
restriction that a rust local task was required if you wanted to catch errors
with I/O. While certainly an surmountable difficulty, this was always a bit of
a thorn in the side of conditions.
* Functions raising conditions are not always clear that they are raising
conditions. This suffers a similar problem to exceptions where you don't
actually know whether a function raises a condition or not. The documentation
likely explains, but if someone retroactively adds a condition to a function
there's nothing forcing upstream users to acknowledge a new point of task
failure.
* Libaries using I/O are not guaranteed to correctly raise on conditions when an
error occurs. In developing various I/O libraries, it's much easier to just
return `None` from a read rather than raising an error. The silent contract of
"don't raise on EOF" was a little difficult to understand and threw a wrench
into the answer of the question "when do I raise a condition?"
Many of these difficulties can be overcome through documentation, examples, and
general practice. In the end, all of these difficulties added together ended up
being too overwhelming and improving various aspects didn't end up helping that
much.
A result-based I/O error handling strategy also has shortcomings, but the
cognitive burden is much smaller. The tooling necessary to make this strategy as
usable as conditions were is much smaller than the tooling necessary for
conditions.
Perhaps conditions may manifest themselves as a future entity, but for now
we're going to remove them from the standard library.
Closes #9795
Closes #8968
2014-02-04 19:02:10 -08:00
|
|
|
let result = task::try(proc() {
|
2014-06-18 20:25:36 +02:00
|
|
|
Path::new("test").set_filename(b"f\0o")
|
Remove std::condition
This has been a long time coming. Conditions in rust were initially envisioned
as being a good alternative to error code return pattern. The idea is that all
errors are fatal-by-default, and you can opt-in to handling the error by
registering an error handler.
While sounding nice, conditions ended up having some unforseen shortcomings:
* Actually handling an error has some very awkward syntax:
let mut result = None;
let mut answer = None;
io::io_error::cond.trap(|e| { result = Some(e) }).inside(|| {
answer = Some(some_io_operation());
});
match result {
Some(err) => { /* hit an I/O error */ }
None => {
let answer = answer.unwrap();
/* deal with the result of I/O */
}
}
This pattern can certainly use functions like io::result, but at its core
actually handling conditions is fairly difficult
* The "zero value" of a function is often confusing. One of the main ideas
behind using conditions was to change the signature of I/O functions. Instead
of read_be_u32() returning a result, it returned a u32. Errors were notified
via a condition, and if you caught the condition you understood that the "zero
value" returned is actually a garbage value. These zero values are often
difficult to understand, however.
One case of this is the read_bytes() function. The function takes an integer
length of the amount of bytes to read, and returns an array of that size. The
array may actually be shorter, however, if an error occurred.
Another case is fs::stat(). The theoretical "zero value" is a blank stat
struct, but it's a little awkward to create and return a zero'd out stat
struct on a call to stat().
In general, the return value of functions that can raise error are much more
natural when using a Result as opposed to an always-usable zero-value.
* Conditions impose a necessary runtime requirement on *all* I/O. In theory I/O
is as simple as calling read() and write(), but using conditions imposed the
restriction that a rust local task was required if you wanted to catch errors
with I/O. While certainly an surmountable difficulty, this was always a bit of
a thorn in the side of conditions.
* Functions raising conditions are not always clear that they are raising
conditions. This suffers a similar problem to exceptions where you don't
actually know whether a function raises a condition or not. The documentation
likely explains, but if someone retroactively adds a condition to a function
there's nothing forcing upstream users to acknowledge a new point of task
failure.
* Libaries using I/O are not guaranteed to correctly raise on conditions when an
error occurs. In developing various I/O libraries, it's much easier to just
return `None` from a read rather than raising an error. The silent contract of
"don't raise on EOF" was a little difficult to understand and threw a wrench
into the answer of the question "when do I raise a condition?"
Many of these difficulties can be overcome through documentation, examples, and
general practice. In the end, all of these difficulties added together ended up
being too overwhelming and improving various aspects didn't end up helping that
much.
A result-based I/O error handling strategy also has shortcomings, but the
cognitive burden is much smaller. The tooling necessary to make this strategy as
usable as conditions were is much smaller than the tooling necessary for
conditions.
Perhaps conditions may manifest themselves as a future entity, but for now
we're going to remove them from the standard library.
Closes #9795
Closes #8968
2014-02-04 19:02:10 -08:00
|
|
|
});
|
|
|
|
assert!(result.is_err());
|
2013-09-01 12:44:07 -07:00
|
|
|
|
Remove std::condition
This has been a long time coming. Conditions in rust were initially envisioned
as being a good alternative to error code return pattern. The idea is that all
errors are fatal-by-default, and you can opt-in to handling the error by
registering an error handler.
While sounding nice, conditions ended up having some unforseen shortcomings:
* Actually handling an error has some very awkward syntax:
let mut result = None;
let mut answer = None;
io::io_error::cond.trap(|e| { result = Some(e) }).inside(|| {
answer = Some(some_io_operation());
});
match result {
Some(err) => { /* hit an I/O error */ }
None => {
let answer = answer.unwrap();
/* deal with the result of I/O */
}
}
This pattern can certainly use functions like io::result, but at its core
actually handling conditions is fairly difficult
* The "zero value" of a function is often confusing. One of the main ideas
behind using conditions was to change the signature of I/O functions. Instead
of read_be_u32() returning a result, it returned a u32. Errors were notified
via a condition, and if you caught the condition you understood that the "zero
value" returned is actually a garbage value. These zero values are often
difficult to understand, however.
One case of this is the read_bytes() function. The function takes an integer
length of the amount of bytes to read, and returns an array of that size. The
array may actually be shorter, however, if an error occurred.
Another case is fs::stat(). The theoretical "zero value" is a blank stat
struct, but it's a little awkward to create and return a zero'd out stat
struct on a call to stat().
In general, the return value of functions that can raise error are much more
natural when using a Result as opposed to an always-usable zero-value.
* Conditions impose a necessary runtime requirement on *all* I/O. In theory I/O
is as simple as calling read() and write(), but using conditions imposed the
restriction that a rust local task was required if you wanted to catch errors
with I/O. While certainly an surmountable difficulty, this was always a bit of
a thorn in the side of conditions.
* Functions raising conditions are not always clear that they are raising
conditions. This suffers a similar problem to exceptions where you don't
actually know whether a function raises a condition or not. The documentation
likely explains, but if someone retroactively adds a condition to a function
there's nothing forcing upstream users to acknowledge a new point of task
failure.
* Libaries using I/O are not guaranteed to correctly raise on conditions when an
error occurs. In developing various I/O libraries, it's much easier to just
return `None` from a read rather than raising an error. The silent contract of
"don't raise on EOF" was a little difficult to understand and threw a wrench
into the answer of the question "when do I raise a condition?"
Many of these difficulties can be overcome through documentation, examples, and
general practice. In the end, all of these difficulties added together ended up
being too overwhelming and improving various aspects didn't end up helping that
much.
A result-based I/O error handling strategy also has shortcomings, but the
cognitive burden is much smaller. The tooling necessary to make this strategy as
usable as conditions were is much smaller than the tooling necessary for
conditions.
Perhaps conditions may manifest themselves as a future entity, but for now
we're going to remove them from the standard library.
Closes #9795
Closes #8968
2014-02-04 19:02:10 -08:00
|
|
|
let result = task::try(proc() {
|
2014-06-18 20:25:36 +02:00
|
|
|
Path::new("test").push(b"f\0o");
|
Remove std::condition
This has been a long time coming. Conditions in rust were initially envisioned
as being a good alternative to error code return pattern. The idea is that all
errors are fatal-by-default, and you can opt-in to handling the error by
registering an error handler.
While sounding nice, conditions ended up having some unforseen shortcomings:
* Actually handling an error has some very awkward syntax:
let mut result = None;
let mut answer = None;
io::io_error::cond.trap(|e| { result = Some(e) }).inside(|| {
answer = Some(some_io_operation());
});
match result {
Some(err) => { /* hit an I/O error */ }
None => {
let answer = answer.unwrap();
/* deal with the result of I/O */
}
}
This pattern can certainly use functions like io::result, but at its core
actually handling conditions is fairly difficult
* The "zero value" of a function is often confusing. One of the main ideas
behind using conditions was to change the signature of I/O functions. Instead
of read_be_u32() returning a result, it returned a u32. Errors were notified
via a condition, and if you caught the condition you understood that the "zero
value" returned is actually a garbage value. These zero values are often
difficult to understand, however.
One case of this is the read_bytes() function. The function takes an integer
length of the amount of bytes to read, and returns an array of that size. The
array may actually be shorter, however, if an error occurred.
Another case is fs::stat(). The theoretical "zero value" is a blank stat
struct, but it's a little awkward to create and return a zero'd out stat
struct on a call to stat().
In general, the return value of functions that can raise error are much more
natural when using a Result as opposed to an always-usable zero-value.
* Conditions impose a necessary runtime requirement on *all* I/O. In theory I/O
is as simple as calling read() and write(), but using conditions imposed the
restriction that a rust local task was required if you wanted to catch errors
with I/O. While certainly an surmountable difficulty, this was always a bit of
a thorn in the side of conditions.
* Functions raising conditions are not always clear that they are raising
conditions. This suffers a similar problem to exceptions where you don't
actually know whether a function raises a condition or not. The documentation
likely explains, but if someone retroactively adds a condition to a function
there's nothing forcing upstream users to acknowledge a new point of task
failure.
* Libaries using I/O are not guaranteed to correctly raise on conditions when an
error occurs. In developing various I/O libraries, it's much easier to just
return `None` from a read rather than raising an error. The silent contract of
"don't raise on EOF" was a little difficult to understand and threw a wrench
into the answer of the question "when do I raise a condition?"
Many of these difficulties can be overcome through documentation, examples, and
general practice. In the end, all of these difficulties added together ended up
being too overwhelming and improving various aspects didn't end up helping that
much.
A result-based I/O error handling strategy also has shortcomings, but the
cognitive burden is much smaller. The tooling necessary to make this strategy as
usable as conditions were is much smaller than the tooling necessary for
conditions.
Perhaps conditions may manifest themselves as a future entity, but for now
we're going to remove them from the standard library.
Closes #9795
Closes #8968
2014-02-04 19:02:10 -08:00
|
|
|
});
|
|
|
|
assert!(result.is_err());
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
2013-09-26 02:10:16 -07:00
|
|
|
#[test]
|
|
|
|
fn test_display_str() {
|
2013-10-06 18:51:49 -07:00
|
|
|
macro_rules! t(
|
|
|
|
($path:expr, $disp:ident, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
2014-06-21 03:39:03 -07:00
|
|
|
assert!(path.$disp().to_string().as_slice() == $exp);
|
2013-10-06 18:51:49 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
t!("foo", display, "foo");
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(b"foo\x80", display, "foo\uFFFD");
|
|
|
|
t!(b"foo\xFFbar", display, "foo\uFFFDbar");
|
|
|
|
t!(b"foo\xFF/bar", filename_display, "bar");
|
|
|
|
t!(b"foo/\xFFbar", filename_display, "\uFFFDbar");
|
|
|
|
t!(b"/", filename_display, "");
|
2013-10-06 18:51:49 -07:00
|
|
|
|
|
|
|
macro_rules! t(
|
|
|
|
($path:expr, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
2014-02-07 19:45:48 -08:00
|
|
|
let mo = path.display().as_maybe_owned();
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(mo.as_slice() == $exp);
|
2013-10-06 18:51:49 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
($path:expr, $exp:expr, filename) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
2014-02-07 19:45:48 -08:00
|
|
|
let mo = path.filename_display().as_maybe_owned();
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(mo.as_slice() == $exp);
|
2013-10-06 18:51:49 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!("foo", "foo");
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(b"foo\x80", "foo\uFFFD");
|
|
|
|
t!(b"foo\xFFbar", "foo\uFFFDbar");
|
|
|
|
t!(b"foo\xFF/bar", "bar", filename);
|
|
|
|
t!(b"foo/\xFFbar", "\uFFFDbar", filename);
|
|
|
|
t!(b"/", "", filename);
|
2013-09-26 02:10:16 -07:00
|
|
|
}
|
|
|
|
|
2013-10-01 14:03:41 -07:00
|
|
|
#[test]
|
|
|
|
fn test_display() {
|
|
|
|
macro_rules! t(
|
|
|
|
($path:expr, $exp:expr, $expf:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
2013-10-01 14:03:41 -07:00
|
|
|
let f = format!("{}", path.display());
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(f.as_slice() == $exp);
|
2013-10-01 14:03:41 -07:00
|
|
|
let f = format!("{}", path.filename_display());
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(f.as_slice() == $expf);
|
2013-10-01 14:03:41 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(b"foo", "foo", "foo");
|
|
|
|
t!(b"foo/bar", "foo/bar", "bar");
|
|
|
|
t!(b"/", "/", "");
|
|
|
|
t!(b"foo\xFF", "foo\uFFFD", "foo\uFFFD");
|
|
|
|
t!(b"foo\xFF/bar", "foo\uFFFD/bar", "bar");
|
|
|
|
t!(b"foo/\xFFbar", "foo/\uFFFDbar", "\uFFFDbar");
|
|
|
|
t!(b"\xFFfoo/bar\xFF", "\uFFFDfoo/bar\uFFFD", "bar\uFFFD");
|
2013-10-01 14:03:41 -07:00
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_components() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $op:ident, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(path.$op() == ($exp).as_bytes());
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
(s: $path:expr, $op:ident, $exp:expr, opt) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
2013-12-23 17:30:49 +01:00
|
|
|
let left = path.$op().map(|x| str::from_utf8(x).unwrap());
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(left == $exp);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $op:ident, $exp:expr) => (
|
|
|
|
{
|
2014-01-15 14:39:08 -05:00
|
|
|
let arg = $path;
|
|
|
|
let path = Path::new(arg);
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(path.$op() == $exp);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
2013-09-26 17:21:59 -07:00
|
|
|
);
|
2013-09-01 12:44:07 -07:00
|
|
|
)
|
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: b"a/b/c", filename, Some(b"c"));
|
|
|
|
t!(v: b"a/b/c\xFF", filename, Some(b"c\xFF"));
|
|
|
|
t!(v: b"a/b\xFF/c", filename, Some(b"c"));
|
2013-09-26 17:21:59 -07:00
|
|
|
t!(s: "a/b/c", filename, Some("c"), opt);
|
|
|
|
t!(s: "/a/b/c", filename, Some("c"), opt);
|
|
|
|
t!(s: "a", filename, Some("a"), opt);
|
|
|
|
t!(s: "/a", filename, Some("a"), opt);
|
|
|
|
t!(s: ".", filename, None, opt);
|
|
|
|
t!(s: "/", filename, None, opt);
|
|
|
|
t!(s: "..", filename, None, opt);
|
|
|
|
t!(s: "../..", filename, None, opt);
|
2013-09-01 12:44:07 -07:00
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: b"a/b/c", dirname, b"a/b");
|
|
|
|
t!(v: b"a/b/c\xFF", dirname, b"a/b");
|
|
|
|
t!(v: b"a/b\xFF/c", dirname, b"a/b\xFF");
|
2013-09-01 12:44:07 -07:00
|
|
|
t!(s: "a/b/c", dirname, "a/b");
|
|
|
|
t!(s: "/a/b/c", dirname, "/a/b");
|
|
|
|
t!(s: "a", dirname, ".");
|
|
|
|
t!(s: "/a", dirname, "/");
|
|
|
|
t!(s: ".", dirname, ".");
|
|
|
|
t!(s: "/", dirname, "/");
|
|
|
|
t!(s: "..", dirname, "..");
|
|
|
|
t!(s: "../..", dirname, "../..");
|
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: b"hi/there.txt", filestem, Some(b"there"));
|
|
|
|
t!(v: b"hi/there\x80.txt", filestem, Some(b"there\x80"));
|
|
|
|
t!(v: b"hi/there.t\x80xt", filestem, Some(b"there"));
|
2013-09-26 17:21:59 -07:00
|
|
|
t!(s: "hi/there.txt", filestem, Some("there"), opt);
|
|
|
|
t!(s: "hi/there", filestem, Some("there"), opt);
|
|
|
|
t!(s: "there.txt", filestem, Some("there"), opt);
|
|
|
|
t!(s: "there", filestem, Some("there"), opt);
|
|
|
|
t!(s: ".", filestem, None, opt);
|
|
|
|
t!(s: "/", filestem, None, opt);
|
|
|
|
t!(s: "foo/.bar", filestem, Some(".bar"), opt);
|
|
|
|
t!(s: ".bar", filestem, Some(".bar"), opt);
|
|
|
|
t!(s: "..bar", filestem, Some("."), opt);
|
|
|
|
t!(s: "hi/there..txt", filestem, Some("there."), opt);
|
|
|
|
t!(s: "..", filestem, None, opt);
|
|
|
|
t!(s: "../..", filestem, None, opt);
|
2013-09-01 12:44:07 -07:00
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: b"hi/there.txt", extension, Some(b"txt"));
|
|
|
|
t!(v: b"hi/there\x80.txt", extension, Some(b"txt"));
|
|
|
|
t!(v: b"hi/there.t\x80xt", extension, Some(b"t\x80xt"));
|
|
|
|
t!(v: b"hi/there", extension, None);
|
|
|
|
t!(v: b"hi/there\x80", extension, None);
|
2013-09-01 12:44:07 -07:00
|
|
|
t!(s: "hi/there.txt", extension, Some("txt"), opt);
|
|
|
|
t!(s: "hi/there", extension, None, opt);
|
|
|
|
t!(s: "there.txt", extension, Some("txt"), opt);
|
|
|
|
t!(s: "there", extension, None, opt);
|
|
|
|
t!(s: ".", extension, None, opt);
|
|
|
|
t!(s: "/", extension, None, opt);
|
|
|
|
t!(s: "foo/.bar", extension, None, opt);
|
|
|
|
t!(s: ".bar", extension, None, opt);
|
|
|
|
t!(s: "..bar", extension, Some("bar"), opt);
|
|
|
|
t!(s: "hi/there..txt", extension, Some("txt"), opt);
|
|
|
|
t!(s: "..", extension, None, opt);
|
|
|
|
t!(s: "../..", extension, None, opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $join:expr) => (
|
|
|
|
{
|
2014-02-26 12:55:23 -08:00
|
|
|
let path = $path;
|
|
|
|
let join = $join;
|
2013-12-03 19:15:12 -08:00
|
|
|
let mut p1 = Path::new(path);
|
2013-09-01 12:44:07 -07:00
|
|
|
let p2 = p1.clone();
|
2013-10-05 19:49:32 -07:00
|
|
|
p1.push(join);
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(p1 == p2.join(join));
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", "..");
|
|
|
|
t!(s: "/a/b/c", "d");
|
|
|
|
t!(s: "a/b", "c/d");
|
|
|
|
t!(s: "a/b", "/c/d");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push_path() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $push:expr, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let mut p = Path::new($path);
|
|
|
|
let push = Path::new($push);
|
2013-10-07 19:16:58 -07:00
|
|
|
p.push(&push);
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(p.as_str() == Some($exp));
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", "d", "a/b/c/d");
|
|
|
|
t!(s: "/a/b/c", "d", "/a/b/c/d");
|
|
|
|
t!(s: "a/b", "c/d", "a/b/c/d");
|
|
|
|
t!(s: "a/b", "/c/d", "/c/d");
|
|
|
|
t!(s: "a/b", ".", "a/b");
|
|
|
|
t!(s: "a/b", "../c", "a/c");
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
#[test]
|
|
|
|
fn test_push_many() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $push:expr, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let mut p = Path::new($path);
|
2013-10-05 19:49:32 -07:00
|
|
|
p.push_many($push);
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(p.as_str() == Some($exp));
|
2013-09-26 17:21:59 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $push:expr, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let mut p = Path::new($path);
|
2013-09-26 17:21:59 -07:00
|
|
|
p.push_many($push);
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(p.as_vec() == $exp);
|
2013-09-26 17:21:59 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
|
|
|
|
t!(s: "a/b/c", ["d", "/e"], "/e");
|
|
|
|
t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
|
2014-05-25 03:17:19 -07:00
|
|
|
t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
|
|
|
|
t!(v: b"a/b/c", [b"d", b"/e", b"f"], b"/e/f");
|
|
|
|
t!(v: b"a/b/c", [Vec::from_slice(b"d"), Vec::from_slice(b"e")], b"a/b/c/d/e");
|
2013-09-26 17:21:59 -07:00
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_pop() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $left:expr, $right:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let mut p = Path::new($path);
|
2013-10-09 22:05:14 -07:00
|
|
|
let result = p.pop();
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(p.as_str() == Some($left));
|
|
|
|
assert!(result == $right);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
);
|
2014-06-18 20:25:36 +02:00
|
|
|
(b: $path:expr, $left:expr, $right:expr) => (
|
2013-09-01 12:44:07 -07:00
|
|
|
{
|
2014-06-18 20:25:36 +02:00
|
|
|
let mut p = Path::new($path);
|
2013-10-09 22:05:14 -07:00
|
|
|
let result = p.pop();
|
2014-06-18 20:25:36 +02:00
|
|
|
assert!(p.as_vec() == $left);
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(result == $right);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(b: b"a/b/c", b"a/b", true);
|
|
|
|
t!(b: b"a", b".", true);
|
|
|
|
t!(b: b".", b".", false);
|
|
|
|
t!(b: b"/a", b"/", true);
|
|
|
|
t!(b: b"/", b"/", false);
|
|
|
|
t!(b: b"a/b/c\x80", b"a/b", true);
|
|
|
|
t!(b: b"a/b\x80/c", b"a/b\x80", true);
|
|
|
|
t!(b: b"\xFF", b".", true);
|
|
|
|
t!(b: b"/\xFF", b"/", true);
|
2013-10-09 22:05:14 -07:00
|
|
|
t!(s: "a/b/c", "a/b", true);
|
|
|
|
t!(s: "a", ".", true);
|
|
|
|
t!(s: ".", ".", false);
|
|
|
|
t!(s: "/a", "/", true);
|
|
|
|
t!(s: "/", "/", false);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
#[test]
|
|
|
|
fn test_root_path() {
|
2014-06-18 20:25:36 +02:00
|
|
|
assert!(Path::new(b"a/b/c").root_path() == None);
|
|
|
|
assert!(Path::new(b"/a/b/c").root_path() == Some(Path::new("/")));
|
2013-09-26 17:21:59 -07:00
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_join() {
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: Path::new(b"a/b/c").join(b".."), b"a/b");
|
|
|
|
t!(v: Path::new(b"/a/b/c").join(b"d"), b"/a/b/c/d");
|
|
|
|
t!(v: Path::new(b"a/\x80/c").join(b"\xFF"), b"a/\x80/c/\xFF");
|
2013-12-03 19:15:12 -08:00
|
|
|
t!(s: Path::new("a/b/c").join(".."), "a/b");
|
|
|
|
t!(s: Path::new("/a/b/c").join("d"), "/a/b/c/d");
|
|
|
|
t!(s: Path::new("a/b").join("c/d"), "a/b/c/d");
|
|
|
|
t!(s: Path::new("a/b").join("/c/d"), "/c/d");
|
|
|
|
t!(s: Path::new(".").join("a/b"), "a/b");
|
|
|
|
t!(s: Path::new("/").join("a/b"), "/a/b");
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_join_path() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $join:expr, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
|
|
|
let join = Path::new($join);
|
2013-10-07 19:16:58 -07:00
|
|
|
let res = path.join(&join);
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(res.as_str() == Some($exp));
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", "..", "a/b");
|
|
|
|
t!(s: "/a/b/c", "d", "/a/b/c/d");
|
|
|
|
t!(s: "a/b", "c/d", "a/b/c/d");
|
|
|
|
t!(s: "a/b", "/c/d", "/c/d");
|
|
|
|
t!(s: ".", "a/b", "a/b");
|
|
|
|
t!(s: "/", "a/b", "/a/b");
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
#[test]
|
|
|
|
fn test_join_many() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $join:expr, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
2013-10-05 19:49:32 -07:00
|
|
|
let res = path.join_many($join);
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(res.as_str() == Some($exp));
|
2013-09-26 17:21:59 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $join:expr, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
2013-09-26 17:21:59 -07:00
|
|
|
let res = path.join_many($join);
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(res.as_vec() == $exp);
|
2013-09-26 17:21:59 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
|
|
|
|
t!(s: "a/b/c", ["..", "d"], "a/b/d");
|
|
|
|
t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
|
2014-05-25 03:17:19 -07:00
|
|
|
t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
|
|
|
|
t!(v: b"a/b/c", [Vec::from_slice(b"d"), Vec::from_slice(b"e")], b"a/b/c/d/e");
|
2013-09-26 17:21:59 -07:00
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_with_helpers() {
|
2013-10-05 19:49:32 -07:00
|
|
|
let empty: &[u8] = [];
|
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: Path::new(b"a/b/c").with_filename(b"d"), b"a/b/d");
|
|
|
|
t!(v: Path::new(b"a/b/c\xFF").with_filename(b"\x80"), b"a/b/\x80");
|
|
|
|
t!(v: Path::new(b"/\xFF/foo").with_filename(b"\xCD"),
|
|
|
|
b"/\xFF/\xCD");
|
2013-12-03 19:15:12 -08:00
|
|
|
t!(s: Path::new("a/b/c").with_filename("d"), "a/b/d");
|
|
|
|
t!(s: Path::new(".").with_filename("foo"), "foo");
|
|
|
|
t!(s: Path::new("/a/b/c").with_filename("d"), "/a/b/d");
|
|
|
|
t!(s: Path::new("/").with_filename("foo"), "/foo");
|
|
|
|
t!(s: Path::new("/a").with_filename("foo"), "/foo");
|
|
|
|
t!(s: Path::new("foo").with_filename("bar"), "bar");
|
|
|
|
t!(s: Path::new("/").with_filename("foo/"), "/foo");
|
|
|
|
t!(s: Path::new("/a").with_filename("foo/"), "/foo");
|
|
|
|
t!(s: Path::new("a/b/c").with_filename(""), "a/b");
|
|
|
|
t!(s: Path::new("a/b/c").with_filename("."), "a/b");
|
|
|
|
t!(s: Path::new("a/b/c").with_filename(".."), "a");
|
|
|
|
t!(s: Path::new("/a").with_filename(""), "/");
|
|
|
|
t!(s: Path::new("foo").with_filename(""), ".");
|
|
|
|
t!(s: Path::new("a/b/c").with_filename("d/e"), "a/b/d/e");
|
|
|
|
t!(s: Path::new("a/b/c").with_filename("/d"), "a/b/d");
|
|
|
|
t!(s: Path::new("..").with_filename("foo"), "../foo");
|
|
|
|
t!(s: Path::new("../..").with_filename("foo"), "../../foo");
|
|
|
|
t!(s: Path::new("..").with_filename(""), "..");
|
|
|
|
t!(s: Path::new("../..").with_filename(""), "../..");
|
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: Path::new(b"hi/there\x80.txt").with_extension(b"exe"),
|
|
|
|
b"hi/there\x80.exe");
|
|
|
|
t!(v: Path::new(b"hi/there.txt\x80").with_extension(b"\xFF"),
|
|
|
|
b"hi/there.\xFF");
|
|
|
|
t!(v: Path::new(b"hi/there\x80").with_extension(b"\xFF"),
|
|
|
|
b"hi/there\x80.\xFF");
|
|
|
|
t!(v: Path::new(b"hi/there.\xFF").with_extension(empty), b"hi/there");
|
2013-12-03 19:15:12 -08:00
|
|
|
t!(s: Path::new("hi/there.txt").with_extension("exe"), "hi/there.exe");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_extension(""), "hi/there");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_extension("."), "hi/there..");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_extension(".."), "hi/there...");
|
|
|
|
t!(s: Path::new("hi/there").with_extension("txt"), "hi/there.txt");
|
|
|
|
t!(s: Path::new("hi/there").with_extension("."), "hi/there..");
|
|
|
|
t!(s: Path::new("hi/there").with_extension(".."), "hi/there...");
|
|
|
|
t!(s: Path::new("hi/there.").with_extension("txt"), "hi/there.txt");
|
|
|
|
t!(s: Path::new("hi/.foo").with_extension("txt"), "hi/.foo.txt");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_extension(".foo"), "hi/there..foo");
|
|
|
|
t!(s: Path::new("/").with_extension("txt"), "/");
|
|
|
|
t!(s: Path::new("/").with_extension("."), "/");
|
|
|
|
t!(s: Path::new("/").with_extension(".."), "/");
|
|
|
|
t!(s: Path::new(".").with_extension("txt"), ".");
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_setters() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
|
|
|
let arg = $arg;
|
2013-12-03 19:15:12 -08:00
|
|
|
let mut p1 = Path::new(path);
|
2013-09-01 12:44:07 -07:00
|
|
|
p1.$set(arg);
|
2013-12-03 19:15:12 -08:00
|
|
|
let p2 = Path::new(path);
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(p1 == p2.$with(arg));
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
|
|
|
let arg = $arg;
|
2013-12-03 19:15:12 -08:00
|
|
|
let mut p1 = Path::new(path);
|
2013-09-01 12:44:07 -07:00
|
|
|
p1.$set(arg);
|
2013-12-03 19:15:12 -08:00
|
|
|
let p2 = Path::new(path);
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(p1 == p2.$with(arg));
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: b"a/b/c", set_filename, with_filename, b"d");
|
|
|
|
t!(v: b"/", set_filename, with_filename, b"foo");
|
|
|
|
t!(v: b"\x80", set_filename, with_filename, b"\xFF");
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: "a/b/c", set_filename, with_filename, "d");
|
|
|
|
t!(s: "/", set_filename, with_filename, "foo");
|
|
|
|
t!(s: ".", set_filename, with_filename, "foo");
|
|
|
|
t!(s: "a/b", set_filename, with_filename, "");
|
|
|
|
t!(s: "a", set_filename, with_filename, "");
|
2013-09-01 12:44:07 -07:00
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: b"hi/there.txt", set_extension, with_extension, b"exe");
|
|
|
|
t!(v: b"hi/there.t\x80xt", set_extension, with_extension, b"exe\xFF");
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: "hi/there.txt", set_extension, with_extension, "exe");
|
|
|
|
t!(s: "hi/there.", set_extension, with_extension, "txt");
|
|
|
|
t!(s: "hi/there", set_extension, with_extension, "txt");
|
|
|
|
t!(s: "hi/there.txt", set_extension, with_extension, "");
|
|
|
|
t!(s: "hi/there", set_extension, with_extension, "");
|
|
|
|
t!(s: ".", set_extension, with_extension, "txt");
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_getters() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
2013-09-26 17:21:59 -07:00
|
|
|
let filename = $filename;
|
|
|
|
assert!(path.filename_str() == filename,
|
2013-10-01 23:41:59 -07:00
|
|
|
"{}.filename_str(): Expected `{:?}`, found {:?}",
|
2013-09-26 17:21:59 -07:00
|
|
|
path.as_str().unwrap(), filename, path.filename_str());
|
|
|
|
let dirname = $dirname;
|
|
|
|
assert!(path.dirname_str() == dirname,
|
2013-10-01 23:41:59 -07:00
|
|
|
"`{}`.dirname_str(): Expected `{:?}`, found `{:?}`",
|
2013-09-26 17:21:59 -07:00
|
|
|
path.as_str().unwrap(), dirname, path.dirname_str());
|
|
|
|
let filestem = $filestem;
|
|
|
|
assert!(path.filestem_str() == filestem,
|
2013-10-01 23:41:59 -07:00
|
|
|
"`{}`.filestem_str(): Expected `{:?}`, found `{:?}`",
|
2013-09-26 17:21:59 -07:00
|
|
|
path.as_str().unwrap(), filestem, path.filestem_str());
|
|
|
|
let ext = $ext;
|
|
|
|
assert!(path.extension_str() == ext,
|
2013-10-01 23:41:59 -07:00
|
|
|
"`{}`.extension_str(): Expected `{:?}`, found `{:?}`",
|
2013-09-26 17:21:59 -07:00
|
|
|
path.as_str().unwrap(), ext, path.extension_str());
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(path.filename() == $filename);
|
|
|
|
assert!(path.dirname() == $dirname);
|
|
|
|
assert!(path.filestem() == $filestem);
|
|
|
|
assert!(path.extension() == $ext);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), None);
|
|
|
|
t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), None);
|
|
|
|
t!(v: Path::new(b"hi/there.\xFF"), Some(b"there.\xFF"), b"hi",
|
|
|
|
Some(b"there"), Some(b"\xFF"));
|
2013-12-03 19:15:12 -08:00
|
|
|
t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), None);
|
|
|
|
t!(s: Path::new("."), None, Some("."), None, None);
|
|
|
|
t!(s: Path::new("/"), None, Some("/"), None, None);
|
|
|
|
t!(s: Path::new(".."), None, Some(".."), None, None);
|
|
|
|
t!(s: Path::new("../.."), None, Some("../.."), None, None);
|
|
|
|
t!(s: Path::new("hi/there.txt"), Some("there.txt"), Some("hi"),
|
2013-09-01 12:44:07 -07:00
|
|
|
Some("there"), Some("txt"));
|
2013-12-03 19:15:12 -08:00
|
|
|
t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), None);
|
|
|
|
t!(s: Path::new("hi/there."), Some("there."), Some("hi"),
|
2013-09-01 12:44:07 -07:00
|
|
|
Some("there"), Some(""));
|
2013-12-03 19:15:12 -08:00
|
|
|
t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), None);
|
|
|
|
t!(s: Path::new("hi/..there"), Some("..there"), Some("hi"),
|
2013-09-01 12:44:07 -07:00
|
|
|
Some("."), Some("there"));
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, None);
|
|
|
|
t!(s: Path::new(b"a/b/\xFF.txt"), None, Some("a/b"), None, Some("txt"));
|
|
|
|
t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), None);
|
|
|
|
t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), None);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-10-09 22:05:14 -07:00
|
|
|
fn test_dir_path() {
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: Path::new(b"hi/there\x80").dir_path(), b"hi");
|
|
|
|
t!(v: Path::new(b"hi\xFF/there").dir_path(), b"hi\xFF");
|
2013-12-03 19:15:12 -08:00
|
|
|
t!(s: Path::new("hi/there").dir_path(), "hi");
|
|
|
|
t!(s: Path::new("hi").dir_path(), ".");
|
|
|
|
t!(s: Path::new("/hi").dir_path(), "/");
|
|
|
|
t!(s: Path::new("/").dir_path(), "/");
|
|
|
|
t!(s: Path::new("..").dir_path(), "..");
|
|
|
|
t!(s: Path::new("../..").dir_path(), "../..");
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_absolute() {
|
2013-09-26 17:21:59 -07:00
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $abs:expr, $rel:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
2013-09-26 17:21:59 -07:00
|
|
|
assert_eq!(path.is_absolute(), $abs);
|
|
|
|
assert_eq!(path.is_relative(), $rel);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
t!(s: "a/b/c", false, true);
|
|
|
|
t!(s: "/a/b/c", true, false);
|
|
|
|
t!(s: "a", false, true);
|
|
|
|
t!(s: "/a", true, false);
|
|
|
|
t!(s: ".", false, true);
|
|
|
|
t!(s: "/", true, false);
|
|
|
|
t!(s: "..", false, true);
|
|
|
|
t!(s: "../..", false, true);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_ancestor_of() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $dest:expr, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
|
|
|
let dest = Path::new($dest);
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(path.is_ancestor_of(&dest), $exp);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", "a/b/c/d", true);
|
|
|
|
t!(s: "a/b/c", "a/b/c", true);
|
|
|
|
t!(s: "a/b/c", "a/b", false);
|
|
|
|
t!(s: "/a/b/c", "/a/b/c", true);
|
|
|
|
t!(s: "/a/b", "/a/b/c", true);
|
|
|
|
t!(s: "/a/b/c/d", "/a/b/c", false);
|
|
|
|
t!(s: "/a/b", "a/b/c", false);
|
|
|
|
t!(s: "a/b", "/a/b/c", false);
|
|
|
|
t!(s: "a/b/c", "a/b/d", false);
|
|
|
|
t!(s: "../a/b/c", "a/b/c", false);
|
|
|
|
t!(s: "a/b/c", "../a/b/c", false);
|
|
|
|
t!(s: "a/b/c", "a/b/cd", false);
|
|
|
|
t!(s: "a/b/cd", "a/b/c", false);
|
|
|
|
t!(s: "../a/b", "../a/b/c", true);
|
|
|
|
t!(s: ".", "a/b", true);
|
|
|
|
t!(s: ".", ".", true);
|
|
|
|
t!(s: "/", "/", true);
|
|
|
|
t!(s: "/", "/a/b", true);
|
|
|
|
t!(s: "..", "a/b", true);
|
|
|
|
t!(s: "../..", "a/b", true);
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
#[test]
|
|
|
|
fn test_ends_with_path() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $child:expr, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
|
|
|
let child = Path::new($child);
|
2013-09-26 17:21:59 -07:00
|
|
|
assert_eq!(path.ends_with_path(&child), $exp);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $child:expr, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
|
|
|
let child = Path::new($child);
|
2013-09-26 17:21:59 -07:00
|
|
|
assert_eq!(path.ends_with_path(&child), $exp);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", "c", true);
|
|
|
|
t!(s: "a/b/c", "d", false);
|
|
|
|
t!(s: "foo/bar/quux", "bar", false);
|
|
|
|
t!(s: "foo/bar/quux", "barquux", false);
|
|
|
|
t!(s: "a/b/c", "b/c", true);
|
|
|
|
t!(s: "a/b/c", "a/b/c", true);
|
|
|
|
t!(s: "a/b/c", "foo/a/b/c", false);
|
|
|
|
t!(s: "/a/b/c", "a/b/c", true);
|
|
|
|
t!(s: "/a/b/c", "/a/b/c", false); // child must be relative
|
|
|
|
t!(s: "/a/b/c", "foo/a/b/c", false);
|
|
|
|
t!(s: "a/b/c", "", false);
|
|
|
|
t!(s: "", "", true);
|
|
|
|
t!(s: "/a/b/c", "d/e/f", false);
|
|
|
|
t!(s: "a/b/c", "a/b", false);
|
|
|
|
t!(s: "a/b/c", "b", false);
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(v: b"a/b/c", b"b/c", true);
|
|
|
|
t!(v: b"a/b/\xFF", b"\xFF", true);
|
|
|
|
t!(v: b"a/b/\xFF", b"b/\xFF", true);
|
2013-09-26 17:21:59 -07:00
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_path_relative_from() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $other:expr, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
|
|
|
let other = Path::new($other);
|
2013-09-01 12:44:07 -07:00
|
|
|
let res = path.path_relative_from(&other);
|
2013-10-15 23:32:14 -07:00
|
|
|
assert_eq!(res.as_ref().and_then(|x| x.as_str()), $exp);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", "a/b", Some("c"));
|
|
|
|
t!(s: "a/b/c", "a/b/d", Some("../c"));
|
|
|
|
t!(s: "a/b/c", "a/b/c/d", Some(".."));
|
|
|
|
t!(s: "a/b/c", "a/b/c", Some("."));
|
|
|
|
t!(s: "a/b/c", "a/b/c/d/e", Some("../.."));
|
|
|
|
t!(s: "a/b/c", "a/d/e", Some("../../b/c"));
|
|
|
|
t!(s: "a/b/c", "d/e/f", Some("../../../a/b/c"));
|
|
|
|
t!(s: "a/b/c", "/a/b/c", None);
|
|
|
|
t!(s: "/a/b/c", "a/b/c", Some("/a/b/c"));
|
|
|
|
t!(s: "/a/b/c", "/a/b/c/d", Some(".."));
|
|
|
|
t!(s: "/a/b/c", "/a/b", Some("c"));
|
|
|
|
t!(s: "/a/b/c", "/a/b/c/d/e", Some("../.."));
|
|
|
|
t!(s: "/a/b/c", "/a/d/e", Some("../../b/c"));
|
|
|
|
t!(s: "/a/b/c", "/d/e/f", Some("../../../a/b/c"));
|
|
|
|
t!(s: "hi/there.txt", "hi/there", Some("../there.txt"));
|
|
|
|
t!(s: ".", "a", Some(".."));
|
|
|
|
t!(s: ".", "a/b", Some("../.."));
|
|
|
|
t!(s: ".", ".", Some("."));
|
|
|
|
t!(s: "a", ".", Some("a"));
|
|
|
|
t!(s: "a/b", ".", Some("a/b"));
|
|
|
|
t!(s: "..", ".", Some(".."));
|
|
|
|
t!(s: "a/b/c", "a/b/c", Some("."));
|
|
|
|
t!(s: "/a/b/c", "/a/b/c", Some("."));
|
|
|
|
t!(s: "/", "/", Some("."));
|
|
|
|
t!(s: "/", ".", Some("/"));
|
|
|
|
t!(s: "../../a", "b", Some("../../../a"));
|
|
|
|
t!(s: "a", "../../b", None);
|
|
|
|
t!(s: "../../a", "../../b", Some("../a"));
|
|
|
|
t!(s: "../../a", "../../a/b", Some(".."));
|
|
|
|
t!(s: "../../a/b", "../../a", Some("b"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-11-23 11:18:51 +01:00
|
|
|
fn test_components_iter() {
|
2013-09-01 12:44:07 -07:00
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $exp:expr) => (
|
|
|
|
{
|
2013-12-03 19:15:12 -08:00
|
|
|
let path = Path::new($path);
|
2014-04-12 20:56:23 +10:00
|
|
|
let comps = path.components().collect::<Vec<&[u8]>>();
|
2013-09-01 12:44:07 -07:00
|
|
|
let exp: &[&str] = $exp;
|
2014-04-12 20:56:23 +10:00
|
|
|
let exps = exp.iter().map(|x| x.as_bytes()).collect::<Vec<&[u8]>>();
|
2013-11-23 11:18:51 +01:00
|
|
|
assert!(comps == exps, "components: Expected {:?}, found {:?}",
|
2013-09-26 17:21:59 -07:00
|
|
|
comps, exps);
|
Deprecate the rev_iter pattern in all places where a DoubleEndedIterator is provided (everywhere but treemap)
This commit deprecates rev_iter, mut_rev_iter, move_rev_iter everywhere (except treemap) and also
deprecates related functions like rsplit, rev_components, and rev_str_components. In every case,
these functions can be replaced with the non-reversed form followed by a call to .rev(). To make this
more concrete, a translation table for all functional changes necessary follows:
* container.rev_iter() -> container.iter().rev()
* container.mut_rev_iter() -> container.mut_iter().rev()
* container.move_rev_iter() -> container.move_iter().rev()
* sliceorstr.rsplit(sep) -> sliceorstr.split(sep).rev()
* path.rev_components() -> path.components().rev()
* path.rev_str_components() -> path.str_components().rev()
In terms of the type system, this change also deprecates any specialized reversed iterator types (except
in treemap), opting instead to use Rev directly if any type annotations are needed. However, since
methods directly returning reversed iterators are now discouraged, the need for such annotations should
be small. However, in those cases, the general pattern for conversion is to take whatever follows Rev in
the original reversed name and surround it with Rev<>:
* RevComponents<'a> -> Rev<Components<'a>>
* RevStrComponents<'a> -> Rev<StrComponents<'a>>
* RevItems<'a, T> -> Rev<Items<'a, T>>
* etc.
The reasoning behind this change is that it makes the standard API much simpler without reducing readability,
performance, or power. The presence of functions such as rev_iter adds more boilerplate code to libraries
(all of which simply call .iter().rev()), clutters up the documentation, and only helps code by saving two
characters. Additionally, the numerous type synonyms that were used to make the type signatures look nice
like RevItems add even more boilerplate and clutter up the docs even more. With this change, all that cruft
goes away.
[breaking-change]
2014-04-20 23:59:12 -05:00
|
|
|
let comps = path.components().rev().collect::<Vec<&[u8]>>();
|
2014-04-12 20:56:23 +10:00
|
|
|
let exps = exps.move_iter().rev().collect::<Vec<&[u8]>>();
|
2013-11-23 11:18:51 +01:00
|
|
|
assert!(comps == exps, "rev_components: Expected {:?}, found {:?}",
|
2013-09-26 17:21:59 -07:00
|
|
|
comps, exps);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
);
|
2014-06-18 20:25:36 +02:00
|
|
|
(b: $arg:expr, [$($exp:expr),*]) => (
|
2013-09-01 12:44:07 -07:00
|
|
|
{
|
2014-06-18 20:25:36 +02:00
|
|
|
let path = Path::new($arg);
|
2014-04-12 20:56:23 +10:00
|
|
|
let comps = path.components().collect::<Vec<&[u8]>>();
|
2014-06-18 20:25:36 +02:00
|
|
|
let exp: &[&[u8]] = [$($exp),*];
|
2014-04-12 20:56:23 +10:00
|
|
|
assert_eq!(comps.as_slice(), exp);
|
Deprecate the rev_iter pattern in all places where a DoubleEndedIterator is provided (everywhere but treemap)
This commit deprecates rev_iter, mut_rev_iter, move_rev_iter everywhere (except treemap) and also
deprecates related functions like rsplit, rev_components, and rev_str_components. In every case,
these functions can be replaced with the non-reversed form followed by a call to .rev(). To make this
more concrete, a translation table for all functional changes necessary follows:
* container.rev_iter() -> container.iter().rev()
* container.mut_rev_iter() -> container.mut_iter().rev()
* container.move_rev_iter() -> container.move_iter().rev()
* sliceorstr.rsplit(sep) -> sliceorstr.split(sep).rev()
* path.rev_components() -> path.components().rev()
* path.rev_str_components() -> path.str_components().rev()
In terms of the type system, this change also deprecates any specialized reversed iterator types (except
in treemap), opting instead to use Rev directly if any type annotations are needed. However, since
methods directly returning reversed iterators are now discouraged, the need for such annotations should
be small. However, in those cases, the general pattern for conversion is to take whatever follows Rev in
the original reversed name and surround it with Rev<>:
* RevComponents<'a> -> Rev<Components<'a>>
* RevStrComponents<'a> -> Rev<StrComponents<'a>>
* RevItems<'a, T> -> Rev<Items<'a, T>>
* etc.
The reasoning behind this change is that it makes the standard API much simpler without reducing readability,
performance, or power. The presence of functions such as rev_iter adds more boilerplate code to libraries
(all of which simply call .iter().rev()), clutters up the documentation, and only helps code by saving two
characters. Additionally, the numerous type synonyms that were used to make the type signatures look nice
like RevItems add even more boilerplate and clutter up the docs even more. With this change, all that cruft
goes away.
[breaking-change]
2014-04-20 23:59:12 -05:00
|
|
|
let comps = path.components().rev().collect::<Vec<&[u8]>>();
|
|
|
|
let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>();
|
2014-04-12 20:56:23 +10:00
|
|
|
assert_eq!(comps, exp)
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(b: b"a/b/c", [b"a", b"b", b"c"]);
|
|
|
|
t!(b: b"/\xFF/a/\x80", [b"\xFF", b"a", b"\x80"]);
|
|
|
|
t!(b: b"../../foo\xCDbar", [b"..", b"..", b"foo\xCDbar"]);
|
2013-09-01 12:44:07 -07:00
|
|
|
t!(s: "a/b/c", ["a", "b", "c"]);
|
|
|
|
t!(s: "a/b/d", ["a", "b", "d"]);
|
|
|
|
t!(s: "a/b/cd", ["a", "b", "cd"]);
|
|
|
|
t!(s: "/a/b/c", ["a", "b", "c"]);
|
|
|
|
t!(s: "a", ["a"]);
|
|
|
|
t!(s: "/a", ["a"]);
|
|
|
|
t!(s: "/", []);
|
|
|
|
t!(s: ".", ["."]);
|
|
|
|
t!(s: "..", [".."]);
|
|
|
|
t!(s: "../..", ["..", ".."]);
|
|
|
|
t!(s: "../../foo", ["..", "..", "foo"]);
|
|
|
|
}
|
2013-09-26 14:38:26 -07:00
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
#[test]
|
2013-11-23 11:18:51 +01:00
|
|
|
fn test_str_components() {
|
2013-09-26 17:21:59 -07:00
|
|
|
macro_rules! t(
|
2014-06-18 20:25:36 +02:00
|
|
|
(b: $arg:expr, $exp:expr) => (
|
2013-09-26 17:21:59 -07:00
|
|
|
{
|
2014-06-18 20:25:36 +02:00
|
|
|
let path = Path::new($arg);
|
2014-04-12 20:56:23 +10:00
|
|
|
let comps = path.str_components().collect::<Vec<Option<&str>>>();
|
2013-09-26 17:21:59 -07:00
|
|
|
let exp: &[Option<&str>] = $exp;
|
2014-04-12 20:56:23 +10:00
|
|
|
assert_eq!(comps.as_slice(), exp);
|
Deprecate the rev_iter pattern in all places where a DoubleEndedIterator is provided (everywhere but treemap)
This commit deprecates rev_iter, mut_rev_iter, move_rev_iter everywhere (except treemap) and also
deprecates related functions like rsplit, rev_components, and rev_str_components. In every case,
these functions can be replaced with the non-reversed form followed by a call to .rev(). To make this
more concrete, a translation table for all functional changes necessary follows:
* container.rev_iter() -> container.iter().rev()
* container.mut_rev_iter() -> container.mut_iter().rev()
* container.move_rev_iter() -> container.move_iter().rev()
* sliceorstr.rsplit(sep) -> sliceorstr.split(sep).rev()
* path.rev_components() -> path.components().rev()
* path.rev_str_components() -> path.str_components().rev()
In terms of the type system, this change also deprecates any specialized reversed iterator types (except
in treemap), opting instead to use Rev directly if any type annotations are needed. However, since
methods directly returning reversed iterators are now discouraged, the need for such annotations should
be small. However, in those cases, the general pattern for conversion is to take whatever follows Rev in
the original reversed name and surround it with Rev<>:
* RevComponents<'a> -> Rev<Components<'a>>
* RevStrComponents<'a> -> Rev<StrComponents<'a>>
* RevItems<'a, T> -> Rev<Items<'a, T>>
* etc.
The reasoning behind this change is that it makes the standard API much simpler without reducing readability,
performance, or power. The presence of functions such as rev_iter adds more boilerplate code to libraries
(all of which simply call .iter().rev()), clutters up the documentation, and only helps code by saving two
characters. Additionally, the numerous type synonyms that were used to make the type signatures look nice
like RevItems add even more boilerplate and clutter up the docs even more. With this change, all that cruft
goes away.
[breaking-change]
2014-04-20 23:59:12 -05:00
|
|
|
let comps = path.str_components().rev().collect::<Vec<Option<&str>>>();
|
|
|
|
let exp = exp.iter().rev().map(|&x|x).collect::<Vec<Option<&str>>>();
|
2014-04-12 20:56:23 +10:00
|
|
|
assert_eq!(comps, exp);
|
2013-09-26 17:21:59 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-06-18 20:25:36 +02:00
|
|
|
t!(b: b"a/b/c", [Some("a"), Some("b"), Some("c")]);
|
|
|
|
t!(b: b"/\xFF/a/\x80", [None, Some("a"), None]);
|
|
|
|
t!(b: b"../../foo\xCDbar", [Some(".."), Some(".."), None]);
|
2013-11-23 11:18:51 +01:00
|
|
|
// str_components is a wrapper around components, so no need to do
|
2013-09-26 17:21:59 -07:00
|
|
|
// the full set of tests
|
|
|
|
}
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
2013-11-27 11:39:07 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod bench {
|
2014-02-14 09:49:11 +08:00
|
|
|
extern crate test;
|
2014-04-01 09:16:35 +08:00
|
|
|
use self::test::Bencher;
|
2013-11-27 11:39:07 +02:00
|
|
|
use super::*;
|
2014-01-06 22:33:37 -08:00
|
|
|
use prelude::*;
|
2013-11-27 11:39:07 +02:00
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn join_home_dir(b: &mut Bencher) {
|
2013-12-03 19:15:12 -08:00
|
|
|
let posix_path = Path::new("/");
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-11-27 11:39:07 +02:00
|
|
|
posix_path.join("home");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn join_abs_path_home_dir(b: &mut Bencher) {
|
2013-12-03 19:15:12 -08:00
|
|
|
let posix_path = Path::new("/");
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-11-27 11:39:07 +02:00
|
|
|
posix_path.join("/home");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn join_many_home_dir(b: &mut Bencher) {
|
2013-12-03 19:15:12 -08:00
|
|
|
let posix_path = Path::new("/");
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-11-27 11:39:07 +02:00
|
|
|
posix_path.join_many(&["home"]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn join_many_abs_path_home_dir(b: &mut Bencher) {
|
2013-12-03 19:15:12 -08:00
|
|
|
let posix_path = Path::new("/");
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-11-27 11:39:07 +02:00
|
|
|
posix_path.join_many(&["/home"]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn push_home_dir(b: &mut Bencher) {
|
2013-12-03 19:15:12 -08:00
|
|
|
let mut posix_path = Path::new("/");
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-11-27 11:39:07 +02:00
|
|
|
posix_path.push("home");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn push_abs_path_home_dir(b: &mut Bencher) {
|
2013-12-03 19:15:12 -08:00
|
|
|
let mut posix_path = Path::new("/");
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-11-27 11:39:07 +02:00
|
|
|
posix_path.push("/home");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn push_many_home_dir(b: &mut Bencher) {
|
2013-12-03 19:15:12 -08:00
|
|
|
let mut posix_path = Path::new("/");
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-11-27 11:39:07 +02:00
|
|
|
posix_path.push_many(&["home"]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn push_many_abs_path_home_dir(b: &mut Bencher) {
|
2013-12-03 19:15:12 -08:00
|
|
|
let mut posix_path = Path::new("/");
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-11-27 11:39:07 +02:00
|
|
|
posix_path.push_many(&["/home"]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn ends_with_path_home_dir(b: &mut Bencher) {
|
2013-12-03 19:15:12 -08:00
|
|
|
let posix_home_path = Path::new("/home");
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-12-03 19:15:12 -08:00
|
|
|
posix_home_path.ends_with_path(&Path::new("home"));
|
2013-11-27 11:39:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn ends_with_path_missmatch_jome_home(b: &mut Bencher) {
|
2013-12-03 19:15:12 -08:00
|
|
|
let posix_home_path = Path::new("/home");
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-12-03 19:15:12 -08:00
|
|
|
posix_home_path.ends_with_path(&Path::new("jome"));
|
2013-11-27 11:39:07 +02:00
|
|
|
});
|
|
|
|
}
|
2013-12-25 02:56:59 +02:00
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn is_ancestor_of_path_with_10_dirs(b: &mut Bencher) {
|
2013-12-25 02:56:59 +02:00
|
|
|
let path = Path::new("/home/1/2/3/4/5/6/7/8/9");
|
|
|
|
let mut sub = path.clone();
|
|
|
|
sub.pop();
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-12-25 02:56:59 +02:00
|
|
|
path.is_ancestor_of(&sub);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn path_relative_from_forward(b: &mut Bencher) {
|
2013-12-25 02:56:59 +02:00
|
|
|
let path = Path::new("/a/b/c");
|
|
|
|
let mut other = path.clone();
|
|
|
|
other.pop();
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-12-25 02:56:59 +02:00
|
|
|
path.path_relative_from(&other);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn path_relative_from_same_level(b: &mut Bencher) {
|
2013-12-25 02:56:59 +02:00
|
|
|
let path = Path::new("/a/b/c");
|
|
|
|
let mut other = path.clone();
|
|
|
|
other.pop();
|
|
|
|
other.push("d");
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-12-25 02:56:59 +02:00
|
|
|
path.path_relative_from(&other);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-04-01 09:16:35 +08:00
|
|
|
fn path_relative_from_backward(b: &mut Bencher) {
|
2013-12-25 02:56:59 +02:00
|
|
|
let path = Path::new("/a/b");
|
|
|
|
let mut other = path.clone();
|
|
|
|
other.push("c");
|
2014-04-01 09:16:35 +08:00
|
|
|
b.iter(|| {
|
2013-12-25 02:56:59 +02:00
|
|
|
path.path_relative_from(&other);
|
|
|
|
});
|
|
|
|
}
|
2013-11-27 11:39:07 +02:00
|
|
|
}
|