core: Move unsafe conversions to str::unsafe
This commit is contained in:
@@ -66,7 +66,7 @@ fn fill_charp_buf(f: fn(*mutable c_char, size_t) -> bool)
|
|||||||
let buf = vec::to_mut(vec::from_elem(tmpbuf_sz, 0u8 as c_char));
|
let buf = vec::to_mut(vec::from_elem(tmpbuf_sz, 0u8 as c_char));
|
||||||
vec::as_mut_buf(buf) { |b|
|
vec::as_mut_buf(buf) { |b|
|
||||||
if f(b, tmpbuf_sz as size_t) unsafe {
|
if f(b, tmpbuf_sz as size_t) unsafe {
|
||||||
some(str::from_buf(b as *u8))
|
some(str::unsafe::from_buf(b as *u8))
|
||||||
} else {
|
} else {
|
||||||
none
|
none
|
||||||
}
|
}
|
||||||
@@ -125,7 +125,7 @@ fn getenv(n: str) -> option<str> unsafe {
|
|||||||
option::none::<str>
|
option::none::<str>
|
||||||
} else {
|
} else {
|
||||||
let s = unsafe::reinterpret_cast(s);
|
let s = unsafe::reinterpret_cast(s);
|
||||||
option::some::<str>(str::from_buf(s))
|
option::some::<str>(str::unsafe::from_buf(s))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,10 +13,6 @@ export
|
|||||||
from_byte,
|
from_byte,
|
||||||
from_char,
|
from_char,
|
||||||
from_chars,
|
from_chars,
|
||||||
from_buf,
|
|
||||||
from_buf_len,
|
|
||||||
from_c_str,
|
|
||||||
from_c_str_len,
|
|
||||||
push_char,
|
push_char,
|
||||||
concat,
|
concat,
|
||||||
connect,
|
connect,
|
||||||
@@ -185,40 +181,6 @@ fn from_chars(chs: [char]) -> str {
|
|||||||
ret buf;
|
ret buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc = "Create a Rust string from a null-terminated *u8 buffer"]
|
|
||||||
unsafe fn from_buf(buf: *u8) -> str {
|
|
||||||
let mut curr = buf, i = 0u;
|
|
||||||
while *curr != 0u8 {
|
|
||||||
i += 1u;
|
|
||||||
curr = ptr::offset(buf, i);
|
|
||||||
}
|
|
||||||
ret from_buf_len(buf, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Create a Rust string from a null-terminated C string"]
|
|
||||||
unsafe fn from_c_str(c_str: *libc::c_char) -> str {
|
|
||||||
from_buf(::unsafe::reinterpret_cast(c_str))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Create a Rust string from a *u8 buffer of the given length"]
|
|
||||||
unsafe fn from_buf_len(buf: *u8, len: uint) -> str {
|
|
||||||
let mut v: [u8] = [];
|
|
||||||
vec::reserve(v, len + 1u);
|
|
||||||
vec::as_buf(v) {|b| ptr::memcpy(b, buf, len); }
|
|
||||||
vec::unsafe::set_len(v, len);
|
|
||||||
v += [0u8];
|
|
||||||
|
|
||||||
assert is_utf8(v);
|
|
||||||
let s: str = ::unsafe::reinterpret_cast(v);
|
|
||||||
::unsafe::leak(v);
|
|
||||||
ret s;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Create a Rust string from a `*c_char` buffer of the given length"]
|
|
||||||
unsafe fn from_c_str_len(c_str: *libc::c_char, len: uint) -> str {
|
|
||||||
from_buf_len(::unsafe::reinterpret_cast(c_str), len)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc = "Concatenate a vector of strings"]
|
#[doc = "Concatenate a vector of strings"]
|
||||||
fn concat(v: [str]) -> str {
|
fn concat(v: [str]) -> str {
|
||||||
let mut s: str = "";
|
let mut s: str = "";
|
||||||
@@ -1522,6 +1484,10 @@ fn reserve(&ss: str, nn: uint) {
|
|||||||
mod unsafe {
|
mod unsafe {
|
||||||
export
|
export
|
||||||
// FIXME: stop exporting several of these
|
// FIXME: stop exporting several of these
|
||||||
|
from_buf,
|
||||||
|
from_buf_len,
|
||||||
|
from_c_str,
|
||||||
|
from_c_str_len,
|
||||||
from_bytes,
|
from_bytes,
|
||||||
from_byte,
|
from_byte,
|
||||||
slice_bytes,
|
slice_bytes,
|
||||||
@@ -1531,6 +1497,42 @@ mod unsafe {
|
|||||||
shift_byte,
|
shift_byte,
|
||||||
set_len;
|
set_len;
|
||||||
|
|
||||||
|
#[doc = "Create a Rust string from a null-terminated *u8 buffer"]
|
||||||
|
unsafe fn from_buf(buf: *u8) -> str {
|
||||||
|
let mut curr = buf, i = 0u;
|
||||||
|
while *curr != 0u8 {
|
||||||
|
i += 1u;
|
||||||
|
curr = ptr::offset(buf, i);
|
||||||
|
}
|
||||||
|
ret from_buf_len(buf, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc = "Create a Rust string from a *u8 buffer of the given length"]
|
||||||
|
unsafe fn from_buf_len(buf: *u8, len: uint) -> str {
|
||||||
|
let mut v: [u8] = [];
|
||||||
|
vec::reserve(v, len + 1u);
|
||||||
|
vec::as_buf(v) {|b| ptr::memcpy(b, buf, len); }
|
||||||
|
vec::unsafe::set_len(v, len);
|
||||||
|
v += [0u8];
|
||||||
|
|
||||||
|
assert is_utf8(v);
|
||||||
|
let s: str = ::unsafe::reinterpret_cast(v);
|
||||||
|
::unsafe::leak(v);
|
||||||
|
ret s;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc = "Create a Rust string from a null-terminated C string"]
|
||||||
|
unsafe fn from_c_str(c_str: *libc::c_char) -> str {
|
||||||
|
from_buf(::unsafe::reinterpret_cast(c_str))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc = "
|
||||||
|
Create a Rust string from a `*c_char` buffer of the given length
|
||||||
|
"]
|
||||||
|
unsafe fn from_c_str_len(c_str: *libc::c_char, len: uint) -> str {
|
||||||
|
from_buf_len(::unsafe::reinterpret_cast(c_str), len)
|
||||||
|
}
|
||||||
|
|
||||||
#[doc = "
|
#[doc = "
|
||||||
Converts a vector of bytes to a string.
|
Converts a vector of bytes to a string.
|
||||||
|
|
||||||
@@ -2222,7 +2224,7 @@ mod tests {
|
|||||||
fn test_from_buf() unsafe {
|
fn test_from_buf() unsafe {
|
||||||
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
|
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
|
||||||
let b = vec::unsafe::to_ptr(a);
|
let b = vec::unsafe::to_ptr(a);
|
||||||
let c = from_buf(b);
|
let c = unsafe::from_buf(b);
|
||||||
assert (c == "AAAAAAA");
|
assert (c == "AAAAAAA");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2230,7 +2232,7 @@ mod tests {
|
|||||||
fn test_from_buf_len() unsafe {
|
fn test_from_buf_len() unsafe {
|
||||||
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
|
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
|
||||||
let b = vec::unsafe::to_ptr(a);
|
let b = vec::unsafe::to_ptr(a);
|
||||||
let c = from_buf_len(b, 3u);
|
let c = unsafe::from_buf_len(b, 3u);
|
||||||
assert (c == "AAA");
|
assert (c == "AAA");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2252,7 +2254,7 @@ mod tests {
|
|||||||
fn test_as_buf2() unsafe {
|
fn test_as_buf2() unsafe {
|
||||||
let s = "hello";
|
let s = "hello";
|
||||||
let sb = as_buf(s, {|b| b });
|
let sb = as_buf(s, {|b| b });
|
||||||
let s_cstr = from_buf(sb);
|
let s_cstr = unsafe::from_buf(sb);
|
||||||
assert (eq(s_cstr, s));
|
assert (eq(s_cstr, s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ fn llvm_err(sess: session, msg: str) -> ! unsafe {
|
|||||||
let cstr = llvm::LLVMRustGetLastError();
|
let cstr = llvm::LLVMRustGetLastError();
|
||||||
if cstr == ptr::null() {
|
if cstr == ptr::null() {
|
||||||
sess.fatal(msg);
|
sess.fatal(msg);
|
||||||
} else { sess.fatal(msg + ": " + str::from_c_str(cstr)); }
|
} else { sess.fatal(msg + ": " + str::unsafe::from_c_str(cstr)); }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_intrinsics_bc(sess: session) -> option<ModuleRef> {
|
fn load_intrinsics_bc(sess: session) -> option<ModuleRef> {
|
||||||
|
|||||||
@@ -216,7 +216,7 @@ fn get_metadata_section(sess: session::session,
|
|||||||
let si = mk_section_iter(of.llof);
|
let si = mk_section_iter(of.llof);
|
||||||
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
|
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
|
||||||
let name_buf = llvm::LLVMGetSectionName(si.llsi);
|
let name_buf = llvm::LLVMGetSectionName(si.llsi);
|
||||||
let name = unsafe { str::from_c_str(name_buf) };
|
let name = unsafe { str::unsafe::from_c_str(name_buf) };
|
||||||
if str::eq(name, sess.targ_cfg.target_strs.meta_sect_name) {
|
if str::eq(name, sess.targ_cfg.target_strs.meta_sect_name) {
|
||||||
let cbuf = llvm::LLVMGetSectionContents(si.llsi);
|
let cbuf = llvm::LLVMGetSectionContents(si.llsi);
|
||||||
let csz = llvm::LLVMGetSectionSize(si.llsi) as uint;
|
let csz = llvm::LLVMGetSectionSize(si.llsi) as uint;
|
||||||
|
|||||||
Reference in New Issue
Block a user