Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Unicode escape support #166

Merged
merged 26 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a52d832
Merge pull request #1 from axuno/master
karljj1 Nov 12, 2020
bf2379c
Merge remote-tracking branch 'Original/main'
karljj1 Apr 12, 2021
afd5b77
Update issue templates
axunonb Apr 17, 2021
1d22508
Inserted a link to merged version/v3 changes
axunonb Apr 27, 2021
11c3033
Updated README
axunonb Apr 27, 2021
d1779f9
Merge remote-tracking branch 'upstream/main' into version/v3.0
axunonb Apr 29, 2021
cb5559a
Make parser fully covered with unit tests
axunonb Apr 29, 2021
e327de5
Merge remote-tracking branch 'upstream/version/v3.0' into version/v3.0
axunonb Apr 29, 2021
cab97df
Updated CHANGES.md
axunonb Apr 29, 2021
e8d9c93
Merge remote-tracking branch 'upstream/version/v3.0' into version/v3.0
axunonb Apr 29, 2021
294f810
Merge remote-tracking branch 'upstream/version/v3.0' into version/v3.0
axunonb Apr 29, 2021
792c391
Merge remote-tracking branch 'upstream/version/v3.0' into version/v3.0
axunonb May 3, 2021
4f45a84
Merge remote-tracking branch 'upstream/version/v3.0' into version/v3.0
axunonb May 9, 2021
e49db1e
Merge remote-tracking branch 'upstream/version/v3.0' into version/v3.0
axunonb May 9, 2021
2c562b5
Merge remote-tracking branch 'upstream/version/v3.0' into version/v3.0
axunonb May 9, 2021
2ba8c5f
Updated .editorconfig
axunonb May 9, 2021
46f02d4
Added support for Unicode Escape sequences
karljj1 May 21, 2021
f358b60
Updated test with unicode escape sequence support and fixed some typos
karljj1 May 21, 2021
a525889
Fixed typo
karljj1 May 21, 2021
933e992
Some additional tests to keep code coverage happy
karljj1 May 21, 2021
7c3a12e
Merge remote-tracking branch 'upstream/version/v3.0' into version/v3.0
axunonb May 25, 2021
bda63e0
Merge remote-tracking branch 'upstream/version/v3.0' into version/v3.0
axunonb May 25, 2021
ef58744
Merge remote-tracking branch 'upstream/version/v3.0' into version/v3.0
axunonb May 26, 2021
503e0a5
Some additional tests to keep code coverage happy
karljj1 May 21, 2021
0477b1e
Rebased to version/v3.0
axunonb May 27, 2021
06c47eb
Rebased to version/v3.0
axunonb May 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/SmartFormat.Tests/Core/LiteralTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ public class LiteralTextTests
public void FormatCharacterLiteralsAsString()
{
const string formatWithFileBehavior = @"No carriage return\r, no line feed\n";
const string formatWithCodeBahavior = "With carriage return\r, and with line feed\n";
const string formatWithCodeBehavior = "With carriage return\r, and with line feed\n";

var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.ConvertCharacterStringLiterals = false;

var result = formatter.Format(formatWithFileBehavior);
Assert.AreEqual(string.Format(formatWithFileBehavior), result);

result = formatter.Format(formatWithCodeBahavior);
Assert.AreEqual(string.Format(formatWithCodeBahavior), result);
result = formatter.Format(formatWithCodeBehavior);
Assert.AreEqual(string.Format(formatWithCodeBehavior), result);
}

[Test]
public void AllSupportedCharacterLiteralsAsUnicode()
{
var data = new Dictionary<string, string> { { "key", "value" } };

const string formatWithFileBehavior = @"All supported literal characters: \' \"" \\ \a \b \f \n \r \t \v {key}\0!";
const string formatWithCodeBahavior = "All supported literal characters: \' \" \\ \a \b \f \n \r \t \v {key}\0!";
const string formatWithFileBehavior = @"All supported literal characters: \' \"" \\ \a \b \f \n \r \t \v \u2022 {key}\0!";
const string formatWithCodeBehavior = "All supported literal characters: \' \" \\ \a \b \f \n \r \t \v \u2022 {key}\0!";

var formatter = Smart.CreateDefaultSmartFormat();
formatter.Settings.ConvertCharacterStringLiterals = true;

var result = formatter.Format(formatWithFileBehavior, data);
Assert.AreEqual(string.Format(formatWithCodeBahavior.Replace("{"+ data.First().Key +"}", data.First().Value)), result);
Assert.AreEqual(string.Format(formatWithCodeBehavior.Replace("{"+ data.First().Key +"}", data.First().Value)), result);
}

[Test]
Expand All @@ -63,15 +63,15 @@ public void IllegalEscapeSequenceThrowsException()
Smart.Default.Settings.ConvertCharacterStringLiterals = true;

Assert.Throws<ArgumentException>(() => {
Smart.Default.Format(@"Illegal excape sequence at end of line = \z");
Smart.Default.Format(@"Illegal escape sequence at end of line = \z");
});

Assert.Throws<ArgumentException>(() => {
Smart.Default.Format(@"Illegal excape sequence \z somewhere in text");
Smart.Default.Format(@"Illegal escape sequence \z somewhere in text");
});

Assert.Throws<ArgumentException>(() => {
Smart.Default.Format(@"Illegal excape sequences at end of line = \");
Smart.Default.Format(@"Illegal escape sequences at end of line = \");
});

Smart.Default.Settings.ConvertCharacterStringLiterals = false;
Expand Down
8 changes: 7 additions & 1 deletion src/SmartFormat/Core/Parsing/LiteralText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

using System;
using System.Globalization;
using SmartFormat.Core.Settings;

namespace SmartFormat.Core.Parsing
Expand Down Expand Up @@ -39,7 +40,7 @@ private string ConvertCharacterLiteralsToUnicode()
if (source[0] != Parser.CharLiteralEscapeChar)
return source;

// The string length should be 2: espace character \ and literal character
// The string length should be 2: escape character \ and literal character
if (source.Length < 2) throw new ArgumentException($"Missing escape sequence in literal: \"{source}\"");

char c;
Expand Down Expand Up @@ -78,6 +79,11 @@ private string ConvertCharacterLiteralsToUnicode()
case 'v':
c = '\v';
break;
case 'u':
if (!int.TryParse(source.Substring(2, source.Length - 2), NumberStyles.HexNumber, null, out var result))
throw new ArgumentException($"Failed to parse unicode escape sequence in literal: \"{source}\"");
c = (char) result;
break;
default:
throw new ArgumentException($"Unrecognized escape sequence in literal: \"{source}\"");
}
Expand Down
8 changes: 7 additions & 1 deletion src/SmartFormat/Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,13 @@ public Format ParseFormat(string format, string[] formatterExtensionNames)

// Finish the last text item:
if (i != lastI) current.Items.Add(new LiteralText(Settings, current, lastI) {endIndex = i});
lastI = i + 2;

// Is this a unicode escape sequence?
if (i + 1 < format.Length && format[i + 1] == 'u')
lastI = i + 6; // \u0000 format
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would be safe to do
i += 6 and break here to save having to iterate over the numbers?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safe enough. It's the same as with other escaped characters. Illegal escape sequences throw when trying to unescape the literal by the formatter. I don't see an advantage already throwing in the parser.

else
lastI = i + 2;

if (lastI > length) lastI = length;

// Next add the character literal INCLUDING the escape character, which LiteralText will expect
Expand Down