Add "" and `` in string literals.

This commit is contained in:
Stephen Chung 2021-12-09 13:34:51 +08:00
parent d9d44a9683
commit 675ddb89f9
3 changed files with 18 additions and 0 deletions

View File

@ -24,6 +24,7 @@ New features
Enhancements Enhancements
------------ ------------
* Two double quotes (`""`) in a string literal now maps to `"`; two back-ticks (``` `` ```) in a literal string now maps to `` ` ``.
* Added `Engine::register_type_with_name_raw` to register a custom type based on a fully-qualified type path. * Added `Engine::register_type_with_name_raw` to register a custom type based on a fully-qualified type path.
* Added `into_array` and `into_typed_array` for `Dynamic`. * Added `into_array` and `into_typed_array` for `Dynamic`.
* Added `FnPtr::call` and `FnPtr::call_within_context` to simplify calling a function pointer. * Added `FnPtr::call` and `FnPtr::call_within_context` to simplify calling a function pointer.

View File

@ -1175,6 +1175,15 @@ pub fn parse_string_literal(
result.push(next_char) result.push(next_char)
} }
// Double wrapper
_ if termination_char == next_char
&& escape.is_empty()
&& stream.peek_next().map_or(false, |c| c == termination_char) =>
{
eat_next(stream, pos);
result.push(termination_char)
}
// Close wrapper // Close wrapper
_ if termination_char == next_char && escape.is_empty() => { _ if termination_char == next_char && escape.is_empty() => {
state.is_within_text_terminated_by = None; state.is_within_text_terminated_by = None;

View File

@ -8,6 +8,10 @@ fn test_string() -> Result<(), Box<EvalAltResult>> {
engine.eval::<String>(r#""Test string: \u2764""#)?, engine.eval::<String>(r#""Test string: \u2764""#)?,
"Test string: ❤" "Test string: ❤"
); );
assert_eq!(
engine.eval::<String>(r#""Test string: ""\u2764""""#)?,
r#"Test string: "❤""#
);
assert_eq!( assert_eq!(
engine.eval::<String>("\"Test\rstring: \\u2764\"")?, engine.eval::<String>("\"Test\rstring: \\u2764\"")?,
"Test\rstring: ❤" "Test\rstring: ❤"
@ -24,6 +28,10 @@ fn test_string() -> Result<(), Box<EvalAltResult>> {
engine.eval::<String>(" `Test string: \\u2764\nhello,\\nworld!`")?, engine.eval::<String>(" `Test string: \\u2764\nhello,\\nworld!`")?,
"Test string: \\u2764\nhello,\\nworld!" "Test string: \\u2764\nhello,\\nworld!"
); );
assert_eq!(
engine.eval::<String>(r#" `Test string: \\u2764\n``hello``,\\n"world"!`"#)?,
r#"Test string: \\u2764\n`hello`,\\n"world"!"#
);
assert_eq!( assert_eq!(
engine.eval::<String>(" `\nTest string: \\u2764\nhello,\\nworld!`")?, engine.eval::<String>(" `\nTest string: \\u2764\nhello,\\nworld!`")?,
"Test string: \\u2764\nhello,\\nworld!" "Test string: \\u2764\nhello,\\nworld!"