2019-06-30 22:01:19 +00:00
|
|
|
|
using System;
|
2019-11-01 02:40:31 +00:00
|
|
|
|
using System.IO;
|
2019-06-30 22:01:19 +00:00
|
|
|
|
using System.Globalization;
|
2019-11-01 02:40:31 +00:00
|
|
|
|
using System.Text;
|
2019-05-17 12:08:06 +00:00
|
|
|
|
|
|
|
|
|
internal static class Formatting
|
|
|
|
|
{
|
2020-08-14 17:35:27 +00:00
|
|
|
|
private static CultureInfo Invariant => CultureInfo.InvariantCulture;
|
2019-05-17 12:08:06 +00:00
|
|
|
|
|
|
|
|
|
public static string ToInvariantString(this float value)
|
|
|
|
|
{
|
|
|
|
|
string result;
|
|
|
|
|
|
|
|
|
|
if (float.IsPositiveInfinity(value))
|
|
|
|
|
result = "INF";
|
|
|
|
|
else if (float.IsNegativeInfinity(value))
|
|
|
|
|
result = "-INF";
|
|
|
|
|
else if (float.IsNaN(value))
|
|
|
|
|
result = "NAN";
|
|
|
|
|
else
|
2020-08-14 17:35:27 +00:00
|
|
|
|
result = value.ToString(Invariant);
|
2019-05-17 12:08:06 +00:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ToInvariantString(this double value)
|
|
|
|
|
{
|
|
|
|
|
string result;
|
|
|
|
|
|
|
|
|
|
if (double.IsPositiveInfinity(value))
|
|
|
|
|
result = "INF";
|
|
|
|
|
else if (double.IsNegativeInfinity(value))
|
|
|
|
|
result = "-INF";
|
|
|
|
|
else if (double.IsNaN(value))
|
|
|
|
|
result = "NAN";
|
|
|
|
|
else
|
2020-08-14 17:35:27 +00:00
|
|
|
|
result = value.ToString(Invariant);
|
2019-05-17 12:08:06 +00:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ToInvariantString(this int value)
|
|
|
|
|
{
|
2020-08-14 17:35:27 +00:00
|
|
|
|
return value.ToString(Invariant);
|
2019-05-17 12:08:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ToInvariantString(this object value)
|
|
|
|
|
{
|
2020-08-14 17:35:27 +00:00
|
|
|
|
switch (value)
|
2019-05-17 12:08:06 +00:00
|
|
|
|
{
|
2020-08-14 17:35:27 +00:00
|
|
|
|
case double d : return d.ToInvariantString();
|
|
|
|
|
case float f : return f.ToInvariantString();
|
|
|
|
|
case int i : return i.ToInvariantString();
|
|
|
|
|
default : return value.ToString();
|
2019-05-17 12:08:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 05:33:59 +00:00
|
|
|
|
public static string ToLowerInvariant(this string str)
|
|
|
|
|
{
|
|
|
|
|
return str.ToLower(Invariant);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ToUpperInvariant(this string str)
|
|
|
|
|
{
|
|
|
|
|
return str.ToUpper(Invariant);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool StartsWithInvariant(this string str, string other)
|
|
|
|
|
{
|
|
|
|
|
return str.StartsWith(other, StringComparison.InvariantCulture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool EndsWithInvariant(this string str, string other)
|
|
|
|
|
{
|
|
|
|
|
return str.EndsWith(other, StringComparison.InvariantCulture);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 12:08:06 +00:00
|
|
|
|
public static float ParseFloat(string value)
|
|
|
|
|
{
|
2020-08-14 17:35:27 +00:00
|
|
|
|
switch (value)
|
|
|
|
|
{
|
|
|
|
|
case "NAN" : return float.NaN;
|
|
|
|
|
case "INF" : return float.PositiveInfinity;
|
|
|
|
|
case "-INF" : return float.NegativeInfinity;
|
|
|
|
|
default : return float.Parse(value, Invariant);
|
|
|
|
|
}
|
2019-05-17 12:08:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static double ParseDouble(string value)
|
|
|
|
|
{
|
2020-08-14 17:35:27 +00:00
|
|
|
|
switch (value)
|
|
|
|
|
{
|
|
|
|
|
case "NAN" : return double.NaN;
|
|
|
|
|
case "INF" : return double.PositiveInfinity;
|
|
|
|
|
case "-INF" : return double.NegativeInfinity;
|
|
|
|
|
default : return double.Parse(value, Invariant);
|
|
|
|
|
}
|
2019-05-17 12:08:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int ParseInt(string s)
|
|
|
|
|
{
|
2020-08-14 17:35:27 +00:00
|
|
|
|
return int.Parse(s, Invariant);
|
2019-05-17 12:08:06 +00:00
|
|
|
|
}
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
|
|
|
|
public static bool FuzzyEquals(this float a, float b, float epsilon = 10e-5f)
|
|
|
|
|
{
|
|
|
|
|
return Math.Abs(a - b) < epsilon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool FuzzyEquals(this double a, double b, double epsilon = 10e-5)
|
|
|
|
|
{
|
|
|
|
|
return Math.Abs(a - b) < epsilon;
|
|
|
|
|
}
|
2019-11-01 02:40:31 +00:00
|
|
|
|
|
2020-07-13 01:19:30 +00:00
|
|
|
|
public static byte[] ReadBuffer(this BinaryReader reader)
|
|
|
|
|
{
|
|
|
|
|
int len = reader.ReadInt32();
|
|
|
|
|
return reader.ReadBytes(len);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-01 02:40:31 +00:00
|
|
|
|
public static string ReadString(this BinaryReader reader, bool useIntLength)
|
|
|
|
|
{
|
|
|
|
|
if (!useIntLength)
|
|
|
|
|
return reader.ReadString();
|
|
|
|
|
|
2020-07-13 01:19:30 +00:00
|
|
|
|
byte[] buffer = reader.ReadBuffer();
|
2019-11-01 02:40:31 +00:00
|
|
|
|
return Encoding.UTF8.GetString(buffer);
|
|
|
|
|
}
|
2019-05-17 12:08:06 +00:00
|
|
|
|
}
|