Skip to content

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

Closed

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

Activity

  1. mysupply-jlm commented on Feb 4, 2025

    @mysupply-jlm
    Author

    Is this issue kind of the same problem as Issue 200?

    Jacob Mogensen
    mySupply ApS
    Denmark

  2. axunonb commented on Feb 5, 2025

    @axunonb
    Member

    The exception is causes by a disallowed escape sequence -\M in particular.

    There are 2 alternatives so resolve this issue, especially when the input string is not under your control:

    1. Disable conversion of character string literals:

    Smart.Default.Settings.Parser.ConvertCharacterStringLiterals = false;
    var result = Smart.Format(@"abc\MSD");
    // result is ""abc\MSD"

    2. Choose another escape character (is must not interfere with other reserved characters like {}[] and others

    Smart.Default.Settings.Parser.CharLiteralEscapeChar = '^';
    var result = Smart.Format(@"abc\MSD");
    // result is ""abc\MSD"

    See also this section in the Wiki..

    Please let us know what would have helped you to resolve the issue by yourself?
    Any suggestions are welcome.

  3. changed the title [-]Problem with escaping charater '\'[/-] [+]How deal with input strings containing the default escape character `\`[/+] on Feb 5, 2025
  4. locked and limited conversation to collaborators on Feb 5, 2025
  5. converted this issue into a discussion #465 on Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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