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
#[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)
})
}