zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
449 stars 86 forks source link

Problem with \\r\\n , string seems to be unescaped twice. #81

Closed cuiliang closed 4 years ago

cuiliang commented 4 years ago

I want to generate such result: "Hello \r\n!" (not new line, just origin characters), but can not success. See code example:https://dotnetfiddle.net/2pfmXx

// @nuget: Z.Expressions.Eval

using System;
using Z.Expressions;

public class Program
{
    public static void Main()
    {       

         var str = "Hello" + "\\r\\n!";
            Console.WriteLine(str);  //  => Hello\r\n!

            var str1 = "'Hello' + '\\r\\n!'";
            str1 = str1.Replace("'", "\"");  // replace ' to ", make it just same as the previous line : var str = "Hello" + "\\r\\n!";

            Console.WriteLine(Eval.Execute(str1).ToString());     ==> Hello<new line>!
    }
}
JonathanMagnan commented 4 years ago

Hello @cuiliang ,

The easiest way is to use the variable as a variable:

Console.WriteLine(Eval.Execute("str", new { str }).ToString()); 

Which works fine.

But it looks you are right, we might have a bug with escaping some character. We will look at it.

Best Regards,

Jon

cuiliang commented 4 years ago

Eval.Execute("str", new {str}) the expression evaluated is "str", not this expression ("Hello"+"\r\n") That's different.

JonathanMagnan commented 4 years ago

See the following Fiddle: https://dotnetfiddle.net/SPVHmk

It evaluates the expression

cuiliang commented 4 years ago

Try this=> https://dotnetfiddle.net/IHSosZ

Console.WriteLine(Eval.Execute("\"Hello\" + \"\\\\r\\\\n!\"").ToString());    //should return "Hello\r\n!" but not right.

The expression to evaluate is : "Hello" + "\\r\\n"

JonathanMagnan commented 4 years ago

Hello @cuiliang ,

The v4.0.4 has been released.

I have updated the Fiddle: https://dotnetfiddle.net/SPVHmk

Let me know if everything now works as expected.

Best Regards,

Jon

cuiliang commented 4 years ago

@JonathanMagnan Working now, thanks.