mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
* Add GetString(ReadOnlySpan<byte>) polyfill * Add ArrayPoolBufferWriter * Use Utf8.IsValid & char.IsAsciiHexDigit on NET8.0+ * Optimize HexTokenizer * Eliminate various Tuple allocations * Eliminate List allocation in CrossReferenceTable * Eliminate various allocations in Ascii85Filter * Spanify HexToken * Spanify Palette * Spanify various Cmap & font methods * Spanify Type1Charstring classes * Spanify PdfDocEncoding.TryConvertBytesToString * Spanify OctalHelpers.FromOctalDigits * Add missing braces * React to HexToken.Byte type changes * Cleanup * [Tests] React to span changes * Add ArgumentNullException check back to Type1CharstringDecryptedBytes * Remove unsafe code * Seal HexToken * Avoid allocation when passing an empty span
55 lines
1.3 KiB
C#
55 lines
1.3 KiB
C#
namespace UglyToad.PdfPig.Tokenization
|
|
{
|
|
using Core;
|
|
using Tokens;
|
|
|
|
internal sealed class HexTokenizer : ITokenizer
|
|
{
|
|
public bool ReadsNextByte { get; } = false;
|
|
|
|
public bool TryTokenize(byte currentByte, IInputBytes inputBytes, out IToken token)
|
|
{
|
|
token = null;
|
|
|
|
if (currentByte != '<')
|
|
{
|
|
return false;
|
|
}
|
|
|
|
using var charBuffer = new ArrayPoolBufferWriter<char>();
|
|
|
|
while (inputBytes.MoveNext())
|
|
{
|
|
var current = inputBytes.CurrentByte;
|
|
|
|
if (ReadHelper.IsWhitespace(current))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (current == '>')
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (!IsValidHexCharacter(current))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
charBuffer.Write((char)current);
|
|
}
|
|
|
|
token = new HexToken(charBuffer.WrittenSpan);
|
|
|
|
return true;
|
|
}
|
|
|
|
private static bool IsValidHexCharacter(byte b)
|
|
{
|
|
return (b >= '0' && b <= '9')
|
|
|| (b >= 'a' && b <= 'f')
|
|
|| (b >= 'A' && b <= 'F');
|
|
}
|
|
}
|
|
} |