Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Added System.Memory.Polyfill Project (#2339)
Browse files Browse the repository at this point in the history
* Bug fix in BufferWriter

* Added System.Memory.Polyfill Project

* PR review feedback and build fix
  • Loading branch information
KrzysztofCwalina authored Jun 5, 2018
1 parent 66fca9c commit 697d518
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
30 changes: 30 additions & 0 deletions corefxlab.sln
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Buffers.ReaderWriter
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Buffers.ReaderWriter.Tests", "tests\System.Buffers.ReaderWriter.Tests\System.Buffers.ReaderWriter.Tests.csproj", "{D9FFEC52-B701-4DB5-969C-BAC4F8EB220C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Memory.Polyfill", "src\System.Memory.Polyfill\System.Memory.Polyfill.csproj", "{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Memory.Polyfill.Tests", "tests\System.Memory.Polyfill.Tests\System.Memory.Polyfill.Tests.csproj", "{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -556,6 +560,30 @@ Global
{D9FFEC52-B701-4DB5-969C-BAC4F8EB220C}.Release|x64.Build.0 = Release|Any CPU
{D9FFEC52-B701-4DB5-969C-BAC4F8EB220C}.Release|x86.ActiveCfg = Release|Any CPU
{D9FFEC52-B701-4DB5-969C-BAC4F8EB220C}.Release|x86.Build.0 = Release|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Debug|x64.ActiveCfg = Debug|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Debug|x64.Build.0 = Debug|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Debug|x86.ActiveCfg = Debug|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Debug|x86.Build.0 = Debug|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Release|Any CPU.Build.0 = Release|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Release|x64.ActiveCfg = Release|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Release|x64.Build.0 = Release|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Release|x86.ActiveCfg = Release|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Release|x86.Build.0 = Release|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Debug|x64.ActiveCfg = Debug|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Debug|x64.Build.0 = Debug|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Debug|x86.ActiveCfg = Debug|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Debug|x86.Build.0 = Debug|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Release|Any CPU.Build.0 = Release|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Release|x64.ActiveCfg = Release|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Release|x64.Build.0 = Release|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Release|x86.ActiveCfg = Release|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -599,6 +627,8 @@ Global
{916370AB-B0D3-4136-850B-AA12FAB23ECD} = {4B000021-5278-4F2A-B734-DE49F55D4024}
{C5F9D191-CA3B-4648-B8A9-62E33B4622EB} = {4B000021-5278-4F2A-B734-DE49F55D4024}
{D9FFEC52-B701-4DB5-969C-BAC4F8EB220C} = {3079E458-D0E6-4F99-8CAB-80011D35C7DA}
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1} = {4B000021-5278-4F2A-B734-DE49F55D4024}
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C} = {3079E458-D0E6-4F99-8CAB-80011D35C7DA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9DD4022C-A010-4A9B-BCC5-171566D4CB17}
Expand Down
13 changes: 13 additions & 0 deletions src/System.Memory.Polyfill/System.Memory.Polyfill.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\tools\common.props" />
<PropertyGroup>
<Description>Adapters for Span APIs</Description>
<TargetFrameworks>netstandard1.3;netcoreapp2.1</TargetFrameworks>
<PackageTags>Span Memory Adapter</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
<PackageReference Include="System.Buffers" Version="$(SystemMemoryVersion)" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="$(SystemCompilerServicesUnsafeVersion)" />
</ItemGroup>
</Project>
18 changes: 18 additions & 0 deletions src/System.Memory.Polyfill/System/Parse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System
{
public static partial class Int32Polyfill
{
public static bool TryParse(ReadOnlySpan<char> buffer, out int value)
{
#if NETCOREAPP2_1
return int.TryParse(buffer, out value);
#else
return int.TryParse(buffer.ToString(), out value);
#endif
}
}
}
32 changes: 32 additions & 0 deletions src/System.Memory.Polyfill/System/Stream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.IO;

namespace System
{
public static partial class MemoryPolyfill
{
public static int Read(this Stream stream, Span<byte> buffer)
{
#if NETCOREAPP2_1
return stream.Read(buffer);
#else
byte[] pooled = null;
try
{
pooled = ArrayPool<byte>.Shared.Rent(buffer.Length);
int read = stream.Read(pooled, 0, pooled.Length);
pooled.AsSpan(0, read).CopyTo(buffer);
return read;
}
finally
{
if(pooled != null) ArrayPool<byte>.Shared.Return(pooled);
}
#endif
}
}
}
19 changes: 19 additions & 0 deletions tests/System.Memory.Polyfill.Tests/Int32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;
using Xunit;

namespace System.Polyfill.Tests
{
public class Int32Tests
{
[Fact]
public void Int32TryParse()
{
ReadOnlySpan<char> span = int.MaxValue.ToString().ToCharArray().AsSpan();
Assert.True(Int32Polyfill.TryParse(span, out int value));
Assert.Equal(int.MaxValue, value);
}
}
}
28 changes: 28 additions & 0 deletions tests/System.Memory.Polyfill.Tests/StreamTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;
using Xunit;

namespace System.Polyfill.Tests
{
public class StreamTests
{
[Fact]
public void StreamRead()
{
var buffer = new byte[100];
for (int i = 0; i < buffer.Length; i++) buffer[i] = (byte)i;

var stream = new MemoryStream(buffer);
var span = new Span<byte>(new byte[100]);

var read = stream.Read(span);
Assert.Equal(buffer.Length, read);
for (int i = 0; i < buffer.Length; i++)
{
Assert.Equal(i, span[i]);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\tools\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp2.1</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetsWindows)' == 'true'">
<TargetFrameworks>net46;netcoreapp2.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
<!-- Project references -->
<ItemGroup>
<ProjectReference Include="..\..\src\System.Memory.Polyfill\System.Memory.Polyfill.csproj" />
</ItemGroup>
<!-- register for test discovery in Visual Studio -->
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>

0 comments on commit 697d518

Please sign in to comment.