malachite_nz/integer/conversion/string/from_sci_string.rs
1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::integer::Integer;
10use crate::natural::conversion::string::from_sci_string::{
11 FromSciStringHelper, from_sci_string_with_options_helper,
12};
13use malachite_base::num::basic::traits::One;
14use malachite_base::num::conversion::string::options::FromSciStringOptions;
15use malachite_base::num::conversion::traits::{FromSciString, FromStringBase};
16
17impl FromSciStringHelper for Integer {
18 fn parse_int(mut cs: &[u8], base: u8) -> Option<Integer> {
19 if let Some(b'+') = cs.first() {
20 cs = &cs[1..];
21 // If the string begins with a '+', the second character cannot be '+' or '-'
22 match cs {
23 [] | [b'+' | b'-', ..] => return None,
24 _ => {}
25 }
26 }
27 Integer::from_string_base(base, core::str::from_utf8(cs).ok()?)
28 }
29
30 fn up_1(self, neg: bool) -> Option<Integer> {
31 Some(if neg {
32 self - Integer::ONE
33 } else {
34 self + Integer::ONE
35 })
36 }
37}
38
39impl FromSciString for Integer {
40 /// Converts a string, possibly in scientfic notation, to an [`Integer`].
41 ///
42 /// Use [`FromSciStringOptions`] to specify the base (from 2 to 36, inclusive) and the rounding
43 /// mode, in case rounding is necessary because the string represents a non-integer.
44 ///
45 /// If the base is greater than 10, the higher digits are represented by the letters `'a'`
46 /// through `'z'` or `'A'` through `'Z'`; the case doesn't matter and doesn't need to be
47 /// consistent.
48 ///
49 /// Exponents are allowed, and are indicated using the character `'e'` or `'E'`. If the base is
50 /// 15 or greater, an ambiguity arises where it may not be clear whether `'e'` is a digit or an
51 /// exponent indicator. To resolve this ambiguity, always use a `'+'` or `'-'` sign after the
52 /// exponent indicator when the base is 15 or greater.
53 ///
54 /// The exponent itself is always parsed using base 10.
55 ///
56 /// Decimal (or other-base) points are allowed. These are most useful in conjunction with
57 /// exponents, but they may be used on their own. If the string represents a non-integer, the
58 /// rounding mode specified in `options` is used to round to an integer.
59 ///
60 /// If the string is unparseable, `None` is returned. `None` is also returned if the rounding
61 /// mode in options is `Exact`, but rounding is necessary.
62 ///
63 /// # Worst-case complexity
64 /// $T(n, m) = O(m^n n \log m (\log n + \log\log m))$
65 ///
66 /// $M(n, m) = O(m^n n \log m)$
67 ///
68 /// where $T$ is time, $M$ is additional memory, $n$ is `s.len()`, and $m$ is `options.base`.
69 ///
70 /// # Examples
71 /// ```
72 /// use malachite_base::num::conversion::string::options::FromSciStringOptions;
73 /// use malachite_base::num::conversion::traits::FromSciString;
74 /// use malachite_base::rounding_modes::RoundingMode::*;
75 /// use malachite_nz::integer::Integer;
76 ///
77 /// assert_eq!(Integer::from_sci_string("123").unwrap(), 123);
78 /// assert_eq!(Integer::from_sci_string("123.5").unwrap(), 124);
79 /// assert_eq!(Integer::from_sci_string("-123.5").unwrap(), -124);
80 /// assert_eq!(Integer::from_sci_string("1.23e10").unwrap(), 12300000000i64);
81 ///
82 /// let mut options = FromSciStringOptions::default();
83 /// assert_eq!(
84 /// Integer::from_sci_string_with_options("123.5", options).unwrap(),
85 /// 124
86 /// );
87 ///
88 /// options.set_rounding_mode(Floor);
89 /// assert_eq!(
90 /// Integer::from_sci_string_with_options("123.5", options).unwrap(),
91 /// 123
92 /// );
93 ///
94 /// options = FromSciStringOptions::default();
95 /// options.set_base(16);
96 /// assert_eq!(
97 /// Integer::from_sci_string_with_options("ff", options).unwrap(),
98 /// 255
99 /// );
100 /// ```
101 #[inline]
102 fn from_sci_string_with_options(s: &str, options: FromSciStringOptions) -> Option<Integer> {
103 from_sci_string_with_options_helper(s, options)
104 }
105}