Skip to content

Commit

Permalink
Fix LongPolyfill incorrectly extending int (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Dec 19, 2024
1 parent 7574ac1 commit e02b568
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 99 deletions.
12 changes: 3 additions & 9 deletions src/Polyfill/Numbers/BytePolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ public static bool TryParse(string? target, IFormatProvider? provider, out byte
public static bool TryParse(ReadOnlySpan<byte> target, IFormatProvider? provider, out byte result) =>
#if NET8_0_OR_GREATER
byte.TryParse(target, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
byte.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, provider, out result);
#else
byte.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, provider, out result);
byte.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, provider, out result);
#endif

/// <summary>
Expand Down Expand Up @@ -71,10 +69,8 @@ public static bool TryParse(ReadOnlySpan<char> target, IFormatProvider? provider
public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IFormatProvider? provider, out byte result) =>
#if NET8_0_OR_GREATER
byte.TryParse(target, style, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
byte.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#else
byte.TryParse(Encoding.UTF8.GetString(target.ToArray()), style, provider, out result);
byte.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#endif

/// <summary>
Expand All @@ -84,10 +80,8 @@ public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IForm
public static bool TryParse(ReadOnlySpan<byte> target, out byte result) =>
#if NET8_0_OR_GREATER
byte.TryParse(target, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
byte.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, null, out result);
#else
byte.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, null, out result);
byte.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, null, out result);
#endif

/// <summary>
Expand Down
12 changes: 3 additions & 9 deletions src/Polyfill/Numbers/DoublePolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ public static bool TryParse(string? target, IFormatProvider? provider, out doubl
public static bool TryParse(ReadOnlySpan<byte> target, IFormatProvider? provider, out double result) =>
#if NET8_0_OR_GREATER
double.TryParse(target, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
double.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Float, provider, out result);
#else
double.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Float, provider, out result);
double.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Float, provider, out result);
#endif

/// <summary>
Expand Down Expand Up @@ -70,10 +68,8 @@ public static bool TryParse(ReadOnlySpan<char> target, IFormatProvider? provider
public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IFormatProvider? provider, out double result) =>
#if NET8_0_OR_GREATER
double.TryParse(target, style, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
double.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#else
double.TryParse(Encoding.UTF8.GetString(target.ToArray()), style, provider, out result);
double.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#endif

/// <summary>
Expand All @@ -83,10 +79,8 @@ public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IForm
public static bool TryParse(ReadOnlySpan<byte> target, out double result) =>
#if NET8_0_OR_GREATER
double.TryParse(target, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
double.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Float, null, out result);
#else
double.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Float, null, out result);
double.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Float, null, out result);
#endif

/// <summary>
Expand Down
12 changes: 3 additions & 9 deletions src/Polyfill/Numbers/IntPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ public static bool TryParse(string? target, IFormatProvider? provider, out int r
public static bool TryParse(ReadOnlySpan<byte> target, IFormatProvider? provider, out int result) =>
#if NET8_0_OR_GREATER
int.TryParse(target, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
int.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, provider, out result);
#else
int.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, provider, out result);
int.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, provider, out result);
#endif

/// <summary>
Expand Down Expand Up @@ -70,10 +68,8 @@ public static bool TryParse(ReadOnlySpan<char> target, IFormatProvider? provider
public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IFormatProvider? provider, out int result) =>
#if NET8_0_OR_GREATER
int.TryParse(target, style, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
int.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#else
int.TryParse(Encoding.UTF8.GetString(target.ToArray()), style, provider, out result);
int.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#endif

/// <summary>
Expand All @@ -83,10 +79,8 @@ public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IForm
public static bool TryParse(ReadOnlySpan<byte> target, out int result) =>
#if NET8_0_OR_GREATER
int.TryParse(target, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
int.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, null, out result);
#else
int.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, null, out result);
int.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, null, out result);
#endif

/// <summary>
Expand Down
48 changes: 21 additions & 27 deletions src/Polyfill/Numbers/LongPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,84 +20,78 @@ static class LongPolyfill
/// Tries to parse a string into a value.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.int64.tryparse#system-int64-tryparse(system-string-system-iformatprovider-system-int64@)
public static bool TryParse(string? target, IFormatProvider? provider, out int result) =>
public static bool TryParse(string? target, IFormatProvider? provider, out long result) =>
#if NET7_0_OR_GREATER
int.TryParse(target, provider, out result);
long.TryParse(target, provider, out result);
#else
int.TryParse(target, NumberStyles.Integer, provider, out result);
long.TryParse(target, NumberStyles.Integer, provider, out result);
#endif

#if FeatureMemory
/// <summary>
/// Tries to parse a span of UTF-8 characters into a value.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.int64.tryparse#system-int64-tryparse(system-readonlyspan((system-byte))-system-iformatprovider-system-int64@)
public static bool TryParse(ReadOnlySpan<byte> target, IFormatProvider? provider, out int result) =>
public static bool TryParse(ReadOnlySpan<byte> target, IFormatProvider? provider, out long result) =>
#if NET8_0_OR_GREATER
int.TryParse(target, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
int.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, provider, out result);
long.TryParse(target, provider, out result);
#else
int.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, provider, out result);
long.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, provider, out result);
#endif

/// <summary>
/// Converts the span representation of a number in a specified style and culture-specific format to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.int64.tryparse#system-int64-tryparse(system-readonlyspan((system-char))-system-int64@)
public static bool TryParse(ReadOnlySpan<char> target, out int result) =>
public static bool TryParse(ReadOnlySpan<char> target, out long result) =>
#if NETCOREAPP2_0 || NETFRAMEWORK || NETSTANDARD2_0
int.TryParse(target.ToString(), out result);
long.TryParse(target.ToString(), out result);
#else
int.TryParse(target, out result);
long.TryParse(target, out result);
#endif

/// <summary>
/// Tries to parse a span of characters into a value.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.int64.tryparse#system-int64-tryparse(system-readonlyspan((system-char))-system-iformatprovider-system-int64@)
public static bool TryParse(ReadOnlySpan<char> target, IFormatProvider? provider, out int result) =>
public static bool TryParse(ReadOnlySpan<char> target, IFormatProvider? provider, out long result) =>
#if NET7_0_OR_GREATER
int.TryParse(target, provider, out result);
long.TryParse(target, provider, out result);
#else
int.TryParse(target.ToString(), NumberStyles.Integer, provider, out result);
long.TryParse(target.ToString(), NumberStyles.Integer, provider, out result);
#endif

/// <summary>
/// Tries to parse a span of UTF-8 characters into a value.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.int64.tryparse#system-int64-tryparse(system-readonlyspan((system-byte))-system-globalization-numberstyles-system-iformatprovider-system-int64@)
public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IFormatProvider? provider, out int result) =>
public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IFormatProvider? provider, out long result) =>
#if NET8_0_OR_GREATER
int.TryParse(target, style, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
int.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
long.TryParse(target, style, provider, out result);
#else
int.TryParse(Encoding.UTF8.GetString(target.ToArray()), style, provider, out result);
long.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#endif

/// <summary>
/// Tries to convert a UTF-8 character span containing the string representation of a number to its 64-bit signed integer equivalent.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.int64.tryparse#system-int64-tryparse(system-readonlyspan((system-char))-system-globalization-numberstyles-system-iformatprovider-system-int64@)
public static bool TryParse(ReadOnlySpan<byte> target, out int result) =>
public static bool TryParse(ReadOnlySpan<byte> target, out long result) =>
#if NET8_0_OR_GREATER
int.TryParse(target, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
int.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, null, out result);
long.TryParse(target, out result);
#else
int.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, null, out result);
long.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, null, out result);
#endif

/// <summary>
/// Converts the span representation of a number in a specified style and culture-specific format to its 64-bit signed integer equivalent. A return value indicates whether the conversion succeeded.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.int64.tryparse#system-int64-tryparse(system-readonlyspan((system-char))-system-globalization-numberstyles-system-iformatprovider-system-int64@)
public static bool TryParse(ReadOnlySpan<char> target, NumberStyles style, IFormatProvider? provider, out int result) =>
public static bool TryParse(ReadOnlySpan<char> target, NumberStyles style, IFormatProvider? provider, out long result) =>
#if NETCOREAPP2_0 || NETSTANDARD2_0 || NETFRAMEWORK
int.TryParse(target.ToString(), style, provider, out result);
long.TryParse(target.ToString(), style, provider, out result);
#else
int.TryParse(target, style, provider, out result);
long.TryParse(target, style, provider, out result);
#endif
#endif
}
12 changes: 3 additions & 9 deletions src/Polyfill/Numbers/SBytePolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ public static bool TryParse(string? target, IFormatProvider? provider, out sbyte
public static bool TryParse(ReadOnlySpan<byte> target, IFormatProvider? provider, out sbyte result) =>
#if NET8_0_OR_GREATER
sbyte.TryParse(target, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
sbyte.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, provider, out result);
#else
sbyte.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, provider, out result);
sbyte.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, provider, out result);
#endif

/// <summary>
Expand Down Expand Up @@ -70,10 +68,8 @@ public static bool TryParse(ReadOnlySpan<char> target, IFormatProvider? provider
public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IFormatProvider? provider, out sbyte result) =>
#if NET8_0_OR_GREATER
sbyte.TryParse(target, style, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
sbyte.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#else
sbyte.TryParse(Encoding.UTF8.GetString(target.ToArray()), style, provider, out result);
sbyte.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#endif

/// <summary>
Expand All @@ -83,10 +79,8 @@ public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IForm
public static bool TryParse(ReadOnlySpan<byte> target, out sbyte result) =>
#if NET8_0_OR_GREATER
sbyte.TryParse(target, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
sbyte.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, null, out result);
#else
sbyte.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, null, out result);
sbyte.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, null, out result);
#endif

/// <summary>
Expand Down
12 changes: 3 additions & 9 deletions src/Polyfill/Numbers/ShortPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ public static bool TryParse(string? target, IFormatProvider? provider, out short
public static bool TryParse(ReadOnlySpan<byte> target, IFormatProvider? provider, out short result) =>
#if NET8_0_OR_GREATER
short.TryParse(target, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
short.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, provider, out result);
#else
short.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, provider, out result);
short.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, provider, out result);
#endif

/// <summary>
Expand Down Expand Up @@ -70,10 +68,8 @@ public static bool TryParse(ReadOnlySpan<char> target, IFormatProvider? provider
public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IFormatProvider? provider, out short result) =>
#if NET8_0_OR_GREATER
short.TryParse(target, style, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
short.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#else
short.TryParse(Encoding.UTF8.GetString(target.ToArray()), style, provider, out result);
short.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#endif

/// <summary>
Expand All @@ -83,10 +79,8 @@ public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IForm
public static bool TryParse(ReadOnlySpan<byte> target, out short result) =>
#if NET8_0_OR_GREATER
short.TryParse(target, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
short.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, null, out result);
#else
short.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, null, out result);
short.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, null, out result);
#endif

/// <summary>
Expand Down
12 changes: 3 additions & 9 deletions src/Polyfill/Numbers/UIntPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ public static bool TryParse(string? target, IFormatProvider? provider, out uint
public static bool TryParse(ReadOnlySpan<byte> target, IFormatProvider? provider, out uint result) =>
#if NET8_0_OR_GREATER
uint.TryParse(target, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
uint.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, provider, out result);
#else
uint.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, provider, out result);
uint.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, provider, out result);
#endif

/// <summary>
Expand Down Expand Up @@ -70,10 +68,8 @@ public static bool TryParse(ReadOnlySpan<char> target, IFormatProvider? provider
public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IFormatProvider? provider, out uint result) =>
#if NET8_0_OR_GREATER
uint.TryParse(target, style, provider, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
uint.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#else
uint.TryParse(Encoding.UTF8.GetString(target.ToArray()), style, provider, out result);
uint.TryParse(Encoding.UTF8.GetString(target), style, provider, out result);
#endif

/// <summary>
Expand All @@ -83,10 +79,8 @@ public static bool TryParse(ReadOnlySpan<byte> target, NumberStyles style, IForm
public static bool TryParse(ReadOnlySpan<byte> target, out uint result) =>
#if NET8_0_OR_GREATER
uint.TryParse(target, out result);
#elif FeatureMemory && AllowUnsafeBlocks || NETCOREAPP2_1_OR_GREATER
uint.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, null, out result);
#else
uint.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, null, out result);
uint.TryParse(Encoding.UTF8.GetString(target), NumberStyles.Integer, null, out result);
#endif

/// <summary>
Expand Down
Loading

0 comments on commit e02b568

Please sign in to comment.