Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poly fill Index and Range types #104170

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if (NETSTANDARD2_0 || NETFRAMEWORK)
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
namespace System.Runtime.CompilerServices
{
internal static class RuntimeHelpers
{
/// <summary>
/// Slices the specified array using the specified range.
/// </summary>
internal static T[] GetSubArray<T>(T[] array, Range range)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}

(int offset, int length) = range.GetOffsetAndLength(array.Length);

if (default(T) != null || typeof(T[]) == array.GetType())
{
// We know the type of the array to be exactly T[].

if (length == 0)
{
return Array.Empty<T>();
}

var dest = new T[length];
Array.Copy(array, offset, dest, 0, length);
return dest;
}
else
{
// The array is actually a U[] where U:T.
var dest = (T[])Array.CreateInstance(array.GetType().GetElementType(), length);
Array.Copy(array, offset, dest, 0, length);
return dest;
}
}
}
}
#endif

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Index))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Range))]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum);$(NetCoreAppCurrent)</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;$(NetFrameworkMinimum);$(NetCoreAppCurrent)</TargetFrameworks>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
Expand All @@ -12,6 +12,18 @@
<Compile Include="Microsoft.Bcl.Memory.Forwards.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' Or '$(TargetFramework)' == 'netstandard2.1'">
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
<Compile Include="Microsoft.Bcl.Memory.Forwards.ns21.cs" />
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' Or '$(TargetFramework)' == 'netstandard2.0'">
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
<Compile Include="Microsoft.Bcl.Memory.ns20.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PackageReference Include="System.ValueTuple" Version="$(SystemValueTupleVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// ------------------------------------------------------------------------------
// Changes to this file must follow the https://aka.ms/api-review process.
// ------------------------------------------------------------------------------

namespace System
{
public readonly partial struct Index : System.IEquatable<System.Index>
{
private readonly int _dummyPrimitive;
public Index(int value, bool fromEnd = false) { throw null; }
public static System.Index End { get { throw null; } }
public bool IsFromEnd { get { throw null; } }
public static System.Index Start { get { throw null; } }
public int Value { get { throw null; } }
public bool Equals(System.Index other) { throw null; }
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? value) { throw null; }
public static System.Index FromEnd(int value) { throw null; }
public static System.Index FromStart(int value) { throw null; }
public override int GetHashCode() { throw null; }
public int GetOffset(int length) { throw null; }
public static implicit operator System.Index (int value) { throw null; }
public override string ToString() { throw null; }
}
public readonly partial struct Range : System.IEquatable<System.Range>
{
private readonly int _dummyPrimitive;
public Range(System.Index start, System.Index end) { throw null; }
public static System.Range All { get { throw null; } }
public System.Index End { get { throw null; } }
public System.Index Start { get { throw null; } }
public static System.Range EndAt(System.Index end) { throw null; }
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? value) { throw null; }
public bool Equals(System.Range other) { throw null; }
public override int GetHashCode() { throw null; }
public (int Offset, int Length) GetOffsetAndLength(int length) { throw null; }
public static System.Range StartAt(System.Index start) { throw null; }
public override string ToString() { throw null; }
}
}
18 changes: 16 additions & 2 deletions src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum);$(NetCoreAppCurrent)</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;$(NetFrameworkMinimum);$(NetCoreAppCurrent)</TargetFrameworks>
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>true</IsPackable>
<!-- Disabling baseline validation since this is a brand new package.
Expand All @@ -14,6 +14,7 @@
Commonly Used Types:
System.Buffers.Text.Base64Url
</PackageDescription>
<DefineConstants>$(DefineConstants);MICROSOFT_BCL_MEMORY</DefineConstants>
</PropertyGroup>

<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
Expand All @@ -25,6 +26,10 @@
<Compile Include="..\ref\Microsoft.Bcl.Memory.Forwards.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' Or '$(TargetFramework)' == 'netstandard2.1'">
<Compile Include="..\ref\Microsoft.Bcl.Memory.Forwards.ns21.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="..\..\System.Private.CoreLib\src\System\Buffers\Text\Base64Helper\Base64Helper.cs">
<Link>System\Buffers\Text\Base64Helper\Base64Helper.cs</Link>
Expand All @@ -49,8 +54,17 @@
</Compile>
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' Or '$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="..\..\System.Private.CoreLib\src\System\Index.cs" />
<Compile Include="..\..\System.Private.CoreLib\src\System\Range.cs" />
<Compile Include="..\..\System.Private.CoreLib\src\System\Numerics\Hashing\HashHelpers.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
</ItemGroup>


<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PackageReference Include="System.ValueTuple" Version="$(SystemValueTupleVersion)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@
<Compile Include="..\..\System.Memory\tests\Base64Url\Base64UrlValidationUnitTests.cs">
<Link>System\Memory\Base64Url\Base64UrlValidationUnitTests.cs</Link>
</Compile>
<Compile Include="..\..\System.Runtime\tests\System.Runtime.Tests\System\RangeTests.cs">
<Link>System\RangeTests.cs</Link>
</Compile>
<Compile Include="..\..\System.Runtime\tests\System.Runtime.Tests\System\IndexTests.cs">
<Link>System\IndexTests.cs</Link>
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
</Compile>
<Compile Include="..\..\Common\tests\System\Runtime\CompilerServices\RuntimeHelpers.cs">
<Link>System\Runtime\CompilerServices\RuntimeHelpers.cs</Link>
</Compile>
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<ProjectReference Include="..\src\Microsoft.Bcl.Memory.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace System
/// int lastElement = someArray[^1]; // lastElement = 5
/// </code>
/// </remarks>
#if SYSTEM_PRIVATE_CORELIB
#if SYSTEM_PRIVATE_CORELIB || MICROSOFT_BCL_MEMORY
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
public
#else
internal
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace System
/// int[] subArray2 = someArray[1..^0]; // { 2, 3, 4, 5 }
/// </code>
/// </remarks>
#if SYSTEM_PRIVATE_CORELIB
#if SYSTEM_PRIVATE_CORELIB || MICROSOFT_BCL_MEMORY
public
#else
internal
Expand Down