Skip to content

Commit

Permalink
Fixes issue #94
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb committed Aug 15, 2017
1 parent 39603d8 commit 1f3cd59
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
17 changes: 17 additions & 0 deletions src/SmartFormat.Tests/Core/FormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
11 changes: 5 additions & 6 deletions src/SmartFormat.Tests/OldTests/CodeProjectExampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
}
1 change: 0 additions & 1 deletion src/SmartFormat.Tests/SmartFormat.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="SmartFormat.snk" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SmartFormat\SmartFormat.csproj">
Expand Down
9 changes: 4 additions & 5 deletions src/SmartFormat/Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SmartFormat/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/SmartFormat/SmartFormat.FxCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ https://github.com/scottrippey/SmartFormat.NET/blob/master/CHANGES.md</PackageRe
<PackageProjectUrl>https://github.com/scottrippey/SmartFormat.NET</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/scottrippey/SmartFormat.NET/master/SmartFormat_365x365.png</PackageIconUrl>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<Version>2.1.0.1</Version>
<Version>2.1.0.2</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down

0 comments on commit 1f3cd59

Please sign in to comment.