#[macro_export]
macro_rules! usize_to_str {
( $n:expr; 1 ) => ({
let bytes = [$n | 48];
unsafe {
StackStr {
bytes,
str_: core::str::from_utf8_unchecked(&bytes)
}
}
});
( $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];
unsafe {
StackStr {
bytes,
str_: core::str::from_utf8_unchecked(&bytes)
}
}
});
( $n:expr; $len:expr ) => ({
let mut bytes = [0u8; $len];
let mut n = $n as u64;
let mut len = $len;
for byte in bytes.iter_mut() {
let divisor = 10_u64.pow(len - 1);
let str_digit = (n / divisor) | 48;
*byte = str_digit as u8;
n %= divisor;
len -= 1;
}
unsafe {
StackStr {
bytes,
str_: core::str::from_utf8_unchecked(&bytes)
}
}
});
( $n:expr; $len:expr, bit_128 ) => ({
let mut bytes = [0u8; $len];
let mut n = $n;
let mut len = $len;
for byte in bytes.iter_mut() {
let divisor = 10_u128.pow(len - 1);
let str_digit = (n / divisor) | 48;
*byte = str_digit as u8;
n %= divisor;
len -= 1;
}
unsafe {
StackStr {
bytes,
str_: core::str::from_utf8_unchecked(&bytes)
}
}
})
}