-
Notifications
You must be signed in to change notification settings - Fork 344
Added System.Memory.Polyfill Project #2339
Changes from all commits
c8d31c4
8ee6979
33da66f
ee23d6b
132efef
f6d44ca
954f079
3c3b978
6b160a7
a5dfae9
b89ba55
23415be
8a5e662
42e41c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may want to avoid using the versioned define as it makes it more difficult if you decide to retarget. Instead use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this api exists only in netcoreapp 2.1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You aren't defining targeting here, you're just ifdefing the code. Imagine that you wanted to cross-compile for netcoreapp2.2 as well in the future, you'd need to touch this ifdef for no reason. I think you're more likely to cross-compile for a future version of netcoreapp than you are an older version that doesn't have this API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I agree it would work today, but it won't if we ever add new targets. Correct? |
||
#else | ||
return int.TryParse(buffer.ToString(), out value); | ||
#endif | ||
} | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. StreamPolyfill instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want to minimize the number of extensions types. |
||
{ | ||
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 | ||
} | ||
} | ||
} |
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); | ||
} | ||
} | ||
} |
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would,'t work for implicit cast from
Span<char>
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not? It's not an extension method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, true... Is a bit ugly then :)
Static extension method would be better dotnet/csharplang#192 though alas not a thing...