2019-03-12 11:31:15 +00:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps.Formats
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Helper methods to parse from string to number and perform very basic validation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class Parsing
|
|
|
|
|
{
|
2020-06-24 09:53:52 +00:00
|
|
|
|
public const int MAX_COORDINATE_VALUE = 131072;
|
2019-03-12 11:31:15 +00:00
|
|
|
|
|
|
|
|
|
public const double MAX_PARSE_VALUE = int.MaxValue;
|
|
|
|
|
|
2022-08-23 01:04:26 +00:00
|
|
|
|
public static float ParseFloat(string input, float parseLimit = (float)MAX_PARSE_VALUE, bool allowNaN = false)
|
2019-03-12 11:31:15 +00:00
|
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
|
float output = float.Parse(input, CultureInfo.InvariantCulture);
|
2019-03-13 02:30:33 +00:00
|
|
|
|
|
|
|
|
|
if (output < -parseLimit) throw new OverflowException("Value is too low");
|
|
|
|
|
if (output > parseLimit) throw new OverflowException("Value is too high");
|
|
|
|
|
|
2022-08-23 01:04:26 +00:00
|
|
|
|
if (!allowNaN && float.IsNaN(output)) throw new FormatException("Not a number");
|
2019-03-12 11:31:15 +00:00
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-23 01:04:26 +00:00
|
|
|
|
public static double ParseDouble(string input, double parseLimit = MAX_PARSE_VALUE, bool allowNaN = false)
|
2019-03-12 11:31:15 +00:00
|
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
|
double output = double.Parse(input, CultureInfo.InvariantCulture);
|
2019-03-13 02:30:33 +00:00
|
|
|
|
|
|
|
|
|
if (output < -parseLimit) throw new OverflowException("Value is too low");
|
|
|
|
|
if (output > parseLimit) throw new OverflowException("Value is too high");
|
|
|
|
|
|
2022-08-23 01:04:26 +00:00
|
|
|
|
if (!allowNaN && double.IsNaN(output)) throw new FormatException("Not a number");
|
2019-03-12 11:31:15 +00:00
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int ParseInt(string input, int parseLimit = (int)MAX_PARSE_VALUE)
|
|
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
|
int output = int.Parse(input, CultureInfo.InvariantCulture);
|
2019-03-13 02:30:33 +00:00
|
|
|
|
|
|
|
|
|
if (output < -parseLimit) throw new OverflowException("Value is too low");
|
|
|
|
|
if (output > parseLimit) throw new OverflowException("Value is too high");
|
2019-03-12 11:31:15 +00:00
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|