diff options
| author | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-11-02 17:27:11 +1100 |
|---|---|---|
| committer | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-11-04 00:20:37 +1100 |
| commit | 138b76b83a067284f25e1f8971600aaf49206816 (patch) | |
| tree | 665ec1e0cd002e3e7e87c64bb8e5fd96da3ea7ab /src/libstd/num/int_macros.rs | |
| parent | 3327ecca422046699315122345c6c050ab73804b (diff) | |
| download | rust-138b76b83a067284f25e1f8971600aaf49206816.tar.gz rust-138b76b83a067284f25e1f8971600aaf49206816.zip | |
Separate string->integer implementation in strconv
Diffstat (limited to 'src/libstd/num/int_macros.rs')
| -rw-r--r-- | src/libstd/num/int_macros.rs | 94 |
1 files changed, 35 insertions, 59 deletions
diff --git a/src/libstd/num/int_macros.rs b/src/libstd/num/int_macros.rs index ca45b40e687..9ae146c840a 100644 --- a/src/libstd/num/int_macros.rs +++ b/src/libstd/num/int_macros.rs @@ -14,31 +14,11 @@ macro_rules! int_module (($T:ty) => ( -// String conversion functions and impl str -> num - -/// Parse a byte slice as a number in the given base -/// -/// Yields an `Option` because `buf` may or may not actually be parseable. -/// -/// # Examples -/// -/// ``` -/// let num = std::i64::parse_bytes([49,50,51,52,53,54,55,56,57], 10); -/// assert!(num == Some(123456789)); -/// ``` -#[inline] -#[experimental = "might need to return Result"] -pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<$T> { - strconv::from_str_bytes_common(buf, radix, true, false, false, - strconv::ExpNone, false, false) -} - #[experimental = "might need to return Result"] impl FromStr for $T { #[inline] fn from_str(s: &str) -> Option<$T> { - strconv::from_str_common(s, 10u, true, false, false, - strconv::ExpNone, false, false) + strconv::from_str_radix_int(s, 10) } } @@ -46,18 +26,14 @@ impl FromStr for $T { impl FromStrRadix for $T { #[inline] fn from_str_radix(s: &str, radix: uint) -> Option<$T> { - strconv::from_str_common(s, radix, true, false, false, - strconv::ExpNone, false, false) + strconv::from_str_radix_int(s, radix) } } #[cfg(test)] mod tests { use prelude::*; - use super::*; - - use i32; - use str::StrSlice; + use num::FromStrRadix; #[test] fn test_from_str() { @@ -73,33 +49,33 @@ mod tests { assert_eq!(from_str::<i32>("-123456789"), Some(-123456789 as i32)); assert_eq!(from_str::<$T>("-00100"), Some(-100 as $T)); - assert!(from_str::<$T>(" ").is_none()); - assert!(from_str::<$T>("x").is_none()); + assert_eq!(from_str::<$T>(""), None); + assert_eq!(from_str::<$T>(" "), None); + assert_eq!(from_str::<$T>("x"), None); } #[test] - fn test_parse_bytes() { - use str::StrSlice; - assert_eq!(parse_bytes("123".as_bytes(), 10u), Some(123 as $T)); - assert_eq!(parse_bytes("1001".as_bytes(), 2u), Some(9 as $T)); - assert_eq!(parse_bytes("123".as_bytes(), 8u), Some(83 as $T)); - assert_eq!(i32::parse_bytes("123".as_bytes(), 16u), Some(291 as i32)); - assert_eq!(i32::parse_bytes("ffff".as_bytes(), 16u), Some(65535 as i32)); - assert_eq!(i32::parse_bytes("FFFF".as_bytes(), 16u), Some(65535 as i32)); - assert_eq!(parse_bytes("z".as_bytes(), 36u), Some(35 as $T)); - assert_eq!(parse_bytes("Z".as_bytes(), 36u), Some(35 as $T)); - - assert_eq!(parse_bytes("-123".as_bytes(), 10u), Some(-123 as $T)); - assert_eq!(parse_bytes("-1001".as_bytes(), 2u), Some(-9 as $T)); - assert_eq!(parse_bytes("-123".as_bytes(), 8u), Some(-83 as $T)); - assert_eq!(i32::parse_bytes("-123".as_bytes(), 16u), Some(-291 as i32)); - assert_eq!(i32::parse_bytes("-ffff".as_bytes(), 16u), Some(-65535 as i32)); - assert_eq!(i32::parse_bytes("-FFFF".as_bytes(), 16u), Some(-65535 as i32)); - assert_eq!(parse_bytes("-z".as_bytes(), 36u), Some(-35 as $T)); - assert_eq!(parse_bytes("-Z".as_bytes(), 36u), Some(-35 as $T)); - - assert!(parse_bytes("Z".as_bytes(), 35u).is_none()); - assert!(parse_bytes("-9".as_bytes(), 2u).is_none()); + fn test_from_str_radix() { + assert_eq!(FromStrRadix::from_str_radix("123", 10), Some(123 as $T)); + assert_eq!(FromStrRadix::from_str_radix("1001", 2), Some(9 as $T)); + assert_eq!(FromStrRadix::from_str_radix("123", 8), Some(83 as $T)); + assert_eq!(FromStrRadix::from_str_radix("123", 16), Some(291 as i32)); + assert_eq!(FromStrRadix::from_str_radix("ffff", 16), Some(65535 as i32)); + assert_eq!(FromStrRadix::from_str_radix("FFFF", 16), Some(65535 as i32)); + assert_eq!(FromStrRadix::from_str_radix("z", 36), Some(35 as $T)); + assert_eq!(FromStrRadix::from_str_radix("Z", 36), Some(35 as $T)); + + assert_eq!(FromStrRadix::from_str_radix("-123", 10), Some(-123 as $T)); + assert_eq!(FromStrRadix::from_str_radix("-1001", 2), Some(-9 as $T)); + assert_eq!(FromStrRadix::from_str_radix("-123", 8), Some(-83 as $T)); + assert_eq!(FromStrRadix::from_str_radix("-123", 16), Some(-291 as i32)); + assert_eq!(FromStrRadix::from_str_radix("-ffff", 16), Some(-65535 as i32)); + assert_eq!(FromStrRadix::from_str_radix("-FFFF", 16), Some(-65535 as i32)); + assert_eq!(FromStrRadix::from_str_radix("-z", 36), Some(-35 as $T)); + assert_eq!(FromStrRadix::from_str_radix("-Z", 36), Some(-35 as $T)); + + assert_eq!(FromStrRadix::from_str_radix("Z", 35), None::<$T>); + assert_eq!(FromStrRadix::from_str_radix("-9", 2), None::<$T>); } #[test] @@ -133,35 +109,35 @@ mod tests { fn test_int_from_str_overflow() { let mut i8_val: i8 = 127_i8; assert_eq!(from_str::<i8>("127"), Some(i8_val)); - assert!(from_str::<i8>("128").is_none()); + assert_eq!(from_str::<i8>("128"), None); i8_val += 1 as i8; assert_eq!(from_str::<i8>("-128"), Some(i8_val)); - assert!(from_str::<i8>("-129").is_none()); + assert_eq!(from_str::<i8>("-129"), None); let mut i16_val: i16 = 32_767_i16; assert_eq!(from_str::<i16>("32767"), Some(i16_val)); - assert!(from_str::<i16>("32768").is_none()); + assert_eq!(from_str::<i16>("32768"), None); i16_val += 1 as i16; assert_eq!(from_str::<i16>("-32768"), Some(i16_val)); - assert!(from_str::<i16>("-32769").is_none()); + assert_eq!(from_str::<i16>("-32769"), None); let mut i32_val: i32 = 2_147_483_647_i32; assert_eq!(from_str::<i32>("2147483647"), Some(i32_val)); - assert!(from_str::<i32>("2147483648").is_none()); + assert_eq!(from_str::<i32>("2147483648"), None); i32_val += 1 as i32; assert_eq!(from_str::<i32>("-2147483648"), Some(i32_val)); - assert!(from_str::<i32>("-2147483649").is_none()); + assert_eq!(from_str::<i32>("-2147483649"), None); let mut i64_val: i64 = 9_223_372_036_854_775_807_i64; assert_eq!(from_str::<i64>("9223372036854775807"), Some(i64_val)); - assert!(from_str::<i64>("9223372036854775808").is_none()); + assert_eq!(from_str::<i64>("9223372036854775808"), None); i64_val += 1 as i64; assert_eq!(from_str::<i64>("-9223372036854775808"), Some(i64_val)); - assert!(from_str::<i64>("-9223372036854775809").is_none()); + assert_eq!(from_str::<i64>("-9223372036854775809"), None); } } |
