Skip to content

Commit 982cf90

Browse files
committed
Add a T_SIGNED type to uint template and eliminate step_down variant
Handle negative steps properly in range_step, fix order of arguments in tests, and such.
1 parent 1a6e1e2 commit 982cf90

File tree

6 files changed

+33
-36
lines changed

6 files changed

+33
-36
lines changed

src/libcore/uint-template.rs

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#[forbid(deprecated_pattern)];
1414

1515
use T = self::inst::T;
16+
use T_SIGNED = self::inst::T_SIGNED;
1617

1718
use char;
1819
use cmp::{Eq, Ord};
@@ -72,50 +73,36 @@ pub pure fn is_nonnegative(x: T) -> bool { x >= 0 as T }
7273
/**
7374
* Iterate over the range [`start`,`start`+`step`..`stop`)
7475
*
75-
* Note that `uint` requires separate `range_step` functions for each
76-
* direction.
77-
*
7876
*/
79-
pub pure fn range_step_up(start: T, stop: T, step: T, it: fn(T) -> bool) {
77+
pub pure fn range_step(start: T, stop: T, step: T_SIGNED, it: fn(T) -> bool) {
8078
let mut i = start;
8179
if step == 0 {
82-
fail ~"range_step_up called with step == 0";
83-
}
84-
while i < stop {
85-
if !it(i) { break }
86-
i += step;
80+
fail ~"range_step called with step == 0";
8781
}
88-
}
89-
90-
#[inline(always)]
91-
/**
92-
* Iterate over the range [`start`,`start`-`step`..`stop`)
93-
*
94-
* Note that `uint` requires separate `range_step` functions for each
95-
* direction.
96-
*
97-
*/
98-
pub pure fn range_step_down(start: T, stop: T, step: T, it: fn(T) -> bool) {
99-
let mut i = start;
100-
if step == 0 {
101-
fail ~"range_step_down called with step == 0";
82+
if step >= 0 {
83+
while i < stop {
84+
if !it(i) { break }
85+
i += step as T;
86+
}
10287
}
103-
while i > stop {
104-
if !it(i) { break }
105-
i -= step;
88+
else {
89+
while i > stop {
90+
if !it(i) { break }
91+
i -= -step as T;
92+
}
10693
}
10794
}
10895

10996
#[inline(always)]
11097
/// Iterate over the range [`lo`..`hi`)
11198
pub pure fn range(lo: T, hi: T, it: fn(T) -> bool) {
112-
range_step_up(lo, hi, 1 as T, it);
99+
range_step(lo, hi, 1 as T_SIGNED, it);
113100
}
114101

115102
#[inline(always)]
116103
/// Iterate over the range [`hi`..`lo`)
117104
pub pure fn range_rev(hi: T, lo: T, it: fn(T) -> bool) {
118-
range_step_down(hi, lo, 1 as T, it);
105+
range_step(hi, lo, -1 as T_SIGNED, it);
119106
}
120107

121108
/// Computes the bitwise complement
@@ -381,10 +368,10 @@ pub fn test_ranges() {
381368
for range_rev(13,10) |i| {
382369
l.push(i);
383370
}
384-
for range_step_up(20,26,2) |i| {
371+
for range_step(20,26,2) |i| {
385372
l.push(i);
386373
}
387-
for range_step_down(36,30,2) |i| {
374+
for range_step(36,30,-2) |i| {
388375
l.push(i);
389376
}
390377

@@ -400,21 +387,21 @@ pub fn test_ranges() {
400387
for range_rev(0,0) |_i| {
401388
fail ~"unreachable";
402389
}
403-
for range_step_up(10,0,1) |_i| {
390+
for range_step(10,0,1) |_i| {
404391
fail ~"unreachable";
405392
}
406-
for range_step_down(0,10,1) |_i| {
393+
for range_step(0,1,-10) |_i| {
407394
fail ~"unreachable";
408395
}
409396
}
410397

411398
#[test]
412399
#[should_fail]
413-
fn test_range_step_up_zero_step() {
414-
for range_step_up(0,10,0) |_i| {}
400+
fn test_range_step_zero_step_up() {
401+
for range_step(0,10,0) |_i| {}
415402
}
416403
#[test]
417404
#[should_fail]
418-
fn test_range_step_down_zero_step() {
419-
for range_step_down(0,10,0) |_i| {}
405+
fn test_range_step_zero_step_down() {
406+
for range_step(0,-10,0) |_i| {}
420407
}

src/libcore/uint-template/u16.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
1313
mod inst {
1414
pub type T = u16;
15+
#[allow(non_camel_case_types)]
16+
pub type T_SIGNED = i16;
1517
pub const bits: uint = 16;
1618
}

src/libcore/uint-template/u32.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
1313
mod inst {
1414
pub type T = u32;
15+
#[allow(non_camel_case_types)]
16+
pub type T_SIGNED = i32;
1517
pub const bits: uint = 32;
1618
}

src/libcore/uint-template/u64.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
1313
mod inst {
1414
pub type T = u64;
15+
#[allow(non_camel_case_types)]
16+
pub type T_SIGNED = i64;
1517
pub const bits: uint = 64;
1618
}

src/libcore/uint-template/u8.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub use self::inst::is_ascii;
1414

1515
mod inst {
1616
pub type T = u8;
17+
#[allow(non_camel_case_types)]
18+
pub type T_SIGNED = i8;
1719
pub const bits: uint = 8;
1820

1921
// Type-specific functions here. These must be reexported by the

src/libcore/uint-template/uint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ mod inst {
2020
use uint;
2121

2222
pub type T = uint;
23+
#[allow(non_camel_case_types)]
24+
pub type T_SIGNED = int;
2325

2426
#[cfg(target_arch = "x86")]
2527
#[cfg(target_arch = "arm")]

0 commit comments

Comments
 (0)