Skip to main content

nanvm_lib/vm/bigint/
debug.rs

1use crate::{
2    common::sized_index::SizedIndex,
3    sign::Sign,
4    vm::{BigInt, IContainer, IVm},
5};
6use core::fmt::{Debug, Formatter, Result, Write};
7
8impl<A: IVm> Debug for BigInt<A> {
9    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
10        if self.is_zero() {
11            return f.write_str("0n");
12        }
13        if self.sign() == Sign::Negative {
14            f.write_char('-')?;
15        }
16        f.write_str("0x")?;
17        let items = self.0.items();
18        // Most significant word first and unpadded, then `_` and sixteen
19        // digits per word. Not `ContainerFmt`'s delimited loop: through a
20        // shared separator helper, the per-word closure would repeat the
21        // helper's first-item test to choose the padding. The loop's twin is
22        // `Display`'s decimal group join, which has no separator.
23        let last = items.length() - 1;
24        write!(f, "{:X}", items[last])?;
25        for i in (0..last).rev() {
26            write!(f, "_{:016X}", items[i])?;
27        }
28        f.write_char('n')
29    }
30}