diff options
| author | Nick Cameron <ncameron@mozilla.com> | 2015-01-02 13:56:28 +1300 |
|---|---|---|
| committer | Nick Cameron <ncameron@mozilla.com> | 2015-01-07 10:46:33 +1300 |
| commit | f7ff37e4c52a1d6562635fcd5bab6309cf75ea08 (patch) | |
| tree | 9c69736bf3830f9048f61d45943bf0fa6326782d /src/libcore | |
| parent | 918255ef8c3c21b2009204c3019239f8dc9f46bf (diff) | |
| download | rust-f7ff37e4c52a1d6562635fcd5bab6309cf75ea08.tar.gz rust-f7ff37e4c52a1d6562635fcd5bab6309cf75ea08.zip | |
Replace full slice notation with index calls
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/array.rs | 28 | ||||
| -rw-r--r-- | src/libcore/fmt/float.rs | 4 | ||||
| -rw-r--r-- | src/libcore/fmt/mod.rs | 6 | ||||
| -rw-r--r-- | src/libcore/fmt/num.rs | 3 | ||||
| -rw-r--r-- | src/libcore/num/mod.rs | 4 | ||||
| -rw-r--r-- | src/libcore/slice.rs | 46 | ||||
| -rw-r--r-- | src/libcore/str/mod.rs | 20 |
7 files changed, 63 insertions, 48 deletions
diff --git a/src/libcore/array.rs b/src/libcore/array.rs index ba7714ad9bc..37a2177b38d 100644 --- a/src/libcore/array.rs +++ b/src/libcore/array.rs @@ -18,7 +18,7 @@ use clone::Clone; use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering}; use fmt; use kinds::Copy; -use ops::Deref; +use ops::{Deref, FullRange, Index}; use option::Option; // macro for implementing n-ary tuple functions and operations @@ -35,7 +35,7 @@ macro_rules! array_impls { #[unstable = "waiting for Show to stabilize"] impl<T:fmt::Show> fmt::Show for [T; $N] { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Show::fmt(&self[], f) + fmt::Show::fmt(&self.index(&FullRange), f) } } @@ -43,11 +43,11 @@ macro_rules! array_impls { impl<A, B> PartialEq<[B; $N]> for [A; $N] where A: PartialEq<B> { #[inline] fn eq(&self, other: &[B; $N]) -> bool { - self[] == other[] + self.index(&FullRange) == other.index(&FullRange) } #[inline] fn ne(&self, other: &[B; $N]) -> bool { - self[] != other[] + self.index(&FullRange) != other.index(&FullRange) } } @@ -57,9 +57,9 @@ macro_rules! array_impls { Rhs: Deref<Target=[B]>, { #[inline(always)] - fn eq(&self, other: &Rhs) -> bool { PartialEq::eq(self[], &**other) } + fn eq(&self, other: &Rhs) -> bool { PartialEq::eq(self.index(&FullRange), &**other) } #[inline(always)] - fn ne(&self, other: &Rhs) -> bool { PartialEq::ne(self[], &**other) } + fn ne(&self, other: &Rhs) -> bool { PartialEq::ne(self.index(&FullRange), &**other) } } #[stable] @@ -68,9 +68,9 @@ macro_rules! array_impls { Lhs: Deref<Target=[A]> { #[inline(always)] - fn eq(&self, other: &[B; $N]) -> bool { PartialEq::eq(&**self, other[]) } + fn eq(&self, other: &[B; $N]) -> bool { PartialEq::eq(&**self, other.index(&FullRange)) } #[inline(always)] - fn ne(&self, other: &[B; $N]) -> bool { PartialEq::ne(&**self, other[]) } + fn ne(&self, other: &[B; $N]) -> bool { PartialEq::ne(&**self, other.index(&FullRange)) } } #[stable] @@ -80,23 +80,23 @@ macro_rules! array_impls { impl<T:PartialOrd> PartialOrd for [T; $N] { #[inline] fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> { - PartialOrd::partial_cmp(&self[], &other[]) + PartialOrd::partial_cmp(&self.index(&FullRange), &other.index(&FullRange)) } #[inline] fn lt(&self, other: &[T; $N]) -> bool { - PartialOrd::lt(&self[], &other[]) + PartialOrd::lt(&self.index(&FullRange), &other.index(&FullRange)) } #[inline] fn le(&self, other: &[T; $N]) -> bool { - PartialOrd::le(&self[], &other[]) + PartialOrd::le(&self.index(&FullRange), &other.index(&FullRange)) } #[inline] fn ge(&self, other: &[T; $N]) -> bool { - PartialOrd::ge(&self[], &other[]) + PartialOrd::ge(&self.index(&FullRange), &other.index(&FullRange)) } #[inline] fn gt(&self, other: &[T; $N]) -> bool { - PartialOrd::gt(&self[], &other[]) + PartialOrd::gt(&self.index(&FullRange), &other.index(&FullRange)) } } @@ -104,7 +104,7 @@ macro_rules! array_impls { impl<T:Ord> Ord for [T; $N] { #[inline] fn cmp(&self, other: &[T; $N]) -> Ordering { - Ord::cmp(&self[], &other[]) + Ord::cmp(&self.index(&FullRange), &other.index(&FullRange)) } } )+ diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index f63242b4f85..d833b8fed77 100644 --- a/src/libcore/fmt/float.rs +++ b/src/libcore/fmt/float.rs @@ -20,7 +20,7 @@ use fmt; use iter::{IteratorExt, range}; use num::{cast, Float, ToPrimitive}; use num::FpCategory as Fp; -use ops::FnOnce; +use ops::{FnOnce, Index}; use result::Result::Ok; use slice::{self, SliceExt}; use str::{self, StrExt}; @@ -332,5 +332,5 @@ pub fn float_to_str_bytes_common<T: Float, U, F>( } } - f(unsafe { str::from_utf8_unchecked(buf[..end]) }) + f(unsafe { str::from_utf8_unchecked(buf.index(&(0..end))) }) } diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 951f5c29f00..19c6b29417f 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -19,8 +19,8 @@ use kinds::{Copy, Sized}; use mem; use option::Option; use option::Option::{Some, None}; -use ops::{Deref, FnOnce}; use result::Result::Ok; +use ops::{Deref, FnOnce, Index}; use result; use slice::SliceExt; use slice; @@ -413,7 +413,7 @@ impl<'a> Formatter<'a> { for c in sign.into_iter() { let mut b = [0; 4]; let n = c.encode_utf8(&mut b).unwrap_or(0); - let b = unsafe { str::from_utf8_unchecked(b[0..n]) }; + let b = unsafe { str::from_utf8_unchecked(b.index(&(0..n))) }; try!(f.buf.write_str(b)); } if prefixed { f.buf.write_str(prefix) } @@ -620,7 +620,7 @@ impl Show for char { let mut utf8 = [0u8; 4]; let amt = self.encode_utf8(&mut utf8).unwrap_or(0); - let s: &str = unsafe { mem::transmute(utf8[..amt]) }; + let s: &str = unsafe { mem::transmute(utf8.index(&(0..amt))) }; Show::fmt(s, f) } } diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index e0724fc2da5..89337e0584b 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -16,6 +16,7 @@ use fmt; use iter::IteratorExt; +use ops::Index; use num::{Int, cast}; use slice::SliceExt; use str; @@ -61,7 +62,7 @@ trait GenericRadix { if x == zero { break }; // No more digits left to accumulate. } } - let buf = unsafe { str::from_utf8_unchecked(buf[curr..]) }; + let buf = unsafe { str::from_utf8_unchecked(buf.index(&(curr..))) }; f.pad_integral(is_positive, self.prefix(), buf) } } diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 192d6063f6b..3bcdd54463f 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -24,7 +24,7 @@ use iter::IteratorExt; use kinds::Copy; use mem::size_of; use ops::{Add, Sub, Mul, Div, Rem, Neg}; -use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr}; +use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr, Index}; use option::Option; use option::Option::{Some, None}; use str::{FromStr, StrExt}; @@ -1577,7 +1577,7 @@ macro_rules! from_str_radix_float_impl { }; // Parse the exponent as decimal integer - let src = src[offset..]; + let src = src.index(&(offset..)); let (is_positive, exp) = match src.slice_shift_char() { Some(('-', src)) => (false, src.parse::<uint>()), Some(('+', src)) => (true, src.parse::<uint>()), diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 369652b215f..a0bb0205c42 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -43,7 +43,7 @@ use default::Default; use iter::*; use kinds::Copy; use num::Int; -use ops::{FnMut, self}; +use ops::{FnMut, self, Index}; use option::Option; use option::Option::{None, Some}; use result::Result; @@ -159,7 +159,7 @@ impl<T> SliceExt for [T] { #[inline] fn split_at(&self, mid: uint) -> (&[T], &[T]) { - (self[..mid], self[mid..]) + (self.index(&(0..mid)), self.index(&(mid..))) } #[inline] @@ -236,11 +236,11 @@ impl<T> SliceExt for [T] { } #[inline] - fn tail(&self) -> &[T] { self[1..] } + fn tail(&self) -> &[T] { self.index(&(1..)) } #[inline] fn init(&self) -> &[T] { - self[..self.len() - 1] + self.index(&(0..(self.len() - 1))) } #[inline] @@ -443,13 +443,13 @@ impl<T> SliceExt for [T] { #[inline] fn starts_with(&self, needle: &[T]) -> bool where T: PartialEq { let n = needle.len(); - self.len() >= n && needle == self[..n] + self.len() >= n && needle == self.index(&(0..n)) } #[inline] fn ends_with(&self, needle: &[T]) -> bool where T: PartialEq { let (m, n) = (self.len(), needle.len()); - m >= n && needle == self[m-n..] + m >= n && needle == self.index(&((m-n)..)) } #[unstable] @@ -622,6 +622,20 @@ impl<T> ops::IndexMut<ops::FullRange, [T]> for [T] { } } +impl<T> ops::Index<ops::Range<uint>, [T]> for [T] { + #[inline] + fn index(&self, index: &ops::Range<uint>) -> &[T] { + assert!(index.start <= index.end); + assert!(index.end <= self.len()); + unsafe { + transmute(RawSlice { + data: self.as_ptr().offset(index.start as int), + len: index.end - index.start + }) + } + } +} + //////////////////////////////////////////////////////////////////////////////// // Common traits @@ -779,7 +793,7 @@ impl<'a, T> ops::Index<ops::RangeFrom<uint>, [T]> for Iter<'a, T> { #[experimental] impl<'a, T> ops::Index<ops::FullRange, [T]> for Iter<'a, T> { #[inline] - fn index(&self, index: &ops::FullRange) -> &[T] { + fn index(&self, _index: &ops::FullRange) -> &[T] { self.as_slice() } } @@ -868,7 +882,7 @@ impl<'a, T> ops::Index<ops::RangeFrom<uint>, [T]> for IterMut<'a, T> { #[experimental] impl<'a, T> ops::Index<ops::FullRange, [T]> for IterMut<'a, T> { #[inline] - fn index(&self, index: &ops::FullRange) -> &[T] { + fn index(&self, _index: &ops::FullRange) -> &[T] { make_slice!(T -> &[T]: self.ptr, self.end) } } @@ -900,7 +914,7 @@ impl<'a, T> ops::IndexMut<ops::RangeFrom<uint>, [T]> for IterMut<'a, T> { #[experimental] impl<'a, T> ops::IndexMut<ops::FullRange, [T]> for IterMut<'a, T> { #[inline] - fn index_mut(&mut self, index: &ops::FullRange) -> &mut [T] { + fn index_mut(&mut self, _index: &ops::FullRange) -> &mut [T] { make_slice!(T -> &mut [T]: self.ptr, self.end) } } @@ -964,8 +978,8 @@ impl<'a, T, P> Iterator for Split<'a, T, P> where P: FnMut(&T) -> bool { match self.v.iter().position(|x| (self.pred)(x)) { None => self.finish(), Some(idx) => { - let ret = Some(self.v[..idx]); - self.v = self.v[idx + 1..]; + let ret = Some(self.v.index(&(0..idx))); + self.v = self.v.index(&((idx + 1)..)); ret } } @@ -990,8 +1004,8 @@ impl<'a, T, P> DoubleEndedIterator for Split<'a, T, P> where P: FnMut(&T) -> boo match self.v.iter().rposition(|x| (self.pred)(x)) { None => self.finish(), Some(idx) => { - let ret = Some(self.v[idx + 1..]); - self.v = self.v[..idx]; + let ret = Some(self.v.index(&((idx + 1)..))); + self.v = self.v.index(&(0..idx)); ret } } @@ -1187,8 +1201,8 @@ impl<'a, T> Iterator for Windows<'a, T> { if self.size > self.v.len() { None } else { - let ret = Some(self.v[..self.size]); - self.v = self.v[1..]; + let ret = Some(self.v.index(&(0..self.size))); + self.v = self.v.index(&(1..)); ret } } @@ -1275,7 +1289,7 @@ impl<'a, T> RandomAccessIterator for Chunks<'a, T> { let mut hi = lo + self.size; if hi < lo || hi > self.v.len() { hi = self.v.len(); } - Some(self.v[lo..hi]) + Some(self.v.index(&(lo..hi))) } else { None } diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index a28e5614417..c4e97fe3b7f 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -26,7 +26,7 @@ use iter::{Map, Iterator, IteratorExt, DoubleEndedIterator}; use kinds::Sized; use mem; use num::Int; -use ops::{Fn, FnMut}; +use ops::{Fn, FnMut, Index}; use option::Option::{self, None, Some}; use ptr::PtrExt; use raw::{Repr, Slice}; @@ -581,7 +581,7 @@ impl NaiveSearcher { fn next(&mut self, haystack: &[u8], needle: &[u8]) -> Option<(uint, uint)> { while self.position + needle.len() <= haystack.len() { - if haystack[self.position .. self.position + needle.len()] == needle { + if haystack.index(&(self.position .. self.position + needle.len())) == needle { let match_pos = self.position; self.position += needle.len(); // add 1 for all matches return Some((match_pos, match_pos + needle.len())); @@ -702,10 +702,10 @@ impl TwoWaySearcher { // // What's going on is we have some critical factorization (u, v) of the // needle, and we want to determine whether u is a suffix of - // v[..period]. If it is, we use "Algorithm CP1". Otherwise we use + // v.index(&(0..period)). If it is, we use "Algorithm CP1". Otherwise we use // "Algorithm CP2", which is optimized for when the period of the needle // is large. - if needle[..crit_pos] == needle[period.. period + crit_pos] { + if needle.index(&(0..crit_pos)) == needle.index(&(period.. period + crit_pos)) { TwoWaySearcher { crit_pos: crit_pos, period: period, @@ -1121,28 +1121,28 @@ mod traits { impl ops::Index<ops::Range<uint>, str> for str { #[inline] - fn index(&self, &index: &ops::Range<uint>) -> &str { + fn index(&self, index: &ops::Range<uint>) -> &str { self.slice(index.start, index.end) } } impl ops::Index<ops::RangeTo<uint>, str> for str { #[inline] - fn index(&self, &index: &ops::RangeTo<uint>) -> &str { + fn index(&self, index: &ops::RangeTo<uint>) -> &str { self.slice_to(index.end) } } impl ops::Index<ops::RangeFrom<uint>, str> for str { #[inline] - fn index(&self, &index: &ops::RangeFrom<uint>) -> &str { + fn index(&self, index: &ops::RangeFrom<uint>) -> &str { self.slice_from(index.start) } } impl ops::Index<ops::FullRange, str> for str { #[inline] - fn index(&self, &index: &ops::FullRange) -> &str { + fn index(&self, _index: &ops::FullRange) -> &str { self } } @@ -1412,13 +1412,13 @@ impl StrExt for str { #[inline] fn starts_with(&self, needle: &str) -> bool { let n = needle.len(); - self.len() >= n && needle.as_bytes() == self.as_bytes()[..n] + self.len() >= n && needle.as_bytes() == self.as_bytes().index(&(0..n)) } #[inline] fn ends_with(&self, needle: &str) -> bool { let (m, n) = (self.len(), needle.len()); - m >= n && needle.as_bytes() == self.as_bytes()[m-n..] + m >= n && needle.as_bytes() == self.as_bytes().index(&((m-n)..)) } #[inline] |
