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
//!
//! Parse &str with common prefixes to integer values
//!
//! ```
//! # use std::error::Error;
//! # fn main() -> Result<(), Box<Error>> {
//! use parse_int::parse;
//! 
//! let d = parse::<usize>("42")?;
//! assert_eq!(42, d);
//! 
//! let d = parse::<isize>("0x42")?;
//! assert_eq!(66, d);
//! 
//! let d = parse::<u8>("042")?;
//! assert_eq!(34, d);
//! #
//! #     Ok(())
//! # }
//! ```

use num_traits::Num;

/// Parse &str with common prefixes to integer values
///
/// ```
/// # use std::error::Error;
/// # fn main() -> Result<(), Box<Error>> {
/// use parse_int::parse;
/// 
/// let d = parse::<usize>("42")?;
/// assert_eq!(42, d);
/// 
/// let d = parse::<isize>("0x42")?;
/// assert_eq!(66, d);
/// 
/// let d = parse::<u8>("042")?;
/// assert_eq!(34, d);
/// #
/// #     Ok(())
/// # }
/// ```
pub fn parse<T: Num>(input: &str) -> Result<T, T::FromStrRadixErr> {
    let input = input.trim();
    if input.starts_with("0x") {
        return T::from_str_radix(input.trim_start_matches("0x"), 16)
    }
    
    if input.starts_with("0") {
        return T::from_str_radix(input.trim_start_matches("0"), 8)
    }
    
    T::from_str_radix(input, 10)
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn usize_dec_turbofish() {
        let s = "42";
        
        let u = parse::<usize>(s).unwrap();
        
        assert_eq!(42, u);
    }
    
    #[test]
    fn usize_dec_deduct() {
        let s = "42";
        
        let u = parse(s).unwrap();
        
        assert_eq!(42usize, u);
    }
    
    macro_rules! int_parse {
        ($type:ident, $func_name:ident, $s:literal, $e:literal) => {
            #[test]
            fn $func_name() {
                let u: $type = parse($s).unwrap();
                assert_eq!($e, u);
            }
        }
    }
    
    int_parse!(usize, usize_dec, "42", 42);
    int_parse!(isize, isize_dec, "42", 42);
    int_parse!(   u8,    u8_dec, "42", 42);
    int_parse!(   i8,    i8_dec, "42", 42);
    int_parse!(  u16,   u16_dec, "42", 42);
    int_parse!(  i16,   i16_dec, "42", 42);
    int_parse!(  u32,   u32_dec, "42", 42);
    int_parse!(  i32,   i32_dec, "42", 42);
    int_parse!(  u64,   u64_dec, "42", 42);
    int_parse!(  i64,   i64_dec, "42", 42);
    int_parse!( u128,  u128_dec, "42", 42);
    int_parse!( i128,  i128_dec, "42", 42);
    
    int_parse!(usize, usize_hex, "0x42", 66);
    int_parse!(isize, isize_hex, "0x42", 66);
    int_parse!(   u8,    u8_hex, "0x42", 66);
    int_parse!(   i8,    i8_hex, "0x42", 66);
    int_parse!(  u16,   u16_hex, "0x42", 66);
    int_parse!(  i16,   i16_hex, "0x42", 66);
    int_parse!(  u32,   u32_hex, "0x42", 66);
    int_parse!(  i32,   i32_hex, "0x42", 66);
    int_parse!(  u64,   u64_hex, "0x42", 66);
    int_parse!(  i64,   i64_hex, "0x42", 66);
    int_parse!( u128,  u128_hex, "0x42", 66);
    int_parse!( i128,  i128_hex, "0x42", 66);
    
    int_parse!(usize, usize_oct, "042", 34);
    int_parse!(isize, isize_oct, "042", 34);
    int_parse!(   u8,    u8_oct, "042", 34);
    int_parse!(   i8,    i8_oct, "042", 34);
    int_parse!(  u16,   u16_oct, "042", 34);
    int_parse!(  i16,   i16_oct, "042", 34);
    int_parse!(  u32,   u32_oct, "042", 34);
    int_parse!(  i32,   i32_oct, "042", 34);
    int_parse!(  u64,   u64_oct, "042", 34);
    int_parse!(  i64,   i64_oct, "042", 34);
    int_parse!( u128,  u128_oct, "042", 34);
    int_parse!( i128,  i128_oct, "042", 34);
}