Skip to content

How deal with input strings containing the default escape character \ #464

@mysupply-jlm

Description

@mysupply-jlm

Hi! Thanks for reporting this bug!

Please fill in the relevant information from this template so that we can help you best.

Smart.Format version: 3.5.3

Framework version: net8.0
Microsoft Visual Studio Professional 2022, Version 17.12.4

  • Please provide a working source code example to reproduce the bug:
            // This will throw the exception 'Unrecognized escape sequence "\M" in literal.')
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();

            string value = @"localhost\MSSQL2019";
            string updated = Smart.Format(value, keyValuePairs);
  • What is the current result?
    Two excaped backslash is reduced to one excaped backslash.
    One excaped backslash is seen as a escape charater, and will throw the exception 'Unrecognized escape sequence "\M" in literal.'

  • What is the expected result?
    That Smart.Format did not change the value / can handle a text string with a backslash.

  • Please post full exception details if applicable (message, stacktrace, inner exceptions)

   at SmartFormat.Core.Parsing.EscapedLiteral.UnEscapeCharLiterals(Char escapingSequenceStart, ReadOnlySpan`1 input, Boolean includeFormatterOptionChars, Span`1 resultBuffer)
   at SmartFormat.Core.Parsing.LiteralText.AsSpan()
   at SmartFormat.Core.Parsing.LiteralText.ToString()
   at SmartFormat.Evaluator.WriteFormat(FormattingInfo formattingInfo)
   at SmartFormat.SmartFormatter.ExecuteFormattingAction(SmartFormatter formatter, IFormatProvider provider, Format formatParsed, IList`1 args, IOutput output, Action`1 doWork)
   at SmartFormat.SmartFormatter.FormatInto(IOutput output, IFormatProvider provider, Format formatParsed, IList`1 args)
   at SmartFormat.SmartFormatter.Format(IFormatProvider provider, Format formatParsed, IList`1 args)
   at SmartFormat.SmartFormatter.Format(IFormatProvider provider, String format, IList`1 args)
   at SmartFormat.SmartFormatter.Format(String format, Object[] args)
   at SmartFormat.Smart.Format(String format, Object arg0)
   at TaskSchedulerServer.UnitTest.TaskScheduler.Utilities.ConfigurationResultUtilTest.ThisWillFail() in ....
  • Did you find a workaround? yes/no
    No.

  • Is there a version in which it worked?
    Don't know

  • Can you help us by writing an unit test?

This illustrate the problem, that two excaped backslash is reduced to one excaped backslash

[Fact]
 public void ValueShouldNotHaveBeenUpdated()
 {            
     IDictionary<string, string> keyValuePairs = new Dictionary<string, string>();

     string value = @"localhost\\MSSQL2019";
     string updated = Smart.Format(value, keyValuePairs);

     Assert.Equal(value, updated);

     /*
     Assert.Equal() Failure: Strings differ
                           ↓ (pos 10)
     Expected: "localhost\\\\MSSQL2019"
     Actual:   "localhost\\MSSQL2019"
                           ↑ (pos 10)
      */
 }       

This demonstrate who Smart.Format, first reduce the two excaped backslash to one, and then see excaped backslash, as a escape charater.

[Fact]
public void ThisWillFail()
{
    try
    {
        IDictionary<string, string> keyValuePairs = new Dictionary<string, string>();
        keyValuePairs["key"] = "value";

        string server = "localhost\\\\MSSQL2019";
        // Server value is 'localhost\\MSSQL2019' - Note the double backslash.

        // This will replace the double backslash, with one backslash
        server = Smart.Format(server, keyValuePairs);
        // Server value is now 'localhost\MSSQL2019' only one backslash

        // If I now try to use the server value, with one backslash,
        // it will throw an exception: 'Unrecognized escape sequence "\M" in literal.'
        server = Smart.Format(server, keyValuePairs);
    }
    catch (Exception ex)
    {
        Assert.Fail(ex.Message);
    }
}

Jacob Mogensen
mySupply ApS
Denmark

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions