Skip to content

range/iter stuff #9203

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 8 commits 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
22 changes: 9 additions & 13 deletions src/libextra/crypto/md5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::uint;
use std::iter::range_step;

use cryptoutil::{write_u32_le, read_u32v_le, FixedBuffer, FixedBuffer64, StandardPadding};
use digest::Digest;
Expand Down Expand Up @@ -86,46 +86,42 @@ impl Md5State {
read_u32v_le(data, input);

// round 1
do uint::range_step(0, 16, 4) |i| {
for i in range_step(0u, 16, 4) {
a = op_f(a, b, c, d, data[i] + C1[i], 7);
d = op_f(d, a, b, c, data[i + 1] + C1[i + 1], 12);
c = op_f(c, d, a, b, data[i + 2] + C1[i + 2], 17);
b = op_f(b, c, d, a, data[i + 3] + C1[i + 3], 22);
true
};
}

// round 2
let mut t = 1;
do uint::range_step(0, 16, 4) |i| {
for i in range_step(0u, 16, 4) {
a = op_g(a, b, c, d, data[t & 0x0f] + C2[i], 5);
d = op_g(d, a, b, c, data[(t + 5) & 0x0f] + C2[i + 1], 9);
c = op_g(c, d, a, b, data[(t + 10) & 0x0f] + C2[i + 2], 14);
b = op_g(b, c, d, a, data[(t + 15) & 0x0f] + C2[i + 3], 20);
t += 20;
true
};
}

// round 3
t = 5;
do uint::range_step(0, 16, 4) |i| {
for i in range_step(0u, 16, 4) {
a = op_h(a, b, c, d, data[t & 0x0f] + C3[i], 4);
d = op_h(d, a, b, c, data[(t + 3) & 0x0f] + C3[i + 1], 11);
c = op_h(c, d, a, b, data[(t + 6) & 0x0f] + C3[i + 2], 16);
b = op_h(b, c, d, a, data[(t + 9) & 0x0f] + C3[i + 3], 23);
t += 12;
true
};
}

// round 4
t = 0;
do uint::range_step(0, 16, 4) |i| {
for i in range_step(0u, 16, 4) {
a = op_i(a, b, c, d, data[t & 0x0f] + C4[i], 6);
d = op_i(d, a, b, c, data[(t + 7) & 0x0f] + C4[i + 1], 10);
c = op_i(c, d, a, b, data[(t + 14) & 0x0f] + C4[i + 2], 15);
b = op_i(b, c, d, a, data[(t + 21) & 0x0f] + C4[i + 3], 21);
t += 28;
true
};
}

self.s0 += a;
self.s1 += b;
Expand Down
22 changes: 9 additions & 13 deletions src/libextra/crypto/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::uint;
use std::iter::range_step;

use cryptoutil::{write_u64_be, write_u32_be, read_u64v_be, read_u32v_be, add_bytes_to_bits,
add_bytes_to_bits_tuple, FixedBuffer, FixedBuffer128, FixedBuffer64, StandardPadding};
Expand Down Expand Up @@ -111,7 +111,7 @@ impl Engine512State {

// Putting the message schedule inside the same loop as the round calculations allows for
// the compiler to generate better code.
do uint::range_step(0, 64, 8) |t| {
for t in range_step(0u, 64, 8) {
schedule_round!(t + 16);
schedule_round!(t + 17);
schedule_round!(t + 18);
Expand All @@ -129,10 +129,9 @@ impl Engine512State {
sha2_round!(d, e, f, g, h, a, b, c, K64, t + 5);
sha2_round!(c, d, e, f, g, h, a, b, K64, t + 6);
sha2_round!(b, c, d, e, f, g, h, a, K64, t + 7);
true
};
}

do uint::range_step(64, 80, 8) |t| {
for t in range_step(64u, 80, 8) {
sha2_round!(a, b, c, d, e, f, g, h, K64, t);
sha2_round!(h, a, b, c, d, e, f, g, K64, t + 1);
sha2_round!(g, h, a, b, c, d, e, f, K64, t + 2);
Expand All @@ -141,8 +140,7 @@ impl Engine512State {
sha2_round!(d, e, f, g, h, a, b, c, K64, t + 5);
sha2_round!(c, d, e, f, g, h, a, b, K64, t + 6);
sha2_round!(b, c, d, e, f, g, h, a, K64, t + 7);
true
};
}

self.H0 += a;
self.H1 += b;
Expand Down Expand Up @@ -527,7 +525,7 @@ impl Engine256State {

// Putting the message schedule inside the same loop as the round calculations allows for
// the compiler to generate better code.
do uint::range_step(0, 48, 8) |t| {
for t in range_step(0u, 48, 8) {
schedule_round!(t + 16);
schedule_round!(t + 17);
schedule_round!(t + 18);
Expand All @@ -545,10 +543,9 @@ impl Engine256State {
sha2_round!(d, e, f, g, h, a, b, c, K32, t + 5);
sha2_round!(c, d, e, f, g, h, a, b, K32, t + 6);
sha2_round!(b, c, d, e, f, g, h, a, K32, t + 7);
true
};
}

do uint::range_step(48, 64, 8) |t| {
for t in range_step(48u, 64, 8) {
sha2_round!(a, b, c, d, e, f, g, h, K32, t);
sha2_round!(h, a, b, c, d, e, f, g, K32, t + 1);
sha2_round!(g, h, a, b, c, d, e, f, K32, t + 2);
Expand All @@ -557,8 +554,7 @@ impl Engine256State {
sha2_round!(d, e, f, g, h, a, b, c, K32, t + 5);
sha2_round!(c, d, e, f, g, h, a, b, K32, t + 6);
sha2_round!(b, c, d, e, f, g, h, a, K32, t + 7);
true
};
}

self.H0 += a;
self.H1 += b;
Expand Down
7 changes: 3 additions & 4 deletions src/libstd/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use cast::transmute;
use option::{None, Option, Some};
use i32;
use iter::{Iterator, range_step};
use str::StrSlice;
use unicode::{derived_property, general_category, decompose};
use to_str::ToStr;
Expand Down Expand Up @@ -286,15 +286,14 @@ pub fn escape_unicode(c: char, f: &fn(char)) {
(c <= '\uffff') { f('u'); 4 }
_ { f('U'); 8 }
);
do i32::range_step(4 * (pad - 1), -1, -4) |offset| {
for offset in range_step::<i32>(4 * (pad - 1), -1, -4) {
unsafe {
match ((c as i32) >> offset) & 0xf {
i @ 0 .. 9 => { f(transmute('0' as i32 + i)); }
i => { f(transmute('a' as i32 + (i - 10))); }
}
}
true
};
}
}

///
Expand Down
58 changes: 35 additions & 23 deletions src/libstd/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,17 +1790,17 @@ pub fn range_inclusive<A: Add<A, A> + Ord + Clone + One>(start: A, stop: A) -> R
RangeInclusive{range: range(start, stop), done: false}
}

impl<A: Add<A, A> + Ord + Clone> Iterator<A> for RangeInclusive<A> {
impl<A: Add<A, A> + Eq + Ord + Clone> Iterator<A> for RangeInclusive<A> {
#[inline]
fn next(&mut self) -> Option<A> {
match self.range.next() {
Some(x) => Some(x),
None => {
if self.done {
None
} else {
if !self.done && self.range.state == self.range.stop {
self.done = true;
Some(self.range.stop.clone())
} else {
None
}
}
}
Expand Down Expand Up @@ -1829,11 +1829,11 @@ impl<A: Sub<A, A> + Integer + Ord + Clone> DoubleEndedIterator<A> for RangeInclu
let result = self.range.stop.clone();
self.range.stop = self.range.stop - self.range.one;
Some(result)
} else if self.done {
None
} else {
} else if !self.done && self.range.state == self.range.stop {
self.done = true;
Some(self.range.stop.clone())
} else {
None
}
}
}
Expand All @@ -1857,7 +1857,7 @@ pub fn range_step<A: CheckedAdd + Ord + Clone + Zero>(start: A, stop: A, step: A
impl<A: CheckedAdd + Ord + Clone> Iterator<A> for RangeStep<A> {
#[inline]
fn next(&mut self) -> Option<A> {
if (self.rev && self.state > self.stop) || self.state < self.stop {
if (self.rev && self.state > self.stop) || (!self.rev && self.state < self.stop) {
let result = self.state.clone();
match self.state.checked_add(&self.step) {
Some(x) => self.state = x,
Expand Down Expand Up @@ -1891,22 +1891,14 @@ pub fn range_step_inclusive<A: CheckedAdd + Ord + Clone + Zero>(start: A, stop:
impl<A: CheckedAdd + Ord + Clone + Eq> Iterator<A> for RangeStepInclusive<A> {
#[inline]
fn next(&mut self) -> Option<A> {
if !self.done {
if (self.rev && self.state > self.stop) || self.state < self.stop {
let result = self.state.clone();
match self.state.checked_add(&self.step) {
Some(x) => self.state = x,
None => self.done = true
}
Some(result)
} else {
if self.state == self.stop {
self.done = true;
Some(self.state.clone())
} else {
None
}
if !self.done && ((self.rev && self.state >= self.stop) ||
(!self.rev && self.state <= self.stop)) {
let result = self.state.clone();
match self.state.checked_add(&self.step) {
Some(x) => self.state = x,
None => self.done = true
}
Some(result)
} else {
None
}
Expand Down Expand Up @@ -2716,24 +2708,44 @@ mod tests {
}
}

#[test]
fn test_range() {
assert_eq!(range(0i, 5).collect::<~[int]>(), ~[0i, 1, 2, 3, 4]);
assert_eq!(range(0i, 5).invert().collect::<~[int]>(), ~[4, 3, 2, 1, 0]);
assert_eq!(range(200, -5).collect::<~[int]>(), ~[]);
assert_eq!(range(200, -5).invert().collect::<~[int]>(), ~[]);
assert_eq!(range(200, 200).collect::<~[int]>(), ~[]);
assert_eq!(range(200, 200).invert().collect::<~[int]>(), ~[]);
}

#[test]
fn test_range_inclusive() {
assert_eq!(range_inclusive(0i, 5).collect::<~[int]>(), ~[0i, 1, 2, 3, 4, 5]);
assert_eq!(range_inclusive(0i, 5).invert().collect::<~[int]>(), ~[5i, 4, 3, 2, 1, 0]);
assert_eq!(range_inclusive(200, -5).collect::<~[int]>(), ~[]);
assert_eq!(range_inclusive(200, -5).invert().collect::<~[int]>(), ~[]);
assert_eq!(range_inclusive(200, 200).collect::<~[int]>(), ~[200]);
assert_eq!(range_inclusive(200, 200).invert().collect::<~[int]>(), ~[200]);
}

#[test]
fn test_range_step() {
assert_eq!(range_step(0i, 20, 5).collect::<~[int]>(), ~[0, 5, 10, 15]);
assert_eq!(range_step(20i, 0, -5).collect::<~[int]>(), ~[20, 15, 10, 5]);
assert_eq!(range_step(20i, 0, -6).collect::<~[int]>(), ~[20, 14, 8, 2]);
assert_eq!(range_step(200u8, 255, 50).collect::<~[u8]>(), ~[200u8, 250]);
assert_eq!(range_step(200, -5, 1).collect::<~[int]>(), ~[]);
assert_eq!(range_step(200, 200, 1).collect::<~[int]>(), ~[]);
}

#[test]
fn test_range_step_inclusive() {
assert_eq!(range_step_inclusive(0i, 20, 5).collect::<~[int]>(), ~[0, 5, 10, 15, 20]);
assert_eq!(range_step_inclusive(20i, 0, -5).collect::<~[int]>(), ~[20, 15, 10, 5, 0]);
assert_eq!(range_step_inclusive(20i, 0, -6).collect::<~[int]>(), ~[20, 14, 8, 2]);
assert_eq!(range_step_inclusive(200u8, 255, 50).collect::<~[u8]>(), ~[200u8, 250]);
assert_eq!(range_step_inclusive(200, -5, 1).collect::<~[int]>(), ~[]);
assert_eq!(range_step_inclusive(200, 200, 1).collect::<~[int]>(), ~[200]);
}

#[test]
Expand Down
Loading