From 45940ed988ab2d04e1dbecceef440071b04c11a9 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sat, 29 Jun 2013 01:33:23 +1000 Subject: [PATCH] Remove vec::[r]position_between, replaced by slices & iterators. --- src/libextra/terminfo/parser/compiled.rs | 9 +- src/libstd/vec.rs | 110 ++--------------------- 2 files changed, 13 insertions(+), 106 deletions(-) diff --git a/src/libextra/terminfo/parser/compiled.rs b/src/libextra/terminfo/parser/compiled.rs index 2f36845d79b..063d26d1424 100644 --- a/src/libextra/terminfo/parser/compiled.rs +++ b/src/libextra/terminfo/parser/compiled.rs @@ -291,12 +291,13 @@ pub fn parse(file: @Reader, longnames: bool) -> Result<~TermInfo, ~str> { // Find the offset of the NUL we want to go to - let nulpos = vec::position_between(string_table, offset as uint, - string_table_bytes as uint, |&b| b == 0); + let nulpos = string_table.slice(offset as uint, string_table_bytes as uint) + .iter().position_(|&b| b == 0); match nulpos { - Some(x) => { + Some(len) => { string_map.insert(name.to_owned(), - string_table.slice(offset as uint, x).to_owned()) + string_table.slice(offset as uint, + offset as uint + len).to_owned()) }, None => { return Err(~"invalid file: missing NUL in string_table"); diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 07319cbd148..a704e604aa2 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -193,11 +193,11 @@ pub fn split(v: &[T], f: &fn(t: &T) -> bool) -> ~[~[T]] { let mut start = 0u; let mut result = ~[]; while start < ln { - match position_between(v, start, ln, |t| f(t)) { + match v.slice(start, ln).iter().position_(|t| f(t)) { None => break, Some(i) => { - result.push(v.slice(start, i).to_owned()); - start = i + 1u; + result.push(v.slice(start, start + i).to_owned()); + start += i + 1u; } } } @@ -217,7 +217,7 @@ pub fn splitn(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] { let mut count = n; let mut result = ~[]; while start < ln && count > 0u { - match position_between(v, start, ln, |t| f(t)) { + match v.slice(start, ln).iter().position_(|t| f(t)) { None => break, Some(i) => { result.push(v.slice(start, i).to_owned()); @@ -242,7 +242,7 @@ pub fn rsplit(v: &[T], f: &fn(t: &T) -> bool) -> ~[~[T]] { let mut end = ln; let mut result = ~[]; while end > 0 { - match rposition_between(v, 0, end, |t| f(t)) { + match v.slice(0, end).rposition(|t| f(t)) { None => break, Some(i) => { result.push(v.slice(i + 1, end).to_owned()); @@ -267,7 +267,7 @@ pub fn rsplitn(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] { let mut count = n; let mut result = ~[]; while end > 0u && count > 0u { - match rposition_between(v, 0u, end, |t| f(t)) { + match v.slice(0, end).rposition(|t| f(t)) { None => break, Some(i) => { result.push(v.slice(i + 1u, end).to_owned()); @@ -657,25 +657,6 @@ pub fn position_elem(v: &[T], x: &T) -> Option { v.iter().position_(|y| *x == *y) } -/** - * Find the first index matching some predicate within a range - * - * Apply function `f` to each element of `v` between the range - * [`start`, `end`). When function `f` returns true then an option containing - * the index is returned. If `f` matches no elements then none is returned. - */ -pub fn position_between(v: &[T], - start: uint, - end: uint, - f: &fn(t: &T) -> bool) - -> Option { - assert!(start <= end); - assert!(end <= v.len()); - let mut i = start; - while i < end { if f(&v[i]) { return Some::(i); } i += 1u; } - None -} - /// Find the last index containing a matching value pub fn rposition_elem(v: &[T], x: &T) -> Option { rposition(v, |y| *x == *y) @@ -689,31 +670,12 @@ pub fn rposition_elem(v: &[T], x: &T) -> Option { * matches no elements then none is returned. */ pub fn rposition(v: &[T], f: &fn(t: &T) -> bool) -> Option { - rposition_between(v, 0u, v.len(), f) -} - -/** - * Find the last index matching some predicate within a range - * - * Apply function `f` to each element of `v` in reverse order between the - * range [`start`, `end`). When function `f` returns true then an option - * containing the index is returned. If `f` matches no elements then none is - * returned. - */ -pub fn rposition_between(v: &[T], start: uint, end: uint, - f: &fn(t: &T) -> bool) -> Option { - assert!(start <= end); - assert!(end <= v.len()); - let mut i = end; - while i > start { - if f(&v[i - 1u]) { return Some::(i - 1u); } - i -= 1u; + for v.rev_iter().enumerate().advance |(i, t)| { + if f(t) { return Some(v.len() - i - 1); } } None } - - /** * Binary search a sorted vector with a comparator function. * @@ -2885,34 +2847,6 @@ mod tests { assert!(position_elem(v1, &4).is_none()); } - #[test] - fn test_position_between() { - assert!(position_between([], 0u, 0u, f).is_none()); - - fn f(xy: &(int, char)) -> bool { let (_x, y) = *xy; y == 'b' } - let v = ~[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')]; - - assert!(position_between(v, 0u, 0u, f).is_none()); - assert!(position_between(v, 0u, 1u, f).is_none()); - assert_eq!(position_between(v, 0u, 2u, f), Some(1u)); - assert_eq!(position_between(v, 0u, 3u, f), Some(1u)); - assert_eq!(position_between(v, 0u, 4u, f), Some(1u)); - - assert!(position_between(v, 1u, 1u, f).is_none()); - assert_eq!(position_between(v, 1u, 2u, f), Some(1u)); - assert_eq!(position_between(v, 1u, 3u, f), Some(1u)); - assert_eq!(position_between(v, 1u, 4u, f), Some(1u)); - - assert!(position_between(v, 2u, 2u, f).is_none()); - assert!(position_between(v, 2u, 3u, f).is_none()); - assert_eq!(position_between(v, 2u, 4u, f), Some(3u)); - - assert!(position_between(v, 3u, 3u, f).is_none()); - assert_eq!(position_between(v, 3u, 4u, f), Some(3u)); - - assert!(position_between(v, 4u, 4u, f).is_none()); - } - #[test] fn test_rposition() { fn f(xy: &(int, char)) -> bool { let (_x, y) = *xy; y == 'b' } @@ -2923,34 +2857,6 @@ mod tests { assert!(rposition(v, g).is_none()); } - #[test] - fn test_rposition_between() { - assert!(rposition_between([], 0u, 0u, f).is_none()); - - fn f(xy: &(int, char)) -> bool { let (_x, y) = *xy; y == 'b' } - let v = ~[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')]; - - assert!(rposition_between(v, 0u, 0u, f).is_none()); - assert!(rposition_between(v, 0u, 1u, f).is_none()); - assert_eq!(rposition_between(v, 0u, 2u, f), Some(1u)); - assert_eq!(rposition_between(v, 0u, 3u, f), Some(1u)); - assert_eq!(rposition_between(v, 0u, 4u, f), Some(3u)); - - assert!(rposition_between(v, 1u, 1u, f).is_none()); - assert_eq!(rposition_between(v, 1u, 2u, f), Some(1u)); - assert_eq!(rposition_between(v, 1u, 3u, f), Some(1u)); - assert_eq!(rposition_between(v, 1u, 4u, f), Some(3u)); - - assert!(rposition_between(v, 2u, 2u, f).is_none()); - assert!(rposition_between(v, 2u, 3u, f).is_none()); - assert_eq!(rposition_between(v, 2u, 4u, f), Some(3u)); - - assert!(rposition_between(v, 3u, 3u, f).is_none()); - assert_eq!(rposition_between(v, 3u, 4u, f), Some(3u)); - - assert!(rposition_between(v, 4u, 4u, f).is_none()); - } - #[test] fn test_bsearch_elem() { assert_eq!(bsearch_elem([1,2,3,4,5], &5), Some(4));