Skip to content

Commit

Permalink
Added ExpressionHelper.TextSpan_ToString to prevent multiple copies…
Browse files Browse the repository at this point in the history
… at `NumberLiteralBase<T>` (#167)

* Added `ExpressionHelper.TextSpan_ToString` to prevent multiple copies at `NumberLiteralBase<T>`

* Remove unused variable

* Use correct name
  • Loading branch information
gumbarros authored Nov 20, 2024
1 parent a2ced50 commit 3bab85f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/Parlot/Compilation/ExpressionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ public static class ExpressionHelper

internal static readonly ConstructorInfo TextSpan_Constructor = typeof(TextSpan).GetConstructor([typeof(string), typeof(int), typeof(int)])!;

internal static readonly MethodInfo ReadOnlySpan_ToString = typeof(ReadOnlySpan<char>).GetMethod(nameof(ToString), [])!;

internal static readonly MethodInfo MemoryExtensions_AsSpan = typeof(MemoryExtensions).GetMethod(nameof(MemoryExtensions.AsSpan), [typeof(string)])!;

public static Expression ArrayEmpty<T>() => ((Expression<Func<object>>)(() => Array.Empty<T>())).Body;
public static Expression New<T>() where T : new() => ((Expression<Func<T>>)(() => new T())).Body;

Expand Down
7 changes: 1 addition & 6 deletions src/Parlot/Fluent/NumberLiteralBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace Parlot.Fluent;
public abstract class NumberLiteralBase<T> : Parser<T>, ICompilable
{
private static readonly MethodInfo _defaultTryParseMethodInfo = typeof(T).GetMethod("TryParse", [typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(T).MakeByRefType()])!;
private static readonly MethodInfo _rosToString = typeof(ReadOnlySpan<char>).GetMethod(nameof(ToString), [])!;

private readonly char _decimalSeparator;
private readonly char _groupSeparator;
Expand All @@ -26,8 +25,6 @@ public abstract class NumberLiteralBase<T> : Parser<T>, ICompilable
private readonly bool _allowGroupSeparator;
private readonly bool _allowExponent;

private delegate (bool, T) TryParseDelegate();

public abstract bool TryParseNumber(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out T value);

public NumberLiteralBase(NumberOptions numberOptions = NumberOptions.Number, char decimalSeparator = NumberLiterals.DefaultDecimalSeparator, char groupSeparator = NumberLiterals.DefaultGroupSeparator, MethodInfo? tryParseMethodInfo = null)
Expand Down Expand Up @@ -62,8 +59,6 @@ public override bool Parse(ParseContext context, ref ParseResult<T> result)
{
var end = context.Scanner.Cursor.Offset;

var sourceToParse = number.ToString();

if (TryParseNumber(number, _numberStyles, _culture, out T value))
{
result.Set(start, end, value);
Expand Down Expand Up @@ -115,7 +110,7 @@ public CompilationResult Compile(CompilationContext context)
Expression.Assign(result.Success,
Expression.Call(
_tryParseMethodInfo,
Expression.Call(numberSpan, _rosToString),
Expression.Call(numberSpan, ExpressionHelper.ReadOnlySpan_ToString),
numberStyles,
culture,
result.Value)
Expand Down

0 comments on commit 3bab85f

Please sign in to comment.