Eliminate allocation in AsciiHexDecodeFilter

This commit is contained in:
Jason Nelson 2024-04-01 22:29:07 -07:00 committed by BobLd
parent d85ea4f95d
commit b498f5a076

View File

@ -1,7 +1,7 @@
namespace UglyToad.PdfPig.Filters
{
using System;
using System.IO;
using Core;
using Tokens;
/// <inheritdoc />
@ -34,9 +34,8 @@
Span<byte> pair = stackalloc byte[2];
var index = 0;
using (var memoryStream = new MemoryStream())
using (var binaryWriter = new BinaryWriter(memoryStream))
{
using var writer = new ArrayPoolBufferWriter<byte>(input.Length);
for (var i = 0; i < input.Length; i++)
{
if (input[i] == '>')
@ -54,7 +53,7 @@
if (index == 2)
{
WriteHexToByte(pair, binaryWriter);
WriteHexToByte(pair, writer);
index = 0;
}
@ -67,15 +66,13 @@
pair[1] = (byte)'0';
}
WriteHexToByte(pair, binaryWriter);
WriteHexToByte(pair, writer);
}
binaryWriter.Flush();
return memoryStream.ToArray();
}
return writer.WrittenSpan.ToArray();
}
private static void WriteHexToByte(ReadOnlySpan<byte> hexBytes, BinaryWriter writer)
private static void WriteHexToByte(ReadOnlySpan<byte> hexBytes, ArrayPoolBufferWriter<byte> writer)
{
var first = ReverseHex[hexBytes[0]];
var second = ReverseHex[hexBytes[1]];