Skip to content

Commit 6371943

Browse files
authored
fix(felt): full std::fmt specs implem (#150)
1 parent 820c306 commit 6371943

File tree

1 file changed

+3
-38
lines changed

1 file changed

+3
-38
lines changed

crates/starknet-types-core/src/felt/alloc_impls.rs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ impl Felt {
2121
/// The resulting string is guaranted to be 66 chars long, which is enough to represent `Felt::MAX`:
2222
/// 2 chars for the `0x` prefix and 64 chars for the padded hexadecimal felt value.
2323
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-
}
24+
alloc::format!("{self:#066x}")
3025
}
3126
}
3227

@@ -35,22 +30,7 @@ impl fmt::LowerHex for Felt {
3530
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3631
let hex = alloc::string::ToString::to_string(&self.0);
3732
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)
33+
f.pad_integral(true, if f.alternate() { "0x" } else { "" }, hex)
5434
}
5535
}
5636

@@ -59,22 +39,7 @@ impl fmt::UpperHex for Felt {
5939
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6040
let hex = alloc::string::ToString::to_string(&self.0);
6141
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)
42+
f.pad_integral(true, if f.alternate() { "0x" } else { "" }, &hex)
7843
}
7944
}
8045

0 commit comments

Comments
 (0)