From 72a029027fcb6cce01093442a6084cbe64843e51 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Oct 2022 13:49:50 +0200 Subject: [PATCH] Update Index and Range polyfills to match runtime --- .../EmbeddedResources/System.Index.cs | 16 ++++++---- .../EmbeddedResources/System.Range.cs | 29 +++++++++++++++---- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/System.Index.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/System.Index.cs index 29720db..8a09e6f 100644 --- a/src/PolySharp.SourceGenerators/EmbeddedResources/System.Index.cs +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/System.Index.cs @@ -7,7 +7,7 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; - + namespace System { /// Represent a type can be used to index a collection either from the start or the end. @@ -33,7 +33,7 @@ public Index(int value, bool fromEnd = false) { if (value < 0) { - ThrowValueArgumentOutOfRange_NeedNonNegNumException(nameof(value)); + ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException(); } if (fromEnd) @@ -61,7 +61,7 @@ public static Index FromStart(int value) { if (value < 0) { - ThrowValueArgumentOutOfRange_NeedNonNegNumException(nameof(value)); + ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException(); } return new Index(value); @@ -74,7 +74,7 @@ public static Index FromEnd(int value) { if (value < 0) { - ThrowValueArgumentOutOfRange_NeedNonNegNumException(nameof(value)); + ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException(); } return new Index(~value); @@ -146,9 +146,13 @@ private string ToStringFromEnd() return '^' + Value.ToString(); } - private static void ThrowValueArgumentOutOfRange_NeedNonNegNumException(string paramName) + private static class ThrowHelper { - throw new ArgumentOutOfRangeException(paramName, "value must be non-negative"); + [DoesNotReturn] + public static void ThrowValueArgumentOutOfRange_NeedNonNegNumException() + { + throw new ArgumentOutOfRangeException("value", "Non-negative number required."); + } } } } \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/System.Range.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/System.Range.cs index 9cf5537..45c6cf1 100644 --- a/src/PolySharp.SourceGenerators/EmbeddedResources/System.Range.cs +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/System.Range.cs @@ -48,10 +48,16 @@ value is Range r && public bool Equals(Range other) => other.Start.Equals(Start) && other.End.Equals(End); /// Returns the hash code for this instance. - public override int GetHashCode() => Start.GetHashCode() * 31 + End.GetHashCode(); + public override int GetHashCode() + { + return HashHelpers.Combine(Start.GetHashCode(), End.GetHashCode()); + } /// Converts the value of the current Range object to its equivalent string representation. - public override string ToString() => Start.ToString() + ".." + End.ToString(); + public override string ToString() + { + return Start.ToString() + ".." + End.ToString(); + } /// Create a Range object starting from start index to the end of the collection. public static Range StartAt(Index start) => new Range(start, Index.End); @@ -88,15 +94,28 @@ value is Range r && if ((uint)end > (uint)length || (uint)start > (uint)end) { - ThrowArgumentOutOfRangeException(nameof(length)); + ThrowHelper.ThrowArgumentOutOfRangeException(); } return (start, end - start); } - private static void ThrowArgumentOutOfRangeException(string paramName) + private static class HashHelpers { - throw new ArgumentOutOfRangeException(paramName); + public static int Combine(int h1, int h2) + { + uint rol5 = ((uint)h1 << 5) | ((uint)h1 >> 27); + return ((int)rol5 + h1) ^ h2; + } + } + + private static class ThrowHelper + { + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeException() + { + throw new ArgumentOutOfRangeException("length"); + } } } } \ No newline at end of file