implement negation

This commit is contained in:
Folkert 2021-03-12 16:17:11 +01:00
parent 182fd5201d
commit e5e38f03bc

View file

@ -80,7 +80,17 @@ pub fn float_expr_from_result(
pub fn finish_parsing_int(raw: &str, is_negative: bool) -> Result<i64, (&str, IntErrorKind)> {
// Ignore underscores.
let radix = 10;
from_str_radix::<i64>(raw.replace("_", "").as_str(), radix).map_err(|e| (raw, e.kind))
let number =
from_str_radix::<i64>(raw.replace("_", "").as_str(), radix).map_err(|e| (raw, e.kind))?;
if is_negative {
match number.checked_neg() {
None => Err((raw, IntErrorKind::Overflow)),
Some(value) => Ok(value),
}
} else {
Ok(number)
}
}
#[inline(always)]
@ -108,7 +118,11 @@ pub fn finish_parsing_base(
#[inline(always)]
pub fn finish_parsing_float(raw: &str, is_negative: bool) -> Result<f64, (&str, FloatErrorKind)> {
// Ignore underscores.
match raw.replace("_", "").parse::<f64>() {
match raw
.replace("_", "")
.parse::<f64>()
.map(|x| if is_negative { -x } else { x })
{
Ok(float) if float.is_finite() => Ok(float),
Ok(float) => {
if float.is_sign_positive() {