From 1f3cd59f43dbbfa2d5e983357d9f1adb9be167b3 Mon Sep 17 00:00:00 2001 From: axunonb Date: Tue, 15 Aug 2017 16:09:29 +0200 Subject: [PATCH] Fixes issue https://github.com/scottrippey/SmartFormat.NET/issues/94 --- src/SmartFormat.Tests/Core/FormatterTests.cs | 17 +++++++++++++++++ .../OldTests/CodeProjectExampleTests.cs | 11 +++++------ src/SmartFormat.Tests/SmartFormat.Tests.csproj | 1 - src/SmartFormat/Core/Parsing/Parser.cs | 9 ++++----- src/SmartFormat/Properties/AssemblyInfo.cs | 2 +- src/SmartFormat/SmartFormat.FxCore.csproj | 2 +- 6 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/SmartFormat.Tests/Core/FormatterTests.cs b/src/SmartFormat.Tests/Core/FormatterTests.cs index 169db167..1f8f9174 100644 --- a/src/SmartFormat.Tests/Core/FormatterTests.cs +++ b/src/SmartFormat.Tests/Core/FormatterTests.cs @@ -78,5 +78,22 @@ public void Formatter_NotifyFormattingError() var res = formatter.Format("{NoName} {Name} {OtherMissing}", obj); Assert.That(badPlaceholder.Count == 2 && badPlaceholder[0] == "{NoName}" && badPlaceholder[1] == "{OtherMissing}"); } + + [Test] + public void LeadingBackslashMustNotEscapeBraces() + { + var smart = Smart.CreateDefaultSmartFormat(); ; + smart.Settings.ConvertCharacterStringLiterals = false; + + var expected = "\\Hello"; + var actual = smart.Format("\\{Test}", new { Test = "Hello" }); + Assert.AreEqual(expected, actual); + + smart.Settings.ConvertCharacterStringLiterals = true; + + expected = @"\Hello"; + actual = smart.Format(@"\\{Test}", new { Test = "Hello" }); // double backslash means escaping the backslash + Assert.AreEqual(expected, actual); + } } } \ No newline at end of file diff --git a/src/SmartFormat.Tests/OldTests/CodeProjectExampleTests.cs b/src/SmartFormat.Tests/OldTests/CodeProjectExampleTests.cs index a6d88c53..ad2e16a2 100644 --- a/src/SmartFormat.Tests/OldTests/CodeProjectExampleTests.cs +++ b/src/SmartFormat.Tests/OldTests/CodeProjectExampleTests.cs @@ -459,19 +459,18 @@ public void EscapingDoubleBraces() [Test] public void EscapingSlashBraces() { - var Smart = new SmartFormatter(); - Smart.Parser.UseAlternativeEscapeChar('\\'); - Smart.AddExtensions(new DefaultFormatter()); - Smart.AddExtensions(new DefaultSource(Smart)); + var smart = new SmartFormatter(); + smart.Parser.UseAlternativeEscapeChar('\\'); + smart.AddExtensions(new DefaultFormatter()); + smart.AddExtensions(new DefaultSource(smart)); var args = new object[] { "Zero", "One", "Two", 3 }; var format = @"{0} \{0\} \{{0}\} {3:00\}} {3:00}\}"; var expected = "Zero {0} {Zero} 03} 03}"; - var actual = Smart.Format(format, args); + var actual = smart.Format(format, args); Assert.AreEqual(expected, actual); } - } } diff --git a/src/SmartFormat.Tests/SmartFormat.Tests.csproj b/src/SmartFormat.Tests/SmartFormat.Tests.csproj index dce85c07..b438e7fa 100644 --- a/src/SmartFormat.Tests/SmartFormat.Tests.csproj +++ b/src/SmartFormat.Tests/SmartFormat.Tests.csproj @@ -82,7 +82,6 @@ - diff --git a/src/SmartFormat/Core/Parsing/Parser.cs b/src/SmartFormat/Core/Parsing/Parser.cs index 2670696a..4fc5f8b1 100644 --- a/src/SmartFormat/Core/Parsing/Parser.cs +++ b/src/SmartFormat/Core/Parsing/Parser.cs @@ -252,17 +252,16 @@ public Format ParseFormat(string format, string[] formatterExtensionNames) current = current.parent.parent; namedFormatterStartIndex = -1; } - else if (c == CharLiteralEscapeChar || c ==_alternativeEscapeChar) + else if ((c == CharLiteralEscapeChar && Settings.ConvertCharacterStringLiterals) || (_alternativeEscaping && c ==_alternativeEscapeChar)) { + namedFormatterStartIndex = -1; + // See that is the next character var nextI = i + 1; // **** Alternative brace escaping with { or } following the escape character **** - if (_alternativeEscaping && nextI < length && - (format[nextI] == openingBrace || format[nextI] == closingBrace)) + if (nextI < length && (format[nextI] == openingBrace || format[nextI] == closingBrace)) { - namedFormatterStartIndex = -1; - // Finish the last text item: if (i != lastI) { diff --git a/src/SmartFormat/Properties/AssemblyInfo.cs b/src/SmartFormat/Properties/AssemblyInfo.cs index 7e42f828..c72e7310 100644 --- a/src/SmartFormat/Properties/AssemblyInfo.cs +++ b/src/SmartFormat/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ [assembly: AssemblyProduct("SmartFormat")] [assembly: AssemblyCopyright("Copyright 2011-2017 Scott Rippey, axuno gGmbH, Bernhard Millauer and other contributors.")] [assembly: AssemblyDescription("A string composition library written in C# that can format data into a string with a minimal, intuitive syntax. It uses extensions to provide named placeholders, pluralization, gender conjugation, and time and list formatting. Project hosted on GitHub https://github.com/scottrippey/SmartFormat.NET")] -[assembly: AssemblyVersion("2.1.0.0")] +[assembly: AssemblyVersion("2.1.0.2")] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from diff --git a/src/SmartFormat/SmartFormat.FxCore.csproj b/src/SmartFormat/SmartFormat.FxCore.csproj index edebc41c..b2832927 100644 --- a/src/SmartFormat/SmartFormat.FxCore.csproj +++ b/src/SmartFormat/SmartFormat.FxCore.csproj @@ -22,7 +22,7 @@ https://github.com/scottrippey/SmartFormat.NET/blob/master/CHANGES.mdhttps://github.com/scottrippey/SmartFormat.NET https://raw.githubusercontent.com/scottrippey/SmartFormat.NET/master/SmartFormat_365x365.png False - 2.1.0.1 + 2.1.0.2