1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::*;
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Components {
pub sign: Option<bool>,
pub exp: BitPattern,
pub mant: BitPattern,
}
impl Components {
pub fn new(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
Ok(Components {
sign,
exp: BitPattern::from_str(exp)?,
mant: BitPattern::from_str(mant)?,
})
}
pub fn new_bin(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
Ok(Components {
sign,
exp: BitPattern::from_bin_str(exp)?,
mant: BitPattern::from_bin_str(mant)?,
})
}
pub fn new_oct(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
Ok(Components {
sign,
exp: BitPattern::from_oct_str(exp)?,
mant: BitPattern::from_oct_str(mant)?,
})
}
pub fn new_hex(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
Ok(Components {
sign,
exp: BitPattern::from_hex_str(exp)?,
mant: BitPattern::from_hex_str(mant)?,
})
}
pub fn format_with_excess(&self, excess: u32) -> Format {
Format::new_with_sign(
self.sign.is_some(),
self.exp.len() as u8,
self.mant.len(),
excess,
)
}
pub fn format(&self) -> Format {
self.format_with_excess(0)
}
pub fn len(&self) -> usize {
self.sign.is_some() as usize + self.exp.len() + self.mant.len()
}
}
impl std::fmt::Debug for Components {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Components {{ sign: {}, exp: {}, mant: {} }}",
self.sign.map(|b| if b == true { "-" } else { "+" }).unwrap_or("None"),
self.exp.iter().map(|b| if b == true { '1' } else { '0' }).collect::<String>(),
self.mant.iter().map(|b| if b == true { '1' } else { '0' }).collect::<String>(),
)
}
}