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
use crate::{Error, Result};

const BASE: u64 = 0x20;

/// Attempts to decode a Crockford Base32-encoded string into a `u64` value.
pub fn decode(input: impl AsRef<str>) -> Result<u64> {
    let input = input.as_ref();
    match input.len() {
        0 => Err(Error::EmptyString),
        n if n > 13 => Err(Error::OutOfRange),
        _ => decode_str(input),
    }
}

#[inline]
fn decode_str(input: &str) -> Result<u64> {
    let mut place = BASE.pow(input.len() as u32 - 1);
    let mut n: u64 = 0;

    for (idx, u) in input.bytes().enumerate() {
        let digit = to_normal_digit(idx, u)?;
        n = u64::from(digit)
            .checked_mul(place)
            .and_then(|m| n.checked_add(m))
            .ok_or(Error::OutOfRange)?;
        place >>= 5;
    }

    Ok(n)
}

/// Attempts to convert an ascii digit to a normalized form.
#[inline]
fn to_normal_digit(idx: usize, u: u8) -> Result<u8> {
    static VALUE_MAPPING: [i8; 256] = include!("../resources/u8-mapping.txt");
    unsafe {
        match VALUE_MAPPING.get_unchecked(u as usize) {
            -1 => Err(Error::InvalidDigit {
                index: idx,
                value: u,
            }),
            -2 => Err(Error::CheckDigitUnsupported {
                index: idx,
                value: u,
            }),
            &result => Ok(result as u8),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{decode, Error};

    #[test]
    fn zero_length_strings_fail() {
        let input = "";
        let expected = Err(Error::EmptyString);
        let actual = decode(input);
        assert_eq!(expected, actual);
    }

    #[test]
    fn long_strings_fail() {
        let input = "12345678910121";
        let expected = Err(Error::OutOfRange);
        let actual = decode(input);

        assert_eq!(expected, actual);
    }

    #[test]
    fn invalid_bytes_fail() {
        let input = "fZZ!2";
        let expected = Err(Error::InvalidDigit {
            index: 3,
            value: 33,
        });
        let actual = decode(input);
        assert_eq!(expected, actual);
    }

    #[test]
    fn zero_becomes_zero() {
        let input = "0";
        let expected = Ok(0);
        let actual = decode(input);

        assert_eq!(expected, actual);
    }

    #[test]
    fn large_values_become_large_values() {
        assert_eq!(Ok(65535), decode("1zzz"));
        assert_eq!(Ok(65535), decode("1ZZZ"));
    }

    #[test]
    fn map_to_0() {
        assert_eq!(Ok(0), decode("O"));
        assert_eq!(Ok(0), decode("o"));
    }

    #[test]
    fn map_to_1() {
        assert_eq!(Ok(1), decode("I"));
        assert_eq!(Ok(1), decode("i"));
        assert_eq!(Ok(1), decode("L"));
        assert_eq!(Ok(1), decode("l"));
    }

    #[test]
    fn z_equals_31() {
        assert_eq!(Ok(31), decode("z"));
        assert_eq!(Ok(31), decode("Z"));
    }

    #[test]
    fn q_equals_23() {
        assert_eq!(Ok(23), decode("q"));
        assert_eq!(Ok(23), decode("Q"));
    }

    #[test]
    fn four_z_q_works() {
        assert_eq!(Ok(5111), decode("4zq"));
        assert_eq!(Ok(5111), decode("4ZQ"));
    }

    #[test]
    fn max_value_works() {
        assert_eq!(Ok(18446744073709551615), decode("fzzzzzzzzzzzz"));
    }

    #[test]
    fn u_produces_an_error_instead_of_a_crash() {
        assert!(decode("iVuv").is_err());
        assert!(decode("iVUv").is_err());
    }

    #[test]
    /// This is not a valid u64: ZJ75K085CMJ1A
    fn issue_16() {
        assert!(decode("ZJ75K085CMJ1A").is_err());
    }
}