Parse a 64-bit signed number.
(s: &str)
| 161 | |
| 162 | /// Parse a 64-bit signed number. |
| 163 | fn parse_i64(s: &str) -> Result<i64, &'static str> { |
| 164 | let negative = s.starts_with('-'); |
| 165 | let s2 = if negative || s.starts_with('+') { |
| 166 | &s[1..] |
| 167 | } else { |
| 168 | s |
| 169 | }; |
| 170 | |
| 171 | let mut value = parse_u64(s2)?; |
| 172 | |
| 173 | // We support the range-and-a-half from -2^63 .. 2^64-1. |
| 174 | if negative { |
| 175 | value = value.wrapping_neg(); |
| 176 | // Don't allow large negative values to wrap around and become positive. |
| 177 | if value as i64 > 0 { |
| 178 | return Err("Negative number too small"); |
| 179 | } |
| 180 | } |
| 181 | Ok(value as i64) |
| 182 | } |
| 183 | |
| 184 | impl FromStr for Imm64 { |
| 185 | type Err = &'static str; |
no test coverage detected