mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-06-28 15:30:17 +08:00
Eliminate allocation in AsciiHexDecodeFilter
This commit is contained in:
parent
d85ea4f95d
commit
b498f5a076
@ -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]];
|
||||
|
Loading…
Reference in New Issue
Block a user