Skip to content

Commit

Permalink
Update Index and Range polyfills to match runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Oct 23, 2022
1 parent 5578d33 commit 72a0290
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
16 changes: 10 additions & 6 deletions src/PolySharp.SourceGenerators/EmbeddedResources/System.Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System
{
/// <summary>Represent a type can be used to index a collection either from the start or the end.</summary>
Expand All @@ -33,7 +33,7 @@ public Index(int value, bool fromEnd = false)
{
if (value < 0)
{
ThrowValueArgumentOutOfRange_NeedNonNegNumException(nameof(value));
ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException();
}

if (fromEnd)
Expand Down Expand Up @@ -61,7 +61,7 @@ public static Index FromStart(int value)
{
if (value < 0)
{
ThrowValueArgumentOutOfRange_NeedNonNegNumException(nameof(value));
ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException();
}

return new Index(value);
Expand All @@ -74,7 +74,7 @@ public static Index FromEnd(int value)
{
if (value < 0)
{
ThrowValueArgumentOutOfRange_NeedNonNegNumException(nameof(value));
ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException();
}

return new Index(~value);
Expand Down Expand Up @@ -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.");
}
}
}
}
29 changes: 24 additions & 5 deletions src/PolySharp.SourceGenerators/EmbeddedResources/System.Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ value is Range r &&
public bool Equals(Range other) => other.Start.Equals(Start) && other.End.Equals(End);

/// <summary>Returns the hash code for this instance.</summary>
public override int GetHashCode() => Start.GetHashCode() * 31 + End.GetHashCode();
public override int GetHashCode()
{
return HashHelpers.Combine(Start.GetHashCode(), End.GetHashCode());
}

/// <summary>Converts the value of the current Range object to its equivalent string representation.</summary>
public override string ToString() => Start.ToString() + ".." + End.ToString();
public override string ToString()
{
return Start.ToString() + ".." + End.ToString();
}

/// <summary>Create a Range object starting from start index to the end of the collection.</summary>
public static Range StartAt(Index start) => new Range(start, Index.End);
Expand Down Expand Up @@ -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");
}
}
}
}

0 comments on commit 72a0290

Please sign in to comment.