core: Rename str::from_cstr et. al to from_buf

This commit is contained in:
Brian Anderson
2012-03-14 14:02:07 -07:00
parent 9e480708a2
commit 3a2df84d89
4 changed files with 26 additions and 26 deletions

View File

@@ -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) { if f(b, tmpbuf_sz as size_t) {
some(str::from_cstr(b as *u8)) some(str::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_cstr(s)) option::some::<str>(str::from_buf(s))
}; };
} }

View File

@@ -14,8 +14,8 @@ export
push_char, push_char,
from_char, from_char,
from_chars, from_chars,
from_cstr, from_buf,
from_cstr_len, from_buf_len,
concat, concat,
connect, connect,
@@ -182,27 +182,27 @@ fn from_chars(chs: [char]) -> str {
ret buf; ret buf;
} }
#[doc = "Create a Rust string from a null-terminated C string"] #[doc = "Create a Rust string from a null-terminated *u8 buffer"]
fn from_cstr(cstr: *u8) -> str unsafe { fn from_buf(buf: *u8) -> str unsafe {
let mut curr = cstr, i = 0u; let mut curr = buf, i = 0u;
while *curr != 0u8 { while *curr != 0u8 {
i += 1u; i += 1u;
curr = ptr::offset(cstr, i); curr = ptr::offset(buf, i);
} }
ret from_cstr_len(cstr, i); ret from_buf_len(buf, i);
} }
#[doc = "Create a Rust string from a C string of the given length"] #[doc = "Create a Rust string from a *u8 buffer of the given length"]
fn from_cstr_len(cstr: *u8, len: uint) -> str unsafe { fn from_buf_len(buf: *u8, len: uint) -> str unsafe {
let mut buf: [u8] = []; let mut v: [u8] = [];
vec::reserve(buf, len + 1u); vec::reserve(v, len + 1u);
vec::as_buf(buf) {|b| ptr::memcpy(b, cstr, len); } vec::as_buf(v) {|b| ptr::memcpy(b, buf, len); }
vec::unsafe::set_len(buf, len); vec::unsafe::set_len(v, len);
buf += [0u8]; v += [0u8];
assert is_utf8(buf); assert is_utf8(v);
let s: str = ::unsafe::reinterpret_cast(buf); let s: str = ::unsafe::reinterpret_cast(v);
::unsafe::leak(buf); ::unsafe::leak(v);
ret s; ret s;
} }
@@ -1946,18 +1946,18 @@ mod tests {
} }
#[test] #[test]
fn test_from_cstr() 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_cstr(b); let c = from_buf(b);
assert (c == "AAAAAAA"); assert (c == "AAAAAAA");
} }
#[test] #[test]
fn test_from_cstr_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_cstr_len(b, 3u); let c = from_buf_len(b, 3u);
assert (c == "AAA"); assert (c == "AAA");
} }
@@ -1979,7 +1979,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_cstr(sb); let s_cstr = from_buf(sb);
assert (eq(s_cstr, s)); assert (eq(s_cstr, s));
} }

View File

@@ -27,7 +27,7 @@ fn llvm_err(sess: session, msg: str) -> ! unsafe {
let buf = llvm::LLVMRustGetLastError(); let buf = llvm::LLVMRustGetLastError();
if buf == ptr::null() { if buf == ptr::null() {
sess.fatal(msg); sess.fatal(msg);
} else { sess.fatal(msg + ": " + str::from_cstr(buf)); } } else { sess.fatal(msg + ": " + str::from_buf(buf)); }
} }
fn load_intrinsics_bc(sess: session) -> option<ModuleRef> { fn load_intrinsics_bc(sess: session) -> option<ModuleRef> {

View File

@@ -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_cstr(name_buf) }; let name = unsafe { str::from_buf(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;