Skip to content

Commit 280072a

Browse files
authored
Fix numbers printing (#12504)
* fix: print uint as they are * fix: adjust only negative int * test: check that formatting is correct for uint and int of any size
1 parent bedc119 commit 280072a

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

crates/chisel/src/executor.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,8 @@ fn format_token(token: DynSolValue) -> String {
249249
format!(
250250
"0x{}",
251251
format!("{i:x}")
252-
.char_indices()
253-
.skip(64 - bit_len / 4)
254-
.take(bit_len / 4)
255-
.map(|(_, c)| c)
252+
.chars()
253+
.skip(if i.is_negative() { 64 - bit_len / 4 } else { 0 })
256254
.collect::<String>()
257255
)
258256
.cyan(),
@@ -264,16 +262,7 @@ fn format_token(token: DynSolValue) -> String {
264262
format!(
265263
"Type: {}\n├ Hex: {}\n├ Hex (full word): {}\n└ Decimal: {}",
266264
format!("uint{bit_len}").red(),
267-
format!(
268-
"0x{}",
269-
format!("{i:x}")
270-
.char_indices()
271-
.skip(64 - bit_len / 4)
272-
.take(bit_len / 4)
273-
.map(|(_, c)| c)
274-
.collect::<String>()
275-
)
276-
.cyan(),
265+
format!("0x{i:x}").cyan(),
277266
hex::encode_prefixed(B256::from(i)).cyan(),
278267
i.cyan()
279268
)

crates/chisel/tests/it/repl/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,32 @@ repl_test!(test_full_word_hex_formatting, |repl| {
223223
"Hex (full word): 0x0a6b316b47a0cd26c1b582ae3dcffbd175283c221c3cb3d1c614e3e47f62a700",
224224
);
225225
});
226+
227+
// Test that uint is printed properly with any size.
228+
repl_test!(test_uint_formatting, |repl| {
229+
for size in (8..=256).step_by(8) {
230+
repl.sendln(&format!("type(uint{size}).max"));
231+
repl.expect(&format!("Hex: 0x{}", "f".repeat(size / 4)));
232+
233+
repl.sendln(&format!("uint{size}(2)"));
234+
repl.expect("Hex: 0x2");
235+
}
236+
});
237+
238+
// Test that int is printed properly with any size.
239+
repl_test!(test_int_formatting, |repl| {
240+
for size in (8..=256).step_by(8) {
241+
let size_minus_1: usize = size / 4 - 1;
242+
repl.sendln(&format!("type(int{size}).max"));
243+
repl.expect(&format!("Hex: 0x7{}", "f".repeat(size_minus_1)));
244+
245+
repl.sendln(&format!("int{size}(2)"));
246+
repl.expect("Hex: 0x2");
247+
248+
repl.sendln(&format!("type(int{size}).min"));
249+
repl.expect(&format!("Hex: 0x8{}", "0".repeat(size_minus_1)));
250+
251+
repl.sendln(&format!("int{size}(-2)"));
252+
repl.expect(&format!("Hex: 0x{}e", "f".repeat(size_minus_1)));
253+
}
254+
});

0 commit comments

Comments
 (0)