Skip to content

Cannot output the string "\]", throws error Unrecognized escape sequence in literal: \]. No way of escaping #200

Closed

Description

@raxuser100

Cannot escape the string "\]". Need to output this "as is" because using SmartFormat to auto generate regex's and other code nuggets.

Smart.Format version: 2.7.0
Framework version: net5.0

  • Please provide a working source code example to reproduce the bug
const string c_input = @"{a}, {b}, {{x}} \]";
string output = Smart.Format(c_input, new {a="1", b="2", x="3"});

// throws error
// ArgumentException: Unrecognized escape sequence in literal: "\]"
// should return "1, 2, {x} \]"
console.write(output);
  • What is the current result?
    Error thrown:
    Unrecognized escape sequence in literal: \]

  • What is the expected result?
    should be string 1, 2, {x} \]

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

StackTrace	   at SmartFormat.Core.Parsing.LiteralText.ConvertCharacterLiteralsToUnicode()
   at SmartFormat.Core.Parsing.LiteralText.ToString()
   at SmartFormat.SmartFormatter.Format(FormattingInfo formattingInfo)
   at SmartFormat.SmartFormatter.Format(FormatDetails formatDetails, Format format, Object current)
   at SmartFormat.SmartFormatter.Format(IFormatProvider provider, String format, Object[] args)
   at SmartFormat.SmartFormatter.Format(String format, Object[] args)
   at SmartFormat.Smart.Format(String format, Object arg0)
   at UserQuery.DoStuff.test_escape_reserved_chars() in C:\Users\raxp\AppData\Local\Temp\LINQPad6\_wwimbzqr\inaqdk\LINQPadQuery:line 16
   at UserQuery.Main() in C:\Users\raxp\AppData\Local\Temp\LINQPad6\_wwimbzqr\inaqdk\LINQPadQuery:line 4
   at LINQPad.ExecutionModel.ClrQueryRunner.Run()
   at LINQPad.ExecutionModel.Server.RunQuery(QueryRunner runner)

  • Did you find a workaround? yes/no
    no
  • Is there a version in which it worked?
    only tried latest
  • Can you help us by writing an unit test?
    Sure I will submit one

Activity

  1. changed the title [-]Cannot output the string "\]", throws error Unrecognized escape sequence in literal: \]. No way of escaping[/-] [+]Cannot output the string "\]", throws error `Unrecognized escape sequence in literal: \].` No way of escaping[/+] on Sep 8, 2021
  2. changed the title [-]Cannot output the string "\]", throws error `Unrecognized escape sequence in literal: \].` No way of escaping[/-] [+]Cannot output the string "`\]`", throws error `Unrecognized escape sequence in literal: \].` No way of escaping[/+] on Sep 8, 2021
  3. added this to the version/v3 milestone on Sep 8, 2021
  4. axunonb commented on Sep 8, 2021

    @axunonb
    Member

    Thanks for pointing this out. It is a know limitation of v2.x versions and one of the reasons for the next major release. The current version on NuGet is 3.0.0-alpha.2 which includes a fix also for escaping of literals and format options (e.g. for RegEx). Please give it a try. Any other feedback is welcome, too.

    Regarding your sample code, for [ there is no need to escape in literals, neither in v2.7.0 nor in v3.0.0. It's only required in Placeholders.

  5. axunonb commented on Sep 11, 2021

    @axunonb
    Member

    @raxuser100 Should we close this issue?

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions