diff --git a/src/calculate.rs b/src/calculate.rs index 3ebc697..e5c659e 100644 --- a/src/calculate.rs +++ b/src/calculate.rs @@ -980,7 +980,9 @@ fn calculate_right(number_string: (types::Value, types::Value)) -> types::Value let trimmed_string = match string { types::Value::Text(s) => { let temp: &'static str = Box::leak(s.into_boxed_str()); - &temp[(temp.len() - trim_length)..] + let len = temp.len(); + let start = if trim_length > len { len } else { trim_length }; + &temp[(len - start)..] } _ => "", }; @@ -997,7 +999,9 @@ fn calculate_left(number_string: (types::Value, types::Value)) -> types::Value { let trimmed_string = match string { types::Value::Text(s) => { let temp: &'static str = Box::leak(s.into_boxed_str()); - &temp[..trim_length] + let len = temp.len(); + let end = if trim_length > len { len } else { trim_length }; + &temp[..end] } _ => "", }; diff --git a/tests/test.rs b/tests/test.rs index ac2c946..3120e1d 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -779,6 +779,9 @@ fn it_evaluate_left_and_right_functions() { assert_eq!(evaluate_formula_string("=LEFT(\"apple\")"), "a",); assert_eq!(evaluate_formula_string("=\"P\"&LEFT(\"000\"&1,3)"), "P000",); + + assert_eq!(evaluate_formula_string("=LEFT(\"apple\", 10)"), "apple",); + assert_eq!(evaluate_formula_string("=RIGHT(\"apple\", 10)"), "apple",); } #[test]