From a12401a1fe464bf8bba6f21af983d0797ba5a721 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 17 Jul 2022 12:09:19 +0800 Subject: [PATCH] New range variant. --- CHANGELOG.md | 1 + src/packages/iter_basic.rs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80d9917d..73f3a40c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Enhancements * `switch` cases can now include multiple values separated by `|`. * `EvalContext::eval_expression_tree_raw` and `Expression::eval_with_context_raw` are added to allow for not rewinding the `Scope` at the end of a statements block. +* A new `range` function variant that takes an exclusive range with a step. Version 1.8.0 diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 5627c834..367bf5ff 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -287,7 +287,7 @@ macro_rules! reg_range { "/// Return an iterator over the exclusive range of `from..to`, each iteration increasing by `step`.", "/// The value `to` is never included.", "///", - "/// If `from` > `to` and `step` < 0, the iteration goes backwards.", + "/// If `from` > `to` and `step` < 0, iteration goes backwards.", "///", "/// If `from` > `to` and `step` > 0 or `from` < `to` and `step` < 0, an empty iterator is returned.", "///", @@ -305,6 +305,35 @@ macro_rules! reg_range { "/// }", "/// ```" ]); + + let _hash = $lib.set_native_fn($x, |range: std::ops::Range<$y>, step: $y| StepRange::new(range.start, range.end, step, $add)); + + #[cfg(feature = "metadata")] + $lib.update_fn_metadata_with_comments(_hash, [ + concat!("range: Range<", stringify!($y), ">"), + concat!("step: ", stringify!($y)), + concat!("Iterator") + ], [ + "/// Return an iterator over an exclusive range, each iteration increasing by `step`.", + "///", + "/// If `range` is reversed and `step` < 0, iteration goes backwards.", + "///", + "/// Otherwise, if `range` is empty, an empty iterator is returned.", + "///", + "/// # Example", + "///", + "/// ```rhai", + "/// // prints all values from 8 to 17 in steps of 3", + "/// for n in range(8..18, 3) {", + "/// print(n);", + "/// }", + "///", + "/// // prints all values down from 18 to 9 in steps of -3", + "/// for n in range(18..8, -3) {", + "/// print(n);", + "/// }", + "/// ```" + ]); )* }; }