From b5d560a9e09498db5434ea26338a3d2bfc545eee Mon Sep 17 00:00:00 2001 From: blake2-ppc Date: Wed, 18 Sep 2013 02:17:47 +0200 Subject: [PATCH] std: Remove {float,f64,f32}::from_str in favor of from_str Like issue #9209, remove float::{from_str, from_str_radix} in favor of the two corresponding traits. The same for modules f64 and f32. New usage is from_str::("1.2e34") --- src/compiletest/compiletest.rs | 3 +- src/librustc/middle/const_eval.rs | 5 +- src/libstd/num/f32.rs | 112 +++++++++---------- src/libstd/num/f64.rs | 112 +++++++++---------- src/libstd/num/float.rs | 174 ++++++++++++++---------------- 5 files changed, 190 insertions(+), 216 deletions(-) diff --git a/src/compiletest/compiletest.rs b/src/compiletest/compiletest.rs index 166638bc359bf..270b1097c554b 100644 --- a/src/compiletest/compiletest.rs +++ b/src/compiletest/compiletest.rs @@ -17,7 +17,6 @@ extern mod extra; use std::os; use std::rt; -use std::f64; use extra::getopts; use extra::getopts::groups::{optopt, optflag, reqopt}; @@ -131,7 +130,7 @@ pub fn parse_config(args: ~[~str]) -> config { ratchet_noise_percent: getopts::opt_maybe_str(matches, "ratchet-noise-percent").map_move(|s| - f64::from_str(s).unwrap()), + from_str::(s).unwrap()), runtool: getopts::opt_maybe_str(matches, "runtool"), rustcflags: getopts::opt_maybe_str(matches, "rustcflags"), jit: getopts::opt_present(matches, "jit"), diff --git a/src/librustc/middle/const_eval.rs b/src/librustc/middle/const_eval.rs index 2e5e87f225a2a..bba3ca9f212a9 100644 --- a/src/librustc/middle/const_eval.rs +++ b/src/librustc/middle/const_eval.rs @@ -19,7 +19,6 @@ use syntax::visit; use syntax::visit::Visitor; use syntax::ast::*; -use std::float; use std::hashmap::{HashMap, HashSet}; // @@ -476,9 +475,9 @@ pub fn lit_to_const(lit: &lit) -> const_val { lit_int(n, _) => const_int(n), lit_uint(n, _) => const_uint(n), lit_int_unsuffixed(n) => const_int(n), - lit_float(n, _) => const_float(float::from_str(n).unwrap() as f64), + lit_float(n, _) => const_float(from_str::(n).unwrap() as f64), lit_float_unsuffixed(n) => - const_float(float::from_str(n).unwrap() as f64), + const_float(from_str::(n).unwrap() as f64), lit_nil => const_int(0i64), lit_bool(b) => const_bool(b) } diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index 0addcce3eb608..1bb488d02780c 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -822,39 +822,6 @@ impl num::ToStrRadix for f32 { } } -/// -/// Convert a string in base 10 to a float. -/// Accepts a optional decimal exponent. -/// -/// This function accepts strings such as -/// -/// * '3.14' -/// * '+3.14', equivalent to '3.14' -/// * '-3.14' -/// * '2.5E10', or equivalently, '2.5e10' -/// * '2.5E-10' -/// * '.' (understood as 0) -/// * '5.' -/// * '.5', or, equivalently, '0.5' -/// * '+inf', 'inf', '-inf', 'NaN' -/// -/// Leading and trailing whitespace represent an error. -/// -/// # Arguments -/// -/// * num - A string -/// -/// # Return value -/// -/// `none` if the string did not represent a valid number. Otherwise, -/// `Some(n)` where `n` is the floating-point number represented by `num`. -/// -#[inline] -pub fn from_str(num: &str) -> Option { - strconv::from_str_common(num, 10u, true, true, true, - strconv::ExpDec, false, false) -} - /// /// Convert a string in base 16 to a float. /// Accepts a optional binary exponent. @@ -888,40 +855,65 @@ pub fn from_str_hex(num: &str) -> Option { strconv::ExpBin, false, false) } -/// -/// Convert a string in an given base to a float. -/// -/// Due to possible conflicts, this function does **not** accept -/// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor** -/// does it recognize exponents of any kind. -/// -/// Leading and trailing whitespace represent an error. -/// -/// # Arguments -/// -/// * num - A string -/// * radix - The base to use. Must lie in the range [2 .. 36] -/// -/// # Return value -/// -/// `none` if the string did not represent a valid number. Otherwise, -/// `Some(n)` where `n` is the floating-point number represented by `num`. -/// -#[inline] -pub fn from_str_radix(num: &str, rdx: uint) -> Option { - strconv::from_str_common(num, rdx, true, true, false, - strconv::ExpNone, false, false) -} - impl FromStr for f32 { + /// + /// Convert a string in base 10 to a float. + /// Accepts a optional decimal exponent. + /// + /// This function accepts strings such as + /// + /// * '3.14' + /// * '+3.14', equivalent to '3.14' + /// * '-3.14' + /// * '2.5E10', or equivalently, '2.5e10' + /// * '2.5E-10' + /// * '.' (understood as 0) + /// * '5.' + /// * '.5', or, equivalently, '0.5' + /// * '+inf', 'inf', '-inf', 'NaN' + /// + /// Leading and trailing whitespace represent an error. + /// + /// # Arguments + /// + /// * num - A string + /// + /// # Return value + /// + /// `none` if the string did not represent a valid number. Otherwise, + /// `Some(n)` where `n` is the floating-point number represented by `num`. + /// #[inline] - fn from_str(val: &str) -> Option { from_str(val) } + fn from_str(val: &str) -> Option { + strconv::from_str_common(val, 10u, true, true, true, + strconv::ExpDec, false, false) + } } impl num::FromStrRadix for f32 { + /// + /// Convert a string in an given base to a float. + /// + /// Due to possible conflicts, this function does **not** accept + /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor** + /// does it recognize exponents of any kind. + /// + /// Leading and trailing whitespace represent an error. + /// + /// # Arguments + /// + /// * num - A string + /// * radix - The base to use. Must lie in the range [2 .. 36] + /// + /// # Return value + /// + /// `none` if the string did not represent a valid number. Otherwise, + /// `Some(n)` where `n` is the floating-point number represented by `num`. + /// #[inline] fn from_str_radix(val: &str, rdx: uint) -> Option { - from_str_radix(val, rdx) + strconv::from_str_common(val, rdx, true, true, false, + strconv::ExpNone, false, false) } } diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index b0675278238e4..604eac0a0a750 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -869,39 +869,6 @@ impl num::ToStrRadix for f64 { } } -/// -/// Convert a string in base 10 to a float. -/// Accepts a optional decimal exponent. -/// -/// This function accepts strings such as -/// -/// * '3.14' -/// * '+3.14', equivalent to '3.14' -/// * '-3.14' -/// * '2.5E10', or equivalently, '2.5e10' -/// * '2.5E-10' -/// * '.' (understood as 0) -/// * '5.' -/// * '.5', or, equivalently, '0.5' -/// * '+inf', 'inf', '-inf', 'NaN' -/// -/// Leading and trailing whitespace represent an error. -/// -/// # Arguments -/// -/// * num - A string -/// -/// # Return value -/// -/// `none` if the string did not represent a valid number. Otherwise, -/// `Some(n)` where `n` is the floating-point number represented by `num`. -/// -#[inline] -pub fn from_str(num: &str) -> Option { - strconv::from_str_common(num, 10u, true, true, true, - strconv::ExpDec, false, false) -} - /// /// Convert a string in base 16 to a float. /// Accepts a optional binary exponent. @@ -935,40 +902,65 @@ pub fn from_str_hex(num: &str) -> Option { strconv::ExpBin, false, false) } -/// -/// Convert a string in an given base to a float. -/// -/// Due to possible conflicts, this function does **not** accept -/// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor** -/// does it recognize exponents of any kind. -/// -/// Leading and trailing whitespace represent an error. -/// -/// # Arguments -/// -/// * num - A string -/// * radix - The base to use. Must lie in the range [2 .. 36] -/// -/// # Return value -/// -/// `none` if the string did not represent a valid number. Otherwise, -/// `Some(n)` where `n` is the floating-point number represented by `num`. -/// -#[inline] -pub fn from_str_radix(num: &str, rdx: uint) -> Option { - strconv::from_str_common(num, rdx, true, true, false, - strconv::ExpNone, false, false) -} - impl FromStr for f64 { + /// + /// Convert a string in base 10 to a float. + /// Accepts a optional decimal exponent. + /// + /// This function accepts strings such as + /// + /// * '3.14' + /// * '+3.14', equivalent to '3.14' + /// * '-3.14' + /// * '2.5E10', or equivalently, '2.5e10' + /// * '2.5E-10' + /// * '.' (understood as 0) + /// * '5.' + /// * '.5', or, equivalently, '0.5' + /// * '+inf', 'inf', '-inf', 'NaN' + /// + /// Leading and trailing whitespace represent an error. + /// + /// # Arguments + /// + /// * num - A string + /// + /// # Return value + /// + /// `none` if the string did not represent a valid number. Otherwise, + /// `Some(n)` where `n` is the floating-point number represented by `num`. + /// #[inline] - fn from_str(val: &str) -> Option { from_str(val) } + fn from_str(val: &str) -> Option { + strconv::from_str_common(val, 10u, true, true, true, + strconv::ExpDec, false, false) + } } impl num::FromStrRadix for f64 { + /// + /// Convert a string in an given base to a float. + /// + /// Due to possible conflicts, this function does **not** accept + /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor** + /// does it recognize exponents of any kind. + /// + /// Leading and trailing whitespace represent an error. + /// + /// # Arguments + /// + /// * num - A string + /// * radix - The base to use. Must lie in the range [2 .. 36] + /// + /// # Return value + /// + /// `none` if the string did not represent a valid number. Otherwise, + /// `Some(n)` where `n` is the floating-point number represented by `num`. + /// #[inline] fn from_str_radix(val: &str, rdx: uint) -> Option { - from_str_radix(val, rdx) + strconv::from_str_common(val, rdx, true, true, false, + strconv::ExpNone, false, false) } } diff --git a/src/libstd/num/float.rs b/src/libstd/num/float.rs index 3952f5478f7fd..b86422edc0356 100644 --- a/src/libstd/num/float.rs +++ b/src/libstd/num/float.rs @@ -187,39 +187,6 @@ impl num::ToStrRadix for float { } } -/// -/// Convert a string in base 10 to a float. -/// Accepts a optional decimal exponent. -/// -/// This function accepts strings such as -/// -/// * '3.14' -/// * '+3.14', equivalent to '3.14' -/// * '-3.14' -/// * '2.5E10', or equivalently, '2.5e10' -/// * '2.5E-10' -/// * '.' (understood as 0) -/// * '5.' -/// * '.5', or, equivalently, '0.5' -/// * '+inf', 'inf', '-inf', 'NaN' -/// -/// Leading and trailing whitespace represent an error. -/// -/// # Arguments -/// -/// * num - A string -/// -/// # Return value -/// -/// `none` if the string did not represent a valid number. Otherwise, -/// `Some(n)` where `n` is the floating-point number represented by `num`. -/// -#[inline] -pub fn from_str(num: &str) -> Option { - strconv::from_str_common(num, 10u, true, true, true, - strconv::ExpDec, false, false) -} - /// /// Convert a string in base 16 to a float. /// Accepts a optional binary exponent. @@ -253,40 +220,65 @@ pub fn from_str_hex(num: &str) -> Option { strconv::ExpBin, false, false) } -/// -/// Convert a string in an given base to a float. -/// -/// Due to possible conflicts, this function does **not** accept -/// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor** -/// does it recognize exponents of any kind. -/// -/// Leading and trailing whitespace represent an error. -/// -/// # Arguments -/// -/// * num - A string -/// * radix - The base to use. Must lie in the range [2 .. 36] -/// -/// # Return value -/// -/// `none` if the string did not represent a valid number. Otherwise, -/// `Some(n)` where `n` is the floating-point number represented by `num`. -/// -#[inline] -pub fn from_str_radix(num: &str, radix: uint) -> Option { - strconv::from_str_common(num, radix, true, true, false, - strconv::ExpNone, false, false) -} - impl FromStr for float { + /// + /// Convert a string in base 10 to a float. + /// Accepts a optional decimal exponent. + /// + /// This function accepts strings such as + /// + /// * '3.14' + /// * '+3.14', equivalent to '3.14' + /// * '-3.14' + /// * '2.5E10', or equivalently, '2.5e10' + /// * '2.5E-10' + /// * '.' (understood as 0) + /// * '5.' + /// * '.5', or, equivalently, '0.5' + /// * '+inf', 'inf', '-inf', 'NaN' + /// + /// Leading and trailing whitespace represent an error. + /// + /// # Arguments + /// + /// * num - A string + /// + /// # Return value + /// + /// `none` if the string did not represent a valid number. Otherwise, + /// `Some(n)` where `n` is the floating-point number represented by `num`. + /// #[inline] - fn from_str(val: &str) -> Option { from_str(val) } + fn from_str(val: &str) -> Option { + strconv::from_str_common(val, 10u, true, true, true, + strconv::ExpDec, false, false) + } } impl num::FromStrRadix for float { + /// + /// Convert a string in an given base to a float. + /// + /// Due to possible conflicts, this function does **not** accept + /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor** + /// does it recognize exponents of any kind. + /// + /// Leading and trailing whitespace represent an error. + /// + /// # Arguments + /// + /// * num - A string + /// * radix - The base to use. Must lie in the range [2 .. 36] + /// + /// # Return value + /// + /// `none` if the string did not represent a valid number. Otherwise, + /// `Some(n)` where `n` is the floating-point number represented by `num`. + /// #[inline] fn from_str_radix(val: &str, radix: uint) -> Option { - from_str_radix(val, radix) + strconv::from_str_common(val, radix, true, true, false, + strconv::ExpNone, false, false) } } @@ -1316,49 +1308,49 @@ mod tests { #[test] pub fn test_from_str() { - assert_eq!(from_str("3"), Some(3.)); - assert_eq!(from_str("3.14"), Some(3.14)); - assert_eq!(from_str("+3.14"), Some(3.14)); - assert_eq!(from_str("-3.14"), Some(-3.14)); - assert_eq!(from_str("2.5E10"), Some(25000000000.)); - assert_eq!(from_str("2.5e10"), Some(25000000000.)); - assert_eq!(from_str("25000000000.E-10"), Some(2.5)); - assert_eq!(from_str("."), Some(0.)); - assert_eq!(from_str(".e1"), Some(0.)); - assert_eq!(from_str(".e-1"), Some(0.)); - assert_eq!(from_str("5."), Some(5.)); - assert_eq!(from_str(".5"), Some(0.5)); - assert_eq!(from_str("0.5"), Some(0.5)); - assert_eq!(from_str("-.5"), Some(-0.5)); - assert_eq!(from_str("-5"), Some(-5.)); - assert_eq!(from_str("inf"), Some(infinity)); - assert_eq!(from_str("+inf"), Some(infinity)); - assert_eq!(from_str("-inf"), Some(neg_infinity)); + assert_eq!(from_str::("3"), Some(3.)); + assert_eq!(from_str::("3.14"), Some(3.14)); + assert_eq!(from_str::("+3.14"), Some(3.14)); + assert_eq!(from_str::("-3.14"), Some(-3.14)); + assert_eq!(from_str::("2.5E10"), Some(25000000000.)); + assert_eq!(from_str::("2.5e10"), Some(25000000000.)); + assert_eq!(from_str::("25000000000.E-10"), Some(2.5)); + assert_eq!(from_str::("."), Some(0.)); + assert_eq!(from_str::(".e1"), Some(0.)); + assert_eq!(from_str::(".e-1"), Some(0.)); + assert_eq!(from_str::("5."), Some(5.)); + assert_eq!(from_str::(".5"), Some(0.5)); + assert_eq!(from_str::("0.5"), Some(0.5)); + assert_eq!(from_str::("-.5"), Some(-0.5)); + assert_eq!(from_str::("-5"), Some(-5.)); + assert_eq!(from_str::("inf"), Some(infinity)); + assert_eq!(from_str::("+inf"), Some(infinity)); + assert_eq!(from_str::("-inf"), Some(neg_infinity)); // note: NaN != NaN, hence this slightly complex test - match from_str("NaN") { + match from_str::("NaN") { Some(f) => assert!(f.is_NaN()), None => fail!() } // note: -0 == 0, hence these slightly more complex tests - match from_str("-0") { + match from_str::("-0") { Some(v) if v.is_zero() => assert!(v.is_negative()), _ => fail!() } - match from_str("0") { + match from_str::("0") { Some(v) if v.is_zero() => assert!(v.is_positive()), _ => fail!() } - assert!(from_str("").is_none()); - assert!(from_str("x").is_none()); - assert!(from_str(" ").is_none()); - assert!(from_str(" ").is_none()); - assert!(from_str("e").is_none()); - assert!(from_str("E").is_none()); - assert!(from_str("E1").is_none()); - assert!(from_str("1e1e1").is_none()); - assert!(from_str("1e1.1").is_none()); - assert!(from_str("1e1-1").is_none()); + assert!(from_str::("").is_none()); + assert!(from_str::("x").is_none()); + assert!(from_str::(" ").is_none()); + assert!(from_str::(" ").is_none()); + assert!(from_str::("e").is_none()); + assert!(from_str::("E").is_none()); + assert!(from_str::("E1").is_none()); + assert!(from_str::("1e1e1").is_none()); + assert!(from_str::("1e1.1").is_none()); + assert!(from_str::("1e1-1").is_none()); } #[test]