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
219
220
221
222
223
224
225
226
227
228
229
230
//! JavaScript-style parseInt-like parsing of numbers from strings in Rust.
use super::*;
use num;

/// Internal function to parse uint values from a char-iterator with a given radix.
pub fn parse_uint_internal<
    T: num::Integer + num::CheckedAdd + num::CheckedMul + num::FromPrimitive,
>(
    chars: &mut dyn PeekableIterator<Item = char>,
    mut radix: Option<u32>,
) -> Option<T> {
    let mut ret = T::zero();
    let mut any = false;

    if radix.is_none() {
        if let Some('0') = chars.peek() {
            chars.next();
            any = true;

            match chars.peek() {
                Some('x') | Some('X') => {
                    radix = Some(16);
                    chars.next();
                    any = false; // expecting a hex number!
                }
                _ => {}
            }
        }
    }

    let radix = radix.unwrap_or(10);

    while let Some(dig) = chars.peek() {
        match dig.to_digit(radix) {
            Some(digit) => {
                ret = ret.checked_mul(&T::from_u32(radix).unwrap()).unwrap();
                ret = ret.checked_add(&T::from_u32(digit).unwrap()).unwrap();

                chars.next();
                any = true;
            }
            None => break,
        }
    }

    if any {
        Some(ret)
    } else {
        None
    }
}

/// Parse uint values from an iterator with a given radix.
pub fn parse_uint_from_iter_with_radix<
    T: num::Integer + num::CheckedAdd + num::CheckedMul + num::FromPrimitive,
>(
    chars: &mut dyn PeekableIterator<Item = char>,
    radix: Option<u32>,
    whitespace: bool,
) -> Option<T> {
    while let Some(ch) = chars.peek() {
        if whitespace && ch.is_whitespace() {
            chars.next();
            continue;
        }
        else if *ch == '+' {
            chars.next();
        }

        break;
    }

    parse_uint_internal::<T>(chars, radix)
}

/// Parse decimal uint values from an iterator.
pub fn parse_uint_from_iter<
    T: num::Integer + num::CheckedAdd + num::CheckedMul + num::FromPrimitive,
>(
    chars: &mut dyn PeekableIterator<Item = char>,
    whitespace: bool,
) -> Option<T> {
    parse_uint_from_iter_with_radix(chars, None, whitespace)
}

/// Parse int values from an iterator with a given radix.
pub fn parse_int_from_iter_with_radix<
    T: num::Integer + num::CheckedAdd + num::CheckedMul + num::FromPrimitive + num::Signed,
>(
    chars: &mut dyn PeekableIterator<Item = char>,
    radix: Option<u32>,
    whitespace: bool,
) -> Option<T> {
    let mut neg = false;

    while let Some(ch) = chars.peek() {
        if whitespace && ch.is_whitespace() {
            chars.next();
            continue;
        }

        if *ch == '+' || *ch == '-' {
            neg = *ch == '-';
            chars.next();
        }

        break;
    }

    if let Some(ret) = parse_uint_internal::<T>(chars, radix) {
        if neg {
            Some(-ret)
        } else {
            Some(ret)
        }
    } else {
        None
    }
}

/// Parse decimal int values from an iterator.
pub fn parse_int_from_iter<
    T: num::Integer + num::CheckedAdd + num::CheckedMul + num::FromPrimitive + num::Signed,
>(
    chars: &mut dyn PeekableIterator<Item = char>,
    whitespace: bool,
) -> Option<T> {
    parse_int_from_iter_with_radix::<T>(chars, None, whitespace)
}

/// Parse uint values from a &str with a given radix.
pub fn parse_uint_with_radix<
    T: num::Integer + num::CheckedAdd + num::CheckedMul + num::FromPrimitive,
>(
    s: &str,
    radix: u32,
) -> Option<T> {
    parse_uint_from_iter_with_radix::<T>(&mut s.chars().peekable(), Some(radix), true)
}

/// Parse decimal uint values from a &str.
pub fn parse_uint<T: num::Integer + num::CheckedAdd + num::CheckedMul + num::FromPrimitive>(
    s: &str,
) -> Option<T> {
    parse_uint_from_iter_with_radix::<T>(&mut s.chars().peekable(), None, true)
}

/// Parse int values from a &str with a given radix.
pub fn parse_int_with_radix<
    T: num::Integer + num::CheckedAdd + num::CheckedMul + num::FromPrimitive + num::Signed,
>(
    s: &str,
    radix: u32,
) -> Option<T> {
    parse_int_from_iter_with_radix::<T>(&mut s.chars().peekable(), Some(radix), true)
}

/// Parse decimal int values from a &str.
pub fn parse_int<
    T: num::Integer + num::CheckedAdd + num::CheckedMul + num::FromPrimitive + num::Signed,
>(
    s: &str,
) -> Option<T> {
    parse_int_from_iter_with_radix::<T>(&mut s.chars().peekable(), None, true)
}

#[test]
fn test_parse_uint_i64() {
    assert_eq!(parse_uint::<i64>(" 123hello "), Some(123i64));
    assert_eq!(parse_uint::<i64>(" 0xcafebabe "), Some(3405691582i64));
    assert_eq!(parse_uint::<i64>(" 0 "), Some(0));
    assert_eq!(parse_uint::<i64>(" 0x "), None);
    assert_eq!(parse_uint::<i64>(" 0x1 "), Some(1));
    assert_eq!(parse_uint::<i64>(" 456hello "), Some(456i64));
    assert_eq!(parse_uint::<i64>(" -789hello "), None);
}

#[test]
fn test_parse_uint_base16_i64() {
    assert_eq!(
        parse_uint_with_radix::<i64>("CAFEBABE", 16),
        Some(3405691582i64)
    );
    assert_eq!(
        parse_uint_with_radix::<i64>("  cafebabeyeah", 16),
        Some(3405691582i64)
    );
    assert_eq!(parse_int_with_radix::<i64>("  0xcafebabeyeah", 16), Some(0));
}

#[test]
fn test_parse_int_i64() {
    assert_eq!(parse_int::<i64>("123hello"), Some(123i64));
    assert_eq!(parse_int::<i64>("    456hello"), Some(456i64));
    assert_eq!(parse_int::<i64>("  -789hello"), Some(-789i64));
}

#[test]
fn test_parse_int_base16_i64() {
    assert_eq!(
        parse_int_with_radix::<i64>("  -CAFEBABE", 16),
        Some(-3405691582i64)
    );
    assert_eq!(
        parse_int_with_radix::<i64>("  -cafebabeyeah", 16),
        Some(-3405691582i64)
    );
    assert_eq!(
        parse_int_with_radix::<i64>("  -0xcafebabeyeah", 16),
        Some(0)
    );
}

#[test]
fn test_readme() {
    assert_eq!(parse_uint::<i32>("+123 as i32 "), Some(123i32));
    assert_eq!(parse_int::<i32>(" -123 as i32 "), Some(-123i32));
    assert_eq!(parse_uint::<i64>("+123 as i64 "), Some(123i64));
    assert_eq!(parse_int::<i64>(" -123 as i64 "), Some(-123i64));

    assert_eq!(parse_int::<i64>(" - 1 is invalid "), None);
    assert_eq!(
        parse_uint::<u64>(" -123 as u64, parse_int() not available for this type "),
        None
    );
    assert_eq!(
        parse_uint::<usize>(" 0xcafebabe triggers hex-mode parsing "),
        Some(3405691582usize)
    );
}