1
0
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:
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 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");

View File

@ -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;
} }