Skip to content

Commit

Permalink
fix: allows all printable characters as format values
Browse files Browse the repository at this point in the history
The existing implementation used .IsPunctuation + IsLetterOrDigit with
an override for '+' and ' '. That left some conspicuous holes and the
spec itself is just "^\}" so basically anything goes in a format string.
This allows for them and this does seem to have a reasonably large
impact on the DefaultConsoleOutputTemplate parsing method. Everything
else was within error bounds and no impact at all on memory allocs.
  • Loading branch information
Insomniak47 authored and nblumhardt committed May 27, 2024
1 parent e1a0047 commit 920b8c8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
11 changes: 2 additions & 9 deletions src/Serilog/Parsing/MessageTemplateParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,10 @@ static bool TryGetDestructuringHint(char c, out Destructuring destructuring)
}
}

static bool IsValidInDestructuringHint(char c)
{
return c is '@' or '$';
}

static bool IsValidInFormat(char c)
{
return c != '}' &&
(char.IsLetterOrDigit(c) ||
char.IsPunctuation(c) ||
c is ' ' or '+');
// This is the entire ascii printable range minus '}' and DEL
return (byte)c >= 32 && (byte)c < 127 && c != '}';
}

static TextToken ParseTextToken(int startAt, string messageTemplate, out int next)
Expand Down
16 changes: 14 additions & 2 deletions test/Serilog.Tests/Parsing/MessageTemplateParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,22 @@ public void FormatsCanContainColons()
}

[Fact]
public void APropertyWithValidNameAndInvalidFormatIsParsedAsText()
public void FormatsCanContainSymbolsAndOperators()
{
AssertParsedAs("{Hello:HH$MM}",
new TextToken("{Hello:HH$MM}"));
new PropertyToken("Hello", "{Hello:HH$MM}", "HH$MM"));

AssertParsedAs("{Hello:HH<MM}",
new PropertyToken("Hello", "{Hello:HH<MM}", "HH<MM"));

AssertParsedAs("{Hello:HH#MM}",
new PropertyToken("Hello", "{Hello:HH#MM}", "HH#MM"));

AssertParsedAs("{Hello:HH+MM}",
new PropertyToken("Hello", "{Hello:HH+MM}", "HH+MM"));

AssertParsedAs("{Hello:HH`MM}",
new PropertyToken("Hello", "{Hello:HH`MM}", "HH`MM"));
}

[Fact]
Expand Down

0 comments on commit 920b8c8

Please sign in to comment.