Parse `s` with any sign and leading 0s removed
(s: &str, negative: bool)
| 132 | |
| 133 | /// Parse `s` with any sign and leading 0s removed |
| 134 | fn parse_impl(s: &str, negative: bool) -> Result<i256, ParseI256Error> { |
| 135 | if s.len() <= 38 { |
| 136 | let low = i128::from_str(s)?; |
| 137 | return Ok(match negative { |
| 138 | true => i256::from_parts(low.neg() as _, -1), |
| 139 | false => i256::from_parts(low as _, 0), |
| 140 | }); |
| 141 | } |
| 142 | |
| 143 | let split = s.len() - 38; |
| 144 | if !s.as_bytes()[split].is_ascii_digit() { |
| 145 | // Ensures not splitting codepoint and no sign |
| 146 | return Err(ParseI256Error {}); |
| 147 | } |
| 148 | let (hs, ls) = s.split_at(split); |
| 149 | |
| 150 | let mut low = i128::from_str(ls)?; |
| 151 | let high = parse_impl(hs, negative)?; |
| 152 | |
| 153 | if negative { |
| 154 | low = -low; |
| 155 | } |
| 156 | |
| 157 | let low = i256::from_i128(low); |
| 158 | |
| 159 | high.checked_mul(i256::from_i128(10_i128.pow(38))) |
| 160 | .and_then(|high| high.checked_add(low)) |
| 161 | .ok_or(ParseI256Error {}) |
| 162 | } |
| 163 | |
| 164 | impl PartialOrd for i256 { |
| 165 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
no test coverage detected