| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
/// An iterator over the decimal digits of an integer, from least to most significant |
| 6 |
pub struct IntIterator<T> { |
| 7 |
/// Digits remaining to be returned from the iterator |
| 8 |
unum: T, |
| 9 |
/// Whether the number is negative |
| 10 |
pub is_negative: bool, |
| 11 |
} |
| 12 |
|
| 13 |
macro_rules! impl_iterator_unsigned_integer_type { |
| 14 |
($utype: ident) => { |
| 15 |
impl Iterator for IntIterator<$utype> { |
| 16 |
type Item = u8; |
| 17 |
#[allow(trivial_numeric_casts)] |
| 18 |
fn next(&mut self) -> Option<Self::Item> { |
| 19 |
if self.unum == 0 { |
| 20 |
None |
| 21 |
} else { |
| 22 |
let div = self.unum / 10; |
| 23 |
let rem = self.unum % 10; |
| 24 |
self.unum = div; |
| 25 |
Some(rem as u8) |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
}; |
| 30 |
} |
| 31 |
|
| 32 |
macro_rules! impl_from_signed_integer_type { |
| 33 |
($itype:ident, $utype:ident) => { |
| 34 |
impl From<$itype> for IntIterator<$utype> { |
| 35 |
fn from(value: $itype) -> Self { |
| 36 |
Self { |
| 37 |
unum: { |
| 38 |
if value == $itype::MIN { |
| 39 |
$itype::MAX as $utype + 1 |
| 40 |
} else { |
| 41 |
value.unsigned_abs() |
| 42 |
} |
| 43 |
}, |
| 44 |
is_negative: value.is_negative(), |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
}; |
| 49 |
} |
| 50 |
|
| 51 |
macro_rules! impl_from_unsigned_integer_type { |
| 52 |
($utype:ident) => { |
| 53 |
impl From<$utype> for IntIterator<$utype> { |
| 54 |
fn from(value: $utype) -> Self { |
| 55 |
Self { |
| 56 |
unum: value, |
| 57 |
is_negative: false, |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
impl_iterator_unsigned_integer_type!($utype); |
| 62 |
}; |
| 63 |
} |
| 64 |
|
| 65 |
impl_from_signed_integer_type!(isize, usize); |
| 66 |
impl_from_signed_integer_type!(i128, u128); |
| 67 |
impl_from_signed_integer_type!(i64, u64); |
| 68 |
impl_from_signed_integer_type!(i32, u32); |
| 69 |
impl_from_signed_integer_type!(i16, u16); |
| 70 |
impl_from_signed_integer_type!(i8, u8); |
| 71 |
|
| 72 |
impl_from_unsigned_integer_type!(usize); |
| 73 |
impl_from_unsigned_integer_type!(u128); |
| 74 |
impl_from_unsigned_integer_type!(u64); |
| 75 |
impl_from_unsigned_integer_type!(u32); |
| 76 |
impl_from_unsigned_integer_type!(u16); |
| 77 |
impl_from_unsigned_integer_type!(u8); |
| 78 |
|
| 79 |
#[test] |
| 80 |
fn test_basic() { |
| 81 |
let mut it = IntIterator { |
| 82 |
unum: 123usize, |
| 83 |
is_negative: false, |
| 84 |
}; |
| 85 |
assert_eq!(Some(3), it.next()); |
| 86 |
assert_eq!(Some(2), it.next()); |
| 87 |
assert_eq!(Some(1), it.next()); |
| 88 |
assert_eq!(None, it.next()); |
| 89 |
} |
| 90 |
|
| 91 |
#[test] |
| 92 |
fn test_zeros() { |
| 93 |
let mut it = IntIterator { |
| 94 |
unum: 9080usize, |
| 95 |
is_negative: false, |
| 96 |
}; |
| 97 |
assert_eq!(Some(0), it.next()); |
| 98 |
assert_eq!(Some(8), it.next()); |
| 99 |
assert_eq!(Some(0), it.next()); |
| 100 |
assert_eq!(Some(9), it.next()); |
| 101 |
assert_eq!(None, it.next()); |
| 102 |
} |