|
17 | 17 | use intrinsics; |
18 | 18 | use mem; |
19 | 19 | use num::{FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN}; |
20 | | -use num::Float; |
| 20 | +use num::{Float, FromStrRadix}; |
| 21 | +use num::strconv; |
| 22 | +use str::FromStr; |
21 | 23 | use option::Option; |
22 | 24 |
|
23 | 25 | pub const RADIX: uint = 2u; |
@@ -424,3 +426,66 @@ impl Float for f32 { |
424 | 426 | self * (value / 180.0f32) |
425 | 427 | } |
426 | 428 | } |
| 429 | + |
| 430 | +#[inline] |
| 431 | +#[allow(missing_docs)] |
| 432 | +#[deprecated="Use `FromStrRadix::from_str_radix(src, 16)`"] |
| 433 | +pub fn from_str_hex(src: &str) -> Option<f32> { |
| 434 | + strconv::from_str_radix_float(src, 16) |
| 435 | +} |
| 436 | + |
| 437 | +impl FromStr for f32 { |
| 438 | + /// Convert a string in base 10 to a float. |
| 439 | + /// Accepts an optional decimal exponent. |
| 440 | + /// |
| 441 | + /// This function accepts strings such as |
| 442 | + /// |
| 443 | + /// * '3.14' |
| 444 | + /// * '+3.14', equivalent to '3.14' |
| 445 | + /// * '-3.14' |
| 446 | + /// * '2.5E10', or equivalently, '2.5e10' |
| 447 | + /// * '2.5E-10' |
| 448 | + /// * '.' (understood as 0) |
| 449 | + /// * '5.' |
| 450 | + /// * '.5', or, equivalently, '0.5' |
| 451 | + /// * '+inf', 'inf', '-inf', 'NaN' |
| 452 | + /// |
| 453 | + /// Leading and trailing whitespace represent an error. |
| 454 | + /// |
| 455 | + /// # Arguments |
| 456 | + /// |
| 457 | + /// * src - A string |
| 458 | + /// |
| 459 | + /// # Return value |
| 460 | + /// |
| 461 | + /// `None` if the string did not represent a valid number. Otherwise, |
| 462 | + /// `Some(n)` where `n` is the floating-point number represented by `src`. |
| 463 | + #[inline] |
| 464 | + fn from_str(src: &str) -> Option<f32> { |
| 465 | + strconv::from_str_radix_float(src, 10u) |
| 466 | + } |
| 467 | +} |
| 468 | + |
| 469 | +impl FromStrRadix for f32 { |
| 470 | + /// Convert a string in a given base to a float. |
| 471 | + /// |
| 472 | + /// Due to possible conflicts, this function does **not** accept |
| 473 | + /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor** |
| 474 | + /// does it recognize exponents of any kind. |
| 475 | + /// |
| 476 | + /// Leading and trailing whitespace represent an error. |
| 477 | + /// |
| 478 | + /// # Arguments |
| 479 | + /// |
| 480 | + /// * src - A string |
| 481 | + /// * radix - The base to use. Must lie in the range [2 .. 36] |
| 482 | + /// |
| 483 | + /// # Return value |
| 484 | + /// |
| 485 | + /// `None` if the string did not represent a valid number. Otherwise, |
| 486 | + /// `Some(n)` where `n` is the floating-point number represented by `src`. |
| 487 | + #[inline] |
| 488 | + fn from_str_radix(src: &str, radix: uint) -> Option<f32> { |
| 489 | + strconv::from_str_radix_float(src, radix) |
| 490 | + } |
| 491 | +} |
0 commit comments