diff --git a/CHANGELOG.md b/CHANGELOG.md index 3de6bcfc..99e9987f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ Rhai Release Notes ================== +This version adds string interpolation. + Version 0.19.16 =============== diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs index 3f556f05..a7d7cf7b 100644 --- a/src/bin/rhai-repl.rs +++ b/src/bin/rhai-repl.rs @@ -140,6 +140,7 @@ fn main() { // Set a file module resolver without caching #[cfg(not(feature = "no_module"))] + #[cfg(not(feature = "no_std"))] { let mut resolver = rhai::module_resolvers::FileModuleResolver::new(); resolver.enable_cache(false); diff --git a/src/optimize.rs b/src/optimize.rs index 9a86e1de..ea30d5cf 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -616,7 +616,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { if let Stmt::Expr(e) = mem::take(&mut x.statements[0]) { *expr = e; } else { - unreachable!(); + unreachable!(); } } // { stmt; ... } - do not count promotion as dirty because it gets turned back into an array diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 4fd3062b..e2e533e3 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -36,14 +36,6 @@ mod string_functions { format!("{}{}", string, s).into() } } - #[rhai_fn(name = "+=")] - pub fn append(ctx: NativeCallContext, string: &mut ImmutableString, mut item: Dynamic) { - let s = print_with_func(FUNC_TO_STRING, &ctx, &mut item); - - if !s.is_empty() { - string.make_mut().push_str(&s); - } - } #[rhai_fn(name = "+", pure)] pub fn add_prepend( ctx: NativeCallContext, @@ -59,16 +51,13 @@ mod string_functions { s.into() } } - #[rhai_fn(name = "+=")] - pub fn prepend(ctx: NativeCallContext, item: &mut Dynamic, string: &str) { - let mut s = print_with_func(FUNC_TO_STRING, &ctx, item); - if !string.is_empty() { - s.make_mut().push_str(string); - } + #[rhai_fn(name = "+", name = "append")] + pub fn add_append_str(string1: ImmutableString, string2: ImmutableString) -> ImmutableString { + string1 + string2 } - #[rhai_fn(name = "+")] + #[rhai_fn(name = "+", name = "append")] pub fn add_append_unit(string: ImmutableString, _item: ()) -> ImmutableString { string } @@ -76,8 +65,6 @@ mod string_functions { pub fn add_prepend_unit(_item: (), string: ImmutableString) -> ImmutableString { string } - #[rhai_fn(name = "+=")] - pub fn add_append_assign_unit(_string: &mut ImmutableString, _item: ()) {} #[rhai_fn(name = "len", get = "len")] pub fn len(string: &str) -> INT { diff --git a/src/parser.rs b/src/parser.rs index 7f3a4315..96482ca6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1056,7 +1056,6 @@ fn parse_primary( } } - println!("Interpolated string: {:?}", segments); Expr::InterpolatedString(Box::new(segments)) } diff --git a/src/token.rs b/src/token.rs index 187d3726..3a64bd84 100644 --- a/src/token.rs +++ b/src/token.rs @@ -1,7 +1,5 @@ //! Main module defining the lexer and parser. -use std::iter::FusedIterator; - use crate::engine::{ Precedence, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_FN_PTR_CALL, KEYWORD_FN_PTR_CURRY, KEYWORD_IS_DEF_VAR, KEYWORD_PRINT, KEYWORD_THIS, KEYWORD_TYPE_OF, @@ -10,7 +8,7 @@ use crate::stdlib::{ borrow::Cow, cell::Cell, char, fmt, format, - iter::Peekable, + iter::{FusedIterator, Peekable}, num::NonZeroUsize, ops::{Add, AddAssign}, str::{Chars, FromStr}, diff --git a/tests/string.rs b/tests/string.rs index 4428d490..9c72b27d 100644 --- a/tests/string.rs +++ b/tests/string.rs @@ -367,5 +367,21 @@ fn test_string_interpolated() -> Result<(), Box> { "hello 42 worlds!" ); + assert_eq!( + engine.eval::( + r#" + let x = 42; + let y = 123; + + ` +Undeniable logic: +1) Hello, ${let w = `${x} world`; if x > 1 { w += "s" } w}! +2) If ${y} > ${x} then it is ${y > x}! +` + "# + )?, + "Undeniable logic:\n1) Hello, 42 worlds!\n2) If 123 > 42 then it is true!\n", + ); + Ok(()) }