From 8a5889d2a2eb4b2c9d41f6f3991fdd2622933047 Mon Sep 17 00:00:00 2001 From: blake2-ppc Date: Sun, 18 Aug 2013 13:57:34 +0200 Subject: [PATCH] std::str: Add str::raw::slice_unchecked Add a function like raw::slice_bytes, but it doesn't check slice boundaries. For iterator use where we always know the begin, end indices are in range. --- src/libstd/str.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/libstd/str.rs b/src/libstd/str.rs index c5c2150617c5..5022e558884f 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -847,12 +847,21 @@ pub mod raw { /// If end is greater than the length of the string. #[inline] pub unsafe fn slice_bytes<'a>(s: &'a str, begin: uint, end: uint) -> &'a str { - do s.as_imm_buf |sbuf, n| { - assert!((begin <= end)); - assert!((end <= n)); + assert!(begin <= end); + assert!(end <= s.len()); + slice_unchecked(s, begin, end) + } + /// Takes a bytewise (not UTF-8) slice from a string. + /// + /// Returns the substring from [`begin`..`end`). + /// + /// Caller must check slice boundaries! + #[inline] + pub unsafe fn slice_unchecked<'a>(s: &'a str, begin: uint, end: uint) -> &'a str { + do s.as_imm_buf |sbuf, _n| { cast::transmute(Slice { - data: ptr::offset(sbuf, begin as int), + data: sbuf.offset_inbounds(begin as int), len: end - begin, }) }