about summary refs log tree commit diff
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2013-02-18 22:48:18 +0000
committerArmin Ronacher <armin.ronacher@active-4.com>2013-02-18 22:48:18 +0000
commitc8d8f6cfec50cad6b35e3b5fc604abc01a26143e (patch)
tree74dd499c39957457b78045076bfd72021b39a017
parent1171a214a633128bbbd067bd0e582f8b40172e01 (diff)
downloadrust-c8d8f6cfec50cad6b35e3b5fc604abc01a26143e.tar.gz
rust-c8d8f6cfec50cad6b35e3b5fc604abc01a26143e.zip
Refactored make_absolute into functionality on the Path
-rw-r--r--src/libcore/os.rs8
-rw-r--r--src/libcore/path.rs30
2 files changed, 32 insertions, 6 deletions
diff --git a/src/libcore/os.rs b/src/libcore/os.rs
index 98bac3a49d8..8667efb7468 100644
--- a/src/libcore/os.rs
+++ b/src/libcore/os.rs
@@ -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)
 }
 
 
diff --git a/src/libcore/path.rs b/src/libcore/path.rs
index 531ce95d067..46f8743ff15 100644
--- a/src/libcore/path.rs
+++ b/src/libcore/path.rs
@@ -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;
 }
 
@@ -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| {
@@ -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| {