1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 20:22:55 +08:00

Allow NaN during beatmap parsing if desired

This commit is contained in:
Khang 2022-08-22 21:04:26 -04:00
parent 3fb3a18e68
commit e8d4bc4497
2 changed files with 10 additions and 5 deletions

View File

@ -14,7 +14,12 @@ namespace osu.Game.Tests.Beatmaps.Formats
public class ParsingTest
{
[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]
public void TestBadStringHandling() => allThrow<FormatException>("Random string 123");

View File

@ -17,26 +17,26 @@ namespace osu.Game.Beatmaps.Formats
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);
if (output < -parseLimit) throw new OverflowException("Value is too low");
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;
}
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);
if (output < -parseLimit) throw new OverflowException("Value is too low");
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;
}