mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-04-05 20:55:01 +08:00
#453 handle messed up number format
This commit is contained in:
parent
559f3af5f3
commit
f2188729a3
src
UglyToad.PdfPig.Tests/Parser
UglyToad.PdfPig.Tokenization
@ -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', ' ');
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user