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::*;

/// Components of a floating point number.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Components {
    /// The sign of the number, `Some(true)` if negative, `None` if the format is unsigned.
    pub sign: Option<bool>,

    /// The exponent bit pattern of the number, assumed to be `String` with '1's and '0's.
    pub exp: BitPattern,

    /// The mantissa bit pattern of the number, assumed to be `String` with '1's and '0's.
    pub mant: BitPattern,
}

impl Components {
    /// Create from the given values for `sign`, `exp`, and `mant`.
    /// The radix of `exp` and `mant` is deduced from the first 2 chars.
    /// '0b' => binary, '0x' => hexadecimal, '0o' => octal.
    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)?,
        })
    }

    /// Create from the given values for `sign`, `exp`, and `mant`.
    /// The `exp` and `mant` should be `String` with '1's and '0's.
    /// Any characters other than '0' and '1' are ignored.
    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)?,
        })
    }

    /// Create from the given values for `sign`, `exp`, and `mant`.
    /// The `exp` and `mant` should be `String` with octal digits.
    /// Any characters other than octal digits are ignored.
    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)?,
        })
    }

    /// Create from the given values for `sign`, `exp`, and `mant`.
    /// The `exp` and `mant` should be `String` with hexadecimal digits.
    /// Any characters other than hexadecimal digits are ignored.
    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)?,
        })
    }

    /// Get the format of the components with the given `excess`.
    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,
        )
    }

    /// Get the format of the components.
    /// The excess value is default to 0.
    pub fn format(&self) -> Format {
        self.format_with_excess(0)
    }

    /// Get the number of bits for the format.
    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>(),
        )
    }
}