core: Rename str::from_cstr et. al to from_buf
This commit is contained in:
parent
9e480708a2
commit
3a2df84d89
4 changed files with 26 additions and 26 deletions
|
@ -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) {
|
||||
some(str::from_cstr(b as *u8))
|
||||
some(str::from_buf(b as *u8))
|
||||
} else {
|
||||
none
|
||||
}
|
||||
|
@ -125,7 +125,7 @@ fn getenv(n: str) -> option<str> unsafe {
|
|||
option::none::<str>
|
||||
} else {
|
||||
let s = unsafe::reinterpret_cast(s);
|
||||
option::some::<str>(str::from_cstr(s))
|
||||
option::some::<str>(str::from_buf(s))
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -14,8 +14,8 @@ export
|
|||
push_char,
|
||||
from_char,
|
||||
from_chars,
|
||||
from_cstr,
|
||||
from_cstr_len,
|
||||
from_buf,
|
||||
from_buf_len,
|
||||
concat,
|
||||
connect,
|
||||
|
||||
|
@ -182,27 +182,27 @@ fn from_chars(chs: [char]) -> str {
|
|||
ret buf;
|
||||
}
|
||||
|
||||
#[doc = "Create a Rust string from a null-terminated C string"]
|
||||
fn from_cstr(cstr: *u8) -> str unsafe {
|
||||
let mut curr = cstr, i = 0u;
|
||||
#[doc = "Create a Rust string from a null-terminated *u8 buffer"]
|
||||
fn from_buf(buf: *u8) -> str unsafe {
|
||||
let mut curr = buf, i = 0u;
|
||||
while *curr != 0u8 {
|
||||
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"]
|
||||
fn from_cstr_len(cstr: *u8, len: uint) -> str unsafe {
|
||||
let mut buf: [u8] = [];
|
||||
vec::reserve(buf, len + 1u);
|
||||
vec::as_buf(buf) {|b| ptr::memcpy(b, cstr, len); }
|
||||
vec::unsafe::set_len(buf, len);
|
||||
buf += [0u8];
|
||||
#[doc = "Create a Rust string from a *u8 buffer of the given length"]
|
||||
fn from_buf_len(buf: *u8, len: uint) -> str unsafe {
|
||||
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(buf);
|
||||
let s: str = ::unsafe::reinterpret_cast(buf);
|
||||
::unsafe::leak(buf);
|
||||
assert is_utf8(v);
|
||||
let s: str = ::unsafe::reinterpret_cast(v);
|
||||
::unsafe::leak(v);
|
||||
ret s;
|
||||
}
|
||||
|
||||
|
@ -1946,18 +1946,18 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_cstr() unsafe {
|
||||
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_cstr(b);
|
||||
let c = from_buf(b);
|
||||
assert (c == "AAAAAAA");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_cstr_len() unsafe {
|
||||
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_cstr_len(b, 3u);
|
||||
let c = from_buf_len(b, 3u);
|
||||
assert (c == "AAA");
|
||||
}
|
||||
|
||||
|
@ -1979,7 +1979,7 @@ mod tests {
|
|||
fn test_as_buf2() unsafe {
|
||||
let s = "hello";
|
||||
let sb = as_buf(s, {|b| b });
|
||||
let s_cstr = from_cstr(sb);
|
||||
let s_cstr = from_buf(sb);
|
||||
assert (eq(s_cstr, s));
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ fn llvm_err(sess: session, msg: str) -> ! unsafe {
|
|||
let buf = llvm::LLVMRustGetLastError();
|
||||
if buf == ptr::null() {
|
||||
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> {
|
||||
|
|
|
@ -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_cstr(name_buf) };
|
||||
let name = unsafe { str::from_buf(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;
|
||||
|
|
Loading…
Reference in a new issue