| 1 | use crate::limb::LeakyLimb; |
| 2 | use core::mem::size_of; |
| 3 | |
| 4 | const fn parse_digit(d: u8) -> u8 { |
| 5 | match d.to_ascii_lowercase() { |
| 6 | b'0' ..=b'9' => d - b'0' , |
| 7 | b'a' ..=b'f' => d - b'a' + 10, |
| 8 | _ => panic!(), |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | // TODO: this would be nicer as a trait, but currently traits don't support const functions |
| 13 | pub const fn limbs_from_hex<const LIMBS: usize>(hex: &str) -> [LeakyLimb; LIMBS] { |
| 14 | let hex: &[u8] = hex.as_bytes(); |
| 15 | let mut limbs: [u64; LIMBS] = [0; LIMBS]; |
| 16 | let limb_nibbles: usize = size_of::<LeakyLimb>() * 2; |
| 17 | let mut i: usize = 0; |
| 18 | |
| 19 | while i < hex.len() { |
| 20 | let char: u8 = hex[hex.len() - 1 - i]; |
| 21 | let val: u8 = parse_digit(char); |
| 22 | limbs[i / limb_nibbles] |= (val as LeakyLimb) << ((i % limb_nibbles) * 4); |
| 23 | i += 1; |
| 24 | } |
| 25 | |
| 26 | limbs |
| 27 | } |
| 28 | |