handle negative numbers more accurately in str-insert
This commit is contained in:
parent
047fd1d3de
commit
9965fe99fe
@ -185,11 +185,17 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
|
|||||||
);
|
);
|
||||||
} else if index == Number::from(0) {
|
} else if index == Number::from(0) {
|
||||||
string.insert_str(0, &substr);
|
string.insert_str(0, &substr);
|
||||||
|
} else {
|
||||||
|
let idx = index.abs().to_integer().to_usize().unwrap();
|
||||||
|
if idx > len {
|
||||||
|
string.insert_str(0, &substr)
|
||||||
} else {
|
} else {
|
||||||
string.insert_str(
|
string.insert_str(
|
||||||
len - index.abs().to_integer().to_usize().unwrap().min(len),
|
len - idx + 1,
|
||||||
&substr,
|
&substr,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Value::Ident(string, quotes))
|
Ok(Value::Ident(string, quotes))
|
||||||
|
@ -150,9 +150,19 @@ test!(
|
|||||||
"a {\n color: Xabcd;\n}\n"
|
"a {\n color: Xabcd;\n}\n"
|
||||||
);
|
);
|
||||||
test!(
|
test!(
|
||||||
str_insert_negative_idx,
|
str_insert_negative_1,
|
||||||
"a {\n color: str-insert(abcd, \"X\", -2);\n}\n",
|
"a {\n color: str-insert(abc, \"X\", -1);\n}\n",
|
||||||
"a {\n color: abXcd;\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!(
|
error!(
|
||||||
float_idx,
|
float_idx,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user