Skip to content
Prev Previous commit
Next Next commit
Change ParseRealLiteral to remove literals before parsing
  • Loading branch information
RenanCarlosPereira authored Jun 25, 2024
commit a27a91f7dc0538806d7e7d34b1bf06b1d816f349
25 changes: 20 additions & 5 deletions src/System.Linq.Dynamic.Core/Parser/NumberParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Globalization;
using System.Globalization;
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Dynamic.Core.Validation;
using System.Linq.Expressions;
Expand Down Expand Up @@ -161,19 +161,34 @@ public Expression ParseIntegerLiteral(int tokenPosition, string text)
/// </summary>
public Expression ParseRealLiteral(string text, char qualifier, bool stripQualifier)
{
if (stripQualifier)
{
var pos = text.Length - 1;
while (pos >= 0 && Qualifiers.Contains(text[pos]))
{
pos--;
}

if (pos < text.Length - 1)
{
qualifier = text[pos + 1];
text = text.Substring(0, pos + 1);
}
}

switch (qualifier)
{
case 'f':
case 'F':
return _constantExpressionHelper.CreateLiteral(ParseNumber(stripQualifier ? text.Substring(0, text.Length - 1) : text, typeof(float))!, text);
return ConstantExpressionHelper.CreateLiteral(ParseNumber(text, typeof(float))!, text);

case 'm':
case 'M':
return _constantExpressionHelper.CreateLiteral(ParseNumber(stripQualifier ? text.Substring(0, text.Length - 1) : text, typeof(decimal))!, text);
return ConstantExpressionHelper.CreateLiteral(ParseNumber(text, typeof(decimal))!, text);

case 'd':
case 'D':
return _constantExpressionHelper.CreateLiteral(ParseNumber(stripQualifier ? text.Substring(0, text.Length - 1) : text, typeof(double))!, text);
return ConstantExpressionHelper.CreateLiteral(ParseNumber(text, typeof(double))!, text);

default:
return _constantExpressionHelper.CreateLiteral(ParseNumber(text, typeof(double))!, text);
Expand Down Expand Up @@ -298,4 +313,4 @@ private Expression ParseAsBinary(int tokenPosition, string text, bool isNegative
throw new ParseException(string.Format(_culture, Res.InvalidBinaryIntegerLiteral, text), tokenPosition);
}
}
}
}