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
/// Formats a positive number into a `StackStr<N>`. The parameters required are the number that must be converted and the length of `N`, namely the number of digits of the given number, separated by a semicolon. In case the number has less digits than the specified length, it will have trailing zeros, represented as their utf-8 value: `48`. In case the length is less than required, it will result in undefined behavior.
/// 
/// # Examples
/// 
/// ```
/// 
/// assert_eq!("291", usize_to_str!(291; 3).str());
/// assert_eq!("0291", usize_to_str!(291; 4).str());
/// 
/// ```
/// 
///  This function by default accepts values up to `u64`. If values up to `u128` are needed, as a third parameter specify u128.
/// 
/// ```
/// 
/// assert_eq!("00000123714384710312239874874388", usize_to_str!(123714384710312239874874388; 32, u128).str());
/// 
/// 
/// ```
/// 
///  With `u128` numbers it is rather slow. To get a faster version specify a length that is multiple of 4 and as a fourth paramter specify 4.
/// 
/// 
/// ```
/// 
/// assert_eq!("00000123714384710312239874874388", usize_to_str!(123714384710312239874874388; 32, u128, 4).str());
/// 
/// ```
/// 
#[macro_export]
macro_rules! usize_to_str {
    ( $n:expr; 1 ) => ({
        let bytes = [$n | 48];

        StackStr::from(bytes, 0..1)
    });
    ( $n:expr; 2 ) => ({
        let tens_str_digit = ($n / 10) | 48;
        let units_str_digit = ($n % 10) | 48;
        let bytes = [tens_str_digit, units_str_digit];

        StackStr::from(bytes, 0..2)
    });
    ( $n:expr; $len:expr ) => ({
        let mut bytes = [0_u8; $len];
        let mut n = $n as u64;
        let mut divisor = 10_u64.pow($len - 1);

        for byte in bytes.iter_mut() {
            let str_digit = ((n / divisor) | 48) as u8;
            n %= divisor;
            divisor /= 10;

            *byte = str_digit
        }

        StackStr::from(bytes, 0..$len)
        
    });
    ( $n:expr; $len:expr, u128 ) => ({
        let mut bytes = [0_u8; $len];
        let mut n = $n as u128;
        let mut divisor = 10_u128.pow($len - 1);

        for byte in bytes.iter_mut() {
            let str_digit = ((n / divisor) | 48) as u8;
            n %= divisor;
            divisor /= 10;

            unsafe {
                *byte = str_digit
            }
        }

        StackStr::from(bytes, 0..$len)
    });
    ( $n:expr; $len:expr, u128, 4 ) => ({
        let mut proto_bytes = [0_u32; $len / 4];
        let mut n = $n as u128;
        let mut divisor = 10_u128.pow($len - 1);

        for byte in proto_bytes.iter_mut() {
            let str_digit_0 = ((n / divisor) | 48) as u8;
            n %= divisor;
            divisor /= 10;

            let str_digit_1 = ((n / divisor) | 48) as u8;
            n %= divisor;
            divisor /= 10;

            let str_digit_2 = ((n / divisor) | 48) as u8;
            n %= divisor;
            divisor /= 10;

            let str_digit_3 = ((n / divisor) | 48) as u8;
            n %= divisor;
            divisor /= 10;

            unsafe {
                *byte = u32::from_le_bytes([
                            str_digit_0,
                            str_digit_1,
                            str_digit_2,
                            str_digit_3
                        ])
            }
        }
        let bytes: [u8; $len] = unsafe {
            core::mem::transmute(proto_bytes)
        };

        StackStr::from(bytes, 0..$len)
    })
}