diff options
| author | blake2-ppc <blake2-ppc> | 2013-08-18 13:57:34 +0200 |
|---|---|---|
| committer | blake2-ppc <blake2-ppc> | 2013-08-19 11:19:59 +0200 |
| commit | 8a5889d2a2eb4b2c9d41f6f3991fdd2622933047 (patch) | |
| tree | 2e9b24610fd78b96ff3f5bf37e27bccdcbf287f1 /src/libstd/str.rs | |
| parent | 3cb5b8dc1849c5958c62caf990faf75fcec6b2ea (diff) | |
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.
Diffstat (limited to 'src/libstd/str.rs')
| -rw-r--r-- | src/libstd/str.rs | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs index c5c2150617c..5022e558884 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, }) } |
