Skip to content

Commit f425c6a

Browse files
authored
Add netstandard2.0 support (#9)
* Add netstandard2.0 support. * CI: only run tests for full framework on windows * CI: Need to remove --no-build for full framework tests
1 parent 3bb82df commit f425c6a

8 files changed

Lines changed: 46 additions & 12 deletions

File tree

.github/workflows/dotnet.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ jobs:
4040
- name: Tests
4141
run: dotnet test --no-build --configuration ${{ matrix.configuration }} src
4242
continue-on-error: ${{ matrix.os == 'macos-latest' }}
43+
- name: Tests (Full Framework)
44+
run: dotnet test --configuration ${{ matrix.configuration }} src/VCDiff.Tests/VCDiff.Tests.NetFx.csproj
45+
if: ${{ matrix.os == 'windows-latest' }}
4346
- name: Upload code coverage
4447
uses: codecov/codecov-action@v1.2.1
4548
with:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Import Project="VCDiff.Tests.csproj" />
4+
5+
<PropertyGroup>
6+
<TargetFrameworks>net462;net48</TargetFrameworks>
7+
</PropertyGroup>
8+
9+
</Project>

src/VCDiff.Tests/VCDiff.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netcoreapp3.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
55
<IsPackable>false</IsPackable>
6+
<LangVersion>latest</LangVersion>
67
</PropertyGroup>
78

89
<ItemGroup>

src/VCDiff/Encoders/BlockHash.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ private unsafe bool BlockContentsMatch(long block1, long tOffset, byte *sourcePt
302302
}
303303
}
304304
}
305-
#else
305+
#elif NETSTANDARD2_1
306306
int vectorSize = Vector<byte>.Count;
307307
if (lengthToExamine >= vectorSize)
308308
{
@@ -476,7 +476,7 @@ private unsafe long MatchingBytesToLeftSse2(long start, long tstart, byte* sourc
476476
[MethodImpl(MethodImplOptions.AggressiveInlining)]
477477
private unsafe long MatchingBytesToLeft(long start, long tstart, byte* sourcePtr, byte* targetPtr, ByteBuffer target, long maxBytes)
478478
{
479-
#if NETCOREAPP3_1 || NET5_0
479+
#if NETCOREAPP3_1 || NET5_0 || NET5_0_OR_GREATER
480480
if (Avx2.IsSupported) return MatchingBytesToLeftAvx2(start, tstart, sourcePtr, targetPtr, maxBytes);
481481
if (Sse2.IsSupported) return MatchingBytesToLeftSse2(start, tstart, sourcePtr, targetPtr, maxBytes);
482482
#endif
@@ -490,6 +490,7 @@ private unsafe long MatchingBytesToLeft(long start, long tstart, byte* sourcePtr
490490
var tBuf = target.AsSpan();
491491
var sBuf = source.AsSpan();
492492

493+
#if NETCOREAPP3_1 || NET5_0 || NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
493494
while (sindex >= vectorSize && tindex >= vectorSize && bytesFound <= maxBytes - vectorSize)
494495
{
495496
tindex -= vectorSize;
@@ -505,6 +506,7 @@ private unsafe long MatchingBytesToLeft(long start, long tstart, byte* sourcePtr
505506

506507
bytesFound += vectorSize;
507508
}
509+
#endif
508510

509511
while (bytesFound < maxBytes)
510512
{
@@ -624,6 +626,8 @@ private unsafe long MatchingBytesToRight(long end, long tstart, byte* sourcePtr,
624626
long trgLength = target.Length;
625627
byte* tPtr = targetPtr;
626628
byte* sPtr = sourcePtr;
629+
630+
#if NETCOREAPP3_1 || NET5_0 || NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
627631
int vectorSize = Vector<byte>.Count;
628632
var tBuf = target.AsSpan();
629633
var sBuf = source.AsSpan();
@@ -641,6 +645,7 @@ private unsafe long MatchingBytesToRight(long end, long tstart, byte* sourcePtr,
641645
tindex += vectorSize;
642646
sindex += vectorSize;
643647
}
648+
#endif
644649

645650
while (bytesFound < maxBytes)
646651
{

src/VCDiff/Encoders/InstructionMap.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void Add(byte first, byte inst, byte size, byte mode, byte opcode)
114114
private int[] NewSizeOpcodeArray(int size)
115115
{
116116
int[] nn = new int[size];
117-
Array.Fill(nn, CodeTable.kNoOpcode);
117+
new Span<int>(nn).Fill(CodeTable.kNoOpcode);
118118
return nn;
119119
}
120120

@@ -147,8 +147,7 @@ public OpcodeMap(int numInstAndModes, int maxSize)
147147
this.maxSize = maxSize + 1;
148148
this.numInstAndModes = numInstAndModes;
149149
opcodes = new int[numInstAndModes * this.maxSize];
150-
151-
Array.Fill(opcodes, CodeTable.kNoOpcode);
150+
new Span<int>(opcodes).Fill(CodeTable.kNoOpcode);
152151
}
153152

154153
public void Add(byte inst, byte size, byte mode, byte opcode)

src/VCDiff/Shared/ByteBuffer.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace VCDiff.Shared
1313
public class ByteBuffer : IByteBuffer, IDisposable
1414
{
1515
private MemoryHandle? byteHandle;
16-
private unsafe byte* bytePtr;
16+
private unsafe byte* bytePtr;
1717
private int length;
1818
private int offset;
1919

@@ -31,7 +31,7 @@ private ByteBuffer()
3131
public unsafe ByteBuffer(byte[] bytes)
3232
{
3333
offset = 0;
34-
var memory = bytes != null ? new Memory<byte>(bytes) : Memory<byte>.Empty;
34+
var memory = bytes != null ? new Memory<byte>(bytes) : Memory<byte>.Empty;
3535
this.byteHandle = memory.Pin();
3636
CreateFromPointer((byte*)this.byteHandle.Value.Pointer, memory.Length);
3737
}
@@ -65,9 +65,14 @@ private unsafe void CreateFromPointer(byte* pointer, int length)
6565
this.bytePtr = pointer;
6666
this.length = length;
6767
}
68-
68+
69+
#if NET5_0 || NET5_0_OR_GREATER || NETCOREAPP3_1 || NETSTANDARD2_1_OR_GREATER
6970
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7071
public unsafe Span<byte> AsSpan() => MemoryMarshal.CreateSpan(ref Unsafe.AsRef<byte>(bytePtr), length);
72+
#else
73+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
74+
public unsafe Span<byte> AsSpan() => new Span<byte>(bytePtr, length);
75+
#endif
7176

7277
/// <summary>
7378
/// Dangerously gets the byte pointer.
@@ -104,7 +109,8 @@ public int Position
104109
set => offset = value;
105110
}
106111

107-
public int Length {
112+
public int Length
113+
{
108114
[MethodImpl(MethodImplOptions.AggressiveInlining)]
109115
get => length;
110116
}
@@ -119,7 +125,11 @@ public int Length {
119125
public unsafe Span<byte> PeekBytes(int len)
120126
{
121127
int sliceLen = offset + len > this.length ? this.length - offset : len;
128+
#if NET5_0 || NET5_0_OR_GREATER || NETCOREAPP3_1 || NETSTANDARD2_1_OR_GREATER
122129
return MemoryMarshal.CreateSpan(ref Unsafe.AsRef<byte>(bytePtr + offset), sliceLen);
130+
#else
131+
return new Span<byte>(bytePtr + offset, sliceLen);
132+
#endif
123133
}
124134

125135
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/VCDiff/Shared/Intrinsics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static unsafe void FillArrayVectorized(long* first, int numValues, long v
6565
}
6666
#else
6767
// Accelerate via loop unrolled solution.
68-
MemoryMarshal.CreateSpan(ref Unsafe.AsRef<long>((void*) first), numValues).Fill(value);
68+
new Span<long>((void*)first, numValues).Fill(value);
6969
#endif
7070
}
7171

src/VCDiff/VCDiff.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
<PropertyGroup>
55
<RootNamespace>VCDiff</RootNamespace>
6-
<TargetFrameworks>netstandard2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
6+
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
77
<Nullable>enable</Nullable>
88
<Version>4.0.1</Version>
99
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
11+
<NoWarn>$(NoWarn);CS1591</NoWarn>
1112
</PropertyGroup>
1213

1314
<PropertyGroup>
@@ -20,8 +21,14 @@
2021
<IncludeSymbols>true</IncludeSymbols>
2122
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2223
</PropertyGroup>
24+
2325
<ItemGroup>
2426
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.2.0" />
25-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
27+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" Condition="$(TargetFramework.StartsWith('netstandard'))" />
28+
<PackageReference Include="PolyShim" Version="1.8.0">
29+
<PrivateAssets>all</PrivateAssets>
30+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
31+
</PackageReference>
2632
</ItemGroup>
33+
2734
</Project>

0 commit comments

Comments
 (0)