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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use crate::*;
use bitvec::prelude::*;

pub trait IeeeBinary {
    fn ieee_binary32() -> Self;
    fn ieee_binary64() -> Self;
}

/// Special interpretation of bit patterns for special values.
/// This function is called during output.
pub type Interpret = fn(&Components) -> Option<String>;

fn ieee_interpret(comps: &Components) -> Option<String> {
    match comps {
        c if c.sign == Some(false) && c.exp.is_all_zero() && c.mant.is_all_zero() => {
            Some("0".to_owned())
        },
        c if c.sign == Some(true) && c.exp.is_all_zero() && c.mant.is_all_zero() => {
            Some("-0".to_owned())
        },
        c if c.sign == Some(false) && c.exp.is_all_one() && c.mant.is_all_zero() => {
            Some("inf".to_owned())
        },
        c if c.sign == Some(true) && c.exp.is_all_one() && c.mant.is_all_zero() => {
            Some("-inf".to_owned())
        },
        c if c.exp.is_all_one() && !c.mant.is_all_zero() && c.mant.iter().next().map(|b| *b) == Some(true) => {
            Some("NaN".to_owned())
        },
        c if c.exp.is_all_one() && !c.mant.is_all_zero() && c.mant.iter().next().map(|b| *b) == Some(false) => {
            Some("sNaN".to_owned())
        },
        _ => None,
    }
}

impl IeeeBinary for Interpret {
    fn ieee_binary32() -> Self {
        ieee_interpret
    }

    fn ieee_binary64() -> Self {
        ieee_interpret
    }
}

pub type BitPattern = BitVec<usize, Msb0>;

pub trait BitPatternExt where Self: Sized {
    fn from_value<T>(val: T) -> Self
    where
        T: BitStore;

    fn from_str(s: &str) -> Result<Self, error::Error>;
    fn from_bin_str(s: &str) -> Result<Self, error::Error>;
    fn from_oct_str(s: &str) -> Result<Self, error::Error>;
    fn from_hex_str(s: &str) -> Result<Self, error::Error>;
    
    fn is_all_one(&self) -> bool;
    fn is_all_zero(&self) -> bool;

    fn to_bin_string(&self) -> String;
    fn into_bin_string(self) -> String;
}

impl BitPatternExt for BitPattern {
    fn from_value<T>(val: T) -> Self
    where
        T: BitStore
    {
        val.view_bits::<Msb0>().iter().collect()
    }

    fn from_str(s: &str) -> Result<Self, error::Error> {
        if s.len() < 3 {
            return Err(error::Error::InvalidRadixPrefix);
        }

        match s[0..2].as_ref() {
            "0b" => Ok(Self::from_bin_str(&s[2..])?),
            "0o" => Ok(Self::from_oct_str(&s[2..])?),
            "0x" => Ok(Self::from_hex_str(&s[2..])?),
            _ => Err(error::Error::InvalidRadixPrefix),
        }
    }
    
    fn from_bin_str(s: &str) -> Result<Self, error::Error> {
        Ok(s
            .chars()
            .filter(|c| c.is_digit(2))
            .map(|c| c == '1')
            .collect()
        )
    }

    fn from_oct_str(s: &str) -> Result<Self, error::Error> {
        Ok(s
            .chars()
            .filter(|c| c.is_digit(8))
            .flat_map(|c| format!("{:3b}", c.to_digit(8).unwrap())
                .chars()
                .collect::<Vec<_>>()
            )
            .map(|c| c == '1')
            .collect()
        )
    }
    
    fn from_hex_str(s: &str) -> Result<Self, error::Error> {
        Ok(s
            .chars()
            .filter(|c| c.is_digit(16))
            .flat_map(|c| format!("{:4b}", c.to_digit(16).unwrap())
                .chars()
                .collect::<Vec<_>>()
            )
            .map(|c| c == '1')
            .collect()
        )
    }

    fn is_all_one(&self) -> bool {
        self.iter().all(|b| *b)
    }

    fn is_all_zero(&self) -> bool {
        self.iter().all(|b| !*b)
    }
    
    fn to_bin_string(&self) -> String {
        self
            .iter()
            .map(|b| if b == true { '1' } else { '0' })
            .collect()
    }
    
    fn into_bin_string(self) -> String {
        self
            .into_iter()
            .map(|b| if b == true { '1' } else { '0' })
            .collect()
    }
}