Skip to content

add str::get_unchecked(_mut) in accordance with slice #40436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -1815,4 +1815,20 @@ impl str {
s.extend((0..n).map(|_| self));
s
}

#[unstable(feature = "str_range_slice", issue = "39932")]
pub fn get<I>(&self, index: I) -> Option<&I::Output>
where I: SliceIndex<u8>
{
let bytes : &[u8] = unsafe { mem::transmute(self) };
index.get(bytes)
}

#[unstable(feature = "str_range_slice", issue = "39932")]
pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
where I: SliceIndex<u8>
{
let bytes : &mut [u8] = unsafe { mem::transmute(self) };
index.get_mut(bytes)
}
}
24 changes: 23 additions & 1 deletion src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use char;
use fmt;
use iter::{Map, Cloned, FusedIterator};
use mem;
use slice;
use slice::{self, SliceIndex};

pub mod pattern;

Expand Down Expand Up @@ -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<I>(&self, index: I) -> &I::Output
where I: SliceIndex<u8>;
#[unstable(feature = "str_range_slice", issue = "39932")]
unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
where I: SliceIndex<u8>;
#[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")]
Expand Down Expand Up @@ -2046,6 +2052,22 @@ impl StrExt for str {

#[inline]
fn parse<T: FromStr>(&self) -> Result<T, T::Err> { FromStr::from_str(self) }

#[inline]
unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
where I: SliceIndex<u8>
{
let bytes : &[u8] = mem::transmute(self);
index.get_unchecked(bytes)
}

#[inline]
unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
where I: SliceIndex<u8>
{
let bytes : &mut [u8] = mem::transmute(self);
index.get_unchecked_mut(bytes)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down