diff --git a/corefxlab.sln b/corefxlab.sln index 141d2d1ccd4..b82bdeaeea9 100644 --- a/corefxlab.sln +++ b/corefxlab.sln @@ -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 @@ -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 @@ -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} diff --git a/src/System.Memory.Polyfill/System.Memory.Polyfill.csproj b/src/System.Memory.Polyfill/System.Memory.Polyfill.csproj new file mode 100644 index 00000000000..a33d3f88699 --- /dev/null +++ b/src/System.Memory.Polyfill/System.Memory.Polyfill.csproj @@ -0,0 +1,13 @@ + + + + Adapters for Span APIs + netstandard1.3;netcoreapp2.1 + Span Memory Adapter + + + + + + + diff --git a/src/System.Memory.Polyfill/System/Parse.cs b/src/System.Memory.Polyfill/System/Parse.cs new file mode 100644 index 00000000000..ba210af9cca --- /dev/null +++ b/src/System.Memory.Polyfill/System/Parse.cs @@ -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 buffer, out int value) + { +#if NETCOREAPP2_1 + return int.TryParse(buffer, out value); +#else + return int.TryParse(buffer.ToString(), out value); +#endif + } + } +} diff --git a/src/System.Memory.Polyfill/System/Stream.cs b/src/System.Memory.Polyfill/System/Stream.cs new file mode 100644 index 00000000000..e1d046326e6 --- /dev/null +++ b/src/System.Memory.Polyfill/System/Stream.cs @@ -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 buffer) + { +#if NETCOREAPP2_1 + return stream.Read(buffer); +#else + byte[] pooled = null; + try + { + pooled = ArrayPool.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.Shared.Return(pooled); + } +#endif + } + } +} diff --git a/tests/System.Memory.Polyfill.Tests/Int32.cs b/tests/System.Memory.Polyfill.Tests/Int32.cs new file mode 100644 index 00000000000..59275764edc --- /dev/null +++ b/tests/System.Memory.Polyfill.Tests/Int32.cs @@ -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 span = int.MaxValue.ToString().ToCharArray().AsSpan(); + Assert.True(Int32Polyfill.TryParse(span, out int value)); + Assert.Equal(int.MaxValue, value); + } + } +} diff --git a/tests/System.Memory.Polyfill.Tests/StreamTests.cs b/tests/System.Memory.Polyfill.Tests/StreamTests.cs new file mode 100644 index 00000000000..67a4f156890 --- /dev/null +++ b/tests/System.Memory.Polyfill.Tests/StreamTests.cs @@ -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(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]); + } + } + } +} diff --git a/tests/System.Memory.Polyfill.Tests/System.Memory.Polyfill.Tests.csproj b/tests/System.Memory.Polyfill.Tests/System.Memory.Polyfill.Tests.csproj new file mode 100644 index 00000000000..68de1e22c13 --- /dev/null +++ b/tests/System.Memory.Polyfill.Tests/System.Memory.Polyfill.Tests.csproj @@ -0,0 +1,21 @@ + + + + netcoreapp2.1 + + + net46;netcoreapp2.1 + + + + + + + + + + + + + +