Add iterator support for strings.

This commit is contained in:
Stephen Chung
2020-06-16 09:34:30 +08:00
parent 4603f8026f
commit ff37e02443
5 changed files with 57 additions and 14 deletions

View File

@@ -2265,14 +2265,14 @@ fn run_builtin_binary_op(
#[cfg(not(feature = "unchecked"))]
match op {
"+" => return add(x, y).map(Into::<Dynamic>::into).map(Some),
"-" => return sub(x, y).map(Into::<Dynamic>::into).map(Some),
"*" => return mul(x, y).map(Into::<Dynamic>::into).map(Some),
"/" => return div(x, y).map(Into::<Dynamic>::into).map(Some),
"%" => return modulo(x, y).map(Into::<Dynamic>::into).map(Some),
"~" => return pow_i_i(x, y).map(Into::<Dynamic>::into).map(Some),
">>" => return shr(x, y).map(Into::<Dynamic>::into).map(Some),
"<<" => return shl(x, y).map(Into::<Dynamic>::into).map(Some),
"+" => return add(x, y).map(Into::into).map(Some),
"-" => return sub(x, y).map(Into::into).map(Some),
"*" => return mul(x, y).map(Into::into).map(Some),
"/" => return div(x, y).map(Into::into).map(Some),
"%" => return modulo(x, y).map(Into::into).map(Some),
"~" => return pow_i_i(x, y).map(Into::into).map(Some),
">>" => return shr(x, y).map(Into::into).map(Some),
"<<" => return shl(x, y).map(Into::into).map(Some),
_ => (),
}
@@ -2283,9 +2283,9 @@ fn run_builtin_binary_op(
"*" => return Ok(Some((x * y).into())),
"/" => return Ok(Some((x / y).into())),
"%" => return Ok(Some((x % y).into())),
"~" => return pow_i_i_u(x, y).map(Into::<Dynamic>::into).map(Some),
">>" => return shr_u(x, y).map(Into::<Dynamic>::into).map(Some),
"<<" => return shl_u(x, y).map(Into::<Dynamic>::into).map(Some),
"~" => return pow_i_i_u(x, y).map(Into::into).map(Some),
">>" => return shr_u(x, y).map(Into::into).map(Some),
"<<" => return shl_u(x, y).map(Into::into).map(Some),
_ => (),
}
@@ -2359,7 +2359,7 @@ fn run_builtin_binary_op(
"*" => return Ok(Some((x * y).into())),
"/" => return Ok(Some((x / y).into())),
"%" => return Ok(Some((x % y).into())),
"~" => return pow_f_f(x, y).map(Into::<Dynamic>::into).map(Some),
"~" => return pow_f_f(x, y).map(Into::into).map(Some),
"==" => return Ok(Some((x == y).into())),
"!=" => return Ok(Some((x != y).into())),
">" => return Ok(Some((x > y).into())),

View File

@@ -15,6 +15,7 @@ use crate::stdlib::{
fmt::Display,
format,
string::{String, ToString},
vec::Vec,
};
fn prepend<T: Display>(x: T, y: ImmutableString) -> FuncReturn<ImmutableString> {
@@ -290,4 +291,12 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
Ok(())
},
);
// Register string iterator
lib.set_iter(
TypeId::of::<ImmutableString>(),
|arr| Box::new(
arr.cast::<ImmutableString>().chars().collect::<Vec<_>>().into_iter().map(Into::into)
) as Box<dyn Iterator<Item = Dynamic>>,
);
});