| 1 | //! This crate provides a super-fast decimal number parser from strings into |
| 2 | //! floats. |
| 3 | //! |
| 4 | //! ## Usage |
| 5 | //! |
| 6 | //! There's two top-level functions provided: [`parse`](crate::parse()) and |
| 7 | //! [`parse_partial`](crate::parse_partial()), both taking |
| 8 | //! either a string or a bytes slice and parsing the input into either `f32` or |
| 9 | //! `f64`: |
| 10 | //! |
| 11 | //! - [`parse`](crate::parse()) treats the whole string as a decimal number and |
| 12 | //! returns an error if there are invalid characters or if the string is |
| 13 | //! empty. |
| 14 | //! - [`parse_partial`](crate::parse_partial()) tries to find the longest |
| 15 | //! substring at the beginning of the given input string that can be parsed as |
| 16 | //! a decimal number and, in the case of success, returns the parsed value |
| 17 | //! along the number of characters processed; an error is returned if the |
| 18 | //! string doesn't start with a decimal number or if it is empty. This |
| 19 | //! function is most useful as a building block when constructing more complex |
| 20 | //! parsers, or when parsing streams of data. |
| 21 | //! |
| 22 | //! ## Examples |
| 23 | //! |
| 24 | //! ```rust |
| 25 | //! // Parse the entire string as a decimal number. |
| 26 | //! let s = "1.23e-02"; |
| 27 | //! let x: f32 = fast_float2::parse(s).unwrap(); |
| 28 | //! assert_eq!(x, 0.0123); |
| 29 | //! |
| 30 | //! // Parse as many characters as possible as a decimal number. |
| 31 | //! let s = "1.23e-02foo"; |
| 32 | //! let (x, n) = fast_float2::parse_partial::<f32, _>(s).unwrap(); |
| 33 | //! assert_eq!(x, 0.0123); |
| 34 | //! assert_eq!(n, 8); |
| 35 | //! assert_eq!(&s[n..], "foo"); |
| 36 | //! ``` |
| 37 | |
| 38 | #![cfg_attr(not(feature = "std"), no_std)] |
| 39 | #![allow(unused_unsafe)] |
| 40 | #![warn(unsafe_op_in_unsafe_fn)] |
| 41 | #![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)] |
| 42 | #![deny( |
| 43 | clippy::doc_markdown, |
| 44 | clippy::unnecessary_safety_comment, |
| 45 | clippy::semicolon_if_nothing_returned, |
| 46 | clippy::unwrap_used, |
| 47 | clippy::as_underscore, |
| 48 | clippy::doc_markdown |
| 49 | )] |
| 50 | #![allow( |
| 51 | clippy::cast_possible_truncation, |
| 52 | clippy::cast_possible_wrap, |
| 53 | clippy::cast_sign_loss, |
| 54 | clippy::cast_lossless, |
| 55 | clippy::cast_precision_loss, |
| 56 | clippy::missing_const_for_fn, |
| 57 | clippy::use_self, |
| 58 | clippy::module_name_repetitions, |
| 59 | clippy::cargo_common_metadata, |
| 60 | clippy::struct_field_names |
| 61 | )] |
| 62 | |
| 63 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::{selfrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/, Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#}; |
| 64 | |
| 65 | mod binaryrust-analyzer cargo fast-float2 0.2.3 binary/; |
| 66 | mod commonrust-analyzer cargo fast-float2 0.2.3 common/; |
| 67 | mod decimalrust-analyzer cargo fast-float2 0.2.3 decimal/; |
| 68 | mod floatrust-analyzer cargo fast-float2 0.2.3 float/; |
| 69 | mod numberrust-analyzer cargo fast-float2 0.2.3 number/; |
| 70 | mod parserust-analyzer cargo fast-float2 0.2.3 parse/; |
| 71 | mod simplerust-analyzer cargo fast-float2 0.2.3 simple/; |
| 72 | mod tablerust-analyzer cargo fast-float2 0.2.3 table/; |
| 73 | |
| 74 | /// Opaque error type for fast-float parsing functions. |
| 75 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 76 | pub struct Errorrust-analyzer cargo fast-float2 0.2.3 Error#; |
| 77 | |
| 78 | impl Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Errorrust-analyzer cargo fast-float2 0.2.3 Error# { |
| 79 | fn fmtrust-analyzer cargo fast-float2 0.2.3 impl#[Error][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 80 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "error while parsing a float") |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | #[cfg(feature = "std")] |
| 85 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for Errorrust-analyzer cargo fast-float2 0.2.3 Error# { |
| 86 | fn descriptionrust-analyzer cargo fast-float2 0.2.3 impl#[Error][Error]description().(&self) -> &'static str { |
| 87 | "error while parsing a float" |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// Result type alias for fast-float parsing functions. |
| 92 | pub type Resultrust-analyzer cargo fast-float2 0.2.3 Result#<T> = corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, Errorrust-analyzer cargo fast-float2 0.2.3 Error#>; |
| 93 | |
| 94 | /// Trait for numerical float types that can be parsed from string. |
| 95 | pub trait FastFloatrust-analyzer cargo fast-float2 0.2.3 FastFloat#: floatrust-analyzer cargo fast-float2 0.2.3 float/::Floatrust-analyzer cargo fast-float2 0.2.3 float/Float# { |
| 96 | /// Parse a decimal number from string into float (full). |
| 97 | /// |
| 98 | /// # Errors |
| 99 | /// |
| 100 | /// Will return an error either if the string is not a valid decimal number. |
| 101 | /// or if any characters are left remaining unparsed. |
| 102 | #[inline] |
| 103 | fn parse_floatrust-analyzer cargo fast-float2 0.2.3 FastFloat#parse_float().<S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(s: S) -> Resultrust-analyzer cargo fast-float2 0.2.3 Result#<Self> { |
| 104 | let s = s.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(); |
| 105 | match Self::parse_float_partialrust-analyzer cargo fast-float2 0.2.3 FastFloat#parse_float_partial().(s) { |
| 106 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((v, n)) if n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().s.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(v), |
| 107 | _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo fast-float2 0.2.3 Error#), |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// Parse a decimal number from string into float (partial). |
| 112 | /// |
| 113 | /// This method parses as many characters as possible and returns the |
| 114 | /// resulting number along with the number of digits processed (in case |
| 115 | /// of success, this number is always positive). |
| 116 | /// |
| 117 | /// # Errors |
| 118 | /// |
| 119 | /// Will return an error either if the string doesn't start with a valid |
| 120 | /// decimal number – that is, if no zero digits were processed. |
| 121 | #[inline] |
| 122 | fn parse_float_partialrust-analyzer cargo fast-float2 0.2.3 FastFloat#parse_float_partial().<S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(s: S) -> Resultrust-analyzer cargo fast-float2 0.2.3 Result#<(Self, usize)> { |
| 123 | parserust-analyzer cargo fast-float2 0.2.3 parse/::parse_floatrust-analyzer cargo fast-float2 0.2.3 parse/parse_float().(s.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(Errorrust-analyzer cargo fast-float2 0.2.3 Error#) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | impl FastFloatrust-analyzer cargo fast-float2 0.2.3 FastFloat# for f32 { |
| 128 | } |
| 129 | impl FastFloatrust-analyzer cargo fast-float2 0.2.3 FastFloat# for f64 { |
| 130 | } |
| 131 | |
| 132 | /// Parse a decimal number from string into float (full). |
| 133 | /// |
| 134 | /// # Errors |
| 135 | /// |
| 136 | /// Will return an error either if the string is not a valid decimal number |
| 137 | /// or if any characters are left remaining unparsed. |
| 138 | #[inline] |
| 139 | pub fn parserust-analyzer cargo fast-float2 0.2.3 parse().<T: FastFloatrust-analyzer cargo fast-float2 0.2.3 FastFloat#, S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(s: S) -> Resultrust-analyzer cargo fast-float2 0.2.3 Result#<T> { |
| 140 | T::parse_floatrust-analyzer cargo fast-float2 0.2.3 FastFloat#parse_float().(s) |
| 141 | } |
| 142 | |
| 143 | /// Parse a decimal number from string into float (partial). |
| 144 | /// |
| 145 | /// This function parses as many characters as possible and returns the |
| 146 | /// resulting number along with the number of digits processed (in case of |
| 147 | /// success, this number is always positive). |
| 148 | /// |
| 149 | /// # Errors |
| 150 | /// |
| 151 | /// Will return an error either if the string doesn't start with a valid decimal |
| 152 | /// number – that is, if no zero digits were processed. |
| 153 | #[inline] |
| 154 | pub fn parse_partialrust-analyzer cargo fast-float2 0.2.3 parse_partial().<T: FastFloatrust-analyzer cargo fast-float2 0.2.3 FastFloat#, S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(s: S) -> Resultrust-analyzer cargo fast-float2 0.2.3 Result#<(T, usize)> { |
| 155 | T::parse_float_partialrust-analyzer cargo fast-float2 0.2.3 FastFloat#parse_float_partial().(s) |
| 156 | } |