From be3f0edc974efdbf01ff7683cccaf39f85039e97 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Wed, 27 Apr 2022 06:39:38 +1200 Subject: [PATCH] Fix 'range' range exclusive (#5334) --- crates/nu-command/src/filters/range.rs | 8 ++++++-- src/tests/test_engine.rs | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/nu-command/src/filters/range.rs b/crates/nu-command/src/filters/range.rs index 92944d7b79..be694122d7 100644 --- a/crates/nu-command/src/filters/range.rs +++ b/crates/nu-command/src/filters/range.rs @@ -1,6 +1,6 @@ use nu_engine::CallExt; -use nu_protocol::ast::Call; +use nu_protocol::ast::{Call, RangeInclusion}; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span, @@ -69,7 +69,11 @@ impl Command for Range { let rows: nu_protocol::Range = call.req(engine_state, stack, 0)?; let rows_from = get_range_val(rows.from); - let rows_to = get_range_val(rows.to); + let rows_to = if rows.inclusion == RangeInclusion::RightExclusive { + get_range_val(rows.to) - 1 + } else { + get_range_val(rows.to) + }; // only collect the input if we have any negative indices if rows_from < 0 || rows_to < 0 { diff --git a/src/tests/test_engine.rs b/src/tests/test_engine.rs index 9bacb6ae53..b37714cc68 100644 --- a/src/tests/test_engine.rs +++ b/src/tests/test_engine.rs @@ -347,3 +347,8 @@ fn better_operator_spans() -> TestResult { "true", ) } + +#[test] +fn range_right_exclusive() -> TestResult { + run_test(r#"[1, 4, 5, 8, 9] | range 1..<3 | math sum"#, "9") +}