Allow strings to be iterable.

This commit is contained in:
Stephen Chung 2022-04-09 13:37:43 +08:00
parent 6422fddd6d
commit 1777ee7f6f
3 changed files with 12 additions and 2 deletions

View File

@ -15,6 +15,11 @@ Script-breaking changes
* `split` now splits a string by whitespaces instead of splitting it into individual characters. This is more in line with common practices. * `split` now splits a string by whitespaces instead of splitting it into individual characters. This is more in line with common practices.
* A new function `to_chars` for strings is added to split the string into individual characters. * A new function `to_chars` for strings is added to split the string into individual characters.
Enhancements
------------
* Strings are not directly iterable (via `for .. in`) yielding individual characters.
Version 1.6.0 Version 1.6.0
============= =============

View File

@ -1,5 +1,6 @@
use crate::plugin::*; use crate::plugin::*;
use crate::{def_package, FnPtr, INT}; use crate::{def_package, FnPtr, INT};
use std::any::TypeId;
use std::fmt::{Binary, LowerHex, Octal}; use std::fmt::{Binary, LowerHex, Octal};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -20,6 +21,12 @@ def_package! {
combine_with_exported_module!(lib, "print_debug", print_debug_functions); combine_with_exported_module!(lib, "print_debug", print_debug_functions);
combine_with_exported_module!(lib, "number_formatting", number_formatting); combine_with_exported_module!(lib, "number_formatting", number_formatting);
// Register characters iterator
#[cfg(not(feature = "no_index"))]
lib.set_iter(TypeId::of::<ImmutableString>(), |value| Box::new(
value.cast::<ImmutableString>().chars().map(Into::into).collect::<crate::Array>().into_iter()
));
} }
} }

View File

@ -316,8 +316,6 @@ impl Expr {
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
Expr::FloatConstant(..) => "a floating-point number", Expr::FloatConstant(..) => "a floating-point number",
Expr::CharConstant(..) => "a character", Expr::CharConstant(..) => "a character",
Expr::StringConstant(..) => "a string",
Expr::InterpolatedString(..) => "a string",
Expr::Map(..) => "an object map", Expr::Map(..) => "an object map",
_ => return Ok(self), _ => return Ok(self),
}; };