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 | |
| parent | 3327ecca422046699315122345c6c050ab73804b (diff) | |
| download | rust-138b76b83a067284f25e1f8971600aaf49206816.tar.gz rust-138b76b83a067284f25e1f8971600aaf49206816.zip | |
Separate string->integer implementation in strconv
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc/metadata/decoder.rs | 6 | ||||
| -rw-r--r-- | src/librustc/metadata/tydecode.rs | 5 | ||||
| -rw-r--r-- | src/libstd/num/f32.rs | 6 | ||||
| -rw-r--r-- | src/libstd/num/f64.rs | 6 | ||||
| -rw-r--r-- | src/libstd/num/int_macros.rs | 94 | ||||
| -rw-r--r-- | src/libstd/num/strconv.rs | 135 | ||||
| -rw-r--r-- | src/libstd/num/uint_macros.rs | 71 | ||||
| -rw-r--r-- | src/test/bench/shootout-pfib.rs | 4 |
8 files changed, 138 insertions, 189 deletions
diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 213f32a1d18..fa8f7c9ad93 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -36,7 +36,7 @@ use std::io::extensions::u64_from_be_bytes; use std::io; use std::collections::hashmap::HashMap; use std::rc::Rc; -use std::u64; +use std::str; use rbml::reader; use rbml; use serialize::Decodable; @@ -215,7 +215,9 @@ fn each_reexport(d: rbml::Doc, f: |rbml::Doc| -> bool) -> bool { fn variant_disr_val(d: rbml::Doc) -> Option<ty::Disr> { reader::maybe_get_doc(d, tag_disr_val).and_then(|val_doc| { - reader::with_doc_data(val_doc, |data| u64::parse_bytes(data, 10u)) + reader::with_doc_data(val_doc, |data| { + str::from_utf8(data).and_then(from_str) + }) }) } diff --git a/src/librustc/metadata/tydecode.rs b/src/librustc/metadata/tydecode.rs index a52d02ccca7..6e7a6dfa094 100644 --- a/src/librustc/metadata/tydecode.rs +++ b/src/librustc/metadata/tydecode.rs @@ -23,7 +23,6 @@ use middle::ty; use std::rc::Rc; use std::str; use std::string::String; -use std::uint; use syntax::abi; use syntax::ast; use syntax::ast::*; @@ -615,12 +614,12 @@ pub fn parse_def_id(buf: &[u8]) -> ast::DefId { let crate_part = buf[0u..colon_idx]; let def_part = buf[colon_idx + 1u..len]; - let crate_num = match uint::parse_bytes(crate_part, 10u) { + let crate_num = match str::from_utf8(crate_part).and_then(from_str::<uint>) { Some(cn) => cn as ast::CrateNum, None => panic!("internal error: parse_def_id: crate number expected, found {}", crate_part) }; - let def_num = match uint::parse_bytes(def_part, 10u) { + let def_num = match str::from_utf8(def_part).and_then(from_str::<uint>) { Some(dn) => dn as ast::NodeId, None => panic!("internal error: parse_def_id: id expected, found {}", def_part) diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index 0b2f17b8f93..87c28c9362c 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -361,7 +361,7 @@ pub fn to_str_exp_digits(num: f32, dig: uint, upper: bool) -> String { #[inline] pub fn from_str_hex(num: &str) -> Option<f32> { strconv::from_str_common(num, 16u, true, true, true, - strconv::ExpBin, false, false) + strconv::ExpBin, false) } impl FromStr for f32 { @@ -393,7 +393,7 @@ impl FromStr for f32 { #[inline] fn from_str(val: &str) -> Option<f32> { strconv::from_str_common(val, 10u, true, true, true, - strconv::ExpDec, false, false) + strconv::ExpDec, false) } } @@ -418,7 +418,7 @@ impl num::FromStrRadix for f32 { #[inline] fn from_str_radix(val: &str, rdx: uint) -> Option<f32> { strconv::from_str_common(val, rdx, true, true, false, - strconv::ExpNone, false, false) + strconv::ExpNone, false) } } diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index 35555b14081..907c860f6a3 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -369,7 +369,7 @@ pub fn to_str_exp_digits(num: f64, dig: uint, upper: bool) -> String { #[inline] pub fn from_str_hex(num: &str) -> Option<f64> { strconv::from_str_common(num, 16u, true, true, true, - strconv::ExpBin, false, false) + strconv::ExpBin, false) } impl FromStr for f64 { @@ -401,7 +401,7 @@ impl FromStr for f64 { #[inline] fn from_str(val: &str) -> Option<f64> { strconv::from_str_common(val, 10u, true, true, true, - strconv::ExpDec, false, false) + strconv::ExpDec, false) } } @@ -426,7 +426,7 @@ impl num::FromStrRadix for f64 { #[inline] fn from_str_radix(val: &str, rdx: uint) -> Option<f64> { strconv::from_str_common(val, rdx, true, true, false, - strconv::ExpNone, false, false) + strconv::ExpNone, false) } } 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); } } diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index 30ecf2284df..1e70a0e571c 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -13,15 +13,18 @@ #![allow(missing_docs)] use char; +use char::Char; use clone::Clone; -use num::{NumCast, Zero, One, cast, Int}; +use from_str::from_str; +use iter::Iterator; +use num::{NumCast, Zero, One, cast, Int, Bounded}; use num::{Float, FPNaN, FPInfinite, ToPrimitive}; use num; use ops::{Add, Sub, Mul, Div, Rem, Neg}; use option::{None, Option, Some}; use slice::{ImmutableSlice, MutableSlice, CloneableVector}; use std::cmp::{PartialOrd, PartialEq}; -use str::StrSlice; +use str::{Str, StrSlice}; use string::String; use vec::Vec; @@ -106,35 +109,11 @@ macro_rules! impl_NumStrConv_Floating (($t:ty) => ( } )) -macro_rules! impl_NumStrConv_Integer (($t:ty) => ( - impl NumStrConv for $t { - #[inline] fn nan() -> Option<$t> { None } - #[inline] fn inf() -> Option<$t> { None } - #[inline] fn neg_inf() -> Option<$t> { None } - #[inline] fn neg_zero() -> Option<$t> { None } - - #[inline] fn round_to_zero(&self) -> $t { *self } - #[inline] fn fractional_part(&self) -> $t { 0 } - } -)) - // FIXME: #4955 // Replace by two generic impls for traits 'Integral' and 'Floating' impl_NumStrConv_Floating!(f32) impl_NumStrConv_Floating!(f64) -impl_NumStrConv_Integer!(int) -impl_NumStrConv_Integer!(i8) -impl_NumStrConv_Integer!(i16) -impl_NumStrConv_Integer!(i32) -impl_NumStrConv_Integer!(i64) - -impl_NumStrConv_Integer!(uint) -impl_NumStrConv_Integer!(u8) -impl_NumStrConv_Integer!(u16) -impl_NumStrConv_Integer!(u32) -impl_NumStrConv_Integer!(u64) - // Special value strings as [u8] consts. static INF_BUF: [u8, ..3] = [b'i', b'n', b'f']; @@ -526,8 +505,6 @@ static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u; * `FFp128`. The exponent string itself is always base 10. * Can conflict with `radix`, see Failure. * - `empty_zero` - Whether to accept an empty `buf` as a 0 or not. - * - `ignore_underscores` - Whether all underscores within the string should - * be ignored. * * # Return value * Returns `Some(n)` if `buf` parses to a number n without overflowing, and @@ -548,7 +525,6 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+ NumStrConv+Clone>( buf: &[u8], radix: uint, negative: bool, fractional: bool, special: bool, exponent: ExponentFormat, empty_zero: bool, - ignore_underscores: bool ) -> Option<T> { match exponent { ExpDec if radix >= DIGIT_E_RADIX // decimal exponent 'e' @@ -646,7 +622,6 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+ last_accum = accum.clone(); } None => match c { - '_' if ignore_underscores => {} 'e' | 'E' | 'p' | 'P' => { exp_found = true; break; // start of exponent @@ -690,7 +665,6 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+ last_accum = accum.clone(); } None => match c { - '_' if ignore_underscores => {} 'e' | 'E' | 'p' | 'P' => { exp_found = true; break; // start of exponent @@ -726,9 +700,7 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+ // parse remaining bytes as decimal integer, // skipping the exponent char - let exp: Option<int> = from_str_bytes_common( - buf[i+1..len], 10, true, false, false, ExpNone, false, - ignore_underscores); + let exp = from_str::<int>(String::from_utf8_lossy(buf[i+1..len]).as_slice()); match exp { Some(exp_pow) => { @@ -754,11 +726,65 @@ pub fn from_str_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+Mul<T,T> Sub<T,T>+Neg<T>+Add<T,T>+NumStrConv+Clone>( buf: &str, radix: uint, negative: bool, fractional: bool, special: bool, exponent: ExponentFormat, empty_zero: bool, - ignore_underscores: bool ) -> Option<T> { from_str_bytes_common(buf.as_bytes(), radix, negative, - fractional, special, exponent, empty_zero, - ignore_underscores) + fractional, special, exponent, empty_zero) +} + +pub fn from_str_radix_int<T: Int>(src: &str, radix: uint) -> Option<T> { + fn cast<T: Int>(x: uint) -> T { + num::cast(x).unwrap() + } + + let _0: T = num::zero(); + let _1: T = num::one(); + let is_signed = _0 > Bounded::min_value(); + + let (is_negative, src) = match src.slice_shift_char() { + (Some('-'), src) if is_signed => (true, src), + (Some(_), _) => (false, src), + (None, _) => return None, + }; + + let mut xs = src.chars().map(|c| { + c.to_digit(radix).map(cast) + }); + let radix = cast(radix); + let mut result = _0; + + if is_negative { + for x in xs { + let x = match x { + Some(x) => x, + None => return None, + }; + result = match result.checked_mul(&radix) { + Some(result) => result, + None => return None, + }; + result = match result.checked_sub(&x) { + Some(result) => result, + None => return None, + }; + } + } else { + for x in xs { + let x = match x { + Some(x) => x, + None => return None, + }; + result = match result.checked_mul(&radix) { + Some(result) => result, + None => return None, + }; + result = match result.checked_add(&x) { + Some(result) => result, + None => return None, + }; + } + } + + Some(result) } #[cfg(test)] @@ -767,44 +793,17 @@ mod test { use option::*; #[test] - fn from_str_ignore_underscores() { - let s : Option<u8> = from_str_common("__1__", 2, false, false, false, - ExpNone, false, true); - assert_eq!(s, Some(1u8)); - - let n : Option<u8> = from_str_common("__1__", 2, false, false, false, - ExpNone, false, false); - assert_eq!(n, None); - - let f : Option<f32> = from_str_common("_1_._5_e_1_", 10, false, true, false, - ExpDec, false, true); - assert_eq!(f, Some(1.5e1f32)); - } - - #[test] - fn from_str_issue5770() { - // try to parse 0b1_1111_1111 = 511 as a u8. Caused problems - // since 255*2+1 == 255 (mod 256) so the overflow wasn't - // detected. - let n : Option<u8> = from_str_common("111111111", 2, false, false, false, - ExpNone, false, false); - assert_eq!(n, None); - } - - #[test] fn from_str_issue7588() { - let u : Option<u8> = from_str_common("1000", 10, false, false, false, - ExpNone, false, false); + let u : Option<u8> = from_str_radix_int("1000", 10); assert_eq!(u, None); - let s : Option<i16> = from_str_common("80000", 10, false, false, false, - ExpNone, false, false); + let s : Option<i16> = from_str_radix_int("80000", 10); assert_eq!(s, None); let f : Option<f32> = from_str_common( "10000000000000000000000000000000000000000", 10, false, false, false, - ExpNone, false, false); + ExpNone, false); assert_eq!(f, NumStrConv::inf()) let fe : Option<f32> = from_str_common("1e40", 10, false, false, false, - ExpDec, false, false); + ExpDec, false); assert_eq!(fe, NumStrConv::inf()) } } diff --git a/src/libstd/num/uint_macros.rs b/src/libstd/num/uint_macros.rs index c69c3ffa41c..aa8e58bab02 100644 --- a/src/libstd/num/uint_macros.rs +++ b/src/libstd/num/uint_macros.rs @@ -15,31 +15,11 @@ macro_rules! uint_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::uint::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, false, 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, false, false, false, - strconv::ExpNone, false, false) + strconv::from_str_radix_int(s, 10) } } @@ -47,8 +27,7 @@ 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, false, false, false, - strconv::ExpNone, false, false) + strconv::from_str_radix_int(s, radix) } } @@ -85,10 +64,7 @@ pub fn to_str_bytes<U>(n: $T, radix: uint, f: |v: &[u8]| -> U) -> U { #[cfg(test)] mod tests { use prelude::*; - use super::*; - - use str::StrSlice; - use u16; + use num::FromStrRadix; #[test] pub fn test_from_str() { @@ -98,23 +74,22 @@ mod tests { assert_eq!(from_str::<u32>("123456789"), Some(123456789 as u32)); assert_eq!(from_str::<$T>("00100"), Some(100u as $T)); - assert!(from_str::<$T>("").is_none()); - 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] pub fn test_parse_bytes() { - use str::StrSlice; - assert_eq!(parse_bytes("123".as_bytes(), 10u), Some(123u as $T)); - assert_eq!(parse_bytes("1001".as_bytes(), 2u), Some(9u as $T)); - assert_eq!(parse_bytes("123".as_bytes(), 8u), Some(83u as $T)); - assert_eq!(u16::parse_bytes("123".as_bytes(), 16u), Some(291u as u16)); - assert_eq!(u16::parse_bytes("ffff".as_bytes(), 16u), Some(65535u as u16)); - assert_eq!(parse_bytes("z".as_bytes(), 36u), Some(35u as $T)); - - assert!(parse_bytes("Z".as_bytes(), 10u).is_none()); - assert!(parse_bytes("_".as_bytes(), 2u).is_none()); + assert_eq!(FromStrRadix::from_str_radix("123", 10), Some(123u as $T)); + assert_eq!(FromStrRadix::from_str_radix("1001", 2), Some(9u as $T)); + assert_eq!(FromStrRadix::from_str_radix("123", 8), Some(83u as $T)); + assert_eq!(FromStrRadix::from_str_radix("123", 16), Some(291u as u16)); + assert_eq!(FromStrRadix::from_str_radix("ffff", 16), Some(65535u as u16)); + assert_eq!(FromStrRadix::from_str_radix("z", 36), Some(35u as $T)); + + assert_eq!(FromStrRadix::from_str_radix("Z", 10), None::<$T>); + assert_eq!(FromStrRadix::from_str_radix("_", 2), None::<$T>); } #[test] @@ -148,35 +123,35 @@ mod tests { fn test_uint_from_str_overflow() { let mut u8_val: u8 = 255_u8; assert_eq!(from_str::<u8>("255"), Some(u8_val)); - assert!(from_str::<u8>("256").is_none()); + assert_eq!(from_str::<u8>("256"), None); u8_val += 1 as u8; assert_eq!(from_str::<u8>("0"), Some(u8_val)); - assert!(from_str::<u8>("-1").is_none()); + assert_eq!(from_str::<u8>("-1"), None); let mut u16_val: u16 = 65_535_u16; assert_eq!(from_str::<u16>("65535"), Some(u16_val)); - assert!(from_str::<u16>("65536").is_none()); + assert_eq!(from_str::<u16>("65536"), None); u16_val += 1 as u16; assert_eq!(from_str::<u16>("0"), Some(u16_val)); - assert!(from_str::<u16>("-1").is_none()); + assert_eq!(from_str::<u16>("-1"), None); let mut u32_val: u32 = 4_294_967_295_u32; assert_eq!(from_str::<u32>("4294967295"), Some(u32_val)); - assert!(from_str::<u32>("4294967296").is_none()); + assert_eq!(from_str::<u32>("4294967296"), None); u32_val += 1 as u32; assert_eq!(from_str::<u32>("0"), Some(u32_val)); - assert!(from_str::<u32>("-1").is_none()); + assert_eq!(from_str::<u32>("-1"), None); let mut u64_val: u64 = 18_446_744_073_709_551_615_u64; assert_eq!(from_str::<u64>("18446744073709551615"), Some(u64_val)); - assert!(from_str::<u64>("18446744073709551616").is_none()); + assert_eq!(from_str::<u64>("18446744073709551616"), None); u64_val += 1 as u64; assert_eq!(from_str::<u64>("0"), Some(u64_val)); - assert!(from_str::<u64>("-1").is_none()); + assert_eq!(from_str::<u64>("-1"), None); } } diff --git a/src/test/bench/shootout-pfib.rs b/src/test/bench/shootout-pfib.rs index 425b2e3e714..7fa13d6074d 100644 --- a/src/test/bench/shootout-pfib.rs +++ b/src/test/bench/shootout-pfib.rs @@ -24,7 +24,6 @@ extern crate time; use std::os; use std::result::{Ok, Err}; use std::task; -use std::uint; fn fib(n: int) -> int { fn pfib(tx: &Sender<int>, n: int) { @@ -102,8 +101,7 @@ fn main() { if opts.stress { stress(2); } else { - let max = uint::parse_bytes(args[1].as_bytes(), 10u).unwrap() as - int; + let max = from_str::<uint>(args[1].as_slice()).unwrap() as int; let num_trials = 10; |
