drastically cleanup the InputValidate class

This commit is contained in:
adelikat 2014-02-03 19:37:43 +00:00
parent 343fbc7ae4
commit 3dfcd8b22a
11 changed files with 127 additions and 320 deletions

View File

@ -2,157 +2,64 @@
namespace BizHawk.Client.Common
{
using System.Linq;
/// <summary>
/// Includes helper functions to validate user input
/// </summary>
public static class InputValidate
{
public static bool IsValidUnsignedNumber(string str)
/// <summary>
/// Validates all chars are 0-9
/// </summary>
public static bool IsUnsigned(string str)
{
var input = str.ToCharArray();
var asciiEncoding = new ASCIIEncoding();
// Check each character in the new label to determine if it is a number.
for (int i = 0; i < input.Length; i++)
{
// Encode the character from the character array to its ASCII code.
var bc = asciiEncoding.GetBytes(input[i].ToString());
// Determine if the ASCII code is within the valid range of numerical values.
if (bc[0] < 47 || bc[0] > 58)
{
return false;
}
}
return true;
}
public static bool IsValidUnsignedNumber(char c)
{
if (c < 47 || c > 58)
{
return false;
}
return true;
return str.All(IsUnsigned);
}
/// <summary>
/// Validates all chars are 0-9 or a dash as the first value
/// Validates the char is 0-9
/// </summary>
public static bool IsValidSignedNumber(string str)
public static bool IsUnsigned(char c)
{
var input = str.Trim().ToCharArray();
var asciiEncoding = new ASCIIEncoding();
// Check each character in the new label to determine if it is a number.
for (int i = 0; i < input.Length; i++)
{
// Encode the character from the character array to its ASCII code.
var bc = asciiEncoding.GetBytes(input[i].ToString());
// Determine if the ASCII code is within the valid range of numerical values.
if (bc[0] > 58)
{
return false;
}
if (bc[0] < 47)
{
if (bc[0] == 45 && i == 0)
{
continue;
}
else
{
return false;
}
}
}
return true;
return char.IsDigit(c);
}
public static bool IsValidSignedNumber(char c)
/// <summary>
/// Validates all chars are 0-9, or a dash as the first value
/// </summary>
public static bool IsSigned(string str)
{
if (c == 45)
return IsSigned(str[0]) && str.Substring(1).All(IsUnsigned);
}
/// <summary>
/// Validates the char is 0-9 or a dash
/// </summary>
public static bool IsSigned(char c)
{
return char.IsDigit(c) || c == '-';
}
/// <summary>
/// Validates all chars are 0-9, A-F or a-f
/// </summary>
public static bool IsHex(string str)
{
return str.All(IsHex);
}
/// <summary>
/// Validates the char is 0-9, A-F or a-f
/// </summary>
public static bool IsHex(char c)
{
if (char.IsDigit(c))
{
return true;
}
if (c < 47 || c > 58)
{
return false;
}
return true;
}
/// <summary>
/// validates is a Hex number 0-9, A-F (must be capital letters)
/// </summary>
public static bool IsValidHexNumber(string str)
{
var input = str.ToCharArray();
var asciiEncoding = new ASCIIEncoding();
// Check each character in the new label to determine if it is a number.
for (int i = 0; i < input.Length; i++)
{
// Encode the character from the character array to its ASCII code.
var bc = asciiEncoding.GetBytes(input[i].ToString());
// Determine if the ASCII code is within the valid range of numerical values.
if (bc[0] < 47) // 0
{
return false;
}
if (bc[0] > 58) // 9
{
if (bc[0] < 65) // A
{
return false;
}
if (bc[0] > 70) // F
{
if (bc[0] < 97 || bc[0] > 102) // a-f
{
return false;
}
}
}
}
return true;
}
public static bool IsValidHexNumber(char c)
{
if (c < 47)
{
return false; // 0
}
if (c > 58) // 9
{
if (c < 65) // A
{
return false;
}
if (c > 70) // F
{
if (c < 97 || c > 102) // a-f
{
return false;
}
}
}
return true;
return char.ToUpper(c) >= 'A' && char.ToUpper(c) <= 'F';
}
/// <summary>
@ -160,168 +67,68 @@ namespace BizHawk.Client.Common
/// </summary>
public static string DoHexString(string raw)
{
raw = raw.ToUpper();
var output = new StringBuilder();
foreach (var chr in raw)
{
if (chr >= 'A' && chr <= 'F')
if (IsHex(chr))
{
output.Append(chr);
}
else if (chr >= '0' && chr <= '9')
{
output.Append(chr);
output.Append(char.ToUpper(chr));
}
}
return output.ToString();
}
public static bool IsValidBinaryNumber(string str)
/// <summary>
/// Validates all chars are 0 or 1
/// </summary>
public static bool IsBinary(string str)
{
var input = str.ToCharArray();
var asciiEncoding = new ASCIIEncoding();
// Check each character in the new label to determine if it is a number.
for (int i = 0; i < input.Length; i++)
{
// Encode the character from the character array to its ASCII code.
var bc = asciiEncoding.GetBytes(input[i].ToString());
// Determine if the ASCII code is within the valid range of numerical values.
if (bc[0] != 48 && bc[0] != 49) // 0 or 1
{
return false;
}
}
return true;
}
public static bool IsValidBinaryNumber(char c)
{
return c == 48 || c == 49;
return str.All(IsBinary);
}
/// <summary>
/// Validates all chars are 0-9 or decimal
/// Validates the char is 0 or 1
/// </summary>
public static bool IsValidFixedPointNumber(string str)
public static bool IsBinary(char c)
{
if (StringHelpers.HowMany(str, '.') > 1)
{
return false;
}
var input = str.Trim().ToCharArray();
var asciiEncoding = new ASCIIEncoding();
// Check each character in the new label to determine if it is a number.
for (int i = 0; i < input.Length; i++)
{
// Encode the character from the character array to its ASCII code.
var bc = asciiEncoding.GetBytes(input[i].ToString());
// Determine if the ASCII code is within the valid range of numerical values.
if (bc[0] > 58)
{
return false;
}
if (bc[0] == 46)
{
continue;
}
if (bc[0] < 48)
{
if (bc[0] == 45 && i == 0)
{
continue;
}
else
{
return false;
}
}
}
return true;
}
public static bool IsValidFixedPointNumber(char c)
{
if (c == 46 || c == 45)
{
return true;
}
if (c < 48 || c > 58)
{
return false;
}
return true;
return c == '0' || c == '1';
}
/// <summary>
/// Validates all chars are 0-9 or decimal or dash as the first character
/// Validates all chars are 0-9, a decimal point, and that there is no more than 1 decimal point, can not be signed
/// </summary>
public static bool IsValidDecimalNumber(string str)
public static bool IsFixedPoint(string str)
{
if (StringHelpers.HowMany(str, '.') > 1)
{
return false;
}
var input = str.Trim().ToCharArray();
var asciiEncoding = new ASCIIEncoding();
// Check each character in the new label to determine if it is a number.
for (int i = 0; i < input.Length; i++)
{
// Encode the character from the character array to its ASCII code.
var bc = asciiEncoding.GetBytes(input[i].ToString());
// Determine if the ASCII code is within the valid range of numerical values.
if (bc[0] > 58)
{
return false;
}
if (bc[0] == 46)
{
continue;
}
if (bc[0] < 48)
{
if (bc[0] == 45 && i == 0)
{
continue;
}
else
{
return false;
}
}
}
return true;
return StringHelpers.HowMany(str, '.') <= 1
&& str.All(IsFixedPoint);
}
public static bool IsValidDecimalNumber(char c)
/// <summary>
/// Validates the char is 0-9, a dash, or a decimal
/// </summary>
public static bool IsFixedPoint(char c)
{
if (c == 45 || c == 46) // 45 = dash, 46 = dot
{
return true;
}
else if (c < 48 || c > 58)
{
return false;
}
return IsUnsigned(c) || c == '.';
}
return true;
/// <summary>
/// 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
/// </summary>
public static bool IsFloat(string str)
{
return StringHelpers.HowMany(str, '.') <= 1
&& IsFloat(str[0])
&& str.Substring(1).All(IsFixedPoint);
}
/// <summary>
/// Validates that the char is 0-9, a dash, or a decimal point
/// </summary>
public static bool IsFloat(char c)
{
return IsFixedPoint(c) || c == '-';
}
}
}

View File

@ -413,7 +413,7 @@ namespace BizHawk.Client.Common
foreach (string name in MnemonicConstants.ANALOGS[ControlType].Keys)
{
if (InputValidate.IsValidSignedNumber(mnemonic.Substring(srcindex + start, 4)))
if (InputValidate.IsSigned(mnemonic.Substring(srcindex + start, 4)))
{
Force("P" + player + " " + name, Int32.Parse(mnemonic.Substring(srcindex + start, 4)));
}

View File

@ -445,7 +445,7 @@ namespace BizHawk.Client.Common
switch (Type)
{
case DisplayType.Unsigned:
if (InputValidate.IsValidUnsignedNumber(value))
if (InputValidate.IsUnsigned(value))
{
val = (byte)int.Parse(value);
}
@ -456,7 +456,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Signed:
if (InputValidate.IsValidSignedNumber(value))
if (InputValidate.IsSigned(value))
{
val = (byte)(sbyte)int.Parse(value);
}
@ -467,7 +467,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Hex:
if (InputValidate.IsValidHexNumber(value))
if (InputValidate.IsHex(value))
{
val = (byte)int.Parse(value, NumberStyles.HexNumber);
}
@ -478,7 +478,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Binary:
if (InputValidate.IsValidBinaryNumber(value))
if (InputValidate.IsBinary(value))
{
val = (byte)Convert.ToInt32(value, 2);
}
@ -650,7 +650,7 @@ namespace BizHawk.Client.Common
switch (Type)
{
case DisplayType.Unsigned:
if (InputValidate.IsValidUnsignedNumber(value))
if (InputValidate.IsUnsigned(value))
{
val = (ushort)int.Parse(value);
}
@ -661,7 +661,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Signed:
if (InputValidate.IsValidSignedNumber(value))
if (InputValidate.IsSigned(value))
{
val = (ushort)(short)int.Parse(value);
}
@ -672,7 +672,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Hex:
if (InputValidate.IsValidHexNumber(value))
if (InputValidate.IsHex(value))
{
val = (ushort)int.Parse(value, NumberStyles.HexNumber);
}
@ -683,7 +683,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Binary:
if (InputValidate.IsValidBinaryNumber(value))
if (InputValidate.IsBinary(value))
{
val = (ushort)Convert.ToInt32(value, 2);
}
@ -694,7 +694,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.FixedPoint_12_4:
if (InputValidate.IsValidFixedPointNumber(value))
if (InputValidate.IsFixedPoint(value))
{
val = (ushort)(double.Parse(value) * 16.0);
}
@ -857,7 +857,7 @@ namespace BizHawk.Client.Common
switch (Type)
{
case DisplayType.Unsigned:
if (InputValidate.IsValidUnsignedNumber(value))
if (InputValidate.IsUnsigned(value))
{
val = (uint)int.Parse(value);
}
@ -868,7 +868,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Signed:
if (InputValidate.IsValidSignedNumber(value))
if (InputValidate.IsSigned(value))
{
val = (uint)int.Parse(value);
}
@ -879,7 +879,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Hex:
if (InputValidate.IsValidHexNumber(value))
if (InputValidate.IsHex(value))
{
val = (uint)int.Parse(value, NumberStyles.HexNumber);
}
@ -890,7 +890,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.FixedPoint_20_12:
if (InputValidate.IsValidFixedPointNumber(value))
if (InputValidate.IsFixedPoint(value))
{
val = (uint)(int)(double.Parse(value) * 4096.0);
}
@ -901,7 +901,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Float:
if (InputValidate.IsValidDecimalNumber(value))
if (InputValidate.IsFloat(value))
{
var bytes = BitConverter.GetBytes(float.Parse(value));
val = BitConverter.ToUInt32(bytes, 0);

View File

@ -66,7 +66,7 @@ namespace BizHawk.Client.EmuHawk
{
return;
}
else if (!InputValidate.IsValidHexNumber(e.KeyChar))
else if (!InputValidate.IsHex(e.KeyChar))
{
e.Handled = true;
}
@ -76,7 +76,7 @@ namespace BizHawk.Client.EmuHawk
{
if (e.KeyCode == Keys.Up)
{
if (InputValidate.IsValidHexNumber(Text) && !String.IsNullOrEmpty(_addressFormatStr))
if (InputValidate.IsHex(Text) && !String.IsNullOrEmpty(_addressFormatStr))
{
uint val = (uint)ToRawInt();
@ -94,7 +94,7 @@ namespace BizHawk.Client.EmuHawk
}
else if (e.KeyCode == Keys.Down)
{
if (InputValidate.IsValidHexNumber(Text) && !String.IsNullOrEmpty(_addressFormatStr))
if (InputValidate.IsHex(Text) && !String.IsNullOrEmpty(_addressFormatStr))
{
uint val = (uint)ToRawInt();
if (val == 0)
@ -174,7 +174,7 @@ namespace BizHawk.Client.EmuHawk
{
return;
}
else if (!InputValidate.IsValidUnsignedNumber(e.KeyChar))
else if (!InputValidate.IsUnsigned(e.KeyChar))
{
e.Handled = true;
}
@ -196,7 +196,7 @@ namespace BizHawk.Client.EmuHawk
{
if (e.KeyCode == Keys.Up)
{
if (InputValidate.IsValidUnsignedNumber(Text))
if (InputValidate.IsUnsigned(Text))
{
uint val = (uint)ToRawInt();
if (val == uint.MaxValue)
@ -212,7 +212,7 @@ namespace BizHawk.Client.EmuHawk
}
else if (e.KeyCode == Keys.Down)
{
if (InputValidate.IsValidUnsignedNumber(Text))
if (InputValidate.IsUnsigned(Text))
{
uint val = (uint)ToRawInt();

View File

@ -98,12 +98,12 @@ namespace BizHawk.Client.EmuHawk
s.RicePlugin.FastLoadTile = RiceFastLoadTile_CB.Checked;
s.RicePlugin.UseSmallerTexture = RiceUseSmallerTexture_CB.Checked;
if (InputValidate.IsValidSignedNumber(RiceVIWidth_Text.Text))
if (InputValidate.IsSigned(RiceVIWidth_Text.Text))
s.RicePlugin.VIWidth = int.Parse(RiceVIWidth_Text.Text);
else
s.RicePlugin.VIWidth = -1;
if (InputValidate.IsValidSignedNumber(RiceVIHeight_Text.Text))
if (InputValidate.IsSigned(RiceVIHeight_Text.Text))
s.RicePlugin.VIHeight = int.Parse(RiceVIHeight_Text.Text);
else
s.RicePlugin.VIHeight = -1;
@ -145,19 +145,19 @@ namespace BizHawk.Client.EmuHawk
s.GlidePlugin.fb_get_info = Glide_fb_get_info.Checked;
s.GlidePlugin.offset_x =
InputValidate.IsValidSignedNumber(Glide_offset_x.Text) ?
InputValidate.IsSigned(Glide_offset_x.Text) ?
int.Parse(Glide_offset_x.Text) : 0;
s.GlidePlugin.offset_y =
InputValidate.IsValidSignedNumber(Glide_offset_y.Text) ?
InputValidate.IsSigned(Glide_offset_y.Text) ?
int.Parse(Glide_offset_y.Text) : 0;
s.GlidePlugin.scale_x =
InputValidate.IsValidSignedNumber(Glide_scale_x.Text) ?
InputValidate.IsSigned(Glide_scale_x.Text) ?
int.Parse(Glide_scale_x.Text) : 100000;
s.GlidePlugin.scale_y =
InputValidate.IsValidSignedNumber(Glide_scale_y.Text) ?
InputValidate.IsSigned(Glide_scale_y.Text) ?
int.Parse(Glide_scale_y.Text) : 100000;
s.GlidePlugin.UseDefaultHacks = GlideUseDefaultHacks1.Checked || GlideUseDefaultHacks2.Checked;
@ -186,22 +186,22 @@ namespace BizHawk.Client.EmuHawk
s.GlidePlugin.wrap_big_tex = Glide_wrap_big_tex.Checked;
s.GlidePlugin.depth_bias =
InputValidate.IsValidSignedNumber(Glide_depth_bias.Text) ?
InputValidate.IsSigned(Glide_depth_bias.Text) ?
int.Parse(Glide_depth_bias.Text) : 20;
s.GlidePlugin.filtering = Glide_filtering.SelectedIndex;
s.GlidePlugin.fix_tex_coord = InputValidate.IsValidSignedNumber(Glide_fix_tex_coord.Text) ?
s.GlidePlugin.fix_tex_coord = InputValidate.IsSigned(Glide_fix_tex_coord.Text) ?
int.Parse(Glide_fix_tex_coord.Text) : 0;
s.GlidePlugin.lodmode = Glide_lodmode.SelectedIndex;
s.GlidePlugin.stipple_mode =
InputValidate.IsValidSignedNumber(Glide_stipple_mode.Text) ?
InputValidate.IsSigned(Glide_stipple_mode.Text) ?
int.Parse(Glide_stipple_mode.Text) : 2;
s.GlidePlugin.stipple_pattern =
InputValidate.IsValidSignedNumber(Glide_stipple_pattern.Text) ?
InputValidate.IsSigned(Glide_stipple_pattern.Text) ?
int.Parse(Glide_stipple_pattern.Text) : 1041204192;
s.GlidePlugin.swapmode = Glide_swapmode.SelectedIndex;
@ -232,11 +232,11 @@ namespace BizHawk.Client.EmuHawk
s.Glide64mk2Plugin.swapmode = Glide64mk2_swapmode.SelectedIndex;
s.Glide64mk2Plugin.stipple_pattern =
InputValidate.IsValidSignedNumber(Glide64mk2_stipple_pattern.Text) ?
InputValidate.IsSigned(Glide64mk2_stipple_pattern.Text) ?
int.Parse(Glide64mk2_stipple_pattern.Text) : 1041204192;
s.Glide64mk2Plugin.stipple_mode =
InputValidate.IsValidSignedNumber(Glide64mk2_stipple_mode.Text) ?
InputValidate.IsSigned(Glide64mk2_stipple_mode.Text) ?
int.Parse(Glide64mk2_stipple_mode.Text) : 2;
s.Glide64mk2Plugin.lodmode = Glide64mk2_lodmode.SelectedIndex;
@ -636,7 +636,7 @@ namespace BizHawk.Client.EmuHawk
public int GetIntFromDB(string parameter, int defaultVal)
{
if (Global.Game.OptionPresent(parameter) && InputValidate.IsValidUnsignedNumber(Global.Game.OptionValue(parameter)))
if (Global.Game.OptionPresent(parameter) && InputValidate.IsUnsigned(Global.Game.OptionValue(parameter)))
{
return int.Parse(Global.Game.OptionValue(parameter));
}

View File

@ -1342,7 +1342,7 @@ namespace BizHawk.Client.EmuHawk
inputPrompt.SetMessage("Enter a hexadecimal value");
inputPrompt.ShowHawkDialog();
if (inputPrompt.UserOK && InputValidate.IsValidHexNumber(inputPrompt.UserText))
if (inputPrompt.UserOK && InputValidate.IsHex(inputPrompt.UserText))
{
GoToAddress(int.Parse(inputPrompt.UserText, NumberStyles.HexNumber));
}

View File

@ -81,7 +81,7 @@ namespace BizHawk.Client.EmuHawk
{
return;
}
else if (!InputValidate.IsValidHexNumber(e.KeyChar))
else if (!InputValidate.IsHex(e.KeyChar))
{
e.Handled = true;
}
@ -91,7 +91,7 @@ namespace BizHawk.Client.EmuHawk
{
return;
}
else if (!InputValidate.IsValidUnsignedNumber(e.KeyChar))
else if (!InputValidate.IsUnsigned(e.KeyChar))
{
e.Handled = true;
}
@ -101,7 +101,7 @@ namespace BizHawk.Client.EmuHawk
{
return;
}
else if (!InputValidate.IsValidSignedNumber(e.KeyChar))
else if (!InputValidate.IsSigned(e.KeyChar))
{
e.Handled = true;
}

View File

@ -150,15 +150,15 @@ namespace BizHawk.Client.EmuHawk
switch (_boxType)
{
case BoxType.Unsigned:
if (!InputValidate.IsValidUnsignedNumber(e.KeyChar))
if (!InputValidate.IsUnsigned(e.KeyChar))
e.Handled = true;
break;
case BoxType.Signed:
if (!InputValidate.IsValidSignedNumber(e.KeyChar))
if (!InputValidate.IsSigned(e.KeyChar))
e.Handled = true;
break;
case BoxType.Hex:
if (!InputValidate.IsValidHexNumber(e.KeyChar))
if (!InputValidate.IsHex(e.KeyChar))
e.Handled = true;
break;
}

View File

@ -76,12 +76,12 @@ namespace BizHawk.Client.EmuHawk
int x = 0;
int y = 0;
if (InputValidate.IsValidSignedNumber(buttons.Substring(14, 4)))
if (InputValidate.IsSigned(buttons.Substring(14, 4)))
{
x = Int32.Parse(buttons.Substring(14, 4));
}
if (InputValidate.IsValidSignedNumber(buttons.Substring(19, 4)))
if (InputValidate.IsSigned(buttons.Substring(19, 4)))
{
y = Int32.Parse(buttons.Substring(19, 4));
}

View File

@ -862,7 +862,7 @@ namespace BizHawk.Client.EmuHawk
if (prompt.UserOK)
{
if (InputValidate.IsValidHexNumber(prompt.UserText))
if (InputValidate.IsHex(prompt.UserText))
{
var addr = int.Parse(prompt.UserText, NumberStyles.HexNumber);
WatchListView.SelectItem(addr, true);

View File

@ -186,38 +186,38 @@ namespace BizHawk.Client.EmuHawk
{
default:
case Watch.DisplayType.Binary:
if (!InputValidate.IsValidBinaryNumber(e.KeyChar))
if (!InputValidate.IsBinary(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.FixedPoint_12_4:
case Watch.DisplayType.FixedPoint_20_12:
if (!InputValidate.IsValidFixedPointNumber(e.KeyChar))
if (!InputValidate.IsFixedPoint(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.Float:
if (!InputValidate.IsValidDecimalNumber(e.KeyChar))
if (!InputValidate.IsFloat(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.Hex:
if (!InputValidate.IsValidHexNumber(e.KeyChar))
if (!InputValidate.IsHex(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.Signed:
if (!InputValidate.IsValidSignedNumber(e.KeyChar))
if (!InputValidate.IsSigned(e.KeyChar))
{
e.Handled = true;
}
break;
case Watch.DisplayType.Unsigned:
if (!InputValidate.IsValidUnsignedNumber(e.KeyChar))
if (!InputValidate.IsUnsigned(e.KeyChar))
{
e.Handled = true;
}