Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for stabilizing libcore #27871

Merged
merged 4 commits into from
Aug 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/liballoc/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@

use boxed::Box;

use core::atomic;
use core::atomic::Ordering::{Relaxed, Release, Acquire, SeqCst};
use core::sync::atomic;
use core::sync::atomic::Ordering::{Relaxed, Release, Acquire, SeqCst};
use core::fmt;
use core::cmp::Ordering;
use core::mem::{align_of_val, size_of_val};
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub mod convert;

pub mod any;
pub mod array;
pub mod atomic;
pub mod sync;
pub mod cell;
pub mod char;
pub mod panicking;
Expand Down
100 changes: 87 additions & 13 deletions src/libcore/num/dec2flt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@
issue = "0")]

use prelude::v1::*;
use num::ParseFloatError as PFE;
use num::FloatErrorKind;
use fmt;
use str::FromStr;

use self::parse::{parse_decimal, Decimal, Sign};
use self::parse::ParseResult::{self, Valid, ShortcutToInf, ShortcutToZero};
use self::num::digits_to_big;
Expand All @@ -110,14 +111,87 @@ mod num;
pub mod rawfp;
pub mod parse;

/// Entry point for decimal-to-f32 conversion.
pub fn to_f32(s: &str) -> Result<f32, PFE> {
dec2flt(s)
macro_rules! from_str_float_impl {
($t:ty, $func:ident) => {
#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for $t {
type Err = ParseFloatError;

/// Converts a string in base 10 to a float.
/// Accepts an optional decimal exponent.
///
/// This function accepts strings such as
///
/// * '3.14'
/// * '-3.14'
/// * '2.5E10', or equivalently, '2.5e10'
/// * '2.5E-10'
/// * '.' (understood as 0)
/// * '5.'
/// * '.5', or, equivalently, '0.5'
/// * 'inf', '-inf', 'NaN'
///
/// Leading and trailing whitespace represent an error.
///
/// # Arguments
///
/// * src - A string
///
/// # Return value
///
/// `Err(ParseFloatError)` if the string did not represent a valid
/// number. Otherwise, `Ok(n)` where `n` is the floating-point
/// number represented by `src`.
#[inline]
fn from_str(src: &str) -> Result<Self, ParseFloatError> {
dec2flt(src)
}
}
}
}
from_str_float_impl!(f32, to_f32);
from_str_float_impl!(f64, to_f64);

/// An error which can be returned when parsing a float.
#[derive(Debug, Clone, PartialEq)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct ParseFloatError {
kind: FloatErrorKind
}

#[derive(Debug, Clone, PartialEq)]
enum FloatErrorKind {
Empty,
Invalid,
}

impl ParseFloatError {
#[unstable(feature = "int_error_internals",
reason = "available through Error trait and this method should \
not be exposed publicly",
issue = "0")]
#[doc(hidden)]
pub fn __description(&self) -> &str {
match self.kind {
FloatErrorKind::Empty => "cannot parse float from empty string",
FloatErrorKind::Invalid => "invalid float literal",
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for ParseFloatError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.__description().fmt(f)
}
}

pub fn pfe_empty() -> ParseFloatError {
ParseFloatError { kind: FloatErrorKind::Empty }
}

/// Entry point for decimal-to-f64 conversion.
pub fn to_f64(s: &str) -> Result<f64, PFE> {
dec2flt(s)
pub fn pfe_invalid() -> ParseFloatError {
ParseFloatError { kind: FloatErrorKind::Invalid }
}

/// Split decimal string into sign and the rest, without inspecting or validating the rest.
Expand All @@ -131,9 +205,9 @@ fn extract_sign(s: &str) -> (Sign, &str) {
}

/// Convert a decimal string into a floating point number.
fn dec2flt<T: RawFloat>(s: &str) -> Result<T, PFE> {
fn dec2flt<T: RawFloat>(s: &str) -> Result<T, ParseFloatError> {
if s.is_empty() {
return Err(PFE { __kind: FloatErrorKind::Empty });
return Err(pfe_empty())
}
let (sign, s) = extract_sign(s);
let flt = match parse_decimal(s) {
Expand All @@ -143,7 +217,7 @@ fn dec2flt<T: RawFloat>(s: &str) -> Result<T, PFE> {
ParseResult::Invalid => match s {
"inf" => T::infinity(),
"NaN" => T::nan(),
_ => { return Err(PFE { __kind: FloatErrorKind::Invalid }); }
_ => { return Err(pfe_invalid()); }
}
};

Expand All @@ -155,7 +229,7 @@ fn dec2flt<T: RawFloat>(s: &str) -> Result<T, PFE> {

/// The main workhorse for the decimal-to-float conversion: Orchestrate all the preprocessing
/// and figure out which algorithm should do the actual conversion.
fn convert<T: RawFloat>(mut decimal: Decimal) -> Result<T, PFE> {
fn convert<T: RawFloat>(mut decimal: Decimal) -> Result<T, ParseFloatError> {
simplify(&mut decimal);
if let Some(x) = trivial_cases(&decimal) {
return Ok(x);
Expand All @@ -172,7 +246,7 @@ fn convert<T: RawFloat>(mut decimal: Decimal) -> Result<T, PFE> {
// If we exceed this, perhaps while calculating `f * 10^e` in Algorithm R or Algorithm M,
// we'll crash. So we error out before getting too close, with a generous safety margin.
if max_digits > 375 {
return Err(PFE { __kind: FloatErrorKind::Invalid });
return Err(pfe_invalid());
}
let f = digits_to_big(decimal.integral, decimal.fractional);

Expand Down
17 changes: 8 additions & 9 deletions src/libcore/num/float_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ macro_rules! from_str_radix_float_impl {
($T:ty) => {
fn from_str_radix(src: &str, radix: u32)
-> Result<$T, ParseFloatError> {
use num::FloatErrorKind::*;
use num::ParseFloatError as PFE;
use num::dec2flt::{pfe_empty, pfe_invalid};

// Special values
match src {
Expand All @@ -35,8 +34,8 @@ macro_rules! from_str_radix_float_impl {
}

let (is_positive, src) = match src.slice_shift_char() {
None => return Err(PFE { __kind: Empty }),
Some(('-', "")) => return Err(PFE { __kind: Empty }),
None => return Err(pfe_empty()),
Some(('-', "")) => return Err(pfe_empty()),
Some(('-', src)) => (false, src),
Some((_, _)) => (true, src),
};
Expand Down Expand Up @@ -88,7 +87,7 @@ macro_rules! from_str_radix_float_impl {
break; // start of fractional part
},
_ => {
return Err(PFE { __kind: Invalid });
return Err(pfe_invalid())
},
},
}
Expand Down Expand Up @@ -122,7 +121,7 @@ macro_rules! from_str_radix_float_impl {
break; // start of exponent
},
_ => {
return Err(PFE { __kind: Invalid });
return Err(pfe_invalid())
},
},
}
Expand All @@ -135,7 +134,7 @@ macro_rules! from_str_radix_float_impl {
let base = match c {
'E' | 'e' if radix == 10 => 10.0,
'P' | 'p' if radix == 16 => 2.0,
_ => return Err(PFE { __kind: Invalid }),
_ => return Err(pfe_invalid()),
};

// Parse the exponent as decimal integer
Expand All @@ -144,13 +143,13 @@ macro_rules! from_str_radix_float_impl {
Some(('-', src)) => (false, src.parse::<usize>()),
Some(('+', src)) => (true, src.parse::<usize>()),
Some((_, _)) => (true, src.parse::<usize>()),
None => return Err(PFE { __kind: Invalid }),
None => return Err(pfe_invalid()),
};

match (is_positive, exp) {
(true, Ok(exp)) => base.powi(exp as i32),
(false, Ok(exp)) => 1.0 / base.powi(exp as i32),
(_, Err(_)) => return Err(PFE { __kind: Invalid }),
(_, Err(_)) => return Err(pfe_invalid()),
}
},
None => 1.0, // no exponent
Expand Down
4 changes: 3 additions & 1 deletion src/libcore/num/i16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Operations and constants for signed 16-bits integers (`i16` type)
//! The 16-bit signed integer type.
//!
//! *[See also the `i16` primitive type](../primitive.i16.html).*

#![stable(feature = "rust1", since = "1.0.0")]

Expand Down
4 changes: 3 additions & 1 deletion src/libcore/num/i32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Operations and constants for signed 32-bits integers (`i32` type)
//! The 32-bit signed integer type.
//!
//! *[See also the `i32` primitive type](../primitive.i32.html).*

#![stable(feature = "rust1", since = "1.0.0")]

Expand Down
4 changes: 3 additions & 1 deletion src/libcore/num/i64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Operations and constants for signed 64-bits integers (`i64` type)
//! The 64-bit signed integer type.
//!
//! *[See also the `i64` primitive type](../primitive.i64.html).*

#![stable(feature = "rust1", since = "1.0.0")]

Expand Down
4 changes: 3 additions & 1 deletion src/libcore/num/i8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Operations and constants for signed 8-bits integers (`i8` type)
//! The 8-bit signed integer type.
//!
//! *[See also the `i8` primitive type](../primitive.i8.html).*

#![stable(feature = "rust1", since = "1.0.0")]

Expand Down
4 changes: 3 additions & 1 deletion src/libcore/num/isize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Operations and constants for pointer-sized signed integers (`isize` type)
//! The pointer-sized signed integer type.
//!
//! *[See also the `isize` primitive type](../primitive.isize.html).*

#![stable(feature = "rust1", since = "1.0.0")]

Expand Down
79 changes: 1 addition & 78 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1365,47 +1365,6 @@ pub trait Float: Sized {
fn to_radians(self) -> Self;
}

macro_rules! from_str_float_impl {
($t:ty, $func:ident) => {
#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for $t {
type Err = ParseFloatError;

/// Converts a string in base 10 to a float.
/// Accepts an optional decimal exponent.
///
/// This function accepts strings such as
///
/// * '3.14'
/// * '-3.14'
/// * '2.5E10', or equivalently, '2.5e10'
/// * '2.5E-10'
/// * '.' (understood as 0)
/// * '5.'
/// * '.5', or, equivalently, '0.5'
/// * 'inf', '-inf', 'NaN'
///
/// Leading and trailing whitespace represent an error.
///
/// # Arguments
///
/// * src - A string
///
/// # Return value
///
/// `Err(ParseFloatError)` if the string did not represent a valid
/// number. Otherwise, `Ok(n)` where `n` is the floating-point
/// number represented by `src`.
#[inline]
fn from_str(src: &str) -> Result<Self, ParseFloatError> {
dec2flt::$func(src)
}
}
}
}
from_str_float_impl!(f32, to_f32);
from_str_float_impl!(f64, to_f64);

macro_rules! from_str_radix_int_impl {
($($t:ty)*) => {$(
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -1548,40 +1507,4 @@ impl fmt::Display for ParseIntError {
}
}

/// An error which can be returned when parsing a float.
#[derive(Debug, Clone, PartialEq)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct ParseFloatError {
#[doc(hidden)]
#[unstable(feature = "float_error_internals",
reason = "should not be exposed publicly",
issue = "0")]
pub __kind: FloatErrorKind
}

#[derive(Debug, Clone, PartialEq)]
#[unstable(feature = "float_error_internals",
reason = "should not be exposed publicly",
issue = "0")]
#[doc(hidden)]
pub enum FloatErrorKind {
Empty,
Invalid,
}

impl ParseFloatError {
#[doc(hidden)]
pub fn __description(&self) -> &str {
match self.__kind {
FloatErrorKind::Empty => "cannot parse float from empty string",
FloatErrorKind::Invalid => "invalid float literal",
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for ParseFloatError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.__description().fmt(f)
}
}
pub use num::dec2flt::ParseFloatError;
4 changes: 3 additions & 1 deletion src/libcore/num/u16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Operations and constants for unsigned 16-bits integers (`u16` type)
//! The 16-bit unsigned integer type.
//!
//! *[See also the `u16` primitive type](../primitive.u16.html).*

#![stable(feature = "rust1", since = "1.0.0")]

Expand Down
4 changes: 3 additions & 1 deletion src/libcore/num/u32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Operations and constants for unsigned 32-bits integers (`u32` type)
//! The 32-bit unsigned integer type.
//!
//! *[See also the `u32` primitive type](../primitive.u32.html).*

#![stable(feature = "rust1", since = "1.0.0")]

Expand Down
Loading