2022-01-13 11:13:27 +01:00
|
|
|
use crate::eval::calc_index;
|
2021-12-15 05:06:17 +01:00
|
|
|
use crate::plugin::*;
|
2021-11-13 15:36:23 +01:00
|
|
|
use crate::types::dynamic::Variant;
|
2022-03-09 02:25:55 +01:00
|
|
|
use crate::{def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, INT, INT_BITS};
|
2021-04-17 09:15:54 +02:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
2021-12-31 10:49:19 +01:00
|
|
|
use std::{
|
|
|
|
iter::{ExactSizeIterator, FusedIterator},
|
|
|
|
ops::{Range, RangeInclusive},
|
|
|
|
};
|
2021-03-04 16:47:52 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-04-24 12:14:48 +02:00
|
|
|
use num_traits::{CheckedAdd as Add, CheckedSub as Sub};
|
2021-03-04 16:47:52 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "unchecked")]
|
2021-04-17 09:15:54 +02:00
|
|
|
use std::ops::{Add, Sub};
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2021-06-07 03:47:49 +02:00
|
|
|
// Range iterator with step
|
2020-04-20 18:11:25 +02:00
|
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
2022-04-09 07:11:32 +02:00
|
|
|
pub struct StepRange<T>(T, T, T)
|
2020-04-20 18:11:25 +02:00
|
|
|
where
|
2021-04-24 12:14:48 +02:00
|
|
|
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>;
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2021-02-27 08:27:40 +01:00
|
|
|
impl<T> StepRange<T>
|
|
|
|
where
|
2021-04-24 12:14:48 +02:00
|
|
|
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>,
|
2021-02-27 08:27:40 +01:00
|
|
|
{
|
2021-12-25 16:49:14 +01:00
|
|
|
pub fn new(from: T, to: T, step: T) -> RhaiResultOf<Self> {
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if let Some(r) = from.checked_add(&step) {
|
|
|
|
if r == from {
|
2021-12-27 15:28:11 +01:00
|
|
|
return Err(crate::ERR::ErrorInFunctionCall(
|
2021-03-04 16:47:52 +01:00
|
|
|
"range".to_string(),
|
2021-11-07 11:12:37 +01:00
|
|
|
String::new(),
|
2021-12-27 15:28:11 +01:00
|
|
|
crate::ERR::ErrorArithmetic(
|
2021-03-04 16:47:52 +01:00
|
|
|
"step value cannot be zero".to_string(),
|
2021-12-27 15:28:11 +01:00
|
|
|
Position::NONE,
|
2021-06-29 15:58:05 +02:00
|
|
|
)
|
|
|
|
.into(),
|
2021-12-27 15:28:11 +01:00
|
|
|
Position::NONE,
|
2021-03-16 11:16:40 +01:00
|
|
|
)
|
2021-10-19 17:52:58 +02:00
|
|
|
.into());
|
2021-03-04 16:47:52 +01:00
|
|
|
}
|
2021-02-27 08:27:40 +01:00
|
|
|
}
|
2021-03-04 16:47:52 +01:00
|
|
|
|
|
|
|
Ok(Self(from, to, step))
|
2021-02-27 08:27:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:11:25 +02:00
|
|
|
impl<T> Iterator for StepRange<T>
|
|
|
|
where
|
2021-04-24 12:14:48 +02:00
|
|
|
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>,
|
2020-04-20 18:11:25 +02:00
|
|
|
{
|
|
|
|
type Item = T;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<T> {
|
2021-02-27 08:27:40 +01:00
|
|
|
if self.0 == self.1 {
|
|
|
|
None
|
|
|
|
} else if self.0 < self.1 {
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-07-24 08:11:16 +02:00
|
|
|
let diff1 = self.1.checked_sub(&self.0)?;
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
let diff1 = self.1 - self.0;
|
2021-03-04 14:43:00 +01:00
|
|
|
|
2021-03-04 16:47:52 +01:00
|
|
|
let v = self.0;
|
2021-03-04 14:43:00 +01:00
|
|
|
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-07-24 08:11:16 +02:00
|
|
|
let n = self.0.checked_add(&self.2)?;
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
let n = self.0 + self.2;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-07-24 08:11:16 +02:00
|
|
|
let diff2 = self.1.checked_sub(&n)?;
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
let diff2 = self.1 - n;
|
2021-03-04 14:43:00 +01:00
|
|
|
|
|
|
|
if diff2 >= diff1 {
|
|
|
|
None
|
|
|
|
} else {
|
2021-03-04 16:47:52 +01:00
|
|
|
self.0 = if n >= self.1 { self.1 } else { n };
|
2021-03-04 14:43:00 +01:00
|
|
|
Some(v)
|
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
} else {
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-07-24 08:11:16 +02:00
|
|
|
let diff1 = self.0.checked_sub(&self.1)?;
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
let diff1 = self.0 - self.1;
|
2021-03-04 14:43:00 +01:00
|
|
|
|
2021-03-04 16:47:52 +01:00
|
|
|
let v = self.0;
|
2021-03-04 14:43:00 +01:00
|
|
|
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-07-24 08:11:16 +02:00
|
|
|
let n = self.0.checked_add(&self.2)?;
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
let n = self.0 + self.2;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2021-07-24 08:11:16 +02:00
|
|
|
let diff2 = n.checked_sub(&self.1)?;
|
2021-03-04 16:47:52 +01:00
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
let diff2 = n - self.1;
|
2021-03-04 14:43:00 +01:00
|
|
|
|
|
|
|
if diff2 >= diff1 {
|
|
|
|
None
|
|
|
|
} else {
|
2021-03-04 16:47:52 +01:00
|
|
|
self.0 = if n <= self.1 { self.1 } else { n };
|
2021-03-04 14:43:00 +01:00
|
|
|
Some(v)
|
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 07:05:38 +02:00
|
|
|
impl<T> FusedIterator for StepRange<T> where
|
|
|
|
T: Variant + Copy + PartialOrd + Add<Output = T> + Sub<Output = T>
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-07 03:47:49 +02:00
|
|
|
// Bit-field iterator with step
|
2021-06-02 08:29:18 +02:00
|
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
2022-04-09 07:11:32 +02:00
|
|
|
pub struct BitRange(INT, INT, usize);
|
2021-06-02 08:29:18 +02:00
|
|
|
|
|
|
|
impl BitRange {
|
2021-12-25 16:49:14 +01:00
|
|
|
pub fn new(value: INT, from: INT, len: INT) -> RhaiResultOf<Self> {
|
2022-03-09 02:25:55 +01:00
|
|
|
let from = calc_index(INT_BITS, from, true, || {
|
|
|
|
crate::ERR::ErrorBitFieldBounds(INT_BITS, from, Position::NONE).into()
|
2022-01-13 11:13:27 +01:00
|
|
|
})?;
|
2021-06-02 08:29:18 +02:00
|
|
|
|
|
|
|
let len = if len < 0 {
|
|
|
|
0
|
2022-03-09 02:25:55 +01:00
|
|
|
} else if from + (len as usize) > INT_BITS {
|
|
|
|
INT_BITS - from
|
2021-06-02 08:29:18 +02:00
|
|
|
} else {
|
|
|
|
len as usize
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Self(value, 1 << from, len))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for BitRange {
|
|
|
|
type Item = bool;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2021-12-31 10:49:19 +01:00
|
|
|
if self.2 == 0 {
|
2021-06-02 08:29:18 +02:00
|
|
|
None
|
|
|
|
} else {
|
2021-12-31 10:49:19 +01:00
|
|
|
let r = (self.0 & self.1) != 0;
|
2021-06-02 08:29:18 +02:00
|
|
|
self.1 <<= 1;
|
|
|
|
self.2 -= 1;
|
|
|
|
Some(r)
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 07:05:38 +02:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(self.2, Some(self.2))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FusedIterator for BitRange {}
|
|
|
|
|
|
|
|
impl ExactSizeIterator for BitRange {
|
|
|
|
#[inline(always)]
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.2
|
|
|
|
}
|
2021-06-02 08:29:18 +02:00
|
|
|
}
|
|
|
|
|
2021-06-07 03:47:49 +02:00
|
|
|
// String iterator over characters
|
|
|
|
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
2022-04-09 07:11:32 +02:00
|
|
|
pub struct CharsStream(Vec<char>, usize);
|
2021-06-07 03:47:49 +02:00
|
|
|
|
|
|
|
impl CharsStream {
|
|
|
|
pub fn new(string: &str, from: INT, len: INT) -> Self {
|
|
|
|
if len <= 0 {
|
2021-11-07 11:12:37 +01:00
|
|
|
return Self(Vec::new(), 0);
|
2021-06-07 03:47:49 +02:00
|
|
|
}
|
|
|
|
if from >= 0 {
|
|
|
|
return Self(
|
|
|
|
string
|
|
|
|
.chars()
|
|
|
|
.skip(from as usize)
|
|
|
|
.take(len as usize)
|
|
|
|
.collect(),
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
return if let Some(abs_from) = from.checked_abs() {
|
|
|
|
let num_chars = string.chars().count();
|
|
|
|
let offset = if num_chars < (abs_from as usize) {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
num_chars - (abs_from as usize)
|
|
|
|
};
|
|
|
|
Self(string.chars().skip(offset).take(len as usize).collect(), 0)
|
|
|
|
} else {
|
|
|
|
Self(string.chars().skip(0).take(len as usize).collect(), 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(feature = "unchecked")]
|
|
|
|
return Self(
|
|
|
|
string
|
|
|
|
.chars()
|
|
|
|
.skip(from as usize)
|
|
|
|
.take(len as usize)
|
2021-06-07 05:21:45 +02:00
|
|
|
.collect(),
|
2021-06-07 03:47:49 +02:00
|
|
|
0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for CharsStream {
|
|
|
|
type Item = char;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.1 >= self.0.len() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let ch = self.0[self.1];
|
|
|
|
self.1 += 1;
|
|
|
|
Some(ch)
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 07:05:38 +02:00
|
|
|
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2021-07-12 07:05:38 +02:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
let remaining = self.0.len() - self.1;
|
|
|
|
(remaining, Some(remaining))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FusedIterator for CharsStream {}
|
|
|
|
|
|
|
|
impl ExactSizeIterator for CharsStream {
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2021-07-12 07:05:38 +02:00
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.0.len() - self.1
|
|
|
|
}
|
2021-06-07 03:47:49 +02:00
|
|
|
}
|
|
|
|
|
2022-04-09 07:11:32 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
pub mod float {
|
|
|
|
use super::*;
|
|
|
|
use crate::FLOAT;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub struct StepFloatRange(FLOAT, FLOAT, FLOAT);
|
|
|
|
|
|
|
|
impl StepFloatRange {
|
|
|
|
pub fn new(from: FLOAT, to: FLOAT, step: FLOAT) -> RhaiResultOf<Self> {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if step == 0.0 {
|
|
|
|
return Err(crate::ERR::ErrorInFunctionCall(
|
|
|
|
"range".to_string(),
|
|
|
|
"".to_string(),
|
|
|
|
crate::ERR::ErrorArithmetic(
|
|
|
|
"step value cannot be zero".to_string(),
|
|
|
|
Position::NONE,
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
Position::NONE,
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self(from, to, step))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for StepFloatRange {
|
|
|
|
type Item = FLOAT;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<FLOAT> {
|
|
|
|
if self.0 == self.1 {
|
|
|
|
None
|
|
|
|
} else if self.0 < self.1 {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if self.2 < 0.0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = self.0;
|
|
|
|
let n = self.0 + self.2;
|
|
|
|
|
|
|
|
self.0 = if n >= self.1 { self.1 } else { n };
|
|
|
|
Some(v)
|
|
|
|
} else {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if self.2 > 0.0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = self.0;
|
|
|
|
let n = self.0 + self.2;
|
|
|
|
|
|
|
|
self.0 = if n <= self.1 { self.1 } else { n };
|
|
|
|
Some(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FusedIterator for StepFloatRange {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
pub mod decimal {
|
|
|
|
use super::*;
|
|
|
|
use rust_decimal::Decimal;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
|
|
|
pub struct StepDecimalRange(Decimal, Decimal, Decimal);
|
|
|
|
|
|
|
|
impl StepDecimalRange {
|
|
|
|
pub fn new(from: Decimal, to: Decimal, step: Decimal) -> RhaiResultOf<Self> {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if step.is_zero() {
|
|
|
|
return Err(crate::ERR::ErrorInFunctionCall(
|
|
|
|
"range".to_string(),
|
|
|
|
"".to_string(),
|
|
|
|
crate::ERR::ErrorArithmetic(
|
|
|
|
"step value cannot be zero".to_string(),
|
|
|
|
Position::NONE,
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
Position::NONE,
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self(from, to, step))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for StepDecimalRange {
|
|
|
|
type Item = Decimal;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Decimal> {
|
|
|
|
if self.0 == self.1 {
|
|
|
|
None
|
|
|
|
} else if self.0 < self.1 {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if self.2.is_sign_negative() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = self.0;
|
|
|
|
let n = self.0 + self.2;
|
|
|
|
|
|
|
|
self.0 = if n >= self.1 { self.1 } else { n };
|
|
|
|
Some(v)
|
|
|
|
} else {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if self.2.is_sign_positive() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = self.0;
|
|
|
|
let n = self.0 + self.2;
|
|
|
|
|
|
|
|
self.0 = if n <= self.1 { self.1 } else { n };
|
|
|
|
Some(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FusedIterator for StepDecimalRange {}
|
|
|
|
}
|
|
|
|
|
2020-10-19 17:49:01 +02:00
|
|
|
macro_rules! reg_range {
|
2021-03-04 16:47:52 +01:00
|
|
|
($lib:ident | $x:expr => $( $y:ty ),*) => {
|
2020-10-19 17:49:01 +02:00
|
|
|
$(
|
|
|
|
$lib.set_iterator::<Range<$y>>();
|
2021-04-24 09:53:02 +02:00
|
|
|
let _hash = $lib.set_native_fn($x, |from: $y, to: $y| Ok(from..to));
|
2021-03-24 12:27:38 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
$lib.update_fn_metadata_with_comments(_hash, [
|
2020-11-22 15:15:17 +01:00
|
|
|
concat!("from: ", stringify!($y)),
|
|
|
|
concat!("to: ", stringify!($y)),
|
2022-01-15 16:34:38 +01:00
|
|
|
concat!("Iterator<Item=", stringify!($y), ">"),
|
|
|
|
], [
|
|
|
|
"/// Return an iterator over the range of `from..to`.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
"/// for n in range(8, 18) {",
|
|
|
|
"/// print(n);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
2020-11-22 15:15:17 +01:00
|
|
|
]);
|
2021-12-31 10:49:19 +01:00
|
|
|
|
|
|
|
$lib.set_iterator::<RangeInclusive<$y>>();
|
2020-10-19 17:49:01 +02:00
|
|
|
)*
|
2021-03-04 16:47:52 +01:00
|
|
|
};
|
|
|
|
($lib:ident | step $x:expr => $( $y:ty ),*) => {
|
2020-10-19 17:49:01 +02:00
|
|
|
$(
|
|
|
|
$lib.set_iterator::<StepRange<$y>>();
|
2021-04-24 09:53:02 +02:00
|
|
|
let _hash = $lib.set_native_fn($x, |from: $y, to: $y, step: $y| StepRange::new(from, to, step));
|
2021-03-24 12:27:38 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
$lib.update_fn_metadata_with_comments(_hash, [
|
2020-11-22 15:15:17 +01:00
|
|
|
concat!("from: ", stringify!($y)),
|
|
|
|
concat!("to: ", stringify!($y)),
|
2021-02-19 16:13:53 +01:00
|
|
|
concat!("step: ", stringify!($y)),
|
|
|
|
concat!("Iterator<Item=", stringify!($y), ">")
|
2022-01-15 16:34:38 +01:00
|
|
|
], [
|
|
|
|
"/// Return an iterator over the range of `from..to`, each iterator increasing by `step`.",
|
|
|
|
"///",
|
|
|
|
"/// If `from` > `to` and `step` < 0, the iteration goes backwards.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
"/// for n in range(8, 18, 3) {",
|
|
|
|
"/// print(n);",
|
|
|
|
"/// }",
|
|
|
|
"///",
|
|
|
|
"/// for n in range(18, 8, -3) {",
|
|
|
|
"/// print(n);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
2020-11-22 15:15:17 +01:00
|
|
|
]);
|
2020-10-19 17:49:01 +02:00
|
|
|
)*
|
2021-03-04 16:47:52 +01:00
|
|
|
};
|
2020-10-19 17:49:01 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
def_package! {
|
|
|
|
/// Package of basic range iterators
|
2022-02-10 05:33:48 +01:00
|
|
|
pub BasicIteratorPackage(lib) {
|
2021-12-20 04:42:39 +01:00
|
|
|
lib.standard = true;
|
2021-11-05 16:22:05 +01:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
reg_range!(lib | "range" => INT);
|
2020-04-21 17:01:10 +02:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
{
|
|
|
|
reg_range!(lib | "range" => i8, u8, i16, u16, i32, u32, i64, u64);
|
2020-06-17 10:50:46 +02:00
|
|
|
|
2022-01-12 01:12:28 +01:00
|
|
|
#[cfg(not(target_family = "wasm"))]
|
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
reg_range!(lib | "range" => i128, u128);
|
|
|
|
}
|
2021-04-24 09:53:02 +02:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
reg_range!(lib | step "range" => INT);
|
2021-04-24 09:53:02 +02:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
{
|
|
|
|
reg_range!(lib | step "range" => i8, u8, i16, u16, i32, u32, i64, u64);
|
2021-04-24 09:53:02 +02:00
|
|
|
|
2022-01-12 01:12:28 +01:00
|
|
|
#[cfg(not(target_family = "wasm"))]
|
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
reg_range!(lib | step "range" => i128, u128);
|
2021-04-24 09:53:02 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
{
|
2022-04-09 07:11:32 +02:00
|
|
|
lib.set_iterator::<float::StepFloatRange>();
|
2021-04-24 09:53:02 +02:00
|
|
|
|
2022-04-09 07:11:32 +02:00
|
|
|
let _hash = lib.set_native_fn("range", float::StepFloatRange::new);
|
2021-12-20 04:42:39 +01:00
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
|
|
|
["from: FLOAT", "to: FLOAT", "step: FLOAT", "Iterator<Item=FLOAT>"],
|
|
|
|
[
|
|
|
|
"/// Return an iterator over the range of `from..to`, each iterator increasing by `step`.",
|
|
|
|
"///",
|
|
|
|
"/// If `from` > `to` and `step` < 0, the iteration goes backwards.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
"/// for n in range(8.0, 18.0, 3.0) {",
|
|
|
|
"/// print(n);",
|
|
|
|
"/// }",
|
|
|
|
"///",
|
|
|
|
"/// for n in range(18.0, 8.0, -3.0) {",
|
|
|
|
"/// print(n);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-12-20 04:42:39 +01:00
|
|
|
}
|
2021-04-24 09:53:02 +02:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
{
|
2022-04-09 07:11:32 +02:00
|
|
|
lib.set_iterator::<decimal::StepDecimalRange>();
|
2021-12-20 04:42:39 +01:00
|
|
|
|
2022-04-09 07:11:32 +02:00
|
|
|
let _hash = lib.set_native_fn("range", decimal::StepDecimalRange::new);
|
2021-12-20 04:42:39 +01:00
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
|
|
|
["from: Decimal", "to: Decimal", "step: Decimal", "Iterator<Item=Decimal>"],
|
|
|
|
[
|
|
|
|
"/// Return an iterator over the range of `from..to`, each iterator increasing by `step`.",
|
|
|
|
"///",
|
|
|
|
"/// If `from` > `to` and `step` < 0, the iteration goes backwards.",
|
|
|
|
]
|
|
|
|
);
|
2020-07-31 16:30:23 +02:00
|
|
|
}
|
2021-03-04 16:47:52 +01:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
// Register string iterator
|
|
|
|
lib.set_iterator::<CharsStream>();
|
2021-03-04 16:47:52 +01:00
|
|
|
|
2022-01-17 14:51:04 +01:00
|
|
|
#[cfg(feature = "metadata")]
|
|
|
|
let (range_type, range_inclusive_type) = (
|
|
|
|
format!("range: Range<{}>", std::any::type_name::<INT>()),
|
|
|
|
format!("range: RangeInclusive<{}>", std::any::type_name::<INT>()),
|
|
|
|
);
|
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
let _hash = lib.set_native_fn("chars", |string, range: ExclusiveRange| {
|
|
|
|
let from = INT::max(range.start, 0);
|
|
|
|
let to = INT::max(range.end, from);
|
|
|
|
Ok(CharsStream::new(string, from, to - from))
|
|
|
|
});
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
2022-01-17 14:51:04 +01:00
|
|
|
["string: &str", &range_type, "Iterator<Item=char>"],
|
2022-01-15 16:34:38 +01:00
|
|
|
[
|
|
|
|
"/// Return an iterator over an exclusive range of characters in the string.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
r#"/// for ch in "hello, world!".chars(2..5) {"#,
|
|
|
|
"/// print(ch);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-03-04 16:47:52 +01:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
let _hash = lib.set_native_fn("chars", |string, range: InclusiveRange| {
|
|
|
|
let from = INT::max(*range.start(), 0);
|
|
|
|
let to = INT::max(*range.end(), from - 1);
|
|
|
|
Ok(CharsStream::new(string, from, to-from + 1))
|
|
|
|
});
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
2022-01-17 14:51:04 +01:00
|
|
|
["string: &str", &range_inclusive_type, "Iterator<Item=char>"],
|
2022-01-15 16:34:38 +01:00
|
|
|
[
|
|
|
|
"/// Return an iterator over an inclusive range of characters in the string.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
r#"/// for ch in "hello, world!".chars(2..=6) {"#,
|
|
|
|
"/// print(ch);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-03-04 16:47:52 +01:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
let _hash = lib.set_native_fn("chars", |string, from, len| Ok(CharsStream::new(string, from, len)));
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
|
|
|
["string: &str", "start: INT", "len: INT", "Iterator<Item=char>"],
|
|
|
|
[
|
|
|
|
"/// Return an iterator over a portion of characters in the string.",
|
|
|
|
"///",
|
|
|
|
"/// * If `start` < 0, position counts from the end of the string (`-1` is the last character).",
|
|
|
|
"/// * If `start` < -length of string, position counts from the beginning of the string.",
|
|
|
|
"/// * If `start` ≥ length of string, an empty iterator is returned.",
|
|
|
|
"/// * If `len` ≤ 0, an empty iterator is returned.",
|
|
|
|
"/// * If `start` position + `len` ≥ length of string, all characters of the string after the `start` position are iterated.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
r#"/// for ch in "hello, world!".chars(2, 4) {"#,
|
|
|
|
"/// print(ch);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-03-04 16:47:52 +01:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
let _hash = lib.set_native_fn("chars", |string, from| Ok(CharsStream::new(string, from, INT::MAX)));
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
|
|
|
["string: &str", "from: INT", "Iterator<Item=char>"],
|
|
|
|
[
|
|
|
|
"/// Return an iterator over the characters in the string starting from the `start` position.",
|
|
|
|
"///",
|
|
|
|
"/// * If `start` < 0, position counts from the end of the string (`-1` is the last character).",
|
|
|
|
"/// * If `start` < -length of string, position counts from the beginning of the string.",
|
|
|
|
"/// * If `start` ≥ length of string, an empty iterator is returned.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
r#"/// for ch in "hello, world!".chars(2) {"#,
|
|
|
|
"/// print(ch);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-03-04 16:47:52 +01:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
let _hash = lib.set_native_fn("chars", |string| Ok(CharsStream::new(string, 0, INT::MAX)));
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
|
|
|
["string: &str", "Iterator<Item=char>"],
|
|
|
|
[
|
|
|
|
"/// Return an iterator over the characters in the string.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
r#"/// for ch in "hello, world!".chars() {"#,
|
|
|
|
"/// print(ch);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-12-20 04:42:39 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
{
|
|
|
|
let _hash = lib.set_getter_fn("chars", |string: &mut ImmutableString| Ok(CharsStream::new(string, 0, INT::MAX)));
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
|
|
|
["string: &mut ImmutableString", "Iterator<Item=char>"],
|
|
|
|
[
|
|
|
|
"/// Return an iterator over all the characters in the string.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
r#"/// for ch in "hello, world!".chars {"#,
|
|
|
|
"/// print(ch);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-03-04 16:47:52 +01:00
|
|
|
}
|
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
// Register bit-field iterator
|
|
|
|
lib.set_iterator::<BitRange>();
|
2021-03-16 11:16:40 +01:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
let _hash = lib.set_native_fn("bits", |value, range: ExclusiveRange| {
|
|
|
|
let from = INT::max(range.start, 0);
|
|
|
|
let to = INT::max(range.end, from);
|
|
|
|
BitRange::new(value, from, to - from)
|
|
|
|
});
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
2022-01-17 14:51:04 +01:00
|
|
|
["value: INT", &range_type, "Iterator<Item=bool>"],
|
2022-01-15 16:34:38 +01:00
|
|
|
[
|
|
|
|
"/// Return an iterator over an exclusive range of bits in the number.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
"/// let x = 123456;",
|
|
|
|
"///",
|
|
|
|
"/// for bit in x.bits(10..24) {",
|
|
|
|
"/// print(bit);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-03-04 16:47:52 +01:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
let _hash = lib.set_native_fn("bits", |value, range: InclusiveRange| {
|
|
|
|
let from = INT::max(*range.start(), 0);
|
|
|
|
let to = INT::max(*range.end(), from - 1);
|
|
|
|
BitRange::new(value, from, to - from + 1)
|
|
|
|
});
|
2021-03-24 15:26:07 +01:00
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
2022-01-17 14:51:04 +01:00
|
|
|
["value: INT", &range_inclusive_type, "Iterator<Item=bool>"],
|
2022-01-15 16:34:38 +01:00
|
|
|
[
|
|
|
|
"/// Return an iterator over an inclusive range of bits in the number.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
"/// let x = 123456;",
|
|
|
|
"///",
|
|
|
|
"/// for bit in x.bits(10..=23) {",
|
|
|
|
"/// print(bit);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-06-02 08:29:18 +02:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
let _hash = lib.set_native_fn("bits", BitRange::new);
|
2021-12-15 05:46:25 +01:00
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
|
|
|
["value: INT", "from: INT", "len: INT", "Iterator<Item=bool>"],
|
|
|
|
[
|
|
|
|
"/// Return an iterator over a portion of bits in the number.",
|
|
|
|
"///",
|
|
|
|
"/// * If `start` < 0, position counts from the MSB (Most Significant Bit)>.",
|
|
|
|
"/// * If `len` ≤ 0, an empty iterator is returned.",
|
|
|
|
"/// * If `start` position + `len` ≥ length of string, all bits of the number after the `start` position are iterated.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
"/// let x = 123456;",
|
|
|
|
"///",
|
|
|
|
"/// for bit in x.bits(10, 8) {",
|
|
|
|
"/// print(bit);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-12-15 05:06:17 +01:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
let _hash = lib.set_native_fn("bits", |value, from| BitRange::new(value, from, INT::MAX));
|
2021-12-15 05:46:25 +01:00
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
|
|
|
["value: INT", "from: INT", "Iterator<Item=bool>"],
|
|
|
|
[
|
|
|
|
"/// Return an iterator over the bits in the number starting from the specified `start` position.",
|
|
|
|
"///",
|
|
|
|
"/// If `start` < 0, position counts from the MSB (Most Significant Bit)>.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
"/// let x = 123456;",
|
|
|
|
"///",
|
|
|
|
"/// for bit in x.bits(10) {",
|
|
|
|
"/// print(bit);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-12-15 05:06:17 +01:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
let _hash = lib.set_native_fn("bits", |value| BitRange::new(value, 0, INT::MAX) );
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
|
|
|
["value: INT", "Iterator<Item=bool>"],
|
|
|
|
[
|
|
|
|
"/// Return an iterator over all the bits in the number.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
"/// let x = 123456;",
|
|
|
|
"///",
|
|
|
|
"/// for bit in x.bits() {",
|
|
|
|
"/// print(bit);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-12-20 04:42:39 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
{
|
|
|
|
let _hash = lib.set_getter_fn("bits", |value: &mut INT| BitRange::new(*value, 0, INT::MAX) );
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-01-15 16:34:38 +01:00
|
|
|
lib.update_fn_metadata_with_comments(
|
|
|
|
_hash,
|
2022-01-17 14:51:04 +01:00
|
|
|
["value: &mut INT", "Iterator<Item=bool>"],
|
2022-01-15 16:34:38 +01:00
|
|
|
[
|
|
|
|
"/// Return an iterator over all the bits in the number.",
|
|
|
|
"///",
|
|
|
|
"/// # Example",
|
|
|
|
"///",
|
|
|
|
"/// ```rhai",
|
|
|
|
"/// let x = 123456;",
|
|
|
|
"///",
|
|
|
|
"/// for bit in x.bits {",
|
|
|
|
"/// print(bit);",
|
|
|
|
"/// }",
|
|
|
|
"/// ```"
|
|
|
|
]
|
|
|
|
);
|
2021-12-20 04:42:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
combine_with_exported_module!(lib, "range", range_functions);
|
|
|
|
}
|
|
|
|
}
|
2021-12-15 05:06:17 +01:00
|
|
|
|
|
|
|
#[export_module]
|
|
|
|
mod range_functions {
|
2022-01-15 16:34:38 +01:00
|
|
|
/// Return the start of the exclusive range.
|
2021-12-15 05:06:17 +01:00
|
|
|
#[rhai_fn(get = "start", name = "start", pure)]
|
2021-12-15 07:18:03 +01:00
|
|
|
pub fn start(range: &mut ExclusiveRange) -> INT {
|
2021-12-15 05:06:17 +01:00
|
|
|
range.start
|
|
|
|
}
|
2022-01-15 16:34:38 +01:00
|
|
|
/// Return the end of the exclusive range.
|
2021-12-15 05:06:17 +01:00
|
|
|
#[rhai_fn(get = "end", name = "end", pure)]
|
2021-12-15 07:18:03 +01:00
|
|
|
pub fn end(range: &mut ExclusiveRange) -> INT {
|
2021-12-15 05:06:17 +01:00
|
|
|
range.end
|
|
|
|
}
|
2022-01-15 16:34:38 +01:00
|
|
|
/// Return `true` if the range is inclusive.
|
2021-12-15 05:06:17 +01:00
|
|
|
#[rhai_fn(get = "is_inclusive", name = "is_inclusive", pure)]
|
2021-12-15 15:12:51 +01:00
|
|
|
pub fn is_inclusive(range: &mut ExclusiveRange) -> bool {
|
2021-12-15 05:06:17 +01:00
|
|
|
let _range = range;
|
|
|
|
false
|
|
|
|
}
|
2022-01-15 16:34:38 +01:00
|
|
|
/// Return `true` if the range is exclusive.
|
2021-12-15 15:12:51 +01:00
|
|
|
#[rhai_fn(get = "is_exclusive", name = "is_exclusive", pure)]
|
|
|
|
pub fn is_exclusive(range: &mut ExclusiveRange) -> bool {
|
|
|
|
let _range = range;
|
|
|
|
true
|
|
|
|
}
|
2022-01-15 16:34:38 +01:00
|
|
|
/// Return the start of the inclusive range.
|
2021-12-15 05:06:17 +01:00
|
|
|
#[rhai_fn(get = "start", name = "start", pure)]
|
2021-12-15 07:18:03 +01:00
|
|
|
pub fn start_inclusive(range: &mut InclusiveRange) -> INT {
|
2021-12-15 05:06:17 +01:00
|
|
|
*range.start()
|
|
|
|
}
|
2022-01-15 16:34:38 +01:00
|
|
|
/// Return the end of the inclusive range.
|
2021-12-15 05:06:17 +01:00
|
|
|
#[rhai_fn(get = "end", name = "end", pure)]
|
2021-12-15 07:18:03 +01:00
|
|
|
pub fn end_inclusive(range: &mut InclusiveRange) -> INT {
|
2021-12-15 05:06:17 +01:00
|
|
|
*range.end()
|
|
|
|
}
|
2022-01-15 16:34:38 +01:00
|
|
|
/// Return `true` if the range is inclusive.
|
2021-12-15 05:06:17 +01:00
|
|
|
#[rhai_fn(get = "is_inclusive", name = "is_inclusive", pure)]
|
2021-12-15 07:18:03 +01:00
|
|
|
pub fn is_inclusive_inclusive(range: &mut InclusiveRange) -> bool {
|
2021-12-15 05:06:17 +01:00
|
|
|
let _range = range;
|
|
|
|
true
|
|
|
|
}
|
2022-01-15 16:34:38 +01:00
|
|
|
/// Return `true` if the range is exclusive.
|
2021-12-15 15:12:51 +01:00
|
|
|
#[rhai_fn(get = "is_exclusive", name = "is_exclusive", pure)]
|
|
|
|
pub fn is_exclusive_inclusive(range: &mut InclusiveRange) -> bool {
|
|
|
|
let _range = range;
|
|
|
|
false
|
|
|
|
}
|
2021-12-15 05:06:17 +01:00
|
|
|
}
|