diff --git a/BizHawk.Client.Common/tools/Watch.cs b/BizHawk.Client.Common/tools/Watch.cs
index 065f89f62f..91acbdd795 100644
--- a/BizHawk.Client.Common/tools/Watch.cs
+++ b/BizHawk.Client.Common/tools/Watch.cs
@@ -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);
diff --git a/BizHawk.Client.EmuHawk/CustomControls/HexTextBox.cs b/BizHawk.Client.EmuHawk/CustomControls/HexTextBox.cs
index 17ffdc0eab..616ebeabf6 100644
--- a/BizHawk.Client.EmuHawk/CustomControls/HexTextBox.cs
+++ b/BizHawk.Client.EmuHawk/CustomControls/HexTextBox.cs
@@ -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();
diff --git a/BizHawk.Client.EmuHawk/config/N64/N64VideoPluginconfig.cs b/BizHawk.Client.EmuHawk/config/N64/N64VideoPluginconfig.cs
index 1cb0b7ae3d..2c8c420f57 100644
--- a/BizHawk.Client.EmuHawk/config/N64/N64VideoPluginconfig.cs
+++ b/BizHawk.Client.EmuHawk/config/N64/N64VideoPluginconfig.cs
@@ -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));
}
diff --git a/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs b/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs
index 259434a9a1..626880185c 100644
--- a/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs
+++ b/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs
@@ -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));
}
diff --git a/BizHawk.Client.EmuHawk/tools/InputPrompt.cs b/BizHawk.Client.EmuHawk/tools/InputPrompt.cs
index 08798342d8..07f33b4dc6 100644
--- a/BizHawk.Client.EmuHawk/tools/InputPrompt.cs
+++ b/BizHawk.Client.EmuHawk/tools/InputPrompt.cs
@@ -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;
}
diff --git a/BizHawk.Client.EmuHawk/tools/Lua/LuaTextBox.cs b/BizHawk.Client.EmuHawk/tools/Lua/LuaTextBox.cs
index b0226d4065..971f29157c 100644
--- a/BizHawk.Client.EmuHawk/tools/Lua/LuaTextBox.cs
+++ b/BizHawk.Client.EmuHawk/tools/Lua/LuaTextBox.cs
@@ -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;
}
diff --git a/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs b/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs
index 7539362e5b..cb05f31090 100644
--- a/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs
+++ b/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs
@@ -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);
diff --git a/BizHawk.Client.EmuHawk/tools/Watch/WatchValueBox.cs b/BizHawk.Client.EmuHawk/tools/Watch/WatchValueBox.cs
index 68386e216c..2a969dc2ea 100644
--- a/BizHawk.Client.EmuHawk/tools/Watch/WatchValueBox.cs
+++ b/BizHawk.Client.EmuHawk/tools/Watch/WatchValueBox.cs
@@ -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;
}
diff --git a/BizHawk.Common/BizHawk.Common.csproj b/BizHawk.Common/BizHawk.Common.csproj
index 9df2146e11..656bb4e2b6 100644
--- a/BizHawk.Common/BizHawk.Common.csproj
+++ b/BizHawk.Common/BizHawk.Common.csproj
@@ -57,7 +57,6 @@
-
diff --git a/BizHawk.Common/Extensions/StringExtensions.cs b/BizHawk.Common/Extensions/StringExtensions.cs
index b50734643b..76299aadfa 100644
--- a/BizHawk.Common/Extensions/StringExtensions.cs
+++ b/BizHawk.Common/Extensions/StringExtensions.cs
@@ -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
+
+ ///
+ /// Validates all chars are 0-9
+ ///
+ public static bool IsUnsigned(this string str)
+ {
+ if (str == null)
+ {
+ return false;
+ }
+
+ return str.All(IsUnsigned);
+ }
+
+ ///
+ /// Validates the char is 0-9
+ ///
+ public static bool IsUnsigned(this char c)
+ {
+ return char.IsDigit(c);
+ }
+
+ ///
+ /// Validates all chars are 0-9, or a dash as the first value
+ ///
+ public static bool IsSigned(this string str)
+ {
+ if (str == null)
+ {
+ return false;
+ }
+
+ return str[0].IsSigned() && str.Substring(1).All(IsUnsigned);
+ }
+
+ ///
+ /// Validates the char is 0-9 or a dash
+ ///
+ public static bool IsSigned(this char c)
+ {
+ return char.IsDigit(c) || c == '-';
+ }
+
+ ///
+ /// Validates all chars are 0-9, A-F or a-f
+ ///
+ public static bool IsHex(this string str)
+ {
+ if (str == null)
+ {
+ return false;
+ }
+
+ return str.All(IsHex);
+ }
+
+ ///
+ /// Validates the char is 0-9, A-F or a-f
+ ///
+ public static bool IsHex(this 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(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();
+ }
+
+ ///
+ /// Validates all chars are 0 or 1
+ ///
+ public static bool IsBinary(this string str)
+ {
+ if (str == null)
+ {
+ return false;
+ }
+
+ return str.All(IsBinary);
+ }
+
+ ///
+ /// Validates the char is 0 or 1
+ ///
+ public static bool IsBinary(this 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(this string str)
+ {
+ if (str == null)
+ {
+ return false;
+ }
+
+ return str.HowMany('.') <= 1
+ && str.All(IsFixedPoint);
+ }
+
+ ///
+ /// Validates the char is 0-9, a dash, or a decimal
+ ///
+ public static bool IsFixedPoint(this char c)
+ {
+ return c.IsUnsigned() || 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(this string str)
+ {
+ if (str == null)
+ {
+ return false;
+ }
+
+ return str.HowMany('.') <= 1
+ && str[0].IsFloat()
+ && str.Substring(1).All(IsFixedPoint);
+ }
+
+ ///
+ /// Validates that the char is 0-9, a dash, or a decimal point
+ ///
+ public static bool IsFloat(this char c)
+ {
+ return c.IsFixedPoint() || c == '-';
+ }
+
+ #endregion
}
}
diff --git a/BizHawk.Common/InputValidate.cs b/BizHawk.Common/InputValidate.cs
deleted file mode 100644
index cec68bdc44..0000000000
--- a/BizHawk.Common/InputValidate.cs
+++ /dev/null
@@ -1,136 +0,0 @@
-using System.Text;
-
-namespace BizHawk.Common
-{
- using System.Linq;
-
- using BizHawk.Common.StringExtensions;
-
- ///
- /// 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 str.HowMany('.') <= 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 str.HowMany('.') <= 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 == '-';
- }
- }
-}