mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 23:12:56 +08:00
Allow NaN during beatmap parsing if desired
This commit is contained in:
parent
3fb3a18e68
commit
e8d4bc4497
@ -14,7 +14,12 @@ namespace osu.Game.Tests.Beatmaps.Formats
|
|||||||
public class ParsingTest
|
public class ParsingTest
|
||||||
{
|
{
|
||||||
[Test]
|
[Test]
|
||||||
public void TestNaNHandling() => allThrow<FormatException>("NaN");
|
public void TestNaNHandling()
|
||||||
|
{
|
||||||
|
allThrow<FormatException>("NaN");
|
||||||
|
Assert.That(Parsing.ParseFloat("NaN", allowNaN: true), Is.NaN);
|
||||||
|
Assert.That(Parsing.ParseDouble("NaN", allowNaN: true), Is.NaN);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestBadStringHandling() => allThrow<FormatException>("Random string 123");
|
public void TestBadStringHandling() => allThrow<FormatException>("Random string 123");
|
||||||
|
@ -17,26 +17,26 @@ namespace osu.Game.Beatmaps.Formats
|
|||||||
|
|
||||||
public const double MAX_PARSE_VALUE = int.MaxValue;
|
public const double MAX_PARSE_VALUE = int.MaxValue;
|
||||||
|
|
||||||
public static float ParseFloat(string input, float parseLimit = (float)MAX_PARSE_VALUE)
|
public static float ParseFloat(string input, float parseLimit = (float)MAX_PARSE_VALUE, bool allowNaN = false)
|
||||||
{
|
{
|
||||||
float output = float.Parse(input, CultureInfo.InvariantCulture);
|
float output = float.Parse(input, CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
if (output < -parseLimit) throw new OverflowException("Value is too low");
|
if (output < -parseLimit) throw new OverflowException("Value is too low");
|
||||||
if (output > parseLimit) throw new OverflowException("Value is too high");
|
if (output > parseLimit) throw new OverflowException("Value is too high");
|
||||||
|
|
||||||
if (float.IsNaN(output)) throw new FormatException("Not a number");
|
if (!allowNaN && float.IsNaN(output)) throw new FormatException("Not a number");
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double ParseDouble(string input, double parseLimit = MAX_PARSE_VALUE)
|
public static double ParseDouble(string input, double parseLimit = MAX_PARSE_VALUE, bool allowNaN = false)
|
||||||
{
|
{
|
||||||
double output = double.Parse(input, CultureInfo.InvariantCulture);
|
double output = double.Parse(input, CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
if (output < -parseLimit) throw new OverflowException("Value is too low");
|
if (output < -parseLimit) throw new OverflowException("Value is too low");
|
||||||
if (output > parseLimit) throw new OverflowException("Value is too high");
|
if (output > parseLimit) throw new OverflowException("Value is too high");
|
||||||
|
|
||||||
if (double.IsNaN(output)) throw new FormatException("Not a number");
|
if (!allowNaN && double.IsNaN(output)) throw new FormatException("Not a number");
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user