1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Module with traits for parsing a formatted string into a number.
//!
//! # Examples
//! ```
//! use num_format::Locale;
//! use num_format::parsing::ParseFormatted;
//!
//! fn main() {
//!     let s = "1,000,000";
//!     let n = s.parse_formatted::<_, u32>(&Locale::en).unwrap();
//!     assert_eq!(n, 1_000_000);
//! }
//! ```

use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
use core::str;

use crate::constants::*;
use crate::error::Error;
use crate::format::Format;
use crate::sealed::Sealed;

/// Trait that provides string-like types with a [`parse_formatted`]
/// method, allowing conversion from a formatted string into a number.
///
/// # Examples
/// ```
/// use num_format::Locale;
/// use num_format::parsing::ParseFormatted;
///
/// fn main() {
///     let s = "1,000,000";
///     let n = s.parse_formatted::<_, u32>(&Locale::en).unwrap();
///     assert_eq!(n, 1_000_000);
/// }
/// ```
///
/// [`parse_formatted`]: trait.ParseFormatted.html#method.parse_formatted
pub trait ParseFormatted {
    /// Converts `self` (typically a formatted string) into a number (see [Examples] above).
    ///
    /// [Examples]: trait.ParseFormatted.html#examples
    fn parse_formatted<F, N>(&self, format: &F) -> Result<N, Error>
    where
        F: Format,
        N: FromFormattedStr;
}

impl<S> ParseFormatted for S
where
    S: AsRef<str>,
{
    fn parse_formatted<F, N>(&self, format: &F) -> Result<N, Error>
    where
        F: Format,
        N: FromFormattedStr,
    {
        FromFormattedStr::from_formatted_str(self.as_ref(), format)
    }
}

/// Marker trait for number types (e.g. `u32`) that string-like types can be parsed
/// into via the [`ParseFormatted`] trait.
///
/// This trait is sealed; so you may not implement it on your own types.
///
/// [`ParseFormatted`]: trait.ParseFormatted.html
pub trait FromFormattedStr: Sealed + Sized {
    #[allow(missing_docs)]
    fn from_formatted_str<F>(s: &str, format: &F) -> Result<Self, Error>
    where
        F: Format;
}

macro_rules! impl_from_formatted_str {
    ($type:ty, $max_len:expr) => {
        impl FromFormattedStr for $type {
            fn from_formatted_str<F>(s: &str, format: &F) -> Result<Self, Error>
            where
                F: Format,
            {
                const BUF_LEN: usize = $max_len;
                let mut buf: [u8; BUF_LEN] = [0; BUF_LEN];

                let minus_sign = format.minus_sign().into_str();
                let is_negative = s.starts_with(minus_sign);

                let mut index = 0;
                if is_negative {
                    buf[index] = '-' as u8;
                    index += 1;
                }
                for c in s.chars() {
                    if c.is_numeric() {
                        if index > BUF_LEN {
                            return Err(Error::parse_number(&s));
                        }
                        buf[index] = c as u8;
                        index += 1;
                    }
                }

                if index == 0 {
                    return Err(Error::parse_number(&s));
                }

                let s2 = unsafe { str::from_utf8_unchecked(&buf[..index]) };
                let n = s2.parse::<$type>().map_err(|_| Error::parse_locale(&s))?;

                Ok(n)
            }
        }
    };
}

impl_from_formatted_str!(u8, U8_MAX_LEN);
impl_from_formatted_str!(u16, U16_MAX_LEN);
impl_from_formatted_str!(u32, U32_MAX_LEN);
impl_from_formatted_str!(usize, USIZE_MAX_LEN);
impl_from_formatted_str!(u64, U64_MAX_LEN);
impl_from_formatted_str!(u128, U128_MAX_LEN);

impl_from_formatted_str!(i8, I8_MAX_LEN);
impl_from_formatted_str!(i16, I16_MAX_LEN);
impl_from_formatted_str!(i32, I32_MAX_LEN);
impl_from_formatted_str!(isize, ISIZE_MAX_LEN);
impl_from_formatted_str!(i64, I64_MAX_LEN);
impl_from_formatted_str!(i128, I128_MAX_LEN);

macro_rules! impl_from_formatted_str_non_zero {
    ($type:ty, $related_type:ty, $max_len:expr) => {
        impl FromFormattedStr for $type {
            fn from_formatted_str<F>(s: &str, format: &F) -> Result<Self, Error>
            where
                F: Format,
            {
                let n = s.parse_formatted::<_, $related_type>(format)?;
                let n = Self::new(n).ok_or_else(|| Error::parse_number(s))?;
                Ok(n)
            }
        }
    };
}

impl_from_formatted_str_non_zero!(NonZeroU8, u8, U8_MAX_LEN);
impl_from_formatted_str_non_zero!(NonZeroU16, u16, U16_MAX_LEN);
impl_from_formatted_str_non_zero!(NonZeroU32, u32, U32_MAX_LEN);
impl_from_formatted_str_non_zero!(NonZeroUsize, usize, USIZE_MAX_LEN);
impl_from_formatted_str_non_zero!(NonZeroU64, u64, U64_MAX_LEN);
impl_from_formatted_str_non_zero!(NonZeroU128, u128, U128_MAX_LEN);

#[cfg(feature = "with-num-bigint")]
mod num {
    use num_bigint::{BigInt, BigUint};

    use super::*;

    macro_rules! impl_from_formatted_str_num_bigint {
        ($type:ty) => {
            impl FromFormattedStr for $type {
                fn from_formatted_str<F>(s: &str, format: &F) -> Result<Self, Error>
                where
                    F: Format,
                {
                    let mut buf = Vec::new();

                    let minus_sign = format.minus_sign().into_str();
                    let is_negative = s.starts_with(minus_sign);

                    if is_negative {
                        buf.push('-' as u8);
                    }
                    for c in s.chars() {
                        if c.is_numeric() {
                            buf.push(c as u8);
                        }
                    }

                    if buf.is_empty() {
                        return Err(Error::parse_number(&s));
                    }

                    let s2 = unsafe { str::from_utf8_unchecked(&buf[..]) };
                    let n = s2.parse::<$type>().map_err(|_| Error::parse_locale(&s))?;

                    Ok(n)
                }
            }
        };
    }

    impl_from_formatted_str_num_bigint!(BigInt);
    impl_from_formatted_str_num_bigint!(BigUint);

    #[cfg(test)]
    mod tests {
        use num_bigint::{ToBigInt, ToBigUint};

        use super::*;
        use crate::locale::Locale;

        #[test]
        fn test_parsing_num_bigint() {
            assert_eq!(
                "1,000,000"
                    .parse_formatted::<_, BigUint>(&Locale::en)
                    .unwrap(),
                1_000_000.to_biguint().unwrap()
            );
            assert_eq!(
                "-1,000,000"
                    .parse_formatted::<_, BigInt>(&Locale::en)
                    .unwrap(),
                (-1_000_000).to_bigint().unwrap()
            );
        }
    }
}