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
144
145
146
147
148
149
150
151
//! Module implementing parsing for `I256` type.

use crate::int::I256;

impl_from_str! {
    impl FromStr for I256;
}

pub const fn const_from_str_prefixed(src: &str) -> I256 {
    assert!(!src.is_empty(), "empty string");

    let bytes = src.as_bytes();
    let (negate, start) = match bytes[0] {
        b'+' => (false, 1),
        b'-' => (true, 1),
        _ => (false, 0),
    };
    let uint = crate::parse::const_from_str_prefixed(bytes, start as _);

    let int = {
        let (hi, lo) = if negate {
            let (hi, lo) = uint.into_words();
            let (lo, carry) = (!lo).overflowing_add(1);
            let hi = (!hi).wrapping_add(carry as _);
            (hi, lo)
        } else {
            uint.into_words()
        };
        I256::from_words(hi as _, lo as _)
    };

    if matches!((negate, int.signum128()), (false, -1) | (true, 1)) {
        panic!("overflows integer type");
    }

    int
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse::from_str_radix;
    use core::num::IntErrorKind;

    #[test]
    fn from_str() {
        assert_eq!("42".parse::<I256>().unwrap(), 42);
    }

    #[test]
    fn from_str_prefixed() {
        assert_eq!(from_str_radix::<I256>("0b101", 2, Some("0b")).unwrap(), 5);
        assert_eq!(from_str_radix::<I256>("-0xf", 16, Some("0x")).unwrap(), -15);
    }

    #[test]
    fn from_str_errors() {
        assert_eq!(
            from_str_radix::<I256>("", 2, None).unwrap_err().kind(),
            &IntErrorKind::Empty,
        );
        assert_eq!(
            from_str_radix::<I256>("?", 2, None).unwrap_err().kind(),
            &IntErrorKind::InvalidDigit,
        );
        assert_eq!(
            from_str_radix::<I256>("1", 16, Some("0x"))
                .unwrap_err()
                .kind(),
            &IntErrorKind::InvalidDigit,
        );
        assert_eq!(
            from_str_radix::<I256>(
                "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
                36,
                None
            )
            .unwrap_err()
            .kind(),
            &IntErrorKind::PosOverflow,
        );
        assert_eq!(
            from_str_radix::<I256>(
                "-zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
                36,
                None
            )
            .unwrap_err()
            .kind(),
            &IntErrorKind::NegOverflow,
        );
    }

    #[test]
    fn const_parse() {
        assert_eq!(const_from_str_prefixed("-0b1101"), -0b1101);
        assert_eq!(const_from_str_prefixed("0o777"), 0o777);
        assert_eq!(const_from_str_prefixed("-0x1f"), -0x1f);
        assert_eq!(const_from_str_prefixed("+42"), 42);

        assert_eq!(
            const_from_str_prefixed(
                "0x7fff_ffff_ffff_ffff_ffff_ffff_ffff_fffe\
                   baae_dce6_af48_a03b_bfd2_5e8c_d036_4141"
            ),
            I256::from_words(
                0x7fff_ffff_ffff_ffff_ffff_ffff_ffff_fffe,
                0xbaae_dce6_af48_a03b_bfd2_5e8c_d036_4141_u128 as _,
            ),
        );

        assert_eq!(
            const_from_str_prefixed(
                "-0x8000_0000_0000_0000_0000_0000_0000_0000\
                    0000_0000_0000_0000_0000_0000_0000_0000"
            ),
            I256::MIN,
        );
        assert_eq!(
            const_from_str_prefixed(
                "+0x7fff_ffff_ffff_ffff_ffff_ffff_ffff_ffff\
                    ffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff"
            ),
            I256::MAX,
        );
    }

    #[test]
    #[should_panic]
    fn const_parse_overflow() {
        const_from_str_prefixed(
            "0x8000_0000_0000_0000_0000_0000_0000_0000\
               0000_0000_0000_0000_0000_0000_0000_0000",
        );
    }

    #[test]
    #[should_panic]
    fn const_parse_negative_overflow() {
        const_from_str_prefixed(
            "-0x8000_0000_0000_0000_0000_0000_0000_0000\
                0000_0000_0000_0000_0000_0000_0000_0001",
        );
    }

    #[test]
    #[should_panic]
    fn const_parse_invalid() {
        const_from_str_prefixed("invalid");
    }
}