This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added System.Memory.Polyfill Project (#2339)
* Bug fix in BufferWriter * Added System.Memory.Polyfill Project * PR review feedback and build fix
- Loading branch information
1 parent
66fca9c
commit 697d518
Showing
7 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/System.Memory.Polyfill.Tests/System.Memory.Polyfill.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |