mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
support for tokenizing arrays and nested arrays
This commit is contained in:
47
src/UglyToad.Pdf/Tokenization/ArrayTokenizer.cs
Normal file
47
src/UglyToad.Pdf/Tokenization/ArrayTokenizer.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
namespace UglyToad.Pdf.Tokenization
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using IO;
|
||||
using Scanner;
|
||||
using Tokens;
|
||||
|
||||
public class ArrayTokenizer : ITokenizer
|
||||
{
|
||||
public bool ReadsNextByte { get; } = false;
|
||||
|
||||
public bool TryTokenize(byte currentByte, IInputBytes inputBytes, out IToken token)
|
||||
{
|
||||
token = null;
|
||||
|
||||
if (currentByte != '[')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var scanner = new CoreTokenScanner(inputBytes, ScannerScope.Array);
|
||||
|
||||
var contents = new List<IToken>();
|
||||
|
||||
IToken previousToken = null;
|
||||
while (!CurrentByteEndsCurrentArray(inputBytes, previousToken) && scanner.MoveNext())
|
||||
{
|
||||
previousToken = scanner.CurrentToken;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user