diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index e27c45773441a..569be9aff2f49 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -32,7 +32,7 @@ use borrow::{Borrow, ToOwned}; use string::String; use std_unicode; use vec::Vec; -use slice::SliceConcatExt; +use slice::{SliceConcatExt, SliceIndex}; use boxed::Box; #[stable(feature = "rust1", since = "1.0.0")] @@ -1815,4 +1815,20 @@ impl str { s.extend((0..n).map(|_| self)); s } + + #[unstable(feature = "str_range_slice", issue = "39932")] + pub fn get(&self, index: I) -> Option<&I::Output> + where I: SliceIndex + { + let bytes : &[u8] = unsafe { mem::transmute(self) }; + index.get(bytes) + } + + #[unstable(feature = "str_range_slice", issue = "39932")] + pub fn get_mut(&mut self, index: I) -> Option<&mut I::Output> + where I: SliceIndex + { + let bytes : &mut [u8] = unsafe { mem::transmute(self) }; + index.get_mut(bytes) + } } diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 52e3301631052..9ab0c9dbb01e3 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -21,7 +21,7 @@ use char; use fmt; use iter::{Map, Cloned, FusedIterator}; use mem; -use slice; +use slice::{self, SliceIndex}; pub mod pattern; @@ -1712,6 +1712,12 @@ pub trait StrExt { unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &str; #[stable(feature = "core", since = "1.6.0")] unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut str; + #[unstable(feature = "str_range_slice", issue = "39932")] + unsafe fn get_unchecked(&self, index: I) -> &I::Output + where I: SliceIndex; + #[unstable(feature = "str_range_slice", issue = "39932")] + unsafe fn get_unchecked_mut(&mut self, index: I) -> &mut I::Output + where I: SliceIndex; #[stable(feature = "core", since = "1.6.0")] fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool; #[stable(feature = "core", since = "1.6.0")] @@ -2046,6 +2052,22 @@ impl StrExt for str { #[inline] fn parse(&self) -> Result { FromStr::from_str(self) } + + #[inline] + unsafe fn get_unchecked(&self, index: I) -> &I::Output + where I: SliceIndex + { + let bytes : &[u8] = mem::transmute(self); + index.get_unchecked(bytes) + } + + #[inline] + unsafe fn get_unchecked_mut(&mut self, index: I) -> &mut I::Output + where I: SliceIndex + { + let bytes : &mut [u8] = mem::transmute(self); + index.get_unchecked_mut(bytes) + } } #[stable(feature = "rust1", since = "1.0.0")]