Skip to content

Commit

Permalink
Use benefits of FrozenDictionary only for net8, no cumbersome net462 …
Browse files Browse the repository at this point in the history
…with an additional package
  • Loading branch information
onizet committed Jan 10, 2025
1 parent 9b8f240 commit 436a600
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*/
#if NET5_0_OR_GREATER
using System.Collections.Frozen;
#endif
using System.Collections.Generic;
using System.Linq;
using AngleSharp.Html.Dom;
Expand Down Expand Up @@ -293,6 +295,10 @@ private static IReadOnlyDictionary<string, AbstractNum> InitKnownLists()
knownAbstractNums.Add(listName, abstractNum);
}

#if NET5_0_OR_GREATER
return knownAbstractNums.ToFrozenDictionary();
#else
return knownAbstractNums;
#endif
}
}
5 changes: 0 additions & 5 deletions src/Html2OpenXml/HtmlToOpenXml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="System.Net.Http" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="System.Collections.Immutable" Version="9.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Collections.Immutable" Version="9.0.0" />
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
Expand Down
26 changes: 10 additions & 16 deletions src/Html2OpenXml/IO/ImageHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* EMF Specifications: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-emf/ae7e7437-cfe5-485e-84ea-c74b51b000be
*/
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -36,22 +35,17 @@ public enum FileType { Unrecognized, Bitmap, Gif, Png, Jpeg, Emf, Xml }

private static readonly byte[] pngSignatureBytes = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];

private static readonly IReadOnlyDictionary<byte[], FileType> imageFormatDecoders = InitKnownFormatDecoders();

private static IReadOnlyDictionary<byte[], FileType> InitKnownFormatDecoders()
private static readonly Dictionary<byte[], FileType> imageFormatDecoders = new()
{
var decoders = new Dictionary<byte[], FileType>() {
{ new byte[] { 0x42, 0x4D }, FileType.Bitmap },
{ Encoding.UTF8.GetBytes("GIF87a"), FileType.Gif },
{ Encoding.UTF8.GetBytes("GIF89a"), FileType.Gif }, // animated gif
{ pngSignatureBytes, FileType.Png },
{ new byte[] { 0xff, 0xd8 }, FileType.Jpeg },
{ new byte[] { 0x1, 0, 0, 0 }, FileType.Emf },
{ Encoding.UTF8.GetBytes("<?xml "), FileType.Xml }, // Xml so potentially Svg
};

return decoders.ToFrozenDictionary();
}
{ new byte[] { 0x42, 0x4D }, FileType.Bitmap },
{ Encoding.UTF8.GetBytes("GIF87a"), FileType.Gif },
{ Encoding.UTF8.GetBytes("GIF89a"), FileType.Gif }, // animated gif
{ pngSignatureBytes, FileType.Png },
{ new byte[] { 0xff, 0xd8 }, FileType.Jpeg },
{ new byte[] { 0x1, 0, 0, 0 }, FileType.Emf },
{ Encoding.UTF8.GetBytes("<?xml "), FileType.Xml }, // Xml so potentially Svg
};


private static readonly int MaxMagicBytesLength = imageFormatDecoders
.Keys.OrderByDescending(x => x.Length).First().Length;
Expand Down
6 changes: 6 additions & 0 deletions src/Html2OpenXml/Primitives/HtmlColor.Named.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
#if NET5_0_OR_GREATER
using System.Collections.Frozen;
#endif
using System.Collections.Generic;

namespace HtmlToOpenXml;
Expand Down Expand Up @@ -169,6 +171,10 @@ private static IReadOnlyDictionary<string, HtmlColor> InitKnownColors()
{ "transparent", FromArgb(0, 0, 0, 0) }
};

#if NET5_0_OR_GREATER
return colors.ToFrozenDictionary();
#else
return colors;
#endif
}
}

0 comments on commit 436a600

Please sign in to comment.