mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-19 10:47:56 +08:00
performance tuning for numeric tokens and parsing
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
internal class ByteArrayInputBytes : IInputBytes
|
||||
{
|
||||
private readonly int upperBound;
|
||||
private readonly byte[] bytes;
|
||||
|
||||
[DebuggerStepThrough]
|
||||
@@ -26,6 +27,8 @@
|
||||
this.bytes = bytes.ToArray();
|
||||
}
|
||||
|
||||
upperBound = this.bytes.Length - 1;
|
||||
|
||||
currentOffset = -1;
|
||||
}
|
||||
|
||||
@@ -34,20 +37,21 @@
|
||||
{
|
||||
this.bytes = bytes ?? throw new ArgumentNullException(nameof(bytes));
|
||||
currentOffset = -1;
|
||||
upperBound = bytes.Length - 1;
|
||||
}
|
||||
|
||||
private long currentOffset;
|
||||
private int currentOffset;
|
||||
public long CurrentOffset => currentOffset + 1;
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (currentOffset == bytes.Length - 1)
|
||||
if (currentOffset == upperBound)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
currentOffset++;
|
||||
CurrentByte = bytes[(int)currentOffset];
|
||||
CurrentByte = bytes[currentOffset];
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -57,23 +61,23 @@
|
||||
|
||||
public byte? Peek()
|
||||
{
|
||||
if (currentOffset == bytes.Length - 1)
|
||||
if (currentOffset == upperBound)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return bytes[(int)currentOffset + 1];
|
||||
return bytes[currentOffset + 1];
|
||||
}
|
||||
|
||||
public bool IsAtEnd()
|
||||
{
|
||||
return currentOffset == bytes.Length - 1;
|
||||
return currentOffset == upperBound;
|
||||
}
|
||||
|
||||
public void Seek(long position)
|
||||
{
|
||||
currentOffset = (int)position - 1;
|
||||
CurrentByte = currentOffset < 0 ? (byte)0 : bytes[(int)currentOffset];
|
||||
CurrentByte = currentOffset < 0 ? (byte)0 : bytes[currentOffset];
|
||||
}
|
||||
|
||||
public int Read(byte[] buffer, int? length = null)
|
||||
@@ -100,8 +104,8 @@
|
||||
}
|
||||
|
||||
var viableLength = (bytes.Length - currentOffset - 1);
|
||||
var readLength = (int)(viableLength < bytesToRead ? viableLength : bytesToRead);
|
||||
var startFrom = (int)currentOffset + 1;
|
||||
var readLength = viableLength < bytesToRead ? viableLength : bytesToRead;
|
||||
var startFrom = currentOffset + 1;
|
||||
|
||||
Array.Copy(bytes, startFrom, buffer, 0, readLength);
|
||||
|
||||
|
@@ -110,10 +110,9 @@
|
||||
/// <remarks>
|
||||
/// These values are specified in table 1 (page 12) of ISO 32000-1:2008.
|
||||
/// </remarks>
|
||||
public static bool IsWhitespace(int c)
|
||||
public static bool IsWhitespace(byte c)
|
||||
{
|
||||
return c == 0 || c == 9 || c == 12 || c == AsciiLineFeed
|
||||
|| c == AsciiCarriageReturn || c == ' ';
|
||||
return c == 0 || c == 32 || c == AsciiLineFeed || c == AsciiCarriageReturn || c == 9 || c == 12;
|
||||
}
|
||||
|
||||
public static bool IsEndOfLine(char c) => IsEndOfLine((byte) c);
|
||||
|
@@ -1,8 +1,8 @@
|
||||
namespace UglyToad.PdfPig.Tokens
|
||||
{
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// PDF supports integer and real numbers. Integer objects represent mathematical integers within a certain interval centered at 0.
|
||||
/// Real objects approximate mathematical real numbers, but with limited range and precision.
|
||||
@@ -16,22 +16,17 @@
|
||||
/// <summary>
|
||||
/// Whether the number represented has a non-zero decimal part.
|
||||
/// </summary>
|
||||
public bool HasDecimalPlaces { get; }
|
||||
public bool HasDecimalPlaces => decimal.Floor(Data) != Data;
|
||||
|
||||
/// <summary>
|
||||
/// The value of this number as an <see langword="int"/>.
|
||||
/// </summary>
|
||||
public int Int { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether the number overflows an integer.
|
||||
/// </summary>
|
||||
public bool IsBiggerThanInt { get; }
|
||||
|
||||
public int Int => (int) Data;
|
||||
|
||||
/// <summary>
|
||||
/// The value of this number as a <see langword="long"/>.
|
||||
/// </summary>
|
||||
public long Long { get; }
|
||||
public long Long => (long) Data;
|
||||
|
||||
/// <summary>
|
||||
/// The value of this number as a <see langword="double"/>.
|
||||
@@ -45,17 +40,6 @@
|
||||
public NumericToken(decimal value)
|
||||
{
|
||||
Data = value;
|
||||
HasDecimalPlaces = decimal.Floor(value) != value;
|
||||
Long = (long) value;
|
||||
|
||||
try
|
||||
{
|
||||
Int = (int) value;
|
||||
}
|
||||
catch (OverflowException)
|
||||
{
|
||||
IsBiggerThanInt = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
Reference in New Issue
Block a user