|
| 1 | +use core::fmt; |
| 2 | + |
| 3 | +use super::alloc; |
| 4 | +use super::Felt; |
| 5 | + |
| 6 | +impl Felt { |
| 7 | + /// Helper to represent the felt value as a zero-padded hexadecimal string. |
| 8 | + /// |
| 9 | + /// Equivalent to calling `format!("{self:#x}")`. |
| 10 | + pub fn to_hex_string(&self) -> alloc::string::String { |
| 11 | + alloc::format!("{self:#x}") |
| 12 | + } |
| 13 | + |
| 14 | + /// Helper to represent the felt value as a zero-padded hexadecimal string. |
| 15 | + /// |
| 16 | + /// The resulting string will consist of: |
| 17 | + /// 1. A`0x` prefix, |
| 18 | + /// 2. an amount of padding zeros so that the resulting string length is fixed (This amount may be 0), |
| 19 | + /// 3. the felt value represented in hexadecimal |
| 20 | + /// |
| 21 | + /// The resulting string is guaranted to be 66 chars long, which is enough to represent `Felt::MAX`: |
| 22 | + /// 2 chars for the `0x` prefix and 64 chars for the padded hexadecimal felt value. |
| 23 | + pub fn to_fixed_hex_string(&self) -> alloc::string::String { |
| 24 | + let hex_str = alloc::format!("{self:#x}"); |
| 25 | + if hex_str.len() < 66 { |
| 26 | + alloc::format!("0x{:0>64}", hex_str.strip_prefix("0x").unwrap()) |
| 27 | + } else { |
| 28 | + hex_str |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// Represents [Felt] in lowercase hexadecimal format. |
| 34 | +impl fmt::LowerHex for Felt { |
| 35 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 36 | + let hex = alloc::string::ToString::to_string(&self.0); |
| 37 | + let hex = hex.strip_prefix("0x").unwrap(); |
| 38 | + |
| 39 | + let width = if f.sign_aware_zero_pad() { |
| 40 | + f.width().unwrap().min(64) |
| 41 | + } else { |
| 42 | + 1 |
| 43 | + }; |
| 44 | + if f.alternate() { |
| 45 | + write!(f, "0x")?; |
| 46 | + } |
| 47 | + |
| 48 | + if hex.len() < width { |
| 49 | + for _ in 0..(width - hex.len()) { |
| 50 | + write!(f, "0")?; |
| 51 | + } |
| 52 | + } |
| 53 | + write!(f, "{}", hex) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/// Represents [Felt] in uppercase hexadecimal format. |
| 58 | +impl fmt::UpperHex for Felt { |
| 59 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 60 | + let hex = alloc::string::ToString::to_string(&self.0); |
| 61 | + let hex = hex.strip_prefix("0x").unwrap().to_uppercase(); |
| 62 | + |
| 63 | + let width = if f.sign_aware_zero_pad() { |
| 64 | + f.width().unwrap().min(64) |
| 65 | + } else { |
| 66 | + 1 |
| 67 | + }; |
| 68 | + if f.alternate() { |
| 69 | + write!(f, "0x")?; |
| 70 | + } |
| 71 | + |
| 72 | + if hex.len() < width { |
| 73 | + for _ in 0..(width - hex.len()) { |
| 74 | + write!(f, "0")?; |
| 75 | + } |
| 76 | + } |
| 77 | + write!(f, "{}", hex) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#[cfg(test)] |
| 82 | +mod tests { |
| 83 | + use super::*; |
| 84 | + use alloc::format; |
| 85 | + use proptest::prelude::*; |
| 86 | + |
| 87 | + proptest! { |
| 88 | + #[test] |
| 89 | + fn to_hex_string_is_same_as_format(ref x in any::<Felt>()) { |
| 90 | + let y = alloc::format!("{x:#x}"); |
| 91 | + prop_assert_eq!(y, x.to_hex_string()); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments