Skip to content

Commit

Permalink
Add support for including System.Index and System.Range downlevel wit…
Browse files Browse the repository at this point in the history
…hin dotnet/runtime (dotnet#87840)

Co-authored-by: Stephen Toub <[email protected]>
  • Loading branch information
jkoritzinsky and stephentoub authored Jun 28, 2023
1 parent c06d77a commit 257c3c1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/libraries/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@
</When>
</Choose>

<ItemGroup Condition="'$(IncludeIndexRangeTypes)' == 'true' and '$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="$(CoreLibSharedDir)System\Index.cs" Link="System\Index.cs" />
<Compile Include="$(CoreLibSharedDir)System\Range.cs" Link="System\Range.cs" />
<Compile Include="$(CoreLibSharedDir)System\Numerics\Hashing.cs" Link="System\Numerics\Hashing.cs" />
</ItemGroup>

<PropertyGroup>
<SkipLocalsInit Condition="'$(SkipLocalsInit)' == '' and '$(MSBuildProjectExtension)' == '.csproj' and '$(IsNETCoreAppSrc)' == 'true' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp'">true</SkipLocalsInit>
</PropertyGroup>
Expand Down
22 changes: 18 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ namespace System
/// int lastElement = someArray[^1]; // lastElement = 5
/// </code>
/// </remarks>
public readonly struct Index : IEquatable<Index>
#if SYSTEM_PRIVATE_CORELIB
public
#else
internal
#endif
readonly struct Index : IEquatable<Index>
{
private readonly int _value;

Expand All @@ -30,7 +35,7 @@ public Index(int value, bool fromEnd = false)
{
if (value < 0)
{
ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException();
ThrowValueArgumentOutOfRange_NeedNonNegNumException();
}

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

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

return new Index(~value);
Expand Down Expand Up @@ -138,6 +143,15 @@ public override string ToString()
return ((uint)Value).ToString();
}

private static void ThrowValueArgumentOutOfRange_NeedNonNegNumException()
{
#if SYSTEM_PRIVATE_CORELIB
throw new ArgumentOutOfRangeException("value", SR.ArgumentOutOfRange_NeedNonNegNum);
#else
throw new ArgumentOutOfRangeException("value", "value must be non-negative");
#endif
}

private string ToStringFromEnd()
{
#if (!NETSTANDARD2_0 && !NETFRAMEWORK)
Expand Down
14 changes: 12 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ namespace System
/// int[] subArray2 = someArray[1..^0]; // { 2, 3, 4, 5 }
/// </code>
/// </remarks>
public readonly struct Range : IEquatable<Range>
#if SYSTEM_PRIVATE_CORELIB
public
#else
internal
#endif
readonly struct Range : IEquatable<Range>
{
/// <summary>Represent the inclusive start index of the Range.</summary>
public Index Start { get; }
Expand Down Expand Up @@ -126,10 +131,15 @@ public override string ToString()

if ((uint)end > (uint)length || (uint)start > (uint)end)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length);
ThrowArgumentOutOfRangeException();
}

return (start, end - start);
}

private static void ThrowArgumentOutOfRangeException()
{
throw new ArgumentOutOfRangeException("length");
}
}
}

0 comments on commit 257c3c1

Please sign in to comment.