From 1359439da825b15afcff93014123b75e2df2e84b Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 22 Aug 2012 17:03:57 -0700 Subject: [PATCH] libcore: Add from_buf_len_nocopy --- src/libcore/str.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 5f1c100a15507..ec61ace2f1305 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -1768,6 +1768,7 @@ mod unsafe { export from_buf, from_buf_len, + from_buf_len_nocopy, from_c_str, from_c_str_len, from_bytes, @@ -1799,6 +1800,13 @@ mod unsafe { return ::unsafe::transmute(v); } + /// Create a Rust string from a *u8 buffer of the given length without copying + unsafe fn from_buf_len_nocopy(buf: &a / *u8, len: uint) -> &a / str { + let v = (*buf, len + 1); + assert is_utf8(::unsafe::reinterpret_cast(v)); + return ::unsafe::transmute(v); + } + /// 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))