From f3d6c2775bcc2aebabc334d122151744df79cc1f Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sun, 20 Mar 2022 04:01:25 +0100 Subject: [PATCH] Add Unsafe.As polyfill for NETSTANDARD 2.1 --- src/Markdig/Parsers/BlockProcessor.cs | 28 +++---------------- src/Markdig/Parsers/InlineProcessor.cs | 4 --- .../Parsers/Inlines/EmphasisInlineParser.cs | 4 --- src/Markdig/Parsers/MarkdownParser.cs | 9 ------ src/Markdig/Polyfills/Unsafe.cs | 17 +++++++++++ 5 files changed, 21 insertions(+), 41 deletions(-) create mode 100644 src/Markdig/Polyfills/Unsafe.cs diff --git a/src/Markdig/Parsers/BlockProcessor.cs b/src/Markdig/Parsers/BlockProcessor.cs index 47252586..9d46ac42 100644 --- a/src/Markdig/Parsers/BlockProcessor.cs +++ b/src/Markdig/Parsers/BlockProcessor.cs @@ -604,12 +604,7 @@ namespace Markdig.Parsers if (block.IsContainerBlock) { - var currentContainer = -#if NETSTANDARD2_1 - (ContainerBlock)block; -#else - Unsafe.As(block); -#endif + var currentContainer = Unsafe.As(block); CurrentContainer = currentContainer; LastBlock = currentContainer.LastChild; CurrentBlock = currentBlock; @@ -699,12 +694,7 @@ namespace Markdig.Parsers } } -#if NETSTANDARD2_1 - ((LeafBlock)block) -#else - Unsafe.As(block) -#endif - .AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia); + Unsafe.As(block).AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia); } } @@ -851,12 +841,7 @@ namespace Markdig.Parsers UnwindAllIndents(); } -#if NETSTANDARD2_1 - ((ParagraphBlock)currentBlock) -#else - Unsafe.As(currentBlock) -#endif - .AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia); + Unsafe.As(currentBlock).AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia); } if (TrackTrivia) { @@ -926,12 +911,7 @@ namespace Markdig.Parsers } } -#if NETSTANDARD2_1 - ((LeafBlock)block) -#else - Unsafe.As(block) -#endif - .AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia); + Unsafe.As(block).AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia); } if (newBlocks.Count > 0) diff --git a/src/Markdig/Parsers/InlineProcessor.cs b/src/Markdig/Parsers/InlineProcessor.cs index 98bfe1a8..e821a2fe 100644 --- a/src/Markdig/Parsers/InlineProcessor.cs +++ b/src/Markdig/Parsers/InlineProcessor.cs @@ -325,11 +325,7 @@ namespace Markdig.Parsers Inline? lastChild = container.LastChild; if (lastChild is not null && lastChild.IsContainerInline && !lastChild.IsClosed) { -#if NETSTANDARD2_1 - container = ((ContainerInline)lastChild); -#else container = Unsafe.As(lastChild); -#endif } else { diff --git a/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs b/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs index 1b1e3342..d53e050c 100644 --- a/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs +++ b/src/Markdig/Parsers/Inlines/EmphasisInlineParser.cs @@ -97,11 +97,7 @@ namespace Markdig.Parsers.Inlines return true; } -#if NETSTANDARD2_1 - ContainerInline container = (ContainerInline)root; -#else ContainerInline container = Unsafe.As(root); -#endif List? delimiters = null; if (container is EmphasisDelimiterInline emphasisDelimiter) diff --git a/src/Markdig/Parsers/MarkdownParser.cs b/src/Markdig/Parsers/MarkdownParser.cs index aeff7a98..b1255c95 100644 --- a/src/Markdig/Parsers/MarkdownParser.cs +++ b/src/Markdig/Parsers/MarkdownParser.cs @@ -152,12 +152,7 @@ namespace Markdig.Parsers var block = container[item.Index]; if (block.IsLeafBlock) { -#if NETSTANDARD2_1 - LeafBlock leafBlock = (LeafBlock)block; -#else LeafBlock leafBlock = Unsafe.As(block); -#endif - leafBlock.OnProcessInlinesBegin(inlineProcessor); if (leafBlock.ProcessInlines) { @@ -192,11 +187,7 @@ namespace Markdig.Parsers Array.Resize(ref blocks, blockCount * 2); ThrowHelper.CheckDepthLimit(blocks.Length); } -#if NETSTANDARD2_1 - blocks[blockCount++] = new ContainerItem((ContainerBlock)block); -#else blocks[blockCount++] = new ContainerItem(Unsafe.As(block)); -#endif block.OnProcessInlinesBegin(inlineProcessor); goto process_new_block; } diff --git a/src/Markdig/Polyfills/Unsafe.cs b/src/Markdig/Polyfills/Unsafe.cs new file mode 100644 index 00000000..5fc650b8 --- /dev/null +++ b/src/Markdig/Polyfills/Unsafe.cs @@ -0,0 +1,17 @@ +// Copyright (c) Alexandre Mutel. All rights reserved. +// This file is licensed under the BSD-Clause 2 license. +// See the license.txt file in the project root for more information. + +namespace System.Runtime.CompilerServices +{ +#if NETSTANDARD2_1 + internal static class Unsafe + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T As(object o) where T : class + { + return (T)o; + } + } +#endif +}