2012-09-19 18:52:32 -05:00
|
|
|
/*!
|
|
|
|
|
|
|
|
Cross-platform file path handling
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
// NB: transitionary, de-mode-ing.
|
|
|
|
#[forbid(deprecated_mode)];
|
|
|
|
#[forbid(deprecated_pattern)];
|
|
|
|
|
2012-08-27 18:26:35 -05:00
|
|
|
use cmp::Eq;
|
|
|
|
|
2012-09-27 17:44:09 -05:00
|
|
|
pub struct WindowsPath {
|
2012-09-07 16:50:47 -05:00
|
|
|
host: Option<~str>,
|
|
|
|
device: Option<~str>,
|
|
|
|
is_absolute: bool,
|
|
|
|
components: ~[~str],
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-09-27 17:44:09 -05:00
|
|
|
pub struct PosixPath {
|
2012-09-07 16:50:47 -05:00
|
|
|
is_absolute: bool,
|
|
|
|
components: ~[~str],
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-09-27 17:44:09 -05:00
|
|
|
pub trait GenericPath {
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
static pure fn from_str((&str)) -> self;
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn dirname() -> ~str;
|
|
|
|
pure fn filename() -> Option<~str>;
|
|
|
|
pure fn filestem() -> Option<~str>;
|
|
|
|
pure fn filetype() -> Option<~str>;
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn with_dirname((&str)) -> self;
|
|
|
|
pure fn with_filename((&str)) -> self;
|
|
|
|
pure fn with_filestem((&str)) -> self;
|
|
|
|
pure fn with_filetype((&str)) -> self;
|
|
|
|
|
|
|
|
pure fn push((&str)) -> self;
|
|
|
|
pure fn push_rel((&self)) -> self;
|
|
|
|
pure fn push_many((&[~str])) -> self;
|
|
|
|
pure fn pop() -> self;
|
2012-08-29 15:59:02 -05:00
|
|
|
|
|
|
|
pure fn normalize() -> self;
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
#[cfg(windows)]
|
2012-09-27 17:44:09 -05:00
|
|
|
pub type Path = WindowsPath;
|
2012-08-29 15:25:31 -05:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
2012-09-27 17:44:09 -05:00
|
|
|
pub pure fn Path(s: &str) -> Path {
|
2012-08-29 15:25:31 -05:00
|
|
|
from_str::<WindowsPath>(s)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
#[cfg(unix)]
|
2012-09-27 17:44:09 -05:00
|
|
|
pub type Path = PosixPath;
|
2012-08-29 15:25:31 -05:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
2012-09-27 17:44:09 -05:00
|
|
|
pub pure fn Path(s: &str) -> Path {
|
2012-08-29 15:25:31 -05:00
|
|
|
from_str::<PosixPath>(s)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
impl PosixPath : ToStr {
|
|
|
|
fn to_str() -> ~str {
|
|
|
|
let mut s = ~"";
|
|
|
|
if self.is_absolute {
|
|
|
|
s += "/";
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
s + str::connect(self.components, "/")
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
impl PosixPath : Eq {
|
|
|
|
pure fn eq(other: &PosixPath) -> bool {
|
|
|
|
return self.is_absolute == (*other).is_absolute &&
|
|
|
|
self.components == (*other).components;
|
|
|
|
}
|
|
|
|
pure fn ne(other: &PosixPath) -> bool { !self.eq(other) }
|
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
impl WindowsPath : Eq {
|
|
|
|
pure fn eq(other: &WindowsPath) -> bool {
|
|
|
|
return self.host == (*other).host &&
|
|
|
|
self.device == (*other).device &&
|
|
|
|
self.is_absolute == (*other).is_absolute &&
|
|
|
|
self.components == (*other).components;
|
|
|
|
}
|
|
|
|
pure fn ne(other: &WindowsPath) -> bool { !self.eq(other) }
|
|
|
|
}
|
2012-08-30 15:22:31 -05:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
// FIXME (#3227): when default methods in traits are working, de-duplicate
|
|
|
|
// PosixPath and WindowsPath, most of their methods are common.
|
|
|
|
impl PosixPath : GenericPath {
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
static pure fn from_str(s: &str) -> PosixPath {
|
|
|
|
let mut components = str::split_nonempty(s, |c| c == '/');
|
|
|
|
let is_absolute = (s.len() != 0 && s[0] == '/' as u8);
|
|
|
|
return PosixPath { is_absolute: is_absolute,
|
2012-08-29 15:59:02 -05:00
|
|
|
components: components }
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn dirname() -> ~str {
|
2012-09-18 13:17:40 -05:00
|
|
|
unsafe {
|
2012-08-29 15:25:31 -05:00
|
|
|
let s = self.dir_path().to_str();
|
|
|
|
if s.len() == 0 {
|
|
|
|
~"."
|
|
|
|
} else {
|
2012-09-10 18:31:00 -05:00
|
|
|
move s
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn filename() -> Option<~str> {
|
|
|
|
match self.components.len() {
|
|
|
|
0 => None,
|
|
|
|
n => Some(copy self.components[n - 1])
|
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn filestem() -> Option<~str> {
|
|
|
|
match self.filename() {
|
|
|
|
None => None,
|
|
|
|
Some(ref f) => {
|
|
|
|
match str::rfind_char(*f, '.') {
|
|
|
|
Some(p) => Some(f.slice(0, p)),
|
|
|
|
None => Some(copy *f)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn filetype() -> Option<~str> {
|
|
|
|
match self.filename() {
|
|
|
|
None => None,
|
|
|
|
Some(ref f) => {
|
|
|
|
match str::rfind_char(*f, '.') {
|
2012-09-18 13:19:52 -05:00
|
|
|
Some(p) if p < f.len() => Some(f.slice(p, f.len())),
|
2012-08-29 15:25:31 -05:00
|
|
|
_ => None
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn with_dirname(d: &str) -> PosixPath {
|
|
|
|
let dpath = from_str::<PosixPath>(d);
|
|
|
|
match self.filename() {
|
|
|
|
Some(ref f) => dpath.push(*f),
|
2012-09-10 18:31:00 -05:00
|
|
|
None => move dpath
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn with_filename(f: &str) -> PosixPath {
|
2012-09-18 13:17:40 -05:00
|
|
|
unsafe {
|
2012-08-29 15:25:31 -05:00
|
|
|
assert ! str::any(f, |c| windows::is_sep(c as u8));
|
|
|
|
self.dir_path().push(f)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn with_filestem(s: &str) -> PosixPath {
|
|
|
|
match self.filetype() {
|
|
|
|
None => self.with_filename(s),
|
2012-09-18 13:19:52 -05:00
|
|
|
Some(ref t) => self.with_filename(str::from_slice(s) + *t)
|
2012-05-08 19:33:48 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn with_filetype(t: &str) -> PosixPath {
|
|
|
|
if t.len() == 0 {
|
|
|
|
match self.filestem() {
|
|
|
|
None => copy self,
|
|
|
|
Some(s) => self.with_filename(s)
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
} else {
|
2012-08-29 15:25:31 -05:00
|
|
|
let t = ~"." + str::from_slice(t);
|
|
|
|
match self.filestem() {
|
|
|
|
None => self.with_filename(t),
|
2012-09-18 13:19:52 -05:00
|
|
|
Some(ref s) => self.with_filename(*s + t)
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn dir_path() -> PosixPath {
|
|
|
|
if self.components.len() != 0 {
|
|
|
|
self.pop()
|
2012-03-02 19:20:00 -06:00
|
|
|
} else {
|
2012-08-29 15:25:31 -05:00
|
|
|
copy self
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn file_path() -> PosixPath {
|
|
|
|
let cs = match self.filename() {
|
|
|
|
None => ~[],
|
|
|
|
Some(ref f) => ~[copy *f]
|
|
|
|
};
|
|
|
|
return PosixPath { is_absolute: false,
|
|
|
|
components: cs }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn push_rel(other: &PosixPath) -> PosixPath {
|
|
|
|
assert !other.is_absolute;
|
|
|
|
self.push_many(other.components)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn push_many(cs: &[~str]) -> PosixPath {
|
2012-09-13 21:34:18 -05:00
|
|
|
let mut v = copy self.components;
|
|
|
|
for cs.each |e| {
|
2012-09-19 18:55:01 -05:00
|
|
|
let mut ss = str::split_nonempty(
|
|
|
|
*e,
|
|
|
|
|c| windows::is_sep(c as u8));
|
2012-09-26 19:33:34 -05:00
|
|
|
unsafe { v.push_all_move(move ss); }
|
2012-09-13 21:34:18 -05:00
|
|
|
}
|
|
|
|
PosixPath { components: move v, ..self }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn push(s: &str) -> PosixPath {
|
2012-09-13 21:34:18 -05:00
|
|
|
let mut v = copy self.components;
|
|
|
|
let mut ss = str::split_nonempty(s, |c| windows::is_sep(c as u8));
|
2012-09-26 19:33:34 -05:00
|
|
|
unsafe { v.push_all_move(move ss); }
|
2012-09-13 21:34:18 -05:00
|
|
|
PosixPath { components: move v, ..self }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn pop() -> PosixPath {
|
|
|
|
let mut cs = copy self.components;
|
|
|
|
if cs.len() != 0 {
|
2012-09-28 00:20:47 -05:00
|
|
|
unsafe { cs.pop(); }
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
return PosixPath { components: move cs, ..self }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2012-08-29 15:59:02 -05:00
|
|
|
|
|
|
|
pure fn normalize() -> PosixPath {
|
|
|
|
return PosixPath {
|
|
|
|
components: normalize(self.components),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
|
|
|
|
impl WindowsPath : ToStr {
|
|
|
|
fn to_str() -> ~str {
|
|
|
|
let mut s = ~"";
|
|
|
|
match self.host {
|
|
|
|
Some(h) => { s += "\\\\"; s += h; }
|
|
|
|
None => { }
|
|
|
|
}
|
|
|
|
match self.device {
|
|
|
|
Some(d) => { s += d; s += ":"; }
|
|
|
|
None => { }
|
|
|
|
}
|
|
|
|
if self.is_absolute {
|
|
|
|
s += "\\";
|
|
|
|
}
|
|
|
|
s + str::connect(self.components, "\\")
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
|
|
|
|
impl WindowsPath : GenericPath {
|
|
|
|
|
|
|
|
static pure fn from_str(s: &str) -> WindowsPath {
|
|
|
|
let host;
|
|
|
|
let device;
|
|
|
|
let rest;
|
|
|
|
|
|
|
|
match windows::extract_drive_prefix(s) {
|
|
|
|
Some((ref d, ref r)) => {
|
|
|
|
host = None;
|
|
|
|
device = Some(copy *d);
|
|
|
|
rest = copy *r;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
match windows::extract_unc_prefix(s) {
|
|
|
|
Some((ref h, ref r)) => {
|
|
|
|
host = Some(copy *h);
|
|
|
|
device = None;
|
|
|
|
rest = copy *r;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
host = None;
|
|
|
|
device = None;
|
|
|
|
rest = str::from_slice(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut components =
|
|
|
|
str::split_nonempty(rest, |c| windows::is_sep(c as u8));
|
|
|
|
let is_absolute = (rest.len() != 0 && windows::is_sep(rest[0]));
|
|
|
|
return WindowsPath { host: host,
|
|
|
|
device: device,
|
|
|
|
is_absolute: is_absolute,
|
2012-08-29 15:59:02 -05:00
|
|
|
components: components }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn dirname() -> ~str {
|
2012-09-18 13:17:40 -05:00
|
|
|
unsafe {
|
2012-08-29 15:25:31 -05:00
|
|
|
let s = self.dir_path().to_str();
|
|
|
|
if s.len() == 0 {
|
|
|
|
~"."
|
|
|
|
} else {
|
2012-09-10 18:31:00 -05:00
|
|
|
move s
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn filename() -> Option<~str> {
|
|
|
|
match self.components.len() {
|
|
|
|
0 => None,
|
|
|
|
n => Some(copy self.components[n - 1])
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn filestem() -> Option<~str> {
|
|
|
|
match self.filename() {
|
|
|
|
None => None,
|
|
|
|
Some(ref f) => {
|
|
|
|
match str::rfind_char(*f, '.') {
|
|
|
|
Some(p) => Some(f.slice(0, p)),
|
|
|
|
None => Some(copy *f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn filetype() -> Option<~str> {
|
|
|
|
match self.filename() {
|
|
|
|
None => None,
|
|
|
|
Some(ref f) => {
|
|
|
|
match str::rfind_char(*f, '.') {
|
2012-09-18 13:19:52 -05:00
|
|
|
Some(p) if p < f.len() => Some(f.slice(p, f.len())),
|
2012-08-29 15:25:31 -05:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn with_dirname(d: &str) -> WindowsPath {
|
|
|
|
let dpath = from_str::<WindowsPath>(d);
|
|
|
|
match self.filename() {
|
|
|
|
Some(ref f) => dpath.push(*f),
|
2012-09-10 18:31:00 -05:00
|
|
|
None => move dpath
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn with_filename(f: &str) -> WindowsPath {
|
|
|
|
assert ! str::any(f, |c| windows::is_sep(c as u8));
|
|
|
|
self.dir_path().push(f)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn with_filestem(s: &str) -> WindowsPath {
|
|
|
|
match self.filetype() {
|
|
|
|
None => self.with_filename(s),
|
2012-09-18 13:19:52 -05:00
|
|
|
Some(ref t) => self.with_filename(str::from_slice(s) + *t)
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn with_filetype(t: &str) -> WindowsPath {
|
|
|
|
if t.len() == 0 {
|
|
|
|
match self.filestem() {
|
|
|
|
None => copy self,
|
|
|
|
Some(s) => self.with_filename(s)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let t = ~"." + str::from_slice(t);
|
|
|
|
match self.filestem() {
|
|
|
|
None => self.with_filename(t),
|
|
|
|
Some(ref s) =>
|
|
|
|
self.with_filename(*s + t)
|
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn dir_path() -> WindowsPath {
|
|
|
|
if self.components.len() != 0 {
|
|
|
|
self.pop()
|
|
|
|
} else {
|
|
|
|
copy self
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn file_path() -> WindowsPath {
|
|
|
|
let cs = match self.filename() {
|
|
|
|
None => ~[],
|
|
|
|
Some(ref f) => ~[copy *f]
|
|
|
|
};
|
|
|
|
return WindowsPath { host: None,
|
|
|
|
device: None,
|
|
|
|
is_absolute: false,
|
|
|
|
components: cs }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn push_rel(other: &WindowsPath) -> WindowsPath {
|
|
|
|
assert !other.is_absolute;
|
|
|
|
self.push_many(other.components)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn push_many(cs: &[~str]) -> WindowsPath {
|
2012-09-13 21:34:18 -05:00
|
|
|
let mut v = copy self.components;
|
|
|
|
for cs.each |e| {
|
2012-09-19 18:55:01 -05:00
|
|
|
let mut ss = str::split_nonempty(
|
|
|
|
*e,
|
|
|
|
|c| windows::is_sep(c as u8));
|
2012-09-26 19:33:34 -05:00
|
|
|
unsafe { v.push_all_move(move ss); }
|
2012-09-13 21:34:18 -05:00
|
|
|
}
|
|
|
|
return WindowsPath { components: move v, ..self }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn push(s: &str) -> WindowsPath {
|
2012-09-13 21:34:18 -05:00
|
|
|
let mut v = copy self.components;
|
|
|
|
let mut ss = str::split_nonempty(s, |c| windows::is_sep(c as u8));
|
2012-09-26 19:33:34 -05:00
|
|
|
unsafe { v.push_all_move(move ss); }
|
2012-09-13 21:34:18 -05:00
|
|
|
return WindowsPath { components: move v, ..self }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
pure fn pop() -> WindowsPath {
|
|
|
|
let mut cs = copy self.components;
|
|
|
|
if cs.len() != 0 {
|
2012-09-28 00:20:47 -05:00
|
|
|
unsafe { cs.pop(); }
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
return WindowsPath { components: move cs, ..self }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2012-08-29 15:59:02 -05:00
|
|
|
|
|
|
|
pure fn normalize() -> WindowsPath {
|
|
|
|
return WindowsPath {
|
|
|
|
components: normalize(self.components),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
|
2012-09-27 17:44:09 -05:00
|
|
|
pub pure fn normalize(components: &[~str]) -> ~[~str] {
|
2012-08-29 15:25:31 -05:00
|
|
|
let mut cs = ~[];
|
2012-09-18 13:17:40 -05:00
|
|
|
unsafe {
|
2012-08-29 15:25:31 -05:00
|
|
|
for components.each |c| {
|
2012-09-18 13:17:40 -05:00
|
|
|
unsafe {
|
2012-09-19 18:55:01 -05:00
|
|
|
if *c == ~"." && components.len() > 1 { loop; }
|
|
|
|
if *c == ~"" { loop; }
|
|
|
|
if *c == ~".." && cs.len() != 0 {
|
2012-09-28 00:20:47 -05:00
|
|
|
cs.pop();
|
2012-09-07 17:32:04 -05:00
|
|
|
loop;
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-09-26 19:33:34 -05:00
|
|
|
cs.push(copy *c);
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2012-09-10 18:31:00 -05:00
|
|
|
move cs
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
|
2012-09-13 21:34:18 -05:00
|
|
|
#[test]
|
|
|
|
fn test_double_slash_collapsing()
|
|
|
|
{
|
|
|
|
let path = from_str::<PosixPath>("tmp/");
|
|
|
|
let path = path.push("/hmm");
|
|
|
|
let path = path.normalize();
|
|
|
|
assert ~"tmp/hmm" == path.to_str();
|
|
|
|
|
|
|
|
let path = from_str::<WindowsPath>("tmp/");
|
|
|
|
let path = path.push("/hmm");
|
|
|
|
let path = path.normalize();
|
|
|
|
assert ~"tmp\\hmm" == path.to_str();
|
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
mod posix {
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:59:02 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
fn mk(s: &str) -> PosixPath { from_str::<PosixPath>(s) }
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
fn t(wp: &PosixPath, s: &str) {
|
|
|
|
let ss = wp.to_str();
|
|
|
|
let sss = str::from_slice(s);
|
|
|
|
if (ss != sss) {
|
|
|
|
debug!("got %s", ss);
|
|
|
|
debug!("expected %s", sss);
|
|
|
|
assert ss == sss;
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-08-29 15:59:02 -05:00
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
|
2012-09-18 13:19:52 -05:00
|
|
|
#[test]
|
|
|
|
fn test_filetype_foo_bar() {
|
|
|
|
let wp = mk("foo.bar");
|
|
|
|
assert wp.filetype() == Some(~".bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_filetype_foo() {
|
|
|
|
let wp = mk("foo");
|
|
|
|
assert wp.filetype() == None;
|
|
|
|
}
|
|
|
|
|
2012-08-29 15:59:02 -05:00
|
|
|
#[test]
|
|
|
|
fn test_posix_paths() {
|
2012-08-29 15:25:31 -05:00
|
|
|
t(&(mk("hi")), "hi");
|
|
|
|
t(&(mk("/lib")), "/lib");
|
|
|
|
t(&(mk("hi/there")), "hi/there");
|
|
|
|
t(&(mk("hi/there.txt")), "hi/there.txt");
|
|
|
|
|
|
|
|
t(&(mk("hi/there.txt")), "hi/there.txt");
|
|
|
|
t(&(mk("hi/there.txt")
|
|
|
|
.with_filetype("")), "hi/there");
|
|
|
|
|
|
|
|
t(&(mk("/a/b/c/there.txt")
|
|
|
|
.with_dirname("hi")), "hi/there.txt");
|
|
|
|
|
|
|
|
t(&(mk("hi/there.txt")
|
2012-08-29 15:59:02 -05:00
|
|
|
.with_dirname(".")), "./there.txt");
|
2012-08-29 15:25:31 -05:00
|
|
|
|
|
|
|
t(&(mk("a/b/c")
|
2012-08-29 15:59:02 -05:00
|
|
|
.push("..")), "a/b/c/..");
|
2012-08-29 15:25:31 -05:00
|
|
|
|
|
|
|
t(&(mk("there.txt")
|
|
|
|
.with_filetype("o")), "there.o");
|
|
|
|
|
|
|
|
t(&(mk("hi/there.txt")
|
|
|
|
.with_filetype("o")), "hi/there.o");
|
|
|
|
|
|
|
|
t(&(mk("hi/there.txt")
|
|
|
|
.with_filetype("o")
|
|
|
|
.with_dirname("/usr/lib")),
|
|
|
|
"/usr/lib/there.o");
|
|
|
|
|
|
|
|
t(&(mk("hi/there.txt")
|
|
|
|
.with_filetype("o")
|
|
|
|
.with_dirname("/usr/lib/")),
|
|
|
|
"/usr/lib/there.o");
|
|
|
|
|
|
|
|
t(&(mk("hi/there.txt")
|
|
|
|
.with_filetype("o")
|
|
|
|
.with_dirname("/usr//lib//")),
|
|
|
|
"/usr/lib/there.o");
|
|
|
|
|
|
|
|
t(&(mk("/usr/bin/rust")
|
|
|
|
.push_many([~"lib", ~"thingy.so"])
|
|
|
|
.with_filestem("librustc")),
|
|
|
|
"/usr/bin/rust/lib/librustc.so");
|
|
|
|
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:59:02 -05:00
|
|
|
#[test]
|
|
|
|
fn test_normalize() {
|
|
|
|
t(&(mk("hi/there.txt")
|
|
|
|
.with_dirname(".").normalize()), "there.txt");
|
|
|
|
|
|
|
|
t(&(mk("a/b/../c/././/../foo.txt/").normalize()),
|
|
|
|
"a/foo.txt");
|
|
|
|
|
|
|
|
t(&(mk("a/b/c")
|
|
|
|
.push("..").normalize()), "a/b");
|
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Various windows helpers, and tests for the impl.
|
|
|
|
mod windows {
|
|
|
|
|
|
|
|
#[inline(always)]
|
2012-09-27 17:44:09 -05:00
|
|
|
pub pure fn is_sep(u: u8) -> bool {
|
2012-08-29 15:25:31 -05:00
|
|
|
u == '/' as u8 || u == '\\' as u8
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-09-27 17:44:09 -05:00
|
|
|
pub pure fn extract_unc_prefix(s: &str) -> Option<(~str,~str)> {
|
2012-08-29 15:25:31 -05:00
|
|
|
if (s.len() > 1 &&
|
|
|
|
s[0] == '\\' as u8 &&
|
|
|
|
s[1] == '\\' as u8) {
|
|
|
|
let mut i = 2;
|
|
|
|
while i < s.len() {
|
|
|
|
if s[i] == '\\' as u8 {
|
|
|
|
let pre = s.slice(2, i);
|
|
|
|
let rest = s.slice(i, s.len());
|
2012-09-10 18:31:00 -05:00
|
|
|
return Some((move pre, move rest));
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2012-09-27 17:44:09 -05:00
|
|
|
pub pure fn extract_drive_prefix(s: &str) -> Option<(~str,~str)> {
|
2012-09-18 13:17:40 -05:00
|
|
|
unsafe {
|
2012-08-29 15:25:31 -05:00
|
|
|
if (s.len() > 1 &&
|
|
|
|
libc::isalpha(s[0] as libc::c_int) != 0 &&
|
|
|
|
s[1] == ':' as u8) {
|
|
|
|
let rest = if s.len() == 2 {
|
|
|
|
~""
|
|
|
|
} else {
|
|
|
|
s.slice(2, s.len())
|
|
|
|
};
|
2012-09-10 18:31:00 -05:00
|
|
|
return Some((s.slice(0,1), move rest));
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-08-29 15:25:31 -05:00
|
|
|
fn test_extract_unc_prefixes() {
|
|
|
|
assert extract_unc_prefix("\\\\").is_none();
|
|
|
|
assert extract_unc_prefix("\\\\hi").is_none();
|
|
|
|
assert extract_unc_prefix("\\\\hi\\") == Some((~"hi", ~"\\"));
|
|
|
|
assert extract_unc_prefix("\\\\hi\\there") ==
|
|
|
|
Some((~"hi", ~"\\there"));
|
|
|
|
assert extract_unc_prefix("\\\\hi\\there\\friends.txt") ==
|
|
|
|
Some((~"hi", ~"\\there\\friends.txt"));
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-08-29 15:25:31 -05:00
|
|
|
fn test_extract_drive_prefixes() {
|
|
|
|
assert extract_drive_prefix("c").is_none();
|
|
|
|
assert extract_drive_prefix("c:") == Some((~"c", ~""));
|
|
|
|
assert extract_drive_prefix("d:") == Some((~"d", ~""));
|
|
|
|
assert extract_drive_prefix("z:") == Some((~"z", ~""));
|
|
|
|
assert extract_drive_prefix("c:\\hi") == Some((~"c", ~"\\hi"));
|
|
|
|
assert extract_drive_prefix("d:hi") == Some((~"d", ~"hi"));
|
|
|
|
assert extract_drive_prefix("c:hi\\there.txt") ==
|
|
|
|
Some((~"c", ~"hi\\there.txt"));
|
|
|
|
assert extract_drive_prefix("c:\\hi\\there.txt") ==
|
|
|
|
Some((~"c", ~"\\hi\\there.txt"));
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-08-29 15:25:31 -05:00
|
|
|
fn test_windows_paths() {
|
|
|
|
fn mk(s: &str) -> WindowsPath { from_str::<WindowsPath>(s) }
|
|
|
|
fn t(wp: &WindowsPath, s: &str) {
|
|
|
|
let ss = wp.to_str();
|
|
|
|
let sss = str::from_slice(s);
|
|
|
|
if (ss != sss) {
|
|
|
|
debug!("got %s", ss);
|
|
|
|
debug!("expected %s", sss);
|
|
|
|
assert ss == sss;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t(&(mk("hi")), "hi");
|
|
|
|
t(&(mk("hi/there")), "hi\\there");
|
|
|
|
t(&(mk("hi/there.txt")), "hi\\there.txt");
|
|
|
|
|
|
|
|
t(&(mk("there.txt")
|
|
|
|
.with_filetype("o")), "there.o");
|
|
|
|
|
|
|
|
t(&(mk("hi/there.txt")
|
|
|
|
.with_filetype("o")), "hi\\there.o");
|
|
|
|
|
|
|
|
t(&(mk("hi/there.txt")
|
|
|
|
.with_filetype("o")
|
|
|
|
.with_dirname("c:\\program files A")),
|
|
|
|
"c:\\program files A\\there.o");
|
|
|
|
|
|
|
|
t(&(mk("hi/there.txt")
|
|
|
|
.with_filetype("o")
|
|
|
|
.with_dirname("c:\\program files B\\")),
|
|
|
|
"c:\\program files B\\there.o");
|
|
|
|
|
|
|
|
t(&(mk("hi/there.txt")
|
|
|
|
.with_filetype("o")
|
|
|
|
.with_dirname("c:\\program files C\\/")),
|
|
|
|
"c:\\program files C\\there.o");
|
|
|
|
|
|
|
|
t(&(mk("c:\\program files (x86)\\rust")
|
|
|
|
.push_many([~"lib", ~"thingy.dll"])
|
|
|
|
.with_filename("librustc.dll")),
|
|
|
|
"c:\\program files (x86)\\rust\\lib\\librustc.dll");
|
|
|
|
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-09-18 13:19:52 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
fn mk(s: &str) -> PosixPath { from_str::<PosixPath>(s) }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_filetype_foo_bar() {
|
|
|
|
let wp = mk("foo.bar");
|
|
|
|
assert wp.filetype() == Some(~".bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_filetype_foo() {
|
|
|
|
let wp = mk("foo");
|
|
|
|
assert wp.filetype() == None;
|
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|