Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion QRCoder/Attributes/NotNullWhenAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if !NETCOREAPP
#if !NETCOREAPP && !NETSTANDARD2_1
namespace System.Diagnostics.CodeAnalysis;

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions QRCoder/PngByteQRCode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1
#if HAS_SPAN
using System.Buffers;
#endif
using System;
Expand Down Expand Up @@ -41,7 +41,7 @@ public byte[] GetGraphic(int pixelsPerModule, bool drawQuietZones = true)
png.WriteHeader(size, size, 1, PngBuilder.ColorType.Greyscale);
var scanLines = DrawScanlines(pixelsPerModule, drawQuietZones);
png.WriteScanlines(scanLines);
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1
#if HAS_SPAN
ArrayPool<byte>.Shared.Return(scanLines.Array!);
#endif
png.WriteEnd();
Expand Down Expand Up @@ -77,7 +77,7 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgba, byte[] light
png.WritePalette(darkColorRgba, lightColorRgba);
var scanLines = DrawScanlines(pixelsPerModule, drawQuietZones);
png.WriteScanlines(scanLines);
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1
#if HAS_SPAN
ArrayPool<byte>.Shared.Return(scanLines.Array!);
#endif
png.WriteEnd();
Expand All @@ -97,7 +97,7 @@ private ArraySegment<byte> DrawScanlines(int pixelsPerModule, bool drawQuietZone
var quietZoneOffset = (drawQuietZones ? 0 : 4);
var bytesPerScanline = (matrixSize * pixelsPerModule + 7) / 8 + 1; // A monochrome scanline is one byte for filter type then one bit per pixel.
var scanLinesLength = bytesPerScanline * matrixSize * pixelsPerModule;
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1
#if HAS_SPAN
var scanlines = ArrayPool<byte>.Shared.Rent(scanLinesLength);
Array.Clear(scanlines, 0, scanLinesLength);
#else
Expand Down
2 changes: 1 addition & 1 deletion QRCoder/QRCodeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public byte[] GetRawData(Compression compressMode)
try
{
//Add header - signature ("QRR")
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1
#if HAS_SPAN
targetStream.Write([0x51, 0x52, 0x52, 0x00]);
#else
targetStream.Write(new byte[] { 0x51, 0x52, 0x52, 0x00 }, 0, 4);
Expand Down
26 changes: 13 additions & 13 deletions QRCoder/QRCodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1
#if HAS_SPAN
using System.Buffers;
#endif
using System.Collections;
Expand Down Expand Up @@ -586,7 +586,7 @@ private static void TrimLeadingZeros(BitArray fStrEcc, ref int index, ref int co

private static void ShiftTowardsBit0(BitArray fStrEcc, int num)
{
#if NETCOREAPP
#if HAS_SPAN
fStrEcc.RightShift(num); // Shift towards bit 0
#else
for (var i = 0; i < fStrEcc.Length - num; i++)
Expand All @@ -598,7 +598,7 @@ private static void ShiftTowardsBit0(BitArray fStrEcc, int num)

private static void ShiftAwayFromBit0(BitArray fStrEcc, int num)
{
#if NETCOREAPP
#if HAS_SPAN
fStrEcc.LeftShift(num); // Shift away from bit 0
#else
for (var i = fStrEcc.Length - 1; i >= num; i--)
Expand Down Expand Up @@ -697,7 +697,7 @@ private static ArraySegment<byte> CalculateECCWords(BitArray bitArray, int offse
generatorPolynom.Dispose();

// Convert the resulting polynomial into a byte array representing the ECC codewords.
#if NETCOREAPP
#if HAS_SPAN
var array = ArrayPool<byte>.Shared.Rent(leadTermSource.Count);
var ret = new ArraySegment<byte>(array, 0, leadTermSource.Count);
#else
Expand Down Expand Up @@ -1009,7 +1009,7 @@ private static BitArray PlainTextToBinaryNumeric(string plainText)
for (int i = 0; i < plainText.Length - 2; i += 3)
{
// Parse the next three characters as a decimal integer.
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1
#if HAS_SPAN
var dec = int.Parse(plainText.AsSpan(i, 3), NumberStyles.None, CultureInfo.InvariantCulture);
#else
var dec = int.Parse(plainText.Substring(i, 3), NumberStyles.None, CultureInfo.InvariantCulture);
Expand All @@ -1021,7 +1021,7 @@ private static BitArray PlainTextToBinaryNumeric(string plainText)
// Handle any remaining digits if the total number is not a multiple of three.
if (plainText.Length % 3 == 2) // Two remaining digits are encoded in 7 bits.
{
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1
#if HAS_SPAN
var dec = int.Parse(plainText.AsSpan(plainText.Length / 3 * 3, 2), NumberStyles.None, CultureInfo.InvariantCulture);
#else
var dec = int.Parse(plainText.Substring(plainText.Length / 3 * 3, 2), NumberStyles.None, CultureInfo.InvariantCulture);
Expand All @@ -1030,7 +1030,7 @@ private static BitArray PlainTextToBinaryNumeric(string plainText)
}
else if (plainText.Length % 3 == 1) // One remaining digit is encoded in 4 bits.
{
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1
#if HAS_SPAN
var dec = int.Parse(plainText.AsSpan(plainText.Length / 3 * 3, 1), NumberStyles.None, CultureInfo.InvariantCulture);
#else
var dec = int.Parse(plainText.Substring(plainText.Length / 3 * 3, 1), NumberStyles.None, CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -1101,7 +1101,7 @@ private static BitArray PlainTextToBinaryByte(string plainText, EciMode eciMode,
}
}

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1
#if HAS_SPAN
// We can use stackalloc for small arrays to prevent heap allocations
const int MAX_STACK_SIZE_IN_BYTES = 512;

Expand Down Expand Up @@ -1132,7 +1132,7 @@ private static BitArray PlainTextToBinaryByte(string plainText, EciMode eciMode,
bitArray = ToBitArray(codeBytes);
}

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1
#if HAS_SPAN
if (bufferFromPool != null)
ArrayPool<byte>.Shared.Return(bufferFromPool);
#endif
Expand All @@ -1148,7 +1148,7 @@ private static BitArray PlainTextToBinaryByte(string plainText, EciMode eciMode,
/// <param name="prefixZeros">The number of leading zeros to prepend to the resulting BitArray.</param>
/// <returns>A BitArray representing the bits of the input byteArray, with optional leading zeros.</returns>
private static BitArray ToBitArray(
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1
#if HAS_SPAN
ReadOnlySpan<byte> byteArray, // byte[] has an implicit cast to ReadOnlySpan<byte>
#else
byte[] byteArray,
Expand Down Expand Up @@ -1250,7 +1250,7 @@ private static Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polyno
}

// Identify and merge terms with the same exponent.
#if NETCOREAPP
#if NET5_0_OR_GREATER
var toGlue = GetNotUniqueExponents(resultPolynom, resultPolynom.Count <= 128 ? stackalloc int[128].Slice(0, resultPolynom.Count) : new int[resultPolynom.Count]);
var gluedPolynoms = toGlue.Length <= 128
? stackalloc PolynomItem[128].Slice(0, toGlue.Length)
Expand All @@ -1276,7 +1276,7 @@ private static Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polyno

// Remove duplicated exponents and add the corrected ones back.
for (int i = resultPolynom.Count - 1; i >= 0; i--)
#if NETCOREAPP
#if NET5_0_OR_GREATER
if (toGlue.Contains(resultPolynom[i].Exponent))
#else
if (Array.IndexOf(toGlue, resultPolynom[i].Exponent) >= 0)
Expand All @@ -1290,7 +1290,7 @@ private static Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polyno
return resultPolynom;

// Auxiliary function to identify exponents that appear more than once in the polynomial.
#if NETCOREAPP
#if NET5_0_OR_GREATER
static ReadOnlySpan<int> GetNotUniqueExponents(Polynom list, Span<int> buffer)
{
// It works as follows:
Expand Down
4 changes: 2 additions & 2 deletions QRCoder/QRCodeGenerator/CodewordBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Threading;

#if NETCOREAPP
#if HAS_SPAN
using System.Buffers;
#endif

Expand Down Expand Up @@ -51,7 +51,7 @@ public static List<CodewordBlock> GetList(int capacity)

public static void ReturnList(List<CodewordBlock> list)
{
#if NETCOREAPP
#if HAS_SPAN
foreach (var item in list)
{
ArrayPool<byte>.Shared.Return(item.ECCWords.Array!);
Expand Down
2 changes: 1 addition & 1 deletion QRCoder/QRCodeGenerator/Polynom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private void AssertCapacity(int min)
void ThrowNotSupportedException() => throw new NotSupportedException("The polynomial capacity is fixed and cannot be increased.");
}

#if NETCOREAPP
#if HAS_SPAN
/// <summary>
/// Rents memory for the polynomial terms from the shared memory pool.
/// </summary>
Expand Down
9 changes: 5 additions & 4 deletions QRCoder/QRCoder.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net35;net40;netstandard1.3;netstandard2.0;net5.0;net5.0-windows;net6.0;net6.0-windows</TargetFrameworks>
<TargetFrameworks>net35;net40;netstandard1.3;netstandard2.0;netstandard2.1;net5.0;net5.0-windows;net6.0;net6.0-windows</TargetFrameworks>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<DefineConstants Condition="'$(TargetFramework)' != 'net6.0' AND '$(TargetFramework)' != 'netstandard1.3'">$(DefineConstants);SYSTEM_DRAWING</DefineConstants>
<DefineConstants Condition="'$(TargetFramework)' == 'net5.0-windows'">$(DefineConstants);NET5_0_WINDOWS</DefineConstants>
<DefineConstants Condition="'$(TargetFramework)' == 'net6.0-windows'">$(DefineConstants);NET6_0_WINDOWS</DefineConstants>
<DefineConstants Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netcoreapp2.1')) OR '$(TargetFramework)' == 'netstandard2.1'">$(DefineConstants);HAS_SPAN</DefineConstants>
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
<WarningsAsErrors>$(WarningsAsErrors);CS1591</WarningsAsErrors>
Expand Down Expand Up @@ -38,17 +39,17 @@
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net35' or '$(TargetFramework)' == 'net40' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'net35' OR '$(TargetFramework)' == 'net40' ">
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="WindowsBase" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' or '$(TargetFramework)' == 'netstandard2.0' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' OR '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1' ">
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net5.0' or '$(TargetFramework)' == 'net5.0-windows' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1' OR '$(TargetFramework)' == 'net5.0' OR '$(TargetFramework)' == 'net5.0-windows' ">
<PackageReference Include="System.Drawing.Common" Version="5.0.3" />
</ItemGroup>

Expand Down