8787 issue = "none"
8888) ]
8989
90- use self :: common:: BiasedFp ;
91- use self :: float:: RawFloat ;
92- use self :: lemire:: compute_float;
93- use self :: parse:: { parse_inf_nan, parse_number} ;
94- use self :: slow:: parse_long_mantissa;
95- use crate :: error :: Error ;
96- use crate :: fmt ;
97- use crate :: str :: FromStr ;
90+ use common:: BiasedFp ;
91+ use float:: RawFloat ;
92+ use lemire:: compute_float;
93+ use parse:: { parse_inf_nan, parse_number} ;
94+ use slow:: parse_long_mantissa;
95+
96+ use crate :: num :: ParseFloatError ;
97+ use crate :: num :: float_parse :: FloatErrorKind ;
9898
9999mod common;
100100pub mod decimal;
@@ -107,131 +107,6 @@ pub mod float;
107107pub mod lemire;
108108pub mod parse;
109109
110- macro_rules! from_str_float_impl {
111- ( $t: ty) => {
112- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
113- impl FromStr for $t {
114- type Err = ParseFloatError ;
115-
116- /// Converts a string in base 10 to a float.
117- /// Accepts an optional decimal exponent.
118- ///
119- /// This function accepts strings such as
120- ///
121- /// * '3.14'
122- /// * '-3.14'
123- /// * '2.5E10', or equivalently, '2.5e10'
124- /// * '2.5E-10'
125- /// * '5.'
126- /// * '.5', or, equivalently, '0.5'
127- /// * '7'
128- /// * '007'
129- /// * 'inf', '-inf', '+infinity', 'NaN'
130- ///
131- /// Note that alphabetical characters are not case-sensitive.
132- ///
133- /// Leading and trailing whitespace represent an error.
134- ///
135- /// # Grammar
136- ///
137- /// All strings that adhere to the following [EBNF] grammar when
138- /// lowercased will result in an [`Ok`] being returned:
139- ///
140- /// ```txt
141- /// Float ::= Sign? ( 'inf' | 'infinity' | 'nan' | Number )
142- /// Number ::= ( Digit+ |
143- /// Digit+ '.' Digit* |
144- /// Digit* '.' Digit+ ) Exp?
145- /// Exp ::= 'e' Sign? Digit+
146- /// Sign ::= [+-]
147- /// Digit ::= [0-9]
148- /// ```
149- ///
150- /// [EBNF]: https://www.w3.org/TR/REC-xml/#sec-notation
151- ///
152- /// # Arguments
153- ///
154- /// * src - A string
155- ///
156- /// # Return value
157- ///
158- /// `Err(ParseFloatError)` if the string did not represent a valid
159- /// number. Otherwise, `Ok(n)` where `n` is the closest
160- /// representable floating-point number to the number represented
161- /// by `src` (following the same rules for rounding as for the
162- /// results of primitive operations).
163- // We add the `#[inline(never)]` attribute, since its content will
164- // be filled with that of `dec2flt`, which has #[inline(always)].
165- // Since `dec2flt` is generic, a normal inline attribute on this function
166- // with `dec2flt` having no attributes results in heavily repeated
167- // generation of `dec2flt`, despite the fact only a maximum of 2
168- // possible instances can ever exist. Adding #[inline(never)] avoids this.
169- #[ inline( never) ]
170- fn from_str( src: & str ) -> Result <Self , ParseFloatError > {
171- dec2flt( src)
172- }
173- }
174- } ;
175- }
176-
177- #[ cfg( target_has_reliable_f16) ]
178- from_str_float_impl ! ( f16) ;
179- from_str_float_impl ! ( f32 ) ;
180- from_str_float_impl ! ( f64 ) ;
181-
182- // FIXME(f16): A fallback is used when the backend+target does not support f16 well, in order
183- // to avoid ICEs.
184-
185- #[ cfg( not( target_has_reliable_f16) ) ]
186- impl FromStr for f16 {
187- type Err = ParseFloatError ;
188-
189- #[ inline]
190- fn from_str ( _src : & str ) -> Result < Self , ParseFloatError > {
191- unimplemented ! ( "requires target_has_reliable_f16" )
192- }
193- }
194-
195- /// An error which can be returned when parsing a float.
196- ///
197- /// This error is used as the error type for the [`FromStr`] implementation
198- /// for [`f32`] and [`f64`].
199- ///
200- /// # Example
201- ///
202- /// ```
203- /// use std::str::FromStr;
204- ///
205- /// if let Err(e) = f64::from_str("a.12") {
206- /// println!("Failed conversion to f64: {e}");
207- /// }
208- /// ```
209- #[ derive( Debug , Clone , PartialEq , Eq ) ]
210- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
211- pub struct ParseFloatError {
212- kind : FloatErrorKind ,
213- }
214-
215- #[ derive( Debug , Clone , PartialEq , Eq ) ]
216- enum FloatErrorKind {
217- Empty ,
218- Invalid ,
219- }
220-
221- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
222- impl Error for ParseFloatError { }
223-
224- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
225- impl fmt:: Display for ParseFloatError {
226- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
227- match self . kind {
228- FloatErrorKind :: Empty => "cannot parse float from empty string" ,
229- FloatErrorKind :: Invalid => "invalid float literal" ,
230- }
231- . fmt ( f)
232- }
233- }
234-
235110#[ inline]
236111pub ( super ) fn pfe_empty ( ) -> ParseFloatError {
237112 ParseFloatError { kind : FloatErrorKind :: Empty }
0 commit comments