about summary refs log tree commit diff
path: root/src/libstd/rt
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-02-15 02:36:27 -0800
committerbors <bors@rust-lang.org>2014-02-15 02:36:27 -0800
commitfba32ea79f1828ef441d91abca3635fad57f323d (patch)
tree1d07a3053f3492b25816a46f912df95bd208bd6e /src/libstd/rt
parentc9f13b47febb81ef339a15adf0f6ad7a2265c46d (diff)
parentd22b1646aafd51ffc85fcd24492a6211c2c0dfab (diff)
downloadrust-fba32ea79f1828ef441d91abca3635fad57f323d.tar.gz
rust-fba32ea79f1828ef441d91abca3635fad57f323d.zip
auto merge of #12283 : kballard/rust/env-args-bytes, r=erickt
Change `os::args()` and `os::env()` to use `str::from_utf8_lossy()`.
Add new functions `os::args_as_bytes()` and `os::env_as_bytes()` to retrieve the args/env as byte vectors instead.

The existing methods were left returning strings because I expect that the common use-case is to want string handling.

Fixes #7188.
Diffstat (limited to 'src/libstd/rt')
-rw-r--r--src/libstd/rt/args.rs44
1 files changed, 23 insertions, 21 deletions
diff --git a/src/libstd/rt/args.rs b/src/libstd/rt/args.rs
index cef03d66923..c417ea375fd 100644
--- a/src/libstd/rt/args.rs
+++ b/src/libstd/rt/args.rs
@@ -36,8 +36,8 @@ pub unsafe fn init(argc: int, argv: **u8) { realargs::init(argc, argv) }
 #[cfg(test)]      pub unsafe fn cleanup() { realargs::cleanup() }
 
 /// Take the global arguments from global storage.
-#[cfg(not(test))] pub fn take() -> Option<~[~str]> { imp::take() }
-#[cfg(test)]      pub fn take() -> Option<~[~str]> {
+#[cfg(not(test))] pub fn take() -> Option<~[~[u8]]> { imp::take() }
+#[cfg(test)]      pub fn take() -> Option<~[~[u8]]> {
     match realargs::take() {
         realstd::option::Some(a) => Some(a),
         realstd::option::None => None,
@@ -47,12 +47,12 @@ pub unsafe fn init(argc: int, argv: **u8) { realargs::init(argc, argv) }
 /// Give the global arguments to global storage.
 ///
 /// It is an error if the arguments already exist.
-#[cfg(not(test))] pub fn put(args: ~[~str]) { imp::put(args) }
-#[cfg(test)]      pub fn put(args: ~[~str]) { realargs::put(args) }
+#[cfg(not(test))] pub fn put(args: ~[~[u8]]) { imp::put(args) }
+#[cfg(test)]      pub fn put(args: ~[~[u8]]) { realargs::put(args) }
 
 /// Make a clone of the global arguments.
-#[cfg(not(test))] pub fn clone() -> Option<~[~str]> { imp::clone() }
-#[cfg(test)]      pub fn clone() -> Option<~[~str]> {
+#[cfg(not(test))] pub fn clone() -> Option<~[~[u8]]> { imp::clone() }
+#[cfg(test)]      pub fn clone() -> Option<~[~[u8]]> {
     match realargs::clone() {
         realstd::option::Some(a) => Some(a),
         realstd::option::None => None,
@@ -65,15 +65,12 @@ pub unsafe fn init(argc: int, argv: **u8) { realargs::init(argc, argv) }
 mod imp {
     use cast;
     use clone::Clone;
-    #[cfg(not(test))] use libc;
     use option::{Option, Some, None};
     use ptr::RawPtr;
     use iter::Iterator;
-    #[cfg(not(test))] use str;
     use unstable::finally::Finally;
     use unstable::mutex::{Mutex, MUTEX_INIT};
     use mem;
-    #[cfg(not(test))] use vec;
 
     static mut global_args_ptr: uint = 0;
     static mut lock: Mutex = MUTEX_INIT;
@@ -90,15 +87,15 @@ mod imp {
         lock.destroy();
     }
 
-    pub fn take() -> Option<~[~str]> {
+    pub fn take() -> Option<~[~[u8]]> {
         with_lock(|| unsafe {
             let ptr = get_global_ptr();
             let val = mem::replace(&mut *ptr, None);
-            val.as_ref().map(|s: &~~[~str]| (**s).clone())
+            val.as_ref().map(|s: &~~[~[u8]]| (**s).clone())
         })
     }
 
-    pub fn put(args: ~[~str]) {
+    pub fn put(args: ~[~[u8]]) {
         with_lock(|| unsafe {
             let ptr = get_global_ptr();
             rtassert!((*ptr).is_none());
@@ -106,10 +103,10 @@ mod imp {
         })
     }
 
-    pub fn clone() -> Option<~[~str]> {
+    pub fn clone() -> Option<~[~[u8]]> {
         with_lock(|| unsafe {
             let ptr = get_global_ptr();
-            (*ptr).as_ref().map(|s: &~~[~str]| (**s).clone())
+            (*ptr).as_ref().map(|s: &~~[~[u8]]| (**s).clone())
         })
     }
 
@@ -126,15 +123,20 @@ mod imp {
         })
     }
 
-    fn get_global_ptr() -> *mut Option<~~[~str]> {
+    fn get_global_ptr() -> *mut Option<~~[~[u8]]> {
         unsafe { cast::transmute(&global_args_ptr) }
     }
 
     // Copied from `os`.
     #[cfg(not(test))]
-    unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~str] {
+    unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~[u8]] {
+        use c_str::CString;
+        use {vec, libc};
+        use vec::CloneableVector;
+
         vec::from_fn(argc as uint, |i| {
-            str::raw::from_c_str(*(argv as **libc::c_char).offset(i as int))
+            let cs = CString::new(*(argv as **libc::c_char).offset(i as int), false);
+            cs.as_bytes_no_nul().to_owned()
         })
     }
 
@@ -149,7 +151,7 @@ mod imp {
             // Preserve the actual global state.
             let saved_value = take();
 
-            let expected = ~[~"happy", ~"today?"];
+            let expected = ~[bytes!("happy").to_owned(), bytes!("today?").to_owned()];
 
             put(expected.clone());
             assert!(clone() == Some(expected.clone()));
@@ -179,15 +181,15 @@ mod imp {
     pub fn cleanup() {
     }
 
-    pub fn take() -> Option<~[~str]> {
+    pub fn take() -> Option<~[~[u8]]> {
         fail!()
     }
 
-    pub fn put(_args: ~[~str]) {
+    pub fn put(_args: ~[~[u8]]) {
         fail!()
     }
 
-    pub fn clone() -> Option<~[~str]> {
+    pub fn clone() -> Option<~[~[u8]]> {
         fail!()
     }
 }