1use crate::traits::Digit;
5use crate::BigUint;
6
7impl<T: Digit> std::fmt::LowerExp for BigUint<T> {
10 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11 let val = f64::from(self);
12 std::fmt::LowerExp::fmt(&val, f)
13 }
14}
15impl<T: Digit> std::fmt::UpperExp for BigUint<T> {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 let val = f64::from(self);
18 std::fmt::UpperExp::fmt(&val, f)
19 }
20}
21
22impl<T: Digit> std::fmt::LowerHex for BigUint<T> {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 let mut ret = "".to_string();
25 for c in self.val.iter().rev() {
26 let binary = &format!("{:x}", c);
27 let mut full_binary = "".to_string();
28 for _ in 0..(T::NB_BITS / 4) - binary.len() {
29 full_binary.push('0');
30 }
31 full_binary.push_str(&binary);
32 ret.push_str(&full_binary);
33 }
34
35 write!(f, "{}", ret)
36 }
37}
38impl<T: Digit> std::fmt::UpperHex for BigUint<T> {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 let mut ret = "".to_string();
41 for c in self.val.iter().rev() {
42 let binary = &format!("{:X}", c);
43 let mut full_binary = "".to_string();
44 for _ in 0..(T::NB_BITS / 4) - binary.len() {
45 full_binary.push('0');
46 }
47 full_binary.push_str(&binary);
48 ret.push_str(&full_binary);
49 }
50
51 write!(f, "{}", ret)
52 }
53}
54
55impl<T: Digit> std::fmt::Display for BigUint<T> {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 write!(f, "{}", String::from(self))
58 }
59}
60
61pub(crate) fn bin<T: Digit>(val: &[T], f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 let mut ret = "".to_string();
63 for c in val.iter().rev() {
64 let binary = &format!("{:b}", c);
65 let mut full_binary = "".to_string();
66 for _ in 0..T::NB_BITS - binary.len() {
67 full_binary.push('0');
68 }
69 full_binary.push_str(&binary);
70 ret.push_str(&full_binary);
71 }
72
73 write!(f, "{}", ret)
74}
75
76impl<T: Digit> std::fmt::Binary for BigUint<T> {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 bin(&self.val, f)
79 }
80}