Skip to content

Commit

Permalink
fix format strings with '+' not parsed correctly.
Browse files Browse the repository at this point in the history
fix #1353 Custom format string not accepted by the message
template parser.

Custom numeric format strings may contain plus '+' signs.  That
character was not recognised by the parser.
  • Loading branch information
martinh2011 committed Oct 5, 2019
1 parent ffcdba4 commit 3bb9e24
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/Serilog/Parsing/MessageTemplateParser.cs
Expand Up @@ -253,7 +253,8 @@ static bool IsValidInFormat(char c)
return c != '}' &&
(char.IsLetterOrDigit(c) ||
char.IsPunctuation(c) ||
c == ' ');
c == ' ' ||
c == '+');
}

static TextToken ParseTextToken(int startAt, string messageTemplate, out int next)
Expand Down
28 changes: 12 additions & 16 deletions test/Serilog.Tests/Parsing/MessageTemplateParserTests.cs
Expand Up @@ -173,21 +173,17 @@ public void IndexOutOfRangeExceptionBugHasNotRegressed()
}

[Fact]
public void Bug1353FormatStringIsParsedCorrectly()
{
AssertParsedAs("{0,-25} {1,10:#,##0} {2,10:#,##0} {3,10:+#,##0;-#,##0;0} {4,10:0.00}",
new PropertyToken("0","{0,-25}", startIndex:0),
new TextToken(" ", startIndex:7),
new PropertyToken("1","{1,10:#,##0}", "#,##0", startIndex:8),
new TextToken(" ", startIndex:20),
new PropertyToken("2","{2,10:#,##0}", "#,##0", startIndex:21),
new TextToken(" ", startIndex:33),
new PropertyToken("3","{3,10:+#,##0;-#,##0;0}", "+#,##0;-#,##0;0", startIndex:34),
new TextToken(" ", startIndex:56),
new PropertyToken("4","{4,10:0.00}", "0.00", startIndex:57)
);
}


public void FormatCanContainMultipleSections()
{
var parsed = (PropertyToken)Parse("{Number:##.0;-##.0;zero}").Single();
Assert.Equal("##.0;-##.0;zero", parsed.Format);
}

[Fact]
public void FormatCanContainPlusSign()
{
var parsed = (PropertyToken)Parse("{Number:+##.0}").Single();
Assert.Equal("+##.0", parsed.Format);
}
}
}

0 comments on commit 3bb9e24

Please sign in to comment.