Avoid doing a true file seek for simple peeking the next char in the token parser
Some checks failed
Build, test and publish draft / build (push) Has been cancelled
Build and test [MacOS] / build (push) Has been cancelled
Run Common Crawl Tests / build (0000-0001) (push) Has been cancelled
Run Common Crawl Tests / build (0002-0003) (push) Has been cancelled
Run Common Crawl Tests / build (0004-0005) (push) Has been cancelled
Run Common Crawl Tests / build (0006-0007) (push) Has been cancelled
Run Integration Tests / build (push) Has been cancelled
Nightly Release / Check if this commit has already been published (push) Has been cancelled
Nightly Release / tests (push) Has been cancelled
Nightly Release / build_and_publish_nightly (push) Has been cancelled

This commit is contained in:
Bert Huijben
2025-10-16 10:32:14 +02:00
committed by BobLd
parent 3592fc8438
commit 6fba565d66
4 changed files with 36 additions and 21 deletions

View File

@@ -11,11 +11,12 @@
{
private readonly Stream stream;
private readonly bool shouldDispose;
private byte? peekByte;
private bool isAtEnd;
/// <inheritdoc />
public long CurrentOffset => stream.Position;
public long CurrentOffset => peekByte.HasValue ? stream.Position - 1 : stream.Position;
/// <inheritdoc />
public byte CurrentByte { get; private set; }
@@ -52,7 +53,8 @@
/// <inheritdoc />
public bool MoveNext()
{
var b = stream.ReadByte();
var b = peekByte ?? stream.ReadByte();
peekByte = null;
if (b == -1)
{
@@ -68,18 +70,21 @@
/// <inheritdoc />
public byte? Peek()
{
var current = CurrentOffset;
var b = stream.ReadByte();
stream.Seek(current, SeekOrigin.Begin);
if (b == -1)
if (!peekByte.HasValue)
{
return null;
var v = stream.ReadByte();
if (v >= 0)
{
peekByte = (byte)v;
}
else
{
return null;
}
}
return (byte)b;
return peekByte;
}
/// <inheritdoc />
@@ -92,6 +97,7 @@
public void Seek(long position)
{
isAtEnd = false;
peekByte = null;
if (position == 0)
{
@@ -112,9 +118,15 @@
{
return 0;
}
else if (peekByte.HasValue)
{
buffer[0] = peekByte.Value;
peekByte = null;
return Read(buffer.Slice(1)) + 1;
}
int read = stream.Read(buffer);
if (read > 0)
{
CurrentByte = buffer[read - 1];

View File

@@ -25,9 +25,9 @@
internal static class PdfDocumentFactory
{
public static PdfDocument Open(byte[] fileBytes, ParsingOptions? options = null)
public static PdfDocument Open(ReadOnlyMemory<byte> memory, ParsingOptions? options = null)
{
var inputBytes = new MemoryInputBytes(fileBytes);
var inputBytes = new MemoryInputBytes(memory);
return Open(inputBytes, options);
}

View File

@@ -102,6 +102,14 @@
/// <returns>A <see cref="PdfDocument"/> providing access to the file contents.</returns>
public static PdfDocument Open(byte[] fileBytes, ParsingOptions? options = null) => PdfDocumentFactory.Open(fileBytes, options);
/// <summary>
/// Creates a <see cref="PdfDocument"/> for reading from the provided file bytes.
/// </summary>
/// <param name="memory">The bytes of the PDF file.</param>
/// <param name="options">Optional parameters controlling parsing.</param>
/// <returns>A <see cref="PdfDocument"/> providing access to the file contents.</returns>
public static PdfDocument Open(ReadOnlyMemory<byte> memory, ParsingOptions? options = null) => PdfDocumentFactory.Open(memory, options);
/// <summary>
/// Opens a file and creates a <see cref="PdfDocument"/> for reading from the provided file path.
/// </summary>

View File

@@ -342,14 +342,9 @@
if ((char)inputBytes.CurrentByte == '\r')
{
if (!inputBytes.MoveNext())
if (inputBytes.Peek() == '\n')
{
return false;
}
if ((char)inputBytes.CurrentByte != '\n')
{
inputBytes.Seek(inputBytes.CurrentOffset - 1);
inputBytes.MoveNext();
}
break;
}