From 099d0802336e513089446db954a9b0dd078b5434 Mon Sep 17 00:00:00 2001 From: Jeff Olson Date: Sat, 9 Jun 2012 20:44:15 -0700 Subject: [PATCH] core: adding str::as_slice .. most likely broken --- src/libcore/str.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 35d2edd608ae..2c0f4b4a96b8 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -23,6 +23,7 @@ export as_buf, as_c_str, unpack_slice, + as_slice, // Adding things to and removing things from a string push_char, @@ -1620,6 +1621,16 @@ pure fn unpack_slice(s: str/&, f: fn(*u8, uint) -> T) -> T { } } +pure fn as_slice(s: str, + start: uint, end: uint, + f: fn(str/&) -> T) -> T unsafe { + assert (start <= end); + assert (end <= len(s)); + let p = ptr::addr_of(s); + f(::unsafe::reinterpret_cast( + (ptr::offset(p, start), (end - start) * sys::size_of::()))) +} + #[doc = " Reserves capacity for exactly `n` bytes in the given string, not including the null terminator.