diff options
| author | Brian Anderson <banderson@mozilla.com> | 2012-03-19 15:25:26 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-03-19 15:47:52 -0700 |
| commit | 20417ebf31b811cac736db959a028c994bc91f3c (patch) | |
| tree | 13d2bbe76a1fb4ad8b9b0575d5eb8fbbb0ef8f9e /src/libcore | |
| parent | 1a40aa0935357cbfc8bba6e7b0fd7c5461c9732e (diff) | |
| download | rust-20417ebf31b811cac736db959a028c994bc91f3c.tar.gz rust-20417ebf31b811cac736db959a028c994bc91f3c.zip | |
core: Move unsafe conversions to str::unsafe
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/os.rs | 4 | ||||
| -rw-r--r-- | src/libcore/str.rs | 84 |
2 files changed, 45 insertions, 43 deletions
diff --git a/src/libcore/os.rs b/src/libcore/os.rs index dea86b5af25..cf638470c6e 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<str> unsafe { option::none::<str> } else { let s = unsafe::reinterpret_cast(s); - option::some::<str>(str::from_buf(s)) + option::some::<str>(str::unsafe::from_buf(s)) }; } diff --git a/src/libcore/str.rs b/src/libcore/str.rs index e0c15abe590..b239579ba2d 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)); } |
