Add Unsafe.As polyfill for NETSTANDARD 2.1

This commit is contained in:
Miha Zupan
2022-03-20 04:01:25 +01:00
parent bb6ace15b7
commit f3d6c2775b
5 changed files with 21 additions and 41 deletions

View File

@@ -604,12 +604,7 @@ namespace Markdig.Parsers
if (block.IsContainerBlock)
{
var currentContainer =
#if NETSTANDARD2_1
(ContainerBlock)block;
#else
Unsafe.As<ContainerBlock>(block);
#endif
var currentContainer = Unsafe.As<ContainerBlock>(block);
CurrentContainer = currentContainer;
LastBlock = currentContainer.LastChild;
CurrentBlock = currentBlock;
@@ -699,12 +694,7 @@ namespace Markdig.Parsers
}
}
#if NETSTANDARD2_1
((LeafBlock)block)
#else
Unsafe.As<LeafBlock>(block)
#endif
.AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia);
Unsafe.As<LeafBlock>(block).AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia);
}
}
@@ -851,12 +841,7 @@ namespace Markdig.Parsers
UnwindAllIndents();
}
#if NETSTANDARD2_1
((ParagraphBlock)currentBlock)
#else
Unsafe.As<ParagraphBlock>(currentBlock)
#endif
.AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia);
Unsafe.As<ParagraphBlock>(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<LeafBlock>(block)
#endif
.AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia);
Unsafe.As<LeafBlock>(block).AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia);
}
if (newBlocks.Count > 0)

View File

@@ -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<ContainerInline>(lastChild);
#endif
}
else
{

View File

@@ -97,11 +97,7 @@ namespace Markdig.Parsers.Inlines
return true;
}
#if NETSTANDARD2_1
ContainerInline container = (ContainerInline)root;
#else
ContainerInline container = Unsafe.As<ContainerInline>(root);
#endif
List<EmphasisDelimiterInline>? delimiters = null;
if (container is EmphasisDelimiterInline emphasisDelimiter)

View File

@@ -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<LeafBlock>(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<ContainerBlock>(block));
#endif
block.OnProcessInlinesBegin(inlineProcessor);
goto process_new_block;
}

View File

@@ -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<T>(object o) where T : class
{
return (T)o;
}
}
#endif
}