Skip to content

Commit

Permalink
Merge pull request #38 from xin9le/feature/refactor-ToString
Browse files Browse the repository at this point in the history
Refactor `.ToString()`
  • Loading branch information
xin9le authored Sep 5, 2024
2 parents b479f40 + 73b0069 commit 4d82386
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/libs/FastEnum.Core/FastEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,6 @@ static bool tryParseNameCaseInsensitive(ReadOnlySpan<char> name, out T result)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString<T>(T value)
where T : struct, Enum
=> UnderlyingOperation<T>.ToString(value);
=> UnderlyingOperation<T>.GetName(value) ?? UnderlyingOperation<T>.ToNumberString(value);
#endregion
}
124 changes: 34 additions & 90 deletions src/libs/FastEnum.Core/Internals/UnderlyingOperation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <auto-generated>
// <auto-generated>
// This .cs file is generated by T4 template. Don't change it. Change the .tt file instead.
// </auto-generated>
#nullable enable
Expand Down Expand Up @@ -62,26 +62,26 @@ public static bool IsDefined(T value)


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString(T value)
public static string ToNumberString(T value)
{
switch (EnumInfo<T>.s_typeCode)
{
case TypeCode.SByte:
return SByteOperation.ToString(value);
return SByteOperation.ToNumberString(value);
case TypeCode.Byte:
return ByteOperation.ToString(value);
return ByteOperation.ToNumberString(value);
case TypeCode.Int16:
return Int16Operation.ToString(value);
return Int16Operation.ToNumberString(value);
case TypeCode.UInt16:
return UInt16Operation.ToString(value);
return UInt16Operation.ToNumberString(value);
case TypeCode.Int32:
return Int32Operation.ToString(value);
return Int32Operation.ToNumberString(value);
case TypeCode.UInt32:
return UInt32Operation.ToString(value);
return UInt32Operation.ToNumberString(value);
case TypeCode.Int64:
return Int64Operation.ToString(value);
return Int64Operation.ToNumberString(value);
case TypeCode.UInt64:
return UInt64Operation.ToString(value);
return UInt64Operation.ToNumberString(value);
default:
ThrowHelper.ThrowUnexpectedCodeReached();
return null!;
Expand Down Expand Up @@ -181,17 +181,10 @@ static sbyte toNumber(T value)


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString(T value)
public static string ToNumberString(T value)
{
if (TryGetMember(value, out var member))
{
return member.Name;
}
else
{
ref var x = ref Unsafe.As<T, sbyte>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}
ref var x = ref Unsafe.As<T, sbyte>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}


Expand Down Expand Up @@ -278,17 +271,10 @@ static byte toNumber(T value)


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString(T value)
public static string ToNumberString(T value)
{
if (TryGetMember(value, out var member))
{
return member.Name;
}
else
{
ref var x = ref Unsafe.As<T, byte>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}
ref var x = ref Unsafe.As<T, byte>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}


Expand Down Expand Up @@ -375,17 +361,10 @@ static short toNumber(T value)


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString(T value)
public static string ToNumberString(T value)
{
if (TryGetMember(value, out var member))
{
return member.Name;
}
else
{
ref var x = ref Unsafe.As<T, short>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}
ref var x = ref Unsafe.As<T, short>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}


Expand Down Expand Up @@ -472,17 +451,10 @@ static ushort toNumber(T value)


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString(T value)
public static string ToNumberString(T value)
{
if (TryGetMember(value, out var member))
{
return member.Name;
}
else
{
ref var x = ref Unsafe.As<T, ushort>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}
ref var x = ref Unsafe.As<T, ushort>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}


Expand Down Expand Up @@ -569,17 +541,10 @@ static int toNumber(T value)


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString(T value)
public static string ToNumberString(T value)
{
if (TryGetMember(value, out var member))
{
return member.Name;
}
else
{
ref var x = ref Unsafe.As<T, int>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}
ref var x = ref Unsafe.As<T, int>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}


Expand Down Expand Up @@ -666,17 +631,10 @@ static uint toNumber(T value)


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString(T value)
public static string ToNumberString(T value)
{
if (TryGetMember(value, out var member))
{
return member.Name;
}
else
{
ref var x = ref Unsafe.As<T, uint>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}
ref var x = ref Unsafe.As<T, uint>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}


Expand Down Expand Up @@ -763,17 +721,10 @@ static long toNumber(T value)


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString(T value)
public static string ToNumberString(T value)
{
if (TryGetMember(value, out var member))
{
return member.Name;
}
else
{
ref var x = ref Unsafe.As<T, long>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}
ref var x = ref Unsafe.As<T, long>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}


Expand Down Expand Up @@ -860,17 +811,10 @@ static ulong toNumber(T value)


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString(T value)
public static string ToNumberString(T value)
{
if (TryGetMember(value, out var member))
{
return member.Name;
}
else
{
ref var x = ref Unsafe.As<T, ulong>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}
ref var x = ref Unsafe.As<T, ulong>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}


Expand Down
17 changes: 5 additions & 12 deletions src/libs/FastEnum.Core/Internals/UnderlyingOperation.tt
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ internal static class UnderlyingOperation<T>


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString(T value)
public static string ToNumberString(T value)
{
switch (EnumInfo<T>.s_typeCode)
{
<# foreach (var x in parameters) { #>
case TypeCode.<#= x.TypeName #>:
return <#= x.TypeName #>Operation.ToString(value);
return <#= x.TypeName #>Operation.ToNumberString(value);
<# } #>
default:
ThrowHelper.ThrowUnexpectedCodeReached();
Expand Down Expand Up @@ -147,17 +147,10 @@ internal static class UnderlyingOperation<T>


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToString(T value)
public static string ToNumberString(T value)
{
if (TryGetMember(value, out var member))
{
return member.Name;
}
else
{
ref var x = ref Unsafe.As<T, <#= x.CompatibleName #>>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}
ref var x = ref Unsafe.As<T, <#= x.CompatibleName #>>(ref value);
return x.ToString(null, CultureInfo.InvariantCulture);
}


Expand Down

0 comments on commit 4d82386

Please sign in to comment.