From 28e6e6f7da14dda556df964e6aaad766e3f3be3b Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Fri, 31 Jul 2020 10:46:09 +0000 Subject: [PATCH] Bug fix in substring when using negative end index only --- CHANGELOG.md | 4 ++++ duckscript_sdk/src/sdk/std/string/substring/mod.rs | 6 +++--- duckscript_sdk/src/sdk/std/string/substring/mod_test.rs | 4 ++-- test/std/string/substring_test.ds | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a4f926..48c42f8 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## CHANGELOG +### v0.6.5 (2020-07-31) + +* Bug fix in substring when using end index only. + ### v0.6.4 (2020-07-31) * New json_encode command #124 diff --git a/duckscript_sdk/src/sdk/std/string/substring/mod.rs b/duckscript_sdk/src/sdk/std/string/substring/mod.rs index 44b74d3..6c81d48 100755 --- a/duckscript_sdk/src/sdk/std/string/substring/mod.rs +++ b/duckscript_sdk/src/sdk/std/string/substring/mod.rs @@ -57,15 +57,15 @@ impl Command for CommandImpl { (value, string_len) } } else { - let start_index = value + string_len; + let end_index = string_len + value; - if start_index < 0 { + if end_index < 0 { return CommandResult::Error( "Index from end cannot be bigger than total text size." .to_string(), ); } else { - (start_index, string_len) + (0, end_index) } } } diff --git a/duckscript_sdk/src/sdk/std/string/substring/mod_test.rs b/duckscript_sdk/src/sdk/std/string/substring/mod_test.rs index f97708a..8189776 100644 --- a/duckscript_sdk/src/sdk/std/string/substring/mod_test.rs +++ b/duckscript_sdk/src/sdk/std/string/substring/mod_test.rs @@ -43,8 +43,8 @@ fn run_text_only_start() { fn run_text_only_end() { test::run_script_and_validate( vec![create("")], - "out = substring text -1", - CommandValidation::Match("out".to_string(), "t".to_string()), + "out = substring abcd -1", + CommandValidation::Match("out".to_string(), "abc".to_string()), ); } diff --git a/test/std/string/substring_test.ds b/test/std/string/substring_test.ds index 9ce9757..05e4f51 100644 --- a/test/std/string/substring_test.ds +++ b/test/std/string/substring_test.ds @@ -26,7 +26,7 @@ end fn test_text_with_end output = substring "test" -2 - assert_eq ${output} "st" + assert_eq ${output} "te" end fn test_text_with_range_and_start_too_big @@ -45,4 +45,4 @@ fn test_text_with_range_and_end_too_big output = substring "test" 0 8 assert_eq ${output} "false" -end \ No newline at end of file +end