From 1a67dbed6dfcd73e3ddfc174d2e0de92088d516f Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Sun, 16 Nov 2025 18:38:18 +0200 Subject: [PATCH 1/2] Add polyfills for `Math.Clamp(...)` --- PolyShim.Tests/Net60/MathTests.cs | 17 ++++++++++++++ PolyShim.Tests/NetCore20/MathTests.cs | 20 +++++++++++++++++ PolyShim/Net60/Math.cs | 30 +++++++++++++++++++++++++ PolyShim/NetCore20/Math.cs | 32 +++++++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 PolyShim.Tests/Net60/MathTests.cs create mode 100644 PolyShim.Tests/NetCore20/MathTests.cs create mode 100644 PolyShim/Net60/Math.cs create mode 100644 PolyShim/NetCore20/Math.cs diff --git a/PolyShim.Tests/Net60/MathTests.cs b/PolyShim.Tests/Net60/MathTests.cs new file mode 100644 index 00000000..465a14bf --- /dev/null +++ b/PolyShim.Tests/Net60/MathTests.cs @@ -0,0 +1,17 @@ +using System; +using FluentAssertions; +using Xunit; + +namespace PolyShim.Tests.Net60; + +public class MathTests +{ + [Fact] + public void Clamp_Test() + { + // Act & assert + Math.Clamp((nint)5, (nint)1, (nint)10).Should().Be((nint)5); + Math.Clamp((nint)0, (nint)1, (nint)10).Should().Be((nint)1); + Math.Clamp((nint)15, (nint)1, (nint)10).Should().Be((nint)10); + } +} diff --git a/PolyShim.Tests/NetCore20/MathTests.cs b/PolyShim.Tests/NetCore20/MathTests.cs new file mode 100644 index 00000000..e7b3bb8e --- /dev/null +++ b/PolyShim.Tests/NetCore20/MathTests.cs @@ -0,0 +1,20 @@ +using System; +using FluentAssertions; +using Xunit; + +namespace PolyShim.Tests.NetCore20; + +public class MathTests +{ + [Fact] + public void Clamp_Test() + { + // Act & assert + Math.Clamp(5, 1, 10).Should().Be(5); + Math.Clamp(0, 1, 10).Should().Be(1); + Math.Clamp(15, 1, 10).Should().Be(10); + Math.Clamp(5.5, 1.0, 10.0).Should().Be(5.5); + Math.Clamp(0.5, 1.0, 10.0).Should().Be(1.0); + Math.Clamp(15.5, 1.0, 10.0).Should().Be(10.0); + } +} diff --git a/PolyShim/Net60/Math.cs b/PolyShim/Net60/Math.cs new file mode 100644 index 00000000..0f40cc31 --- /dev/null +++ b/PolyShim/Net60/Math.cs @@ -0,0 +1,30 @@ +// The following comment is required to instruct analyzers to skip this file +// + +#if (NETCOREAPP && !NET6_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) +#nullable enable +// ReSharper disable RedundantUsingDirective +// ReSharper disable CheckNamespace +// ReSharper disable InconsistentNaming +// ReSharper disable PartialTypeWithSinglePart + +using System; + +internal static partial class PolyfillExtensions +{ + extension(Math) + { + // https://learn.microsoft.com/dotnet/api/system.math.clamp#system-math-clamp(system-intptr-system-intptr-system-intptr) + public static IntPtr Clamp(IntPtr value, IntPtr min, IntPtr max) + { + if (value.ToInt64() < min.ToInt64()) + return min; + + if (value.ToInt64() > max.ToInt64()) + return max; + + return value; + } + } +} +#endif \ No newline at end of file diff --git a/PolyShim/NetCore20/Math.cs b/PolyShim/NetCore20/Math.cs new file mode 100644 index 00000000..5b259f7c --- /dev/null +++ b/PolyShim/NetCore20/Math.cs @@ -0,0 +1,32 @@ +// The following comment is required to instruct analyzers to skip this file +// + +#if (NETCOREAPP && !NETCOREAPP2_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD && !NETSTANDARD2_1_OR_GREATER) +#nullable enable +// ReSharper disable RedundantUsingDirective +// ReSharper disable CheckNamespace +// ReSharper disable InconsistentNaming +// ReSharper disable PartialTypeWithSinglePart + +using System; + +internal static partial class PolyfillExtensions +{ + extension(Math) + { + // https://learn.microsoft.com/dotnet/api/system.math.clamp + // Technically, the original implementation has overloads for each separate numeric type, + // but this generic version is fully source-compatible and simpler to maintain. + public static T Clamp(T value, T min, T max) where T : IComparable + { + if (value.CompareTo(min) < 0) + return min; + + if (value.CompareTo(max) > 0) + return max; + + return value; + } + } +} +#endif From 2b6c5b8c31cbab5de19c4b22b5db3a29ceefa147 Mon Sep 17 00:00:00 2001 From: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com> Date: Sun, 16 Nov 2025 18:45:14 +0200 Subject: [PATCH 2/2] Update PolyShim/Net60/Math.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- PolyShim/Net60/Math.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/PolyShim/Net60/Math.cs b/PolyShim/Net60/Math.cs index 0f40cc31..363daf90 100644 --- a/PolyShim/Net60/Math.cs +++ b/PolyShim/Net60/Math.cs @@ -17,10 +17,14 @@ internal static partial class PolyfillExtensions // https://learn.microsoft.com/dotnet/api/system.math.clamp#system-math-clamp(system-intptr-system-intptr-system-intptr) public static IntPtr Clamp(IntPtr value, IntPtr min, IntPtr max) { - if (value.ToInt64() < min.ToInt64()) + var valueAsLong = value.ToInt64(); + var minAsLong = min.ToInt64(); + var maxAsLong = max.ToInt64(); + + if (valueAsLong < minAsLong) return min; - if (value.ToInt64() > max.ToInt64()) + if (valueAsLong > maxAsLong) return max; return value;