sample-code/hex.cl: Fix casting TODO, add to_string_radix function

This commit is contained in:
John 2024-07-27 17:46:27 -05:00
parent 883c2677d9
commit 53cf71608a

View File

@ -1,26 +1,39 @@
//! Formats numbers in hexadecimal, octal, or binary
mod math;
// TODO: casting and/or conversion
const HEX_LUT: [char; 16] = [
'0', '1', '2', '3', //
'4', '5', '6', '7', //
'8', '9', 'a', 'b', //
'c', 'd', 'e', 'f', //
];
fn as_digit(n: u32) -> char {
(if n > 9 {
n - 10 + ('a' as u32)
} else {
n + ('0' as u32)
}) as char
}
pub fn radix(n: i64, radix: i64) {
fn r_str_radix(n: i64, radix: i64) {
if n != 0 {
r_str_radix(n / radix, radix) + as_digit(n % radix)
} else ""
}
if n == 0 {
"0"
} else if n < 0 {
// TODO: breaks at i64::MIN
"-" + r_str_radix(-n, radix)
} else r_str_radix(n, radix)
}
pub fn hex(n: u64) {
let out = "0x";
for xd in min(count_leading_zeroes(n) / 4, 15)..16 {
out += HEX_LUT[(n >> (15 - xd) * 4) & 0xf]
out += as_digit((n >> (15 - xd) * 4) & 0xf)
}
out
}
pub fn oct(n: u64) {
let out = "0o";
for xd in min((count_leading_zeroes(n) + 2) / 3, 21)..22 {
out += HEX_LUT[(n >> max(63 - (3 * xd), 0)) & 7]
out += as_digit((n >> max(63 - (3 * xd), 0)) & 7)
}
out
}
@ -28,7 +41,7 @@ pub fn oct(n: u64) {
pub fn bin(n: u64) {
let out = "0b";
for xd in min(count_leading_zeroes(n), 63)..64 {
out += HEX_LUT[(n >> 63 - xd) & 1]
out += as_digit((n >> 63 - xd) & 1)
}
out
}