]> git.lizzy.rs Git - rust.git/commitdiff
Deprecated `str::raw::from_buf_len`
authorAdolfo Ochagavía <aochagavia92@gmail.com>
Sun, 20 Jul 2014 10:08:40 +0000 (12:08 +0200)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 24 Jul 2014 14:25:43 +0000 (07:25 -0700)
Replaced by `string::raw::from_buf_len`

[breaking-change]

src/libcollections/str.rs
src/libcollections/string.rs
src/librustc/metadata/loader.rs
src/librustdoc/html/markdown.rs
src/libstd/os.rs
src/test/run-pass/const-str-ptr.rs

index e83e617dba7ff53b0b44a1065d457eb463d3b5ba..731c761351c26a8a6c1dee50444a88bee1a5b7d3 100644 (file)
@@ -558,7 +558,6 @@ pub mod raw {
     use core::mem;
     use core::raw::Slice;
     use core::ptr::RawPtr;
-
     use string::{mod, String};
     use vec::Vec;
 
@@ -567,14 +566,10 @@ pub mod raw {
     pub use core::str::raw::{from_utf8, c_str_to_static_slice, slice_bytes};
     pub use core::str::raw::{slice_unchecked};
 
-    /// Create a Rust string from a *u8 buffer of the given length
+    /// Deprecated. Replaced by `string::raw::from_buf_len`
+    #[deprecated = "Use string::raw::from_buf_len"]
     pub unsafe fn from_buf_len(buf: *const u8, len: uint) -> String {
-        let mut result = String::new();
-        result.push_bytes(mem::transmute(Slice {
-            data: buf,
-            len: len,
-        }));
-        result
+        string::raw::from_buf_len(buf, len)
     }
 
     /// Deprecated. Use `CString::as_str().unwrap().to_string()`
@@ -598,22 +593,10 @@ pub unsafe fn from_utf8_owned(v: Vec<u8>) -> String {
         string::raw::from_utf8(v)
     }
 
-    /// Deprecated. Use `String::from_bytes`
-    #[deprecated = "Use String::from_bytes"]
+    /// Deprecated. Use `string::raw::from_utf8`
+    #[deprecated = "Use string::raw::from_utf8"]
     pub unsafe fn from_byte(u: u8) -> String {
-        String::from_bytes(vec![u])
-    }
-
-    #[test]
-    fn test_from_buf_len() {
-        use slice::ImmutableVector;
-
-        unsafe {
-            let a = vec![65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
-            let b = a.as_ptr();
-            let c = from_buf_len(b, 3u);
-            assert_eq!(c, String::from_str("AAA"));
-        }
+        string::raw::from_utf8(vec![u])
     }
 }
 
index 05321f46b11f6bfe30cc10e8c9ffc7be75ff4d01..bb8424bd36338aff5f57dd39ef5e9faab9c28e6c 100644 (file)
@@ -571,6 +571,9 @@ fn add(&self, other: &S) -> String {
 }
 
 pub mod raw {
+    use core::mem;
+    use core::raw::Slice;
+
     use super::String;
     use vec::Vec;
 
@@ -581,6 +584,20 @@ pub mod raw {
     pub unsafe fn from_utf8(bytes: Vec<u8>) -> String {
         String { vec: bytes }
     }
+
+    /// Create a Rust string from a *u8 buffer of the given length
+    ///
+    /// This function is unsafe because of two reasons:
+    /// * A raw pointer is dereferenced and transmuted to `&[u8]`
+    /// * The slice is not checked to see whether it contains valid UTF-8
+    pub unsafe fn from_buf_len(buf: *const u8, len: uint) -> String {
+        use slice::CloneableVector;
+        let slice: &[u8] = mem::transmute(Slice {
+            data: buf,
+            len: len,
+        });
+        self::from_utf8(slice.to_vec())
+    }
 }
 
 #[cfg(test)]
@@ -740,6 +757,14 @@ fn test_from_utf16_lossy() {
                    String::from_str("\uFFFD𐒋\uFFFD"));
     }
 
+    #[test]
+    fn test_from_buf_len() {
+        unsafe {
+            let a = vec![65u8, 65, 65, 65, 65, 65, 65, 0];
+            assert_eq!(super::raw::from_buf_len(a.as_ptr(), 3), String::from_str("AAA"));
+        }
+    }
+
     #[test]
     fn test_push_bytes() {
         let mut s = String::from_str("ABC");
index e7d52ef3b3d6cb1b54cb124e8207bfe39ba47802..1811c4f8612f01560e8678b2451e7f5c4a33d825 100644 (file)
 use std::mem;
 use std::ptr;
 use std::slice;
-use std::str;
+use std::string;
 
 use std::collections::{HashMap, HashSet};
 use flate;
@@ -772,7 +772,7 @@ fn get_metadata_section_imp(os: abi::Os, filename: &Path) -> Result<MetadataBlob
         while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
             let mut name_buf = ptr::null();
             let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
-            let name = str::raw::from_buf_len(name_buf as *const u8,
+            let name = string::raw::from_buf_len(name_buf as *const u8,
                                               name_len as uint);
             debug!("get_metadata_section: name {}", name);
             if read_meta_section_name(os).as_slice() == name.as_slice() {
index 19a9bcb9a17ab97878d6d73fe031bb47df1c0da1..97fa58d50777401da65ee74765eb8073c2384319 100644 (file)
@@ -32,6 +32,7 @@
 use std::fmt;
 use std::slice;
 use std::str;
+use std::string;
 use std::collections::HashMap;
 
 use html::toc::TocBuilder;
@@ -222,7 +223,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
             "".to_string()
         } else {
             unsafe {
-                str::raw::from_buf_len((*text).data, (*text).size as uint)
+                string::raw::from_buf_len((*text).data, (*text).size as uint)
             }
         };
 
index 1b61dec99b4bab94de6aa878665e7cb6cea4931a..f5183ab7188cbe852cdf44664472a980a52cd8fe 100644 (file)
@@ -47,9 +47,9 @@
 use result::{Err, Ok, Result};
 use slice::{Vector, ImmutableVector, MutableVector, ImmutableEqVector};
 use str::{Str, StrSlice, StrAllocating};
-use str;
 use string::String;
 use sync::atomics::{AtomicInt, INIT_ATOMIC_INT, SeqCst};
+use to_str::ToString;
 use vec::Vec;
 
 #[cfg(unix)]
@@ -135,7 +135,7 @@ pub fn getcwd() -> Path {
             fail!();
         }
     }
-    Path::new(String::from_utf16(str::truncate_utf16_at_nul(buf))
+    Path::new(String::from_utf16(::str::truncate_utf16_at_nul(buf))
               .expect("GetCurrentDirectoryW returned invalid UTF-16"))
 }
 
@@ -413,7 +413,7 @@ fn _setenv(n: &str, v: &[u8]) {
     fn _setenv(n: &str, v: &[u8]) {
         let n: Vec<u16> = n.utf16_units().collect();
         let n = n.append_one(0);
-        let v: Vec<u16> = str::from_utf8(v).unwrap().utf16_units().collect();
+        let v: Vec<u16> = ::str::from_utf8(v).unwrap().utf16_units().collect();
         let v = v.append_one(0);
 
         unsafe {
@@ -1045,7 +1045,7 @@ fn FormatMessageW(flags: DWORD,
                 return format!("OS Error {} (FormatMessageW() returned error {})", errnum, fm_err);
             }
 
-            let msg = String::from_utf16(str::truncate_utf16_at_nul(buf));
+            let msg = String::from_utf16(::str::truncate_utf16_at_nul(buf));
             match msg {
                 Some(msg) => format!("OS Error {}: {}", errnum, msg),
                 None => format!("OS Error {} (FormatMessageW() returned invalid UTF-16)", errnum),
@@ -1202,7 +1202,7 @@ fn real_args() -> Vec<String> {
 
         // Push it onto the list.
         let opt_s = slice::raw::buf_as_slice(ptr as *const _, len, |buf| {
-            String::from_utf16(str::truncate_utf16_at_nul(buf))
+            String::from_utf16(::str::truncate_utf16_at_nul(buf))
         });
         opt_s.expect("CommandLineToArgvW returned invalid UTF-16")
     });
index 57d262d3268a16bd5a9558be82943988d6193bbf..0fef3dd4aac24937dc794d4a57425307fac2579a 100644 (file)
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use std::str;
+use std::{str, string};
 
 static A: [u8, ..2] = ['h' as u8, 'i' as u8];
 static B: &'static [u8, ..2] = &A;
@@ -18,8 +18,8 @@ pub fn main() {
     unsafe {
         let foo = &A as *const u8;
         assert_eq!(str::raw::from_utf8(A), "hi");
-        assert_eq!(str::raw::from_buf_len(foo, A.len()), "hi".to_string());
-        assert_eq!(str::raw::from_buf_len(C, B.len()), "hi".to_string());
+        assert_eq!(string::raw::from_buf_len(foo, A.len()), "hi".to_string());
+        assert_eq!(string::raw::from_buf_len(C, B.len()), "hi".to_string());
         assert!(*C == A[0]);
         assert!(*(&B[0] as *const u8) == A[0]);