Files
PdfPig/src/UglyToad.PdfPig.Tokenization/HexTokenizer.cs
Eliot Jones bbde38f656 move tokenizers to their own project
since both pdfs and Adobe Type1 fonts use postscript type objects, tokenization is needed by the main project and the fonts project
2020-01-05 10:40:44 +00:00

56 lines
1.3 KiB
C#

namespace UglyToad.PdfPig.Tokenization
{
using System.Collections.Generic;
using Core;
using Tokens;
internal class HexTokenizer : ITokenizer
{
public bool ReadsNextByte { get; } = false;
public bool TryTokenize(byte currentByte, IInputBytes inputBytes, out IToken token)
{
token = null;
if (currentByte != '<')
{
return false;
}
var characters = new List<char>();
while (inputBytes.MoveNext())
{
var current = inputBytes.CurrentByte;
if (ReadHelper.IsWhitespace(current))
{
continue;
}
if (current == '>')
{
break;
}
if (!IsValidHexCharacter(current))
{
return false;
}
characters.Add((char)current);
}
token = new HexToken(characters);
return true;
}
private static bool IsValidHexCharacter(byte b)
{
return (b >= '0' && b <= '9')
|| (b >= 'a' && b <= 'f')
|| (b >= 'A' && b <= 'F');
}
}
}