Handle negative numbers in str-slice()

This commit is contained in:
ConnorSkees 2020-02-15 08:51:00 -05:00
parent fab55c5105
commit d082583917
3 changed files with 23 additions and 2 deletions

View File

@ -50,12 +50,14 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
let start = match arg!(args, 1, "start-at").eval() {
Value::Dimension(n, Unit::None) if n.to_integer().is_positive() => n.to_integer().to_usize().unwrap(),
Value::Dimension(n, Unit::None) if n == Number::from(0) => 1_usize,
Value::Dimension(n, Unit::None) if n < -Number::from(str_len) => 1_usize,
Value::Dimension(n, Unit::None) => (BigInt::from(str_len + 1) + n.to_integer()).to_usize().unwrap(),
Value::Dimension(..) => todo!("$start: Expected ___ to have no units."),
_ => todo!("$start-at: ____ is not a number")
};
let mut end = match arg!(args, 2, "end-at"=Value::Null).eval() {
Value::Dimension(n, Unit::None) if n.to_integer().is_positive() => n.to_integer().to_usize().unwrap(),
Value::Dimension(n, Unit::None) if n < -Number::from(str_len) => 0_usize,
Value::Dimension(n, Unit::None) => (BigInt::from(str_len + 1) + n.to_integer()).to_usize().unwrap(),
Value::Dimension(..) => todo!("$end: Expected ___ to have no units."),
Value::Null => str_len,
@ -78,6 +80,5 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
QuoteKind::None => Some(Value::Ident(s, QuoteKind::None)),
}
}
});
}

View File

@ -1,6 +1,6 @@
use std::convert::From;
use std::fmt::{self, Display, Write};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign, Neg};
use num_bigint::BigInt;
use num_rational::BigRational;
@ -196,3 +196,13 @@ impl RemAssign for Number {
self.val %= other.val
}
}
impl Neg for Number {
type Output = Self;
fn neg(self) -> Self {
Number {
val: -self.val,
}
}
}

View File

@ -68,3 +68,13 @@ test!(
"a {\n color: str-slice(cde, 0);\n}\n",
"a {\n color: cde;\n}\n"
);
test!(
str_slice_start_below_negative_str_len,
"a {\n color: str-slice(cde, -100);\n}\n",
"a {\n color: cde;\n}\n"
);
test!(
str_slice_end_below_negative_str_len,
"a {\n color: str-slice(\"cde\", 0, -100);\n}\n",
"a {\n color: \"\";\n}\n"
);