rollup merge of #17585 : sfackler/string-slice
This commit is contained in:
commit
735d16b1b0
2 changed files with 55 additions and 0 deletions
|
@ -18,6 +18,7 @@ use core::default::Default;
|
|||
use core::fmt;
|
||||
use core::mem;
|
||||
use core::ptr;
|
||||
use core::ops;
|
||||
// FIXME: ICE's abound if you import the `Slice` type while importing `Slice` trait
|
||||
use core::raw::Slice as RawSlice;
|
||||
|
||||
|
@ -926,6 +927,28 @@ impl<S: Str> Add<S, String> for String {
|
|||
}
|
||||
}
|
||||
|
||||
impl ops::Slice<uint, str> for String {
|
||||
#[inline]
|
||||
fn as_slice_<'a>(&'a self) -> &'a str {
|
||||
self.as_slice()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn slice_from_<'a>(&'a self, from: &uint) -> &'a str {
|
||||
self[][*from..]
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn slice_to_<'a>(&'a self, to: &uint) -> &'a str {
|
||||
self[][..*to]
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn slice_<'a>(&'a self, from: &uint, to: &uint) -> &'a str {
|
||||
self[][*from..*to]
|
||||
}
|
||||
}
|
||||
|
||||
/// Unsafe operations
|
||||
#[unstable = "waiting on raw module conventions"]
|
||||
pub mod raw {
|
||||
|
@ -1290,6 +1313,15 @@ mod tests {
|
|||
#[test] #[should_fail] fn insert_bad1() { "".to_string().insert(1, 't'); }
|
||||
#[test] #[should_fail] fn insert_bad2() { "ệ".to_string().insert(1, 't'); }
|
||||
|
||||
#[test]
|
||||
fn test_slicing() {
|
||||
let s = "foobar".to_string();
|
||||
assert_eq!("foobar", s[]);
|
||||
assert_eq!("foo", s[..3]);
|
||||
assert_eq!("bar", s[3..]);
|
||||
assert_eq!("oob", s[1..4]);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_with_capacity(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
|
|
|
@ -1123,6 +1123,7 @@ pub mod traits {
|
|||
use collections::Collection;
|
||||
use iter::Iterator;
|
||||
use option::{Option, Some};
|
||||
use ops;
|
||||
use str::{Str, StrSlice, eq_slice};
|
||||
|
||||
impl<'a> Ord for &'a str {
|
||||
|
@ -1162,6 +1163,28 @@ pub mod traits {
|
|||
#[inline]
|
||||
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
|
||||
}
|
||||
|
||||
impl ops::Slice<uint, str> for str {
|
||||
#[inline]
|
||||
fn as_slice_<'a>(&'a self) -> &'a str {
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn slice_from_<'a>(&'a self, from: &uint) -> &'a str {
|
||||
self.slice_from(*from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn slice_to_<'a>(&'a self, to: &uint) -> &'a str {
|
||||
self.slice_to(*to)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn slice_<'a>(&'a self, from: &uint, to: &uint) -> &'a str {
|
||||
self.slice(*from, *to)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Any string that can be represented as a slice
|
||||
|
|
Loading…
Add table
Reference in a new issue