diff --git a/README.md b/README.md index 14b205cf..b39b3777 100644 --- a/README.md +++ b/README.md @@ -1076,7 +1076,21 @@ hex ('`\x`_xx_') escape sequences. Hex sequences map to ASCII characters, while '`\u`' maps to 16-bit common Unicode code points and '`\U`' maps the full, 32-bit extended Unicode code points. -Internally Rhai strings are stored as UTF-8 just like Rust (they _are_ Rust `String`s!), but there are major differences. +Standard escape sequences: + +| Escape sequence | Meaning | +| --------------- | ------------------------------ | +| `\\` | back-slash `\` | +| `\t` | tab | +| `\r` | carriage-return `CR` | +| `\n` | line-feed `LF` | +| `\"` | double-quote `"` in strings | +| `\'` | single-quote `'` in characters | +| `\x`_xx_ | Unicode in 2-digit hex | +| `\u`_xxxx_ | Unicode in 4-digit hex | +| `\U`_xxxxxxxx_ | Unicode in 8-digit hex | + +Internally Rhai strings are stored as UTF-8 just like Rust (they _are_ Rust `String`'s!), but there are major differences. In Rhai a string is the same as an array of Unicode characters and can be directly indexed (unlike Rust). This is similar to most other languages where strings are internally represented not as UTF-8 but as arrays of multi-byte Unicode characters. diff --git a/src/parser.rs b/src/parser.rs index d46acef5..b34a2f09 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -978,7 +978,10 @@ impl<'a> TokenIterator<'a> { } // \{enclosing_char} - escaped - ch if enclosing_char == ch && !escape.is_empty() => result.push(ch), + ch if enclosing_char == ch && !escape.is_empty() => { + escape.clear(); + result.push(ch) + } // Close wrapper ch if enclosing_char == ch && escape.is_empty() => break, diff --git a/tests/chars.rs b/tests/chars.rs index 8d6be4d8..db13f840 100644 --- a/tests/chars.rs +++ b/tests/chars.rs @@ -5,6 +5,8 @@ fn test_chars() -> Result<(), EvalAltResult> { let engine = Engine::new(); assert_eq!(engine.eval::("'y'")?, 'y'); + assert_eq!(engine.eval::(r"'\''")?, '\''); + assert_eq!(engine.eval::(r#"'"'"#)?, '"'); assert_eq!(engine.eval::("'\\u2764'")?, '❤'); #[cfg(not(feature = "no_index"))] diff --git a/tests/string.rs b/tests/string.rs index 3eeb3a35..5e60d87e 100644 --- a/tests/string.rs +++ b/tests/string.rs @@ -12,6 +12,7 @@ fn test_string() -> Result<(), EvalAltResult> { engine.eval::(r#""Test string: \x58""#)?, "Test string: X" ); + assert_eq!(engine.eval::(r#""\"hello\"""#)?, r#""hello""#); assert_eq!(engine.eval::(r#""foo" + "bar""#)?, "foobar");