Merge InputValidate into StringExtensions

This commit is contained in:
adelikat 2014-07-03 16:00:57 +00:00
parent 07876e0f12
commit 1964f3754a
11 changed files with 224 additions and 198 deletions

View File

@ -504,7 +504,7 @@ namespace BizHawk.Client.Common
switch (Type)
{
case DisplayType.Unsigned:
if (InputValidate.IsUnsigned(value))
if ( value.IsUnsigned())
{
val = (byte)int.Parse(value);
}
@ -515,7 +515,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Signed:
if (InputValidate.IsSigned(value))
if (value.IsSigned())
{
val = (byte)(sbyte)int.Parse(value);
}
@ -526,7 +526,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Hex:
if (InputValidate.IsHex(value))
if (value.IsHex())
{
val = (byte)int.Parse(value, NumberStyles.HexNumber);
}
@ -537,7 +537,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Binary:
if (InputValidate.IsBinary(value))
if (value.IsBinary())
{
val = (byte)Convert.ToInt32(value, 2);
}
@ -709,7 +709,7 @@ namespace BizHawk.Client.Common
switch (Type)
{
case DisplayType.Unsigned:
if (InputValidate.IsUnsigned(value))
if (value.IsUnsigned())
{
val = (ushort)int.Parse(value);
}
@ -720,7 +720,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Signed:
if (InputValidate.IsSigned(value))
if (value.IsSigned())
{
val = (ushort)(short)int.Parse(value);
}
@ -731,7 +731,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Hex:
if (InputValidate.IsHex(value))
if (value.IsHex())
{
val = (ushort)int.Parse(value, NumberStyles.HexNumber);
}
@ -742,7 +742,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Binary:
if (InputValidate.IsBinary(value))
if (value.IsBinary())
{
val = (ushort)Convert.ToInt32(value, 2);
}
@ -753,7 +753,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.FixedPoint_12_4:
if (InputValidate.IsFixedPoint(value))
if (value.IsFixedPoint())
{
val = (ushort)(double.Parse(value) * 16.0);
}
@ -917,7 +917,7 @@ namespace BizHawk.Client.Common
switch (Type)
{
case DisplayType.Unsigned:
if (InputValidate.IsUnsigned(value))
if (value.IsUnsigned())
{
val = (uint)int.Parse(value);
}
@ -928,7 +928,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Signed:
if (InputValidate.IsSigned(value))
if (value.IsSigned())
{
val = (uint)int.Parse(value);
}
@ -939,7 +939,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Hex:
if (InputValidate.IsHex(value))
if (value.IsHex())
{
val = (uint)int.Parse(value, NumberStyles.HexNumber);
}
@ -950,7 +950,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.FixedPoint_20_12:
if (InputValidate.IsFixedPoint(value))
if (value.IsFixedPoint())
{
val = (uint)(int)(double.Parse(value) * 4096.0);
}
@ -961,7 +961,7 @@ namespace BizHawk.Client.Common
break;
case DisplayType.Float:
if (InputValidate.IsFloat(value))
if (value.IsFloat())
{
var bytes = BitConverter.GetBytes(float.Parse(value));
val = BitConverter.ToUInt32(bytes, 0);

View File

@ -2,7 +2,7 @@
using System.Globalization;
using System.Windows.Forms;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Common.NumberExtensions;
namespace BizHawk.Client.EmuHawk
@ -59,7 +59,7 @@ namespace BizHawk.Client.EmuHawk
return;
}
if (!InputValidate.IsHex(e.KeyChar))
if (!e.KeyChar.IsHex())
{
e.Handled = true;
}
@ -69,7 +69,7 @@ namespace BizHawk.Client.EmuHawk
{
if (e.KeyCode == Keys.Up)
{
if (InputValidate.IsHex(Text) && !string.IsNullOrEmpty(_addressFormatStr))
if (Text.IsHex() && !string.IsNullOrEmpty(_addressFormatStr))
{
var val = (uint)ToRawInt();
@ -87,7 +87,7 @@ namespace BizHawk.Client.EmuHawk
}
else if (e.KeyCode == Keys.Down)
{
if (InputValidate.IsHex(Text) && !string.IsNullOrEmpty(_addressFormatStr))
if (Text.IsHex() && !string.IsNullOrEmpty(_addressFormatStr))
{
var val = (uint)ToRawInt();
if (val == 0)
@ -157,7 +157,7 @@ namespace BizHawk.Client.EmuHawk
return;
}
if (!InputValidate.IsUnsigned(e.KeyChar))
if (!e.KeyChar.IsUnsigned())
{
e.Handled = true;
}
@ -172,7 +172,7 @@ namespace BizHawk.Client.EmuHawk
{
if (e.KeyCode == Keys.Up)
{
if (InputValidate.IsUnsigned(Text))
if (Text.IsHex())
{
var val = (uint)ToRawInt();
if (val == uint.MaxValue)
@ -189,7 +189,7 @@ namespace BizHawk.Client.EmuHawk
}
else if (e.KeyCode == Keys.Down)
{
if (InputValidate.IsUnsigned(Text))
if (Text.IsHex())
{
var val = (uint)ToRawInt();

View File

@ -3,13 +3,13 @@ using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Cores.Nintendo.N64;
using BizHawk.Client.EmuHawk.ControlExtensions;
using BizHawk.Client.Common;
using BizHawk.Emulation.Cores.Nintendo.N64;
using BizHawk.Common;
namespace BizHawk.Client.EmuHawk
{
public partial class N64VideoPluginconfig : Form
@ -157,12 +157,12 @@ namespace BizHawk.Client.EmuHawk
ss.RicePlugin.FastLoadTile = RiceFastLoadTile_CB.Checked;
ss.RicePlugin.UseSmallerTexture = RiceUseSmallerTexture_CB.Checked;
if (InputValidate.IsSigned(RiceVIWidth_Text.Text))
if (RiceVIWidth_Text.Text.IsSigned())
ss.RicePlugin.VIWidth = int.Parse(RiceVIWidth_Text.Text);
else
ss.RicePlugin.VIWidth = -1;
if (InputValidate.IsSigned(RiceVIHeight_Text.Text))
if (RiceVIHeight_Text.Text.IsSigned())
ss.RicePlugin.VIHeight = int.Parse(RiceVIHeight_Text.Text);
else
ss.RicePlugin.VIHeight = -1;
@ -204,19 +204,19 @@ namespace BizHawk.Client.EmuHawk
ss.GlidePlugin.fb_get_info = Glide_fb_get_info.Checked;
ss.GlidePlugin.offset_x =
InputValidate.IsSigned(Glide_offset_x.Text) ?
Glide_offset_x.Text.IsSigned() ?
int.Parse(Glide_offset_x.Text) : 0;
ss.GlidePlugin.offset_y =
InputValidate.IsSigned(Glide_offset_y.Text) ?
Glide_offset_y.Text.IsSigned() ?
int.Parse(Glide_offset_y.Text) : 0;
ss.GlidePlugin.scale_x =
InputValidate.IsSigned(Glide_scale_x.Text) ?
Glide_scale_x.Text.IsSigned() ?
int.Parse(Glide_scale_x.Text) : 100000;
ss.GlidePlugin.scale_y =
InputValidate.IsSigned(Glide_scale_y.Text) ?
Glide_scale_y.Text.IsSigned() ?
int.Parse(Glide_scale_y.Text) : 100000;
ss.GlidePlugin.UseDefaultHacks = GlideUseDefaultHacks1.Checked || GlideUseDefaultHacks2.Checked;
@ -245,22 +245,22 @@ namespace BizHawk.Client.EmuHawk
ss.GlidePlugin.wrap_big_tex = Glide_wrap_big_tex.Checked;
ss.GlidePlugin.depth_bias =
InputValidate.IsSigned(Glide_depth_bias.Text) ?
Glide_depth_bias.Text.IsSigned() ?
int.Parse(Glide_depth_bias.Text) : 20;
ss.GlidePlugin.filtering = Glide_filtering.SelectedIndex;
ss.GlidePlugin.fix_tex_coord = InputValidate.IsSigned(Glide_fix_tex_coord.Text) ?
ss.GlidePlugin.fix_tex_coord = Glide_fix_tex_coord.Text.IsSigned() ?
int.Parse(Glide_fix_tex_coord.Text) : 0;
ss.GlidePlugin.lodmode = Glide_lodmode.SelectedIndex;
ss.GlidePlugin.stipple_mode =
InputValidate.IsSigned(Glide_stipple_mode.Text) ?
Glide_stipple_mode.Text.IsSigned() ?
int.Parse(Glide_stipple_mode.Text) : 2;
ss.GlidePlugin.stipple_pattern =
InputValidate.IsSigned(Glide_stipple_pattern.Text) ?
Glide_stipple_pattern.Text.IsSigned() ?
int.Parse(Glide_stipple_pattern.Text) : 1041204192;
ss.GlidePlugin.swapmode = Glide_swapmode.SelectedIndex;
@ -291,11 +291,11 @@ namespace BizHawk.Client.EmuHawk
ss.Glide64mk2Plugin.swapmode = Glide64mk2_swapmode.SelectedIndex;
ss.Glide64mk2Plugin.stipple_pattern =
InputValidate.IsSigned(Glide64mk2_stipple_pattern.Text) ?
Glide64mk2_stipple_pattern.Text.IsSigned() ?
int.Parse(Glide64mk2_stipple_pattern.Text) : 1041204192;
ss.Glide64mk2Plugin.stipple_mode =
InputValidate.IsSigned(Glide64mk2_stipple_mode.Text) ?
Glide64mk2_stipple_mode.Text.IsSigned() ?
int.Parse(Glide64mk2_stipple_mode.Text) : 2;
ss.Glide64mk2Plugin.lodmode = Glide64mk2_lodmode.SelectedIndex;
@ -707,7 +707,7 @@ namespace BizHawk.Client.EmuHawk
public int GetIntFromDB(string parameter, int defaultVal)
{
if (Global.Game.OptionPresent(parameter) && InputValidate.IsUnsigned(Global.Game.OptionValue(parameter)))
if (Global.Game.OptionPresent(parameter) && Global.Game.OptionValue(parameter).IsUnsigned())
{
return int.Parse(Global.Game.OptionValue(parameter));
}

View File

@ -10,6 +10,7 @@ using System.Windows.Forms;
using BizHawk.Common;
using BizHawk.Common.NumberExtensions;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Client.Common;
@ -1376,7 +1377,7 @@ namespace BizHawk.Client.EmuHawk
if (data != null && data.GetDataPresent(DataFormats.Text))
{
var clipboardRaw = (string)data.GetData(DataFormats.Text);
var hex = InputValidate.DoHexString(clipboardRaw);
var hex = clipboardRaw.DoHexString();
var numBytes = hex.Length / 2;
for (var i = 0; i < numBytes; i++)
@ -1479,7 +1480,7 @@ namespace BizHawk.Client.EmuHawk
inputPrompt.SetMessage("Enter a hexadecimal value");
inputPrompt.ShowHawkDialog();
if (inputPrompt.UserOk && InputValidate.IsHex(inputPrompt.UserText))
if (inputPrompt.UserOk && inputPrompt.UserText.IsHex())
{
GoToAddress(int.Parse(inputPrompt.UserText, NumberStyles.HexNumber));
}

View File

@ -2,7 +2,7 @@
using System.Drawing;
using System.Windows.Forms;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Client.EmuHawk
{
@ -78,7 +78,7 @@ namespace BizHawk.Client.EmuHawk
return;
}
if (!InputValidate.IsHex(e.KeyChar))
if (!e.KeyChar.IsHex())
{
e.Handled = true;
}
@ -90,7 +90,7 @@ namespace BizHawk.Client.EmuHawk
return;
}
if (!InputValidate.IsUnsigned(e.KeyChar))
if (!e.KeyChar.IsUnsigned())
{
e.Handled = true;
}
@ -102,7 +102,7 @@ namespace BizHawk.Client.EmuHawk
return;
}
if (!InputValidate.IsSigned(e.KeyChar))
if (!e.KeyChar.IsSigned())
{
e.Handled = true;
}

View File

@ -2,7 +2,7 @@
using System.Globalization;
using System.Windows.Forms;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk
@ -150,15 +150,15 @@ namespace BizHawk.Client.EmuHawk
switch (_boxType)
{
case BoxType.Unsigned:
if (!InputValidate.IsUnsigned(e.KeyChar))
if (!e.KeyChar.IsUnsigned())
e.Handled = true;
break;
case BoxType.Signed:
if (!InputValidate.IsSigned(e.KeyChar))
if (!e.KeyChar.IsSigned())
e.Handled = true;
break;
case BoxType.Hex:
if (!InputValidate.IsHex(e.KeyChar))
if (!e.KeyChar.IsHex())
e.Handled = true;
break;
}

View File

@ -9,7 +9,7 @@ using System.Reflection;
using System.Text;
using System.Windows.Forms;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Common.NumberExtensions;
using BizHawk.Client.Common;
@ -918,7 +918,7 @@ namespace BizHawk.Client.EmuHawk
if (prompt.UserOk)
{
if (InputValidate.IsHex(prompt.UserText))
if (prompt.UserText.IsHex())
{
var addr = int.Parse(prompt.UserText, NumberStyles.HexNumber);
WatchListView.SelectItem(addr, true);

View File

@ -2,7 +2,7 @@
using System.Globalization;
using System.Windows.Forms;
using BizHawk.Common;
using BizHawk.Common.StringExtensions;
using BizHawk.Common.NumberExtensions;
using BizHawk.Client.Common;
@ -271,7 +271,7 @@ namespace BizHawk.Client.EmuHawk
{
default:
case Watch.DisplayType.Binary:
if (!InputValidate.IsBinary(e.KeyChar))
if (!e.KeyChar.IsBinary())
{
e.Handled = true;
}
@ -279,35 +279,35 @@ namespace BizHawk.Client.EmuHawk
break;
case Watch.DisplayType.FixedPoint_12_4:
case Watch.DisplayType.FixedPoint_20_12:
if (!InputValidate.IsFixedPoint(e.KeyChar))
if (!e.KeyChar.IsFixedPoint())
{
e.Handled = true;
}
break;
case Watch.DisplayType.Float:
if (!InputValidate.IsFloat(e.KeyChar))
if (!e.KeyChar.IsFloat())
{
e.Handled = true;
}
break;
case Watch.DisplayType.Hex:
if (!InputValidate.IsHex(e.KeyChar))
if (!e.KeyChar.IsHex())
{
e.Handled = true;
}
break;
case Watch.DisplayType.Signed:
if (!InputValidate.IsSigned(e.KeyChar))
if (!e.KeyChar.IsSigned())
{
e.Handled = true;
}
break;
case Watch.DisplayType.Unsigned:
if (!InputValidate.IsUnsigned(e.KeyChar))
if (!e.KeyChar.IsUnsigned())
{
e.Handled = true;
}

View File

@ -57,7 +57,6 @@
<Compile Include="Extensions\NumberExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="HawkFile.cs" />
<Compile Include="InputValidate.cs" />
<Compile Include="IPC\IPCRingBuffer.cs" />
<Compile Include="IPC\SharedMemoryBlock.cs" />
<Compile Include="Log.cs" />

View File

@ -1,16 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BizHawk.Common.StringExtensions
{
public static class StringExtensions
{
public static bool IsBinary(this string str)
{
return str.All(c => c == '0' || c == '1');
}
public static string GetPrecedingString(this string str, string value)
{
var index = str.IndexOf(value);
@ -66,6 +62,11 @@ namespace BizHawk.Common.StringExtensions
public static int HowMany(this string str, string s)
{
if (str == null)
{
return 0;
}
var count = 0;
for (var i = 0; i < (str.Length - s.Length); i++)
{
@ -77,5 +78,166 @@ namespace BizHawk.Common.StringExtensions
return count;
}
#region String and Char validation extensions
/// <summary>
/// Validates all chars are 0-9
/// </summary>
public static bool IsUnsigned(this string str)
{
if (str == null)
{
return false;
}
return str.All(IsUnsigned);
}
/// <summary>
/// Validates the char is 0-9
/// </summary>
public static bool IsUnsigned(this char c)
{
return char.IsDigit(c);
}
/// <summary>
/// Validates all chars are 0-9, or a dash as the first value
/// </summary>
public static bool IsSigned(this string str)
{
if (str == null)
{
return false;
}
return str[0].IsSigned() && str.Substring(1).All(IsUnsigned);
}
/// <summary>
/// Validates the char is 0-9 or a dash
/// </summary>
public static bool IsSigned(this char c)
{
return char.IsDigit(c) || c == '-';
}
/// <summary>
/// Validates all chars are 0-9, A-F or a-f
/// </summary>
public static bool IsHex(this string str)
{
if (str == null)
{
return false;
}
return str.All(IsHex);
}
/// <summary>
/// Validates the char is 0-9, A-F or a-f
/// </summary>
public static bool IsHex(this char c)
{
if (char.IsDigit(c))
{
return true;
}
return char.ToUpper(c) >= 'A' && char.ToUpper(c) <= 'F';
}
/// <summary>
/// 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
/// </summary>
public static string DoHexString(this string raw)
{
if (raw == null)
{
return string.Empty;
}
var output = new StringBuilder();
foreach (var chr in raw)
{
if (IsHex(chr))
{
output.Append(char.ToUpper(chr));
}
}
return output.ToString();
}
/// <summary>
/// Validates all chars are 0 or 1
/// </summary>
public static bool IsBinary(this string str)
{
if (str == null)
{
return false;
}
return str.All(IsBinary);
}
/// <summary>
/// Validates the char is 0 or 1
/// </summary>
public static bool IsBinary(this char c)
{
return c == '0' || c == '1';
}
/// <summary>
/// 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 IsFixedPoint(this string str)
{
if (str == null)
{
return false;
}
return str.HowMany('.') <= 1
&& str.All(IsFixedPoint);
}
/// <summary>
/// Validates the char is 0-9, a dash, or a decimal
/// </summary>
public static bool IsFixedPoint(this char c)
{
return c.IsUnsigned() || c == '.';
}
/// <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(this string str)
{
if (str == null)
{
return false;
}
return str.HowMany('.') <= 1
&& str[0].IsFloat()
&& str.Substring(1).All(IsFixedPoint);
}
/// <summary>
/// Validates that the char is 0-9, a dash, or a decimal point
/// </summary>
public static bool IsFloat(this char c)
{
return c.IsFixedPoint() || c == '-';
}
#endregion
}
}

View File

@ -1,136 +0,0 @@
using System.Text;
namespace BizHawk.Common
{
using System.Linq;
using BizHawk.Common.StringExtensions;
/// <summary>
/// Includes helper functions to validate user input
/// </summary>
public static class InputValidate
{
/// <summary>
/// Validates all chars are 0-9
/// </summary>
public static bool IsUnsigned(string str)
{
return str.All(IsUnsigned);
}
/// <summary>
/// Validates the char is 0-9
/// </summary>
public static bool IsUnsigned(char c)
{
return char.IsDigit(c);
}
/// <summary>
/// Validates all chars are 0-9, or a dash as the first value
/// </summary>
public static bool IsSigned(string str)
{
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;
}
return char.ToUpper(c) >= 'A' && char.ToUpper(c) <= 'F';
}
/// <summary>
/// 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
/// </summary>
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();
}
/// <summary>
/// Validates all chars are 0 or 1
/// </summary>
public static bool IsBinary(string str)
{
return str.All(IsBinary);
}
/// <summary>
/// Validates the char is 0 or 1
/// </summary>
public static bool IsBinary(char c)
{
return c == '0' || c == '1';
}
/// <summary>
/// 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 IsFixedPoint(string str)
{
return str.HowMany('.') <= 1
&& str.All(IsFixedPoint);
}
/// <summary>
/// Validates the char is 0-9, a dash, or a decimal
/// </summary>
public static bool IsFixedPoint(char c)
{
return IsUnsigned(c) || c == '.';
}
/// <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 str.HowMany('.') <= 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 == '-';
}
}
}