Skip to content

Commit 45940ed

Browse files
committed
Remove vec::[r]position_between, replaced by slices & iterators.
1 parent 5d46bcc commit 45940ed

File tree

2 files changed

+13
-106
lines changed

2 files changed

+13
-106
lines changed

src/libextra/terminfo/parser/compiled.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,13 @@ pub fn parse(file: @Reader, longnames: bool) -> Result<~TermInfo, ~str> {
291291

292292

293293
// Find the offset of the NUL we want to go to
294-
let nulpos = vec::position_between(string_table, offset as uint,
295-
string_table_bytes as uint, |&b| b == 0);
294+
let nulpos = string_table.slice(offset as uint, string_table_bytes as uint)
295+
.iter().position_(|&b| b == 0);
296296
match nulpos {
297-
Some(x) => {
297+
Some(len) => {
298298
string_map.insert(name.to_owned(),
299-
string_table.slice(offset as uint, x).to_owned())
299+
string_table.slice(offset as uint,
300+
offset as uint + len).to_owned())
300301
},
301302
None => {
302303
return Err(~"invalid file: missing NUL in string_table");

src/libstd/vec.rs

Lines changed: 8 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ pub fn split<T:Copy>(v: &[T], f: &fn(t: &T) -> bool) -> ~[~[T]] {
193193
let mut start = 0u;
194194
let mut result = ~[];
195195
while start < ln {
196-
match position_between(v, start, ln, |t| f(t)) {
196+
match v.slice(start, ln).iter().position_(|t| f(t)) {
197197
None => break,
198198
Some(i) => {
199-
result.push(v.slice(start, i).to_owned());
200-
start = i + 1u;
199+
result.push(v.slice(start, start + i).to_owned());
200+
start += i + 1u;
201201
}
202202
}
203203
}
@@ -217,7 +217,7 @@ pub fn splitn<T:Copy>(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] {
217217
let mut count = n;
218218
let mut result = ~[];
219219
while start < ln && count > 0u {
220-
match position_between(v, start, ln, |t| f(t)) {
220+
match v.slice(start, ln).iter().position_(|t| f(t)) {
221221
None => break,
222222
Some(i) => {
223223
result.push(v.slice(start, i).to_owned());
@@ -242,7 +242,7 @@ pub fn rsplit<T:Copy>(v: &[T], f: &fn(t: &T) -> bool) -> ~[~[T]] {
242242
let mut end = ln;
243243
let mut result = ~[];
244244
while end > 0 {
245-
match rposition_between(v, 0, end, |t| f(t)) {
245+
match v.slice(0, end).rposition(|t| f(t)) {
246246
None => break,
247247
Some(i) => {
248248
result.push(v.slice(i + 1, end).to_owned());
@@ -267,7 +267,7 @@ pub fn rsplitn<T:Copy>(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] {
267267
let mut count = n;
268268
let mut result = ~[];
269269
while end > 0u && count > 0u {
270-
match rposition_between(v, 0u, end, |t| f(t)) {
270+
match v.slice(0, end).rposition(|t| f(t)) {
271271
None => break,
272272
Some(i) => {
273273
result.push(v.slice(i + 1u, end).to_owned());
@@ -657,25 +657,6 @@ pub fn position_elem<T:Eq>(v: &[T], x: &T) -> Option<uint> {
657657
v.iter().position_(|y| *x == *y)
658658
}
659659

660-
/**
661-
* Find the first index matching some predicate within a range
662-
*
663-
* Apply function `f` to each element of `v` between the range
664-
* [`start`, `end`). When function `f` returns true then an option containing
665-
* the index is returned. If `f` matches no elements then none is returned.
666-
*/
667-
pub fn position_between<T>(v: &[T],
668-
start: uint,
669-
end: uint,
670-
f: &fn(t: &T) -> bool)
671-
-> Option<uint> {
672-
assert!(start <= end);
673-
assert!(end <= v.len());
674-
let mut i = start;
675-
while i < end { if f(&v[i]) { return Some::<uint>(i); } i += 1u; }
676-
None
677-
}
678-
679660
/// Find the last index containing a matching value
680661
pub fn rposition_elem<T:Eq>(v: &[T], x: &T) -> Option<uint> {
681662
rposition(v, |y| *x == *y)
@@ -689,31 +670,12 @@ pub fn rposition_elem<T:Eq>(v: &[T], x: &T) -> Option<uint> {
689670
* matches no elements then none is returned.
690671
*/
691672
pub fn rposition<T>(v: &[T], f: &fn(t: &T) -> bool) -> Option<uint> {
692-
rposition_between(v, 0u, v.len(), f)
693-
}
694-
695-
/**
696-
* Find the last index matching some predicate within a range
697-
*
698-
* Apply function `f` to each element of `v` in reverse order between the
699-
* range [`start`, `end`). When function `f` returns true then an option
700-
* containing the index is returned. If `f` matches no elements then none is
701-
* returned.
702-
*/
703-
pub fn rposition_between<T>(v: &[T], start: uint, end: uint,
704-
f: &fn(t: &T) -> bool) -> Option<uint> {
705-
assert!(start <= end);
706-
assert!(end <= v.len());
707-
let mut i = end;
708-
while i > start {
709-
if f(&v[i - 1u]) { return Some::<uint>(i - 1u); }
710-
i -= 1u;
673+
for v.rev_iter().enumerate().advance |(i, t)| {
674+
if f(t) { return Some(v.len() - i - 1); }
711675
}
712676
None
713677
}
714678

715-
716-
717679
/**
718680
* Binary search a sorted vector with a comparator function.
719681
*
@@ -2885,34 +2847,6 @@ mod tests {
28852847
assert!(position_elem(v1, &4).is_none());
28862848
}
28872849

2888-
#[test]
2889-
fn test_position_between() {
2890-
assert!(position_between([], 0u, 0u, f).is_none());
2891-
2892-
fn f(xy: &(int, char)) -> bool { let (_x, y) = *xy; y == 'b' }
2893-
let v = ~[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
2894-
2895-
assert!(position_between(v, 0u, 0u, f).is_none());
2896-
assert!(position_between(v, 0u, 1u, f).is_none());
2897-
assert_eq!(position_between(v, 0u, 2u, f), Some(1u));
2898-
assert_eq!(position_between(v, 0u, 3u, f), Some(1u));
2899-
assert_eq!(position_between(v, 0u, 4u, f), Some(1u));
2900-
2901-
assert!(position_between(v, 1u, 1u, f).is_none());
2902-
assert_eq!(position_between(v, 1u, 2u, f), Some(1u));
2903-
assert_eq!(position_between(v, 1u, 3u, f), Some(1u));
2904-
assert_eq!(position_between(v, 1u, 4u, f), Some(1u));
2905-
2906-
assert!(position_between(v, 2u, 2u, f).is_none());
2907-
assert!(position_between(v, 2u, 3u, f).is_none());
2908-
assert_eq!(position_between(v, 2u, 4u, f), Some(3u));
2909-
2910-
assert!(position_between(v, 3u, 3u, f).is_none());
2911-
assert_eq!(position_between(v, 3u, 4u, f), Some(3u));
2912-
2913-
assert!(position_between(v, 4u, 4u, f).is_none());
2914-
}
2915-
29162850
#[test]
29172851
fn test_rposition() {
29182852
fn f(xy: &(int, char)) -> bool { let (_x, y) = *xy; y == 'b' }
@@ -2923,34 +2857,6 @@ mod tests {
29232857
assert!(rposition(v, g).is_none());
29242858
}
29252859

2926-
#[test]
2927-
fn test_rposition_between() {
2928-
assert!(rposition_between([], 0u, 0u, f).is_none());
2929-
2930-
fn f(xy: &(int, char)) -> bool { let (_x, y) = *xy; y == 'b' }
2931-
let v = ~[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
2932-
2933-
assert!(rposition_between(v, 0u, 0u, f).is_none());
2934-
assert!(rposition_between(v, 0u, 1u, f).is_none());
2935-
assert_eq!(rposition_between(v, 0u, 2u, f), Some(1u));
2936-
assert_eq!(rposition_between(v, 0u, 3u, f), Some(1u));
2937-
assert_eq!(rposition_between(v, 0u, 4u, f), Some(3u));
2938-
2939-
assert!(rposition_between(v, 1u, 1u, f).is_none());
2940-
assert_eq!(rposition_between(v, 1u, 2u, f), Some(1u));
2941-
assert_eq!(rposition_between(v, 1u, 3u, f), Some(1u));
2942-
assert_eq!(rposition_between(v, 1u, 4u, f), Some(3u));
2943-
2944-
assert!(rposition_between(v, 2u, 2u, f).is_none());
2945-
assert!(rposition_between(v, 2u, 3u, f).is_none());
2946-
assert_eq!(rposition_between(v, 2u, 4u, f), Some(3u));
2947-
2948-
assert!(rposition_between(v, 3u, 3u, f).is_none());
2949-
assert_eq!(rposition_between(v, 3u, 4u, f), Some(3u));
2950-
2951-
assert!(rposition_between(v, 4u, 4u, f).is_none());
2952-
}
2953-
29542860
#[test]
29552861
fn test_bsearch_elem() {
29562862
assert_eq!(bsearch_elem([1,2,3,4,5], &5), Some(4));

0 commit comments

Comments
 (0)