From 6e509d3462d20b696a1b5d18f14884b4e391a6ba Mon Sep 17 00:00:00 2001 From: =?utf8?q?Adolfo=20Ochagav=C3=ADa?= Date: Sun, 20 Jul 2014 12:08:40 +0200 Subject: [PATCH] Deprecated `str::raw::from_buf_len` Replaced by `string::raw::from_buf_len` [breaking-change] --- src/libcollections/str.rs | 29 ++++++----------------------- src/libcollections/string.rs | 25 +++++++++++++++++++++++++ src/librustc/metadata/loader.rs | 4 ++-- src/librustdoc/html/markdown.rs | 3 ++- src/libstd/os.rs | 10 +++++----- src/test/run-pass/const-str-ptr.rs | 6 +++--- 6 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index e83e617dba7..731c761351c 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -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) -> 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]) } } diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 05321f46b11..bb8424bd363 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -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) -> 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"); diff --git a/src/librustc/metadata/loader.rs b/src/librustc/metadata/loader.rs index e7d52ef3b3d..1811c4f8612 100644 --- a/src/librustc/metadata/loader.rs +++ b/src/librustc/metadata/loader.rs @@ -233,7 +233,7 @@ 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 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) } }; diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 1b61dec99b4..f5183ab7188 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -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 = n.utf16_units().collect(); let n = n.append_one(0); - let v: Vec = str::from_utf8(v).unwrap().utf16_units().collect(); + let v: Vec = ::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 { // 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") }); diff --git a/src/test/run-pass/const-str-ptr.rs b/src/test/run-pass/const-str-ptr.rs index 57d262d3268..0fef3dd4aac 100644 --- a/src/test/run-pass/const-str-ptr.rs +++ b/src/test/run-pass/const-str-ptr.rs @@ -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]); -- 2.44.0