Skip to content

Commit

Permalink
Deprecated str::raw::from_buf_len
Browse files Browse the repository at this point in the history
Replaced by `string::raw::from_buf_len`

[breaking-change]
  • Loading branch information
aochagavia authored and alexcrichton committed Jul 24, 2014
1 parent feeae27 commit 6e509d3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 34 deletions.
29 changes: 6 additions & 23 deletions src/libcollections/str.rs
Expand Up @@ -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;

Expand All @@ -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()`
Expand All @@ -598,22 +593,10 @@ pub mod raw {
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])
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/libcollections/string.rs
Expand Up @@ -571,6 +571,9 @@ impl<S: Str> Add<S, String> for String {
}

pub mod raw {
use core::mem;
use core::raw::Slice;

use super::String;
use vec::Vec;

Expand All @@ -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)]
Expand Down Expand Up @@ -740,6 +757,14 @@ mod tests {
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");
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/metadata/loader.rs
Expand Up @@ -233,7 +233,7 @@ use std::io;
use std::mem;
use std::ptr;
use std::slice;
use std::str;
use std::string;

use std::collections::{HashMap, HashSet};
use flate;
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/html/markdown.rs
Expand Up @@ -32,6 +32,7 @@ use std::cell::{RefCell, Cell};
use std::fmt;
use std::slice;
use std::str;
use std::string;
use std::collections::HashMap;

use html::toc::TocBuilder;
Expand Down Expand Up @@ -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)
}
};

Expand Down
10 changes: 5 additions & 5 deletions src/libstd/os.rs
Expand Up @@ -47,9 +47,9 @@ use ptr;
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)]
Expand Down Expand Up @@ -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"))
}

Expand Down Expand Up @@ -413,7 +413,7 @@ pub fn setenv<T: BytesContainer>(n: &str, v: T) {
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 {
Expand Down Expand Up @@ -1045,7 +1045,7 @@ pub fn error_string(errnum: uint) -> String {
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),
Expand Down Expand Up @@ -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")
});
Expand Down
6 changes: 3 additions & 3 deletions src/test/run-pass/const-str-ptr.rs
Expand Up @@ -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;
Expand All @@ -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]);

Expand Down

0 comments on commit 6e509d3

Please sign in to comment.