1 //! Permissive float-parsing data interface.
2 
3 use crate::util::*;
4 use super::exponent::*;
5 use super::traits::*;
6 use super::trim::*;
7 use super::validate::*;
8 
9 // Permissive data interface for fast float parsers.
10 //
11 // Guaranteed to parse `NumberFormat::permissive()`.
12 //
13 // The requirements:
14 //     1). Must contain significant digits.
15 //     2). Does not contain any digit separators.
16 data_interface!(
17     struct PermissiveFastDataInterface,
18     struct PermissiveSlowDataInterface,
19     fields => {},
20     integer_iter => (IteratorNoSeparator, iterate_digits_no_separator),
21     fraction_iter => (IteratorNoSeparator, iterate_digits_no_separator),
22     exponent_iter => (IteratorNoSeparator, iterate_digits_no_separator),
23     format => |_| NumberFormat::default(),
24     consume_integer_digits => consume_digits_no_separator,
25     consume_fraction_digits =>  consume_digits_no_separator,
26     extract_exponent => extract_exponent_no_separator,
27     validate_mantissa => validate_permissive_mantissa,
28     validate_exponent => validate_optional_exponent,
29     validate_exponent_fraction => validate_exponent_optional_fraction,
30     validate_exponent_sign => validate_optional_exponent_sign,
31     ltrim_zero => ltrim_zero_no_separator,
32     ltrim_separator => ltrim_separator_no_separator,
33     rtrim_zero => rtrim_zero_no_separator,
34     rtrim_separator => rtrim_separator_no_separator,
35     new => fn new(format: NumberFormat) -> Self {
36         Self {
37             integer: &[],
38             fraction: None,
39             exponent: None,
40             raw_exponent: 0
41         }
42     }
43 );
44 
45 // TESTS
46 // -----
47 
48 #[cfg(test)]
49 mod tests {
50     use super::*;
51 
52     macro_rules! permissive {
53         ($integer:expr, $fraction:expr, $exponent:expr, $raw_exponent:expr) => {
54             PermissiveFastDataInterface {
55                 integer: $integer,
56                 fraction: $fraction,
57                 exponent: $exponent,
58                 raw_exponent: $raw_exponent
59             }
60         };
61     }
62 
63     #[test]
extract_test()64     fn extract_test() {
65         PermissiveFastDataInterface::new(NumberFormat::permissive().unwrap()).run_tests([
66             // Valid
67             ("1.2345", Ok(permissive!(b"1", Some(b!("2345")), None, 0))),
68             ("12.345", Ok(permissive!(b"12", Some(b!("345")), None, 0))),
69             ("12345.6789", Ok(permissive!(b"12345", Some(b!("6789")), None, 0))),
70             ("1.2345e10", Ok(permissive!(b"1", Some(b!("2345")), Some(b!("10")), 10))),
71             ("1.2345e+10", Ok(permissive!(b"1", Some(b!("2345")), Some(b!("+10")), 10))),
72             ("1.2345e-10", Ok(permissive!(b"1", Some(b!("2345")), Some(b!("-10")), -10))),
73             ("100000000000000000000", Ok(permissive!(b"100000000000000000000", None, None, 0))),
74             ("100000000000000000001", Ok(permissive!(b"100000000000000000001", None, None, 0))),
75             ("179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791.9999999999999999999999999999999999999999999999999999999999999999999999", Ok(permissive!(b"179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791", Some(b!("9999999999999999999999999999999999999999999999999999999999999999999999")), None, 0))),
76             ("1009e-31", Ok(permissive!(b"1009", None, Some(b!("-31")), -31))),
77             ("001.0", Ok(permissive!(b"1", Some(b!("")), None, 0))),
78             ("1.", Ok(permissive!(b"1", Some(b!("")), None, 0))),
79             ("12.", Ok(permissive!(b"12", Some(b!("")), None, 0))),
80             ("1234567.", Ok(permissive!(b"1234567", Some(b!("")), None, 0))),
81             (".1", Ok(permissive!(b"", Some(b!("1")), None, 0))),
82             (".12", Ok(permissive!(b"", Some(b!("12")), None, 0))),
83             (".1234567", Ok(permissive!(b"", Some(b!("1234567")), None, 0))),
84             ("1.2345e", Ok(permissive!(b"1", Some(b!("2345")), Some(b!("")), 0))),
85             (".3e", Ok(permissive!(b"", Some(b!("3")), Some(b!("")), 0))),
86 
87             // Invalid
88             ("", Err(ErrorCode::EmptyMantissa)),
89             ("+", Err(ErrorCode::EmptyMantissa)),
90             ("-", Err(ErrorCode::EmptyMantissa)),
91             (".", Err(ErrorCode::EmptyMantissa)),
92             ("+.", Err(ErrorCode::EmptyMantissa)),
93             ("-.", Err(ErrorCode::EmptyMantissa)),
94             ("e", Err(ErrorCode::EmptyMantissa)),
95             ("E", Err(ErrorCode::EmptyMantissa)),
96             ("e1", Err(ErrorCode::EmptyMantissa)),
97             ("e+1", Err(ErrorCode::EmptyMantissa)),
98             ("e-1", Err(ErrorCode::EmptyMantissa)),
99             (".e", Err(ErrorCode::EmptyMantissa)),
100             (".E", Err(ErrorCode::EmptyMantissa)),
101             (".e1", Err(ErrorCode::EmptyMantissa)),
102             (".e+1", Err(ErrorCode::EmptyMantissa)),
103             (".e-1", Err(ErrorCode::EmptyMantissa)),
104         ].iter());
105     }
106 }
107