Rename str::from_byte(s) to str::unsafe::from_byte(s),

mark them as unsafe,
make comp/driver/driver.rs use str::from_bytes...
This commit is contained in:
Kevin Cantu 2012-01-30 19:52:38 -08:00 committed by Brian Anderson
parent 24668d7578
commit 03b8c8df01
2 changed files with 47 additions and 42 deletions

View File

@ -80,7 +80,7 @@ fn parse_input(sess: session, cfg: ast::crate_cfg, input: str)
if !input_is_stdin(input) {
parser::parse_crate_from_file(input, cfg, sess.parse_sess)
} else {
let src = @str::unsafe_from_bytes(io::stdin().read_whole_stream());
let src = @str::from_bytes(io::stdin().read_whole_stream());
parser::parse_crate_from_source_str(input, src, cfg, sess.parse_sess)
}
}

View File

@ -12,9 +12,7 @@ For some heavy-duty uses, we recommend trying std::rope.
export
// Creating a string
from_bytes,
unsafe_from_bytes,
from_byte,
unsafe_from_byte,
//push_utf8_bytes,
from_char,
from_chars,
@ -120,37 +118,11 @@ Function: from_bytes
Convert a vector of bytes to a UTF-8 string. Fails if invalid UTF-8.
*/
fn from_bytes(vv: [u8]) -> str {
fn from_bytes(vv: [u8]) -> str unsafe {
assert is_utf8(vv);
ret unsafe_from_bytes(vv);
ret unsafe::from_bytes(vv);
}
/*
Function: unsafe_from_bytes
Converts a vector of bytes to a string. Does not verify that the
vector contains valid UTF-8.
FIXME: stop exporting
*/
fn unsafe_from_bytes(v: [const u8]) -> str unsafe {
let vcopy: [u8] = v + [0u8];
let scopy: str = unsafe::reinterpret_cast(vcopy);
unsafe::leak(vcopy);
ret scopy;
}
/*
Function: unsafe_from_byte
Converts a byte to a string. Does not verify that the byte is
valid UTF-8.
FIXME: stop exporting
*/
fn unsafe_from_byte(u: u8) -> str { unsafe_from_bytes([u]) }
/*
Function: from_byte
@ -211,6 +183,7 @@ fn from_chars(chs: [char]) -> str {
ret buf;
}
// FIXME: not unsafe now
/*
Function: from_cstr
@ -413,9 +386,9 @@ Converts a string to a vector of bytes. The result vector is not
null-terminated.
*/
fn bytes(s: str) -> [u8] unsafe {
let v = unsafe::reinterpret_cast(s);
let v = ::unsafe::reinterpret_cast(s);
let vcopy = vec::slice(v, 0u, vec::len(v) - 1u);
unsafe::leak(v);
::unsafe::leak(v);
ret vcopy;
}
@ -492,12 +465,12 @@ fn slice(s: str, begin: uint, end: uint) -> str unsafe {
assert (begin <= end);
assert (end <= byte_len(s));
let v: [u8] = unsafe::reinterpret_cast(s);
let v: [u8] = ::unsafe::reinterpret_cast(s);
let v2 = vec::slice(v, begin, end);
unsafe::leak(v);
::unsafe::leak(v);
v2 += [0u8];
let s2: str = unsafe::reinterpret_cast(v2);
unsafe::leak(v2);
let s2: str = ::unsafe::reinterpret_cast(v2);
::unsafe::leak(v2);
ret s2;
}
@ -1093,9 +1066,9 @@ Returns the length in bytes of a string
FIXME: rename to 'len_bytes'?
*/
pure fn byte_len(s: str) -> uint unsafe {
let v: [u8] = unsafe::reinterpret_cast(s);
let v: [u8] = ::unsafe::reinterpret_cast(s);
let vlen = vec::len(v);
unsafe::leak(v);
::unsafe::leak(v);
// There should always be a null terminator
assert (vlen > 0u);
ret vlen - 1u;
@ -1371,7 +1344,7 @@ const tag_six_b: uint = 252u;
// no guarantee that the string is rooted). Instead, use as_buf below.
unsafe fn buf(s: str) -> sbuf {
let saddr = ptr::addr_of(s);
let vaddr: *[u8] = unsafe::reinterpret_cast(saddr);
let vaddr: *[u8] = ::unsafe::reinterpret_cast(saddr);
let buf = vec::to_ptr(*vaddr);
ret buf;
}
@ -1398,6 +1371,38 @@ An unsafe buffer of bytes. Corresponds to a C char pointer.
*/
type sbuf = *u8;
mod unsafe {
export
// UNSAFE
from_bytes,
from_byte;
/*
Function: unsafe::from_bytes
Converts a vector of bytes to a string. Does not verify that the
vector contains valid UTF-8.
FIXME: stop exporting
*/
unsafe fn from_bytes(v: [const u8]) -> str unsafe {
let vcopy: [u8] = v + [0u8];
let scopy: str = ::unsafe::reinterpret_cast(vcopy);
::unsafe::leak(vcopy);
ret scopy;
}
/*
Function: unsafe::from_byte
Converts a byte to a string. Does not verify that the byte is
valid UTF-8.
FIXME: stop exporting
*/
unsafe fn from_byte(u: u8) -> str { unsafe::from_bytes([u]) }
}
#[cfg(test)]
mod tests {
@ -1771,9 +1776,9 @@ mod tests {
}
#[test]
fn test_unsafe_from_bytes() {
fn test_unsafe_from_bytes() unsafe {
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8];
let b = unsafe_from_bytes(a);
let b = unsafe::from_bytes(a);
assert (b == "AAAAAAA");
}