Skip to content

Commit

Permalink
core: Rename str::from_cstr et. al to from_buf
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Mar 15, 2012
1 parent 9e48070 commit 3a2df84
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/libcore/os.rs
Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
};
}

Expand Down
44 changes: 22 additions & 22 deletions src/libcore/str.rs
Expand Up @@ -14,8 +14,8 @@ export
push_char,
from_char,
from_chars,
from_cstr,
from_cstr_len,
from_buf,
from_buf_len,
concat,
connect,

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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");
}

Expand All @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion src/rustc/back/link.rs
Expand Up @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/metadata/creader.rs
Expand Up @@ -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;
Expand Down

0 comments on commit 3a2df84

Please sign in to comment.