From 9965fe99fe903f80a1a272c2efdc67e74948b786 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 22 Mar 2020 17:13:38 -0400 Subject: [PATCH] handle negative numbers more accurately in str-insert --- src/builtin/string.rs | 14 ++++++++++---- tests/strings.rs | 16 +++++++++++++--- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/builtin/string.rs b/src/builtin/string.rs index 9ffc169..637bc6a 100644 --- a/src/builtin/string.rs +++ b/src/builtin/string.rs @@ -186,10 +186,16 @@ pub(crate) fn register(f: &mut HashMap) { } 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)) diff --git a/tests/strings.rs b/tests/strings.rs index 8ac1181..c263ede 100644 --- a/tests/strings.rs +++ b/tests/strings.rs @@ -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,