提交 5f8e0ddc 编写于 作者: B Brian Anderson

Merge pull request #1783 from erickt/master

Adding a str::as_bytes<T>(str, fn([u8] -> T) -> T function
......@@ -95,6 +95,7 @@
char_at,
substr_all,
escape_char,
as_bytes,
as_buf,
//buf,
sbuf,
......@@ -390,10 +391,7 @@ fn trim(s: str) -> str { trim_left(trim_right(s)) }
null-terminated.
*/
fn bytes(s: str) -> [u8] unsafe {
let v = ::unsafe::reinterpret_cast(s);
let vcopy = vec::slice(v, 0u, vec::len(v) - 1u);
::unsafe::leak(v);
ret vcopy;
as_bytes(s) { |v| vec::slice(v, 0u, vec::len(v) - 1u) }
}
/*
......@@ -1026,12 +1024,12 @@ fn is_whitespace(s: str) -> bool {
FIXME: rename to 'len_bytes'
*/
pure fn byte_len(s: str) -> uint unsafe {
let v: [u8] = ::unsafe::reinterpret_cast(s);
let vlen = vec::len(v);
::unsafe::leak(v);
// There should always be a null terminator
assert (vlen > 0u);
ret vlen - 1u;
as_bytes(s) { |v|
let vlen = vec::len(v);
// There should always be a null terminator
assert (vlen > 0u);
vlen - 1u
}
}
/*
......@@ -1299,23 +1297,38 @@ fn escape_char(c: char) -> str {
const tag_six_b: uint = 252u;
/*
Function: as_buf
Function: as_bytes
Work with the byte buffer of a string. Allows for unsafe manipulation
of strings, which is useful for native interop.
Example:
> let s = str::as_buf("PATH", { |path_buf| libc::getenv(path_buf) });
> let i = str::as_bytes("Hello World") { |bytes| vec::len(bytes) };
*/
fn as_buf<T>(s: str, f: fn(sbuf) -> T) -> T unsafe {
fn as_bytes<T>(s: str, f: fn([u8]) -> T) -> T unsafe {
let v: [u8] = ::unsafe::reinterpret_cast(s);
let r = vec::as_buf(v, f);
let r = f(v);
::unsafe::leak(v);
r
}
/*
Function: as_buf
Work with the byte buffer of a string. Allows for unsafe manipulation
of strings, which is useful for native interop.
Example:
> let s = str::as_buf("PATH", { |path_buf| libc::getenv(path_buf) });
*/
fn as_buf<T>(s: str, f: fn(sbuf) -> T) -> T unsafe {
as_bytes(s) { |v| vec::as_buf(v, f) }
}
/*
Type: sbuf
......@@ -1373,13 +1386,11 @@ unsafe fn slice_bytes(s: str, begin: uint, end: uint) -> str unsafe {
assert (begin <= end);
assert (end <= byte_len(s));
let v: [u8] = ::unsafe::reinterpret_cast(s);
let v2 = vec::slice(v, begin, end);
let v = as_bytes(s) { |v| vec::slice(v, begin, end) };
v += [0u8];
let s: str = ::unsafe::reinterpret_cast(v);
::unsafe::leak(v);
v2 += [0u8];
let s2: str = ::unsafe::reinterpret_cast(v2);
::unsafe::leak(v2);
ret s2;
ret s;
}
/*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册