use core::mem;
use core::raw::Slice;
use core::ptr::RawPtr;
-
use string::{mod, String};
use vec::Vec;
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()`
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])
}
}
}
pub mod raw {
+ use core::mem;
+ use core::raw::Slice;
+
use super::String;
use vec::Vec;
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)]
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");
use std::mem;
use std::ptr;
use std::slice;
-use std::str;
+use std::string;
use std::collections::{HashMap, HashSet};
use flate;
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() {
use std::fmt;
use std::slice;
use std::str;
+use std::string;
use std::collections::HashMap;
use html::toc::TocBuilder;
"".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)
}
};
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)]
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"))
}
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 {
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),
// 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")
});
// 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;
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]);