Skip to content

Commit 8a5889d

Browse files
author
blake2-ppc
committed
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.
1 parent 3cb5b8d commit 8a5889d

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/libstd/str.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -847,12 +847,21 @@ pub mod raw {
847847
/// If end is greater than the length of the string.
848848
#[inline]
849849
pub unsafe fn slice_bytes<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
850-
do s.as_imm_buf |sbuf, n| {
851-
assert!((begin <= end));
852-
assert!((end <= n));
850+
assert!(begin <= end);
851+
assert!(end <= s.len());
852+
slice_unchecked(s, begin, end)
853+
}
853854

855+
/// Takes a bytewise (not UTF-8) slice from a string.
856+
///
857+
/// Returns the substring from [`begin`..`end`).
858+
///
859+
/// Caller must check slice boundaries!
860+
#[inline]
861+
pub unsafe fn slice_unchecked<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
862+
do s.as_imm_buf |sbuf, _n| {
854863
cast::transmute(Slice {
855-
data: ptr::offset(sbuf, begin as int),
864+
data: sbuf.offset_inbounds(begin as int),
856865
len: end - begin,
857866
})
858867
}

0 commit comments

Comments
 (0)