Files
PdfPig/src/UglyToad.PdfPig.Tokenization/ArrayTokenizer.cs
BobLd 7c4f5e2424
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
Introduce StackDepthGuard class to check for stack depth in CoreTokenScanner and fix #1217
2025-12-23 16:24:04 +01:00

63 lines
1.7 KiB
C#

namespace UglyToad.PdfPig.Tokenization
{
using System.Collections.Generic;
using Core;
using Scanner;
using Tokens;
internal sealed class ArrayTokenizer : ITokenizer
{
private readonly bool usePdfDocEncoding;
private readonly StackDepthGuard stackDepthGuard;
public bool ReadsNextByte { get; } = false;
public ArrayTokenizer(bool usePdfDocEncoding, StackDepthGuard stackDepthGuard)
{
this.usePdfDocEncoding = usePdfDocEncoding;
this.stackDepthGuard = stackDepthGuard;
}
public bool TryTokenize(byte currentByte, IInputBytes inputBytes, out IToken token)
{
token = null;
if (currentByte != '[')
{
return false;
}
var scanner = new CoreTokenScanner(inputBytes, usePdfDocEncoding, stackDepthGuard, ScannerScope.Array);
var contents = new List<IToken>();
IToken previousToken = null;
while (!CurrentByteEndsCurrentArray(inputBytes, previousToken) && scanner.MoveNext())
{
previousToken = scanner.CurrentToken;
if (scanner.CurrentToken is CommentToken)
{
continue;
}
contents.Add(scanner.CurrentToken);
}
token = new ArrayToken(contents);
return true;
}
private static bool CurrentByteEndsCurrentArray(IInputBytes inputBytes, IToken previousToken)
{
if (inputBytes.CurrentByte == ']' && !(previousToken is ArrayToken))
{
return true;
}
return false;
}
}
}