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
const DIGITS: [u8; 36] = [
    b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'a', b'b', b'c', b'd', b'e', b'f',
    b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v',
    b'w', b'x', b'y', b'z',
];

pub trait ToStringRadix {
    fn to_string_radix(&self, radix: i32) -> Result<String, String>;
}

impl ToStringRadix for i32 {
    /// Convert `i32` to a Hex string
    ///
    /// # Examples
    /// ```
    /// use bczhc_lib::i32::ToStringRadix;
    ///
    /// assert_eq!(51966.to_string_radix(16).unwrap(), "cafe")
    /// ```
    fn to_string_radix(&self, radix: i32) -> Result<String, String> {
        let mut i = *self;

        if !(2..=36).contains(&radix) {
            return Err(String::from("Invalid radix"));
        }

        if radix == 10 {
            return Ok(i.to_string());
        }

        let mut buf = [0; 33];
        let negative = i < 0;
        let mut char_pos = 32;

        if !negative {
            i = -i;
        }

        while i <= -radix {
            buf[char_pos] = DIGITS[-(i % radix) as usize];
            char_pos -= 1;
            i /= radix;
        }
        buf[char_pos] = DIGITS[-i as usize];

        if negative {
            char_pos -= 1;
            buf[char_pos] = b'-';
        }

        let size = 33 - char_pos;
        let mut c = vec![0_u8; size];
        c[..size].copy_from_slice(&buf[char_pos..(size + char_pos)]);
        let result = String::from_utf8(c);
        if let Ok(v) = result {
            Ok(v)
        } else if let Err(e) = result {
            Err(e.to_string())
        } else {
            panic!();
        }
    }
}