using System.Text;
namespace BizHawk.Client.Common
{
using System.Linq;
///
/// Includes helper functions to validate user input
///
public static class InputValidate
{
///
/// Validates all chars are 0-9
///
public static bool IsUnsigned(string str)
{
return str.All(IsUnsigned);
}
///
/// Validates the char is 0-9
///
public static bool IsUnsigned(char c)
{
return char.IsDigit(c);
}
///
/// Validates all chars are 0-9, or a dash as the first value
///
public static bool IsSigned(string str)
{
return IsSigned(str[0]) && str.Substring(1).All(IsUnsigned);
}
///
/// Validates the char is 0-9 or a dash
///
public static bool IsSigned(char c)
{
return char.IsDigit(c) || c == '-';
}
///
/// Validates all chars are 0-9, A-F or a-f
///
public static bool IsHex(string str)
{
return str.All(IsHex);
}
///
/// Validates the char is 0-9, A-F or a-f
///
public static bool IsHex(char c)
{
if (char.IsDigit(c))
{
return true;
}
return char.ToUpper(c) >= 'A' && char.ToUpper(c) <= 'F';
}
///
/// Takes any string and removes any value that is not a valid hex value (0-9, a-f, A-F), returns the remaining characters in uppercase
///
public static string DoHexString(string raw)
{
var output = new StringBuilder();
foreach (var chr in raw)
{
if (IsHex(chr))
{
output.Append(char.ToUpper(chr));
}
}
return output.ToString();
}
///
/// Validates all chars are 0 or 1
///
public static bool IsBinary(string str)
{
return str.All(IsBinary);
}
///
/// Validates the char is 0 or 1
///
public static bool IsBinary(char c)
{
return c == '0' || c == '1';
}
///
/// Validates all chars are 0-9, a decimal point, and that there is no more than 1 decimal point, can not be signed
///
public static bool IsFixedPoint(string str)
{
return StringHelpers.HowMany(str, '.') <= 1
&& str.All(IsFixedPoint);
}
///
/// Validates the char is 0-9, a dash, or a decimal
///
public static bool IsFixedPoint(char c)
{
return IsUnsigned(c) || c == '.';
}
///
/// Validates all chars are 0-9 or decimal, and that there is no more than 1 decimal point, a dash can be the first character
///
public static bool IsFloat(string str)
{
return StringHelpers.HowMany(str, '.') <= 1
&& IsFloat(str[0])
&& str.Substring(1).All(IsFixedPoint);
}
///
/// Validates that the char is 0-9, a dash, or a decimal point
///
public static bool IsFloat(char c)
{
return IsFixedPoint(c) || c == '-';
}
}
}