Skip to content

Commit 72a0290

Browse files
committed
Update Index and Range polyfills to match runtime
1 parent 5578d33 commit 72a0290

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/PolySharp.SourceGenerators/EmbeddedResources/System.Index.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
using System.Diagnostics.CodeAnalysis;
99
using System.Runtime.CompilerServices;
10-
10+
1111
namespace System
1212
{
1313
/// <summary>Represent a type can be used to index a collection either from the start or the end.</summary>
@@ -33,7 +33,7 @@ public Index(int value, bool fromEnd = false)
3333
{
3434
if (value < 0)
3535
{
36-
ThrowValueArgumentOutOfRange_NeedNonNegNumException(nameof(value));
36+
ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException();
3737
}
3838

3939
if (fromEnd)
@@ -61,7 +61,7 @@ public static Index FromStart(int value)
6161
{
6262
if (value < 0)
6363
{
64-
ThrowValueArgumentOutOfRange_NeedNonNegNumException(nameof(value));
64+
ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException();
6565
}
6666

6767
return new Index(value);
@@ -74,7 +74,7 @@ public static Index FromEnd(int value)
7474
{
7575
if (value < 0)
7676
{
77-
ThrowValueArgumentOutOfRange_NeedNonNegNumException(nameof(value));
77+
ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException();
7878
}
7979

8080
return new Index(~value);
@@ -146,9 +146,13 @@ private string ToStringFromEnd()
146146
return '^' + Value.ToString();
147147
}
148148

149-
private static void ThrowValueArgumentOutOfRange_NeedNonNegNumException(string paramName)
149+
private static class ThrowHelper
150150
{
151-
throw new ArgumentOutOfRangeException(paramName, "value must be non-negative");
151+
[DoesNotReturn]
152+
public static void ThrowValueArgumentOutOfRange_NeedNonNegNumException()
153+
{
154+
throw new ArgumentOutOfRangeException("value", "Non-negative number required.");
155+
}
152156
}
153157
}
154158
}

src/PolySharp.SourceGenerators/EmbeddedResources/System.Range.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ value is Range r &&
4848
public bool Equals(Range other) => other.Start.Equals(Start) && other.End.Equals(End);
4949

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

5356
/// <summary>Converts the value of the current Range object to its equivalent string representation.</summary>
54-
public override string ToString() => Start.ToString() + ".." + End.ToString();
57+
public override string ToString()
58+
{
59+
return Start.ToString() + ".." + End.ToString();
60+
}
5561

5662
/// <summary>Create a Range object starting from start index to the end of the collection.</summary>
5763
public static Range StartAt(Index start) => new Range(start, Index.End);
@@ -88,15 +94,28 @@ value is Range r &&
8894

8995
if ((uint)end > (uint)length || (uint)start > (uint)end)
9096
{
91-
ThrowArgumentOutOfRangeException(nameof(length));
97+
ThrowHelper.ThrowArgumentOutOfRangeException();
9298
}
9399

94100
return (start, end - start);
95101
}
96102

97-
private static void ThrowArgumentOutOfRangeException(string paramName)
103+
private static class HashHelpers
98104
{
99-
throw new ArgumentOutOfRangeException(paramName);
105+
public static int Combine(int h1, int h2)
106+
{
107+
uint rol5 = ((uint)h1 << 5) | ((uint)h1 >> 27);
108+
return ((int)rol5 + h1) ^ h2;
109+
}
110+
}
111+
112+
private static class ThrowHelper
113+
{
114+
[DoesNotReturn]
115+
public static void ThrowArgumentOutOfRangeException()
116+
{
117+
throw new ArgumentOutOfRangeException("length");
118+
}
100119
}
101120
}
102121
}

0 commit comments

Comments
 (0)