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:
parent
3fb3a18e68
commit
e8d4bc4497
@ -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");
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user