dashu_float/
parse.rs

1#![allow(deprecated)] // TODO(v0.5): remove after from_str_native is made private.
2
3use crate::{
4    fbig::FBig,
5    repr::{Context, Repr, Word},
6    round::Round,
7};
8use core::{num::IntErrorKind, str::FromStr};
9use dashu_base::{ParseError, Sign};
10use dashu_int::{
11    fmt::{MAX_RADIX, MIN_RADIX},
12    UBig,
13};
14
15impl<const B: Word> Repr<B> {
16    /// Convert a string in the native base (i.e. radix `B`) to [Repr].
17    ///
18    /// Upon success, this method returns an [Repr] and the number of digits (in radix `B`)
19    /// contained in the string.
20    ///
21    /// This method is the underlying implementation of [FBig::from_str_native],
22    /// see the docs for that function for details.
23    #[deprecated(
24        since = "0.5.0",
25        note = "from_str_native will be removed in v0.5. Use core::str::FromStr instead."
26    )] // TODO(v0.5): deprecate
27    pub fn from_str_native(mut src: &str) -> Result<(Self, usize), ParseError> {
28        assert!(MIN_RADIX as Word <= B && B <= MAX_RADIX as Word);
29
30        // parse and remove the sign
31        let sign = match src.strip_prefix('-') {
32            Some(s) => {
33                src = s;
34                Sign::Negative
35            }
36            None => {
37                src = src.strip_prefix('+').unwrap_or(src);
38                Sign::Positive
39            }
40        };
41
42        // determine the position of scale markers
43        let has_prefix = src.starts_with("0x") || src.starts_with("0X");
44        let scale_pos = match B {
45            10 => src.rfind(&['e', 'E', '@']),
46            2 => {
47                if has_prefix {
48                    src.rfind(&['p', 'P', '@'])
49                } else {
50                    src.rfind(&['b', 'B', '@'])
51                }
52            }
53            8 => src.rfind(&['o', 'O', '@']),
54            16 => src.rfind(&['h', 'H', '@']),
55            _ => src.rfind('@'),
56        };
57
58        // parse scale and remove the scale part from the str
59        let (scale, pmarker) = if let Some(pos) = scale_pos {
60            let value = match src[pos + 1..].parse::<isize>() {
61                Err(e) => match e.kind() {
62                    IntErrorKind::Empty => return Err(ParseError::NoDigits),
63                    _ => return Err(ParseError::InvalidDigit),
64                },
65                Ok(v) => v,
66            };
67            let use_p = if B == 2 {
68                src.as_bytes().get(pos) == Some(&b'p') || src.as_bytes().get(pos) == Some(&b'P')
69            } else {
70                false
71            };
72            src = &src[..pos];
73            (value, use_p)
74        } else {
75            (0, false)
76        };
77
78        // parse the body of the float number
79        let mut exponent = scale;
80        let ndigits;
81        let significand = if let Some(dot) = src.find('.') {
82            // check whether both integral part and fractional part are empty
83            if src.len() == 1 {
84                return Err(ParseError::NoDigits);
85            }
86
87            // parse integral part
88            let (int, int_digits, base) = if dot != 0 {
89                let int_str = &src[..dot];
90                if B == 2 && has_prefix {
91                    // only base 2 float is allowed using prefix
92                    let int_str = &int_str[2..];
93                    let digits = 4 * (int_str.len() - int_str.matches('_').count());
94                    if int_str.is_empty() {
95                        (UBig::ZERO, digits, 16)
96                    } else {
97                        (UBig::from_str_radix(int_str, 16)?, digits, 16)
98                    }
99                } else if B == 2 && pmarker && !has_prefix {
100                    return Err(ParseError::UnsupportedRadix);
101                } else {
102                    let digits = int_str.len() - int_str.matches('_').count();
103                    (UBig::from_str_radix(&src[..dot], B as u32)?, digits, B as u32)
104                }
105            } else {
106                if pmarker {
107                    // prefix is required for using `p` as scale marker
108                    return Err(ParseError::UnsupportedRadix);
109                }
110                (UBig::ZERO, 0, B as u32)
111            };
112
113            // parse fractional part
114            src = &src[dot + 1..];
115            let (fract, fract_digits) = if !src.is_empty() {
116                let mut digits = src.len() - src.matches('_').count();
117                if B == 2 && base == 16 {
118                    digits *= 4;
119                }
120                (UBig::from_str_radix(src, base)?, digits)
121            } else {
122                (UBig::ZERO, 0)
123            };
124            ndigits = int_digits + fract_digits;
125
126            if fract.is_zero() {
127                int
128            } else {
129                exponent -= fract_digits as isize;
130                int * UBig::from_word(B).pow(fract_digits) + fract
131            }
132        } else {
133            let has_prefix = src.starts_with("0x") || src.starts_with("0X");
134            if B == 2 && has_prefix {
135                src = &src[2..];
136                ndigits = 4 * (src.len() - src.matches('_').count());
137                UBig::from_str_radix(src, 16)?
138            } else if B == 2 && pmarker && !has_prefix {
139                return Err(ParseError::UnsupportedRadix);
140            } else {
141                ndigits = src.len() - src.matches('_').count();
142                UBig::from_str_radix(src, B as u32)?
143            }
144        };
145
146        let repr = Repr::new(sign * significand, exponent);
147        Ok((repr, ndigits))
148    }
149}
150
151impl<R: Round, const B: Word> FBig<R, B> {
152    /// Convert a string in the native base (i.e. radix `B`) to [FBig].
153    ///
154    /// If the parsing succeeded, the result number will be **losslessly** parsed from the
155    /// input string.
156    ///
157    /// This function is the actual implementation of the [FromStr] trait.
158    ///
159    /// **Note**: Infinites are **intentionally not supported** by this function.
160    ///
161    /// # Format
162    ///
163    /// The valid representations include
164    /// 1. `aaa` or `aaa.`
165    ///     * `aaa` is represented in native base `B` without base prefixes.
166    /// 1. `aaa.bbb` = `aaabbb / base ^ len(bbb)`
167    ///     * `aaa` and `bbb` are represented in native base `B` without base prefixes.
168    ///     * `len(bbb)` represents the number of digits in `bbb`, e.g `len(bbb)` is 3. (Same below)
169    /// 1. `aaa.bbb@cc` = `aaabbb * base ^ (cc - len(bbb))`
170    ///     * `aaa` and `bbb` are represented in native base `B`
171    ///     * This is consistent with the representation used by [GNU GMP](https://gmplib.org/manual/I_002fO-of-Floats).
172    /// 1. `aaa.bbbEcc` = `aaabbb * 10 ^ (cc - len(bbb))`
173    ///     * `E` could be lower case, base `B` must be 10
174    ///     * `aaa` and `bbb` are all represented in decimal
175    /// 1. `0xaaa` or `0xaaa`
176    /// 1. `0xaaa.bbb` = `0xaaabbb / 16 ^ len(bbb)`
177    /// 1. `0xaaa.bbbPcc` = `0xaaabbb / 16 ^ len(bbb) * 2 ^ cc`
178    ///     * `P` could be lower case, base `B` must be 2 (not 16!)
179    ///     * `aaa` and `bbb` are represented in hexadecimal
180    ///     * This is consistent with the [C++ hexadecimal literals](https://en.cppreference.com/w/cpp/language/floating_literal).
181    /// 1. `aaa.bbbBcc` = `aaabbb * 2 ^ (cc - len(bbb))`
182    /// 1. `aaa.bbbOcc` = `aaabbb * 8 ^ (cc - len(bbb))`
183    /// 1. `aaa.bbbHcc` = `aaabbb * 16 ^ (cc - len(bbb))`
184    ///     * `B`/`O`/`H` could be lower case, and base `B` must be consistent with the marker.
185    ///     * `aaa` and `bbb` are represented in binary/octal/hexadecimal correspondingly without prefix.
186    ///     * This is consistent with some scientific notations described in [Wikipedia](https://en.wikipedia.org/wiki/Scientific_notation#Other_bases).
187    ///
188    /// Digits 10-35 are represented by `a-z` or `A-Z`.
189    ///
190    /// Literal `aaa` and `cc` above can be signed, but `bbb` must be unsigned.
191    /// All `cc` are represented in decimal. Either `aaa` or `bbb` can be omitted
192    /// when its value is zero, but they are not allowed to be omitted at the same time.
193    ///
194    /// # Precision
195    ///
196    /// The precision of the parsed number is determined by the number of digits that are presented
197    /// in the input string. For example, the numbers parsed from `12.34` or `1.234e-1` will have a
198    /// precision of 4, while the ones parsed from `12.34000` or `00012.34` will have a precision of 7.
199    ///
200    /// # Examples
201    ///
202    /// ```
203    /// # use dashu_base::ParseError;
204    /// # use dashu_float::DBig;
205    /// use dashu_base::Approximation::*;
206    ///
207    /// let a = DBig::from_str_native("-1.23400e-3")?;
208    /// let b = DBig::from_str_native("-123.4@-05")?;
209    /// assert_eq!(a, b);
210    /// assert_eq!(a.precision(), 6);
211    /// assert_eq!(b.precision(), 4);
212    ///
213    /// assert!(DBig::from_str_native("-0x1.234p-3").is_err());
214    /// assert!(DBig::from_str_native("-1.234H-3").is_err());
215    /// # Ok::<(), ParseError>(())
216    /// ```
217    ///
218    /// # Panics
219    ///
220    /// Panics if the base `B` is not between [MIN_RADIX] and [MAX_RADIX] inclusive.
221    #[inline]
222    #[deprecated(
223        since = "0.5.0",
224        note = "from_str_native will be removed in v0.5. Use core::str::FromStr instead."
225    )] // TODO(v0.5): deprecate
226    pub fn from_str_native(src: &str) -> Result<Self, ParseError> {
227        let (repr, ndigits) = Repr::from_str_native(src)?;
228        Ok(Self {
229            repr,
230            context: Context::new(ndigits),
231        })
232    }
233}
234
235impl<R: Round, const B: Word> FromStr for FBig<R, B> {
236    type Err = ParseError;
237    #[inline]
238    fn from_str(s: &str) -> Result<Self, ParseError> {
239        FBig::from_str_native(s)
240    }
241}