handle negative numbers more accurately in str-insert

This commit is contained in:
ConnorSkees 2020-03-22 17:13:38 -04:00
parent 047fd1d3de
commit 9965fe99fe
2 changed files with 23 additions and 7 deletions

View File

@ -186,10 +186,16 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
} else if index == Number::from(0) {
string.insert_str(0, &substr);
} else {
string.insert_str(
len - index.abs().to_integer().to_usize().unwrap().min(len),
&substr,
);
let idx = index.abs().to_integer().to_usize().unwrap();
if idx > len {
string.insert_str(0, &substr)
} else {
string.insert_str(
len - idx + 1,
&substr,
);
}
}
Ok(Value::Ident(string, quotes))

View File

@ -150,9 +150,19 @@ test!(
"a {\n color: Xabcd;\n}\n"
);
test!(
str_insert_negative_idx,
"a {\n color: str-insert(abcd, \"X\", -2);\n}\n",
"a {\n color: abXcd;\n}\n"
str_insert_negative_1,
"a {\n color: str-insert(abc, \"X\", -1);\n}\n",
"a {\n color: abcX;\n}\n"
);
test!(
str_insert_negative_2,
"a {\n color: str-insert(abc, \"X\", -2);\n}\n",
"a {\n color: abXc;\n}\n"
);
test!(
str_insert_negative_3,
"a {\n color: str-insert(abc, \"X\", -3);\n}\n",
"a {\n color: aXbc;\n}\n"
);
error!(
float_idx,