From 20417ebf31b811cac736db959a028c994bc91f3c Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 19 Mar 2012 15:25:26 -0700 Subject: [PATCH] core: Move unsafe conversions to str::unsafe --- src/libcore/os.rs | 4 +- src/libcore/str.rs | 84 ++++++++++++++++++----------------- src/rustc/back/link.rs | 2 +- src/rustc/metadata/creader.rs | 2 +- 4 files changed, 47 insertions(+), 45 deletions(-) diff --git a/src/libcore/os.rs b/src/libcore/os.rs index dea86b5af250..cf638470c6e2 100644 --- a/src/libcore/os.rs +++ b/src/libcore/os.rs @@ -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)); vec::as_mut_buf(buf) { |b| 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 { none } @@ -125,7 +125,7 @@ fn getenv(n: str) -> option unsafe { option::none:: } else { let s = unsafe::reinterpret_cast(s); - option::some::(str::from_buf(s)) + option::some::(str::unsafe::from_buf(s)) }; } diff --git a/src/libcore/str.rs b/src/libcore/str.rs index e0c15abe590f..b239579ba2df 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -13,10 +13,6 @@ export from_byte, from_char, from_chars, - from_buf, - from_buf_len, - from_c_str, - from_c_str_len, push_char, concat, connect, @@ -185,40 +181,6 @@ fn from_chars(chs: [char]) -> str { 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"] fn concat(v: [str]) -> str { let mut s: str = ""; @@ -1522,6 +1484,10 @@ fn reserve(&ss: str, nn: uint) { mod unsafe { export // FIXME: stop exporting several of these + from_buf, + from_buf_len, + from_c_str, + from_c_str_len, from_bytes, from_byte, slice_bytes, @@ -1531,6 +1497,42 @@ mod unsafe { shift_byte, 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 = " Converts a vector of bytes to a string. @@ -2222,7 +2224,7 @@ mod tests { fn test_from_buf() unsafe { let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8]; let b = vec::unsafe::to_ptr(a); - let c = from_buf(b); + let c = unsafe::from_buf(b); assert (c == "AAAAAAA"); } @@ -2230,7 +2232,7 @@ mod tests { fn test_from_buf_len() unsafe { let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8]; 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"); } @@ -2252,7 +2254,7 @@ mod tests { fn test_as_buf2() unsafe { let s = "hello"; 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)); } diff --git a/src/rustc/back/link.rs b/src/rustc/back/link.rs index 4ecd29d4a979..4ff6e3307974 100644 --- a/src/rustc/back/link.rs +++ b/src/rustc/back/link.rs @@ -27,7 +27,7 @@ fn llvm_err(sess: session, msg: str) -> ! unsafe { let cstr = llvm::LLVMRustGetLastError(); if cstr == ptr::null() { 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 { diff --git a/src/rustc/metadata/creader.rs b/src/rustc/metadata/creader.rs index 1560b4642ce2..6f11b3817a71 100644 --- a/src/rustc/metadata/creader.rs +++ b/src/rustc/metadata/creader.rs @@ -216,7 +216,7 @@ fn get_metadata_section(sess: session::session, let si = mk_section_iter(of.llof); while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False { 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) { let cbuf = llvm::LLVMGetSectionContents(si.llsi); let csz = llvm::LLVMGetSectionSize(si.llsi) as uint;