From 0807c474a1dd86b5ffdb8e099c366303bfdcb80e Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 4 Apr 2021 23:22:45 +0800 Subject: [PATCH] Revise using string interpolation. --- CHANGELOG.md | 2 +- benches/parsing.rs | 4 ++-- examples/threading.rs | 4 ++-- scripts/fibonacci.rhai | 4 ++-- scripts/for1.rhai | 2 +- scripts/for2.rhai | 6 +++--- scripts/mat_mul.rhai | 2 +- scripts/oop.rhai | 4 ++-- scripts/primes.rhai | 4 ++-- scripts/speed_test.rhai | 2 +- scripts/string.rhai | 7 ++++--- scripts/strings_map.rhai | 6 +++--- src/ast.rs | 12 ++++++------ src/fn_args.rs | 2 +- tests/call_fn.rs | 2 +- tests/modules.rs | 2 +- 16 files changed, 33 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99e9987f..e53f2148 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ Rhai Release Notes ================== -This version adds string interpolation. +This version adds string interpolation with `` `... ${`` ... ``} ...` `` syntax. Version 0.19.16 =============== diff --git a/benches/parsing.rs b/benches/parsing.rs index 6bde9dd1..d0f7aced 100644 --- a/benches/parsing.rs +++ b/benches/parsing.rs @@ -93,8 +93,8 @@ fn bench_parse_primes(bench: &mut Bencher) { } } - print("Total " + total_primes_found + " primes."); - print("Run time = " + now.elapsed + " seconds."); + print(`Total ${total_primes_found} primes.`); + print(`Run time = ${now.elapsed} seconds.`); "#; let mut engine = Engine::new(); diff --git a/examples/threading.rs b/examples/threading.rs index 2e0aa9d3..ad80aedb 100644 --- a/examples/threading.rs +++ b/examples/threading.rs @@ -40,9 +40,9 @@ fn main() { loop { let x = get(); - print("Script Read: " + x); + print(`Script Read: ${x}`); x += 1; - print("Script Write: " + x); + print(`Script Write: ${x}`); put(x); } "#, diff --git a/scripts/fibonacci.rhai b/scripts/fibonacci.rhai index 14a35d5e..fb40c64a 100644 --- a/scripts/fibonacci.rhai +++ b/scripts/fibonacci.rhai @@ -21,9 +21,9 @@ for n in range(0, 5) { result = fib(target); } -print("Finished. Run time = " + now.elapsed + " seconds."); +print(`Finished. Run time = ${now.elapsed} seconds.`); -print("Fibonacci number #" + target + " = " + result); +print(`Fibonacci number #${target} = ${result}`); if result != 317_811 { print("The answer is WRONG! Should be 317,811!"); diff --git a/scripts/for1.rhai b/scripts/for1.rhai index 345d17a7..39ac7009 100644 --- a/scripts/for1.rhai +++ b/scripts/for1.rhai @@ -4,7 +4,7 @@ let arr = [1, 2, 3, 4]; for a in arr { for b in [10, 20] { - print(a + "," + b); + print(`${a}, ${b}`); } if a == 3 { break; } diff --git a/scripts/for2.rhai b/scripts/for2.rhai index 8547e7d5..c7b6b816 100644 --- a/scripts/for2.rhai +++ b/scripts/for2.rhai @@ -1,6 +1,6 @@ const MAX = 1_000_000; -print("Iterating an array with " + MAX + " items..."); +print(`Iterating an array with ${MAX} items...`); print("Ready... Go!"); @@ -18,5 +18,5 @@ for i in list { sum += i; } -print("Sum = " + sum); -print("Finished. Run time = " + now.elapsed + " seconds."); +print(`Sum = ${sum}`); +print(`Finished. Run time = ${now.elapsed} seconds.`); diff --git a/scripts/mat_mul.rhai b/scripts/mat_mul.rhai index 54bca636..9fed0d77 100644 --- a/scripts/mat_mul.rhai +++ b/scripts/mat_mul.rhai @@ -68,4 +68,4 @@ for i in range(0, SIZE) { } */ -print("Finished. Run time = " + now.elapsed + " seconds."); +print(`Finished. Run time = ${now.elapsed} seconds.`); diff --git a/scripts/oop.rhai b/scripts/oop.rhai index e90d690b..8caf6d87 100644 --- a/scripts/oop.rhai +++ b/scripts/oop.rhai @@ -7,7 +7,7 @@ let last_value = (); let obj1 = #{ _data: 42, // data field get_data: || this._data, // property getter - action: || print("Data=" + this._data), // method + action: || print(`Data=${this._data}`), // method update: |x| { // property setter this._data = x; last_value = this._data; // capture 'last_value' @@ -38,4 +38,4 @@ if obj2.get_data() > 0 { // property access obj2.update(42); // call method } -print("Should be 84: " + last_value); +print(`Should be 84: ${last_value}`); diff --git a/scripts/primes.rhai b/scripts/primes.rhai index 25fa0690..f5b65763 100644 --- a/scripts/primes.rhai +++ b/scripts/primes.rhai @@ -25,8 +25,8 @@ for p in range(2, MAX_NUMBER_TO_CHECK) { } } -print("Total " + total_primes_found + " primes <= " + MAX_NUMBER_TO_CHECK); -print("Run time = " + now.elapsed + " seconds."); +print(`Total ${total_primes_found} primes <= ${MAX_NUMBER_TO_CHECK}`); +print(`Run time = ${now.elapsed} seconds.`); if total_primes_found != 78_498 { print("The answer is WRONG! Should be 78,498!"); diff --git a/scripts/speed_test.rhai b/scripts/speed_test.rhai index edb9bd0e..d07dadf3 100644 --- a/scripts/speed_test.rhai +++ b/scripts/speed_test.rhai @@ -10,4 +10,4 @@ while x > 0 { x -= 1; } -print("Finished. Run time = " + now.elapsed + " seconds."); +print(`Finished. Run time = ${now.elapsed} seconds.`); diff --git a/scripts/string.rhai b/scripts/string.rhai index 0bb799c5..a507648c 100644 --- a/scripts/string.rhai +++ b/scripts/string.rhai @@ -11,17 +11,18 @@ print("foo" >= "bar"); // string comparison print("the answer is " + 42); // string building using non-string types let s = "\u2764 hello, world! \U0001F603"; // string variable -print("length=" + s.len); // should be 17 +print(`length=${s.len}`); // should be 17 s[s.len-3] = '?'; // change the string -print("Question: " + s); // should print 'Question: hello, world?' +print(`Question: ${s}`); // should print 'Question: hello, world?' // Line continuation: let s = "This is a long \ string constructed using \ line continuation"; -print("One string: " + s); +// String interpolation +print(`One string: ${s}`); // Multi-line literal string: let s = ` diff --git a/scripts/strings_map.rhai b/scripts/strings_map.rhai index 04cd81db..da0dbd87 100644 --- a/scripts/strings_map.rhai +++ b/scripts/strings_map.rhai @@ -75,7 +75,7 @@ let keys = []; for animal in animals { for adjective in adjectives { for adverb in adverbs { - keys.push(adverb + " " + adjective + " " + animal) + keys.push(`${adverb} ${adjective} ${animal}`) } } } @@ -99,5 +99,5 @@ for key in keys { map.remove(key); } -print("Sum = " + sum); -print("Finished. Run time = " + now.elapsed + " seconds."); +print(`Sum = ${sum}`); +print(`Finished. Run time = ${now.elapsed} seconds.`); diff --git a/src/ast.rs b/src/ast.rs index b5dd9518..6335ba31 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -384,7 +384,7 @@ impl AST { /// "#)?; /// /// let ast2 = engine.compile(r#" - /// fn foo(n) { "hello" + n } + /// fn foo(n) { `hello${n}` } /// foo("!") /// "#)?; /// @@ -395,7 +395,7 @@ impl AST { /// /// // 'ast' is essentially: /// // - /// // fn foo(n) { "hello" + n } // <- definition of first 'foo' is overwritten + /// // fn foo(n) { `hello${n}` } // <- definition of first 'foo' is overwritten /// // foo(1) // <- notice this will be "hello1" instead of 43, /// // // but it is no longer the return value /// // foo("!") // returns "hello!" @@ -436,7 +436,7 @@ impl AST { /// "#)?; /// /// let ast2 = engine.compile(r#" - /// fn foo(n) { "hello" + n } + /// fn foo(n) { `hello${n}` } /// foo("!") /// "#)?; /// @@ -447,7 +447,7 @@ impl AST { /// /// // 'ast1' is essentially: /// // - /// // fn foo(n) { "hello" + n } // <- definition of first 'foo' is overwritten + /// // fn foo(n) { `hello${n}` } // <- definition of first 'foo' is overwritten /// // foo(1) // <- notice this will be "hello1" instead of 43, /// // // but it is no longer the return value /// // foo("!") // returns "hello!" @@ -490,7 +490,7 @@ impl AST { /// "#)?; /// /// let ast2 = engine.compile(r#" - /// fn foo(n) { "hello" + n } + /// fn foo(n) { `hello${n}` } /// fn error() { 0 } /// foo("!") /// "#)?; @@ -574,7 +574,7 @@ impl AST { /// "#)?; /// /// let ast2 = engine.compile(r#" - /// fn foo(n) { "hello" + n } + /// fn foo(n) { `hello${n}` } /// fn error() { 0 } /// foo("!") /// "#)?; diff --git a/src/fn_args.rs b/src/fn_args.rs index 13dabf2f..6b3baf78 100644 --- a/src/fn_args.rs +++ b/src/fn_args.rs @@ -41,7 +41,7 @@ pub trait FuncArgs { /// /// let ast = engine.compile(r#" /// fn hello(x, y, z) { - /// if x { "hello " + y } else { y + z } + /// if x { `hello ${y}` } else { y + z } /// } /// "#)?; /// diff --git a/tests/call_fn.rs b/tests/call_fn.rs index fdf82e88..07fdefda 100644 --- a/tests/call_fn.rs +++ b/tests/call_fn.rs @@ -81,7 +81,7 @@ fn test_call_fn_args() -> Result<(), Box> { let ast = engine.compile( r#" fn hello(x, y, z) { - if x { "hello " + y } else { y + z } + if x { `hello ${y}` } else { y + z } } "#, )?; diff --git a/tests/modules.rs b/tests/modules.rs index 64401696..03a2b477 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -315,7 +315,7 @@ fn test_module_from_ast() -> Result<(), Box> { // Final variable values become constant module variable values foo = calc(foo); - hello = "hello, " + foo + " worlds!"; + hello = `hello, ${foo} worlds!`; export x as abc,