Skip to content

Commit

Permalink
Refactored make_absolute into functionality on the Path
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Feb 18, 2013
1 parent 1171a21 commit c8d8f6c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,17 +565,13 @@ pub fn path_exists(p: &Path) -> bool {
*
* If the given path is relative, return it prepended with the current working
* directory. If the given path is already an absolute path, return it
* as is.
* as is. This is a shortcut for calling os::getcwd().unsafe_join(p)
*/
// NB: this is here rather than in path because it is a form of environment
// querying; what it does depends on the process working directory, not just
// the input paths.
pub fn make_absolute(p: &Path) -> Path {
if p.is_absolute {
copy *p
} else {
getcwd().push_many(p.components)
}
getcwd().unsafe_join(p)
}


Expand Down
30 changes: 30 additions & 0 deletions src/libcore/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub trait GenericPath {
pure fn push_many((&[~str])) -> Self;
pure fn pop() -> Self;

pure fn unsafe_join((&Self)) -> Self;

pure fn normalize() -> Self;
}

Expand Down Expand Up @@ -485,6 +487,15 @@ impl GenericPath for PosixPath {
self.push_many(other.components)
}

pure fn unsafe_join(other: &PosixPath) -> PosixPath {
if other.is_absolute {
PosixPath { is_absolute: true,
components: copy other.components }
} else {
self.push_rel(other)
}
}

pure fn push_many(cs: &[~str]) -> PosixPath {
let mut v = copy self.components;
for cs.each |e| {
Expand Down Expand Up @@ -685,6 +696,25 @@ impl GenericPath for WindowsPath {
self.push_many(other.components)
}

pure fn unsafe_join(other: &WindowsPath) -> WindowsPath {
if !other.is_absolute {
self.push_rel(other)
} else {
WindowsPath {
host: match other.host {
None => copy self.host,
Some(copy x) => Some(x)
},
device: match other.device {
None => copy self.device,
Some(copy x) => Some(x)
},
is_absolute: true,
components: copy other.components
}
}
}

pure fn push_many(cs: &[~str]) -> WindowsPath {
let mut v = copy self.components;
for cs.each |e| {
Expand Down

1 comment on commit c8d8f6c

@graydon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this represents improvement; can we discuss before landing?

Please sign in to comment.