handle messed up number format

This commit is contained in:
Eliot Jones 2022-06-17 20:35:21 -04:00
parent 559f3af5f3
commit f2188729a3
2 changed files with 59 additions and 1 deletions
src
UglyToad.PdfPig.Tests/Parser
UglyToad.PdfPig.Tokenization

View File

@ -6,7 +6,6 @@
using Logging;
using PdfPig.Core;
using PdfPig.Graphics;
using PdfPig.Graphics.Core;
using PdfPig.Graphics.Operations.General;
using PdfPig.Graphics.Operations.SpecialGraphicsState;
using PdfPig.Graphics.Operations.TextObjects;
@ -183,6 +182,30 @@ cm BT 0.0001 Tc 19 0 0 19 0 0 Tm /Tc1 1 Tf ( \(sleep 1; printf ""QUIT\\r\\n""\
Assert.Equal(@" (sleep 1; printf ""QUIT\r\n"") | ", text.Text);
}
[Fact]
public void HandlesWeirdNumber()
{
// Issue 453
const string s = @"/Alpha1
gs
0
0
0
rg
0.00-90
151555.0
m
302399.97
151555.0
l";
var input = StringBytesTestConverter.Convert(s, false);
var result = parser.Parse(1, input.Bytes, log);
Assert.Equal(4, result.Count);
}
private static string LineEndingsToWhiteSpace(string str)
{
return str.Replace("\r\n", " ").Replace('\n', ' ').Replace('\r', ' ');

View File

@ -155,6 +155,12 @@
default:
if (!decimal.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out var value))
{
if (TryParseInvalidNumber(str, out value))
{
token = new NumericToken(value);
return true;
}
return false;
}
@ -171,5 +177,34 @@
return false;
}
}
private static bool TryParseInvalidNumber(string numeric, out decimal result)
{
result = 0;
if (!numeric.Contains("-") && !numeric.Contains("+"))
{
return false;
}
var parts = numeric.Split(new string[] { "+", "-" }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0)
{
return false;
}
foreach (var part in parts)
{
if (!decimal.TryParse(part, NumberStyles.Any, CultureInfo.InvariantCulture, out var partNumber))
{
return false;
}
result += partNumber;
}
return true;
}
}
}