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

Added ExpressionHelper.TextSpan_ToString to prevent multiple copies at NumberLiteralBase<T> #167

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
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 TextSpan_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.TextSpan_ToString),
numberStyles,
culture,
result.Value)
Expand Down