aboutsummaryrefslogtreecommitdiff
path: root/src/display.rs
blob: 7dcdfb5a93c065af3de812644be1f65bf0cbf4ea (plain)
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
pub enum NumberStyleHint {
    Signed,
    HexSigned,
    SignedWithSign,
    HexSignedWithSign,
    SignedWithSignSplit,
    HexSignedWithSignSplit,
    Unsigned,
    HexUnsigned,
    UnsignedWithSign,
    HexUnsignedWithSign
}

pub fn format_number_i32(i: i32, hint: NumberStyleHint) -> String {
    match hint {
        NumberStyleHint::Signed => {
            format!("{}", i)
        },
        NumberStyleHint::HexSigned => {
            if i == std::i32::MIN {
                format!("-0x7fffffff")
            } else if i < 0 {
                format!("-{:#x}", -i)
            } else {
                format!("{:#x}", i)
            }
        },
        NumberStyleHint::Unsigned => {
            format!("{}", i as u32)
        },
        NumberStyleHint::HexUnsigned => {
            format!("{:#x}", i as u32)
        },
        NumberStyleHint::SignedWithSignSplit => {
            if i == std::i32::MIN {
                format!("- 2147483647")
            } else if i < 0 {
                format!("- {}", -i)
            } else {
                format!("+ {}", i)
            }
        }
        NumberStyleHint::HexSignedWithSignSplit => {
            if i == std::i32::MIN {
                format!("- 0x7fffffff")
            } else if i < 0 {
                format!("- {:#x}", -i)
            } else {
                format!("+ {:#x}", i)
            }
        },
        NumberStyleHint::HexSignedWithSign => {
            if i == std::i32::MIN {
                format!("-0x7fffffff")
            } else if i < 0 {
                format!("-{:#x}", -i)
            } else {
                format!("+{:#x}", i)
            }
        },
        NumberStyleHint::SignedWithSign => {
            format!("{:+}", i)
        }
        NumberStyleHint::HexUnsignedWithSign => {
            format!("{:+#x}", i as u32)
        },
        NumberStyleHint::UnsignedWithSign => {
            format!("{:+}", i as u32)
        }
    }
}

pub fn signed_i8_hex(imm: i8) -> String {
    let (sign, imm) = if imm == std::i8::MIN {
        (false, imm)
    } else if imm < 0 {
        (true, -imm)
    } else {
        (false, imm)
    };
    format!("{}{:#x}", sign, imm)
}

pub fn signed_i16_hex(imm: i16) -> String {
    let (sign, imm) = if imm == std::i16::MIN {
        (false, imm)
    } else if imm < 0 {
        (true, -imm)
    } else {
        (false, imm)
    };
    format!("{}{:#x}", sign, imm)
}

pub fn signed_i32_hex(imm: i32) -> String {
    let (sign, imm) = if imm == std::i32::MIN {
        (false, imm)
    } else if imm < 0 {
        (true, -imm)
    } else {
        (false, imm)
    };
    format!("{}{:#x}", sign, imm)
}

pub fn signed_i64_hex(imm: i64) -> String {
    let (sign, imm) = if imm == std::i64::MIN {
        (false, imm)
    } else if imm < 0 {
        (true, -imm)
    } else {
        (false, imm)
    };
    format!("{}{:#x}", sign, imm)
}