code optimization
This commit is contained in:
@@ -1,89 +1,87 @@
|
||||
using System;
|
||||
using Microsoft.VisualBasic.CompilerServices;
|
||||
|
||||
namespace SM64Lib.TextValueConverter
|
||||
namespace SM64Lib.TextValueConverter;
|
||||
|
||||
public static class TextValueConverter
|
||||
{
|
||||
public static class TextValueConverter
|
||||
public static event WantIntegerValueModeEventHandler WantIntegerValueMode;
|
||||
public delegate void WantIntegerValueModeEventHandler(WantIntegerValueModeEventArgs e);
|
||||
|
||||
public static int ValueFromText(string Text, int DefaultValue = 0, int useIVM = -1)
|
||||
{
|
||||
public static event WantIntegerValueModeEventHandler WantIntegerValueMode;
|
||||
public delegate void WantIntegerValueModeEventHandler(WantIntegerValueModeEventArgs e);
|
||||
return Convert.ToInt32(LongFromText(Text, DefaultValue, useIVM));
|
||||
}
|
||||
|
||||
public static int ValueFromText(string Text, int DefaultValue = 0, int useIVM = -1)
|
||||
public static long LongFromText(string Text, long DefaultValue = 0, int useIVM = -1)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Convert.ToInt32(LongFromText(Text, DefaultValue, useIVM));
|
||||
}
|
||||
int IVM = useIVM > -1 ? useIVM : GetIntegerValueMode();
|
||||
Text = Text?.ToLower()?.Trim();
|
||||
|
||||
public static long LongFromText(string Text, long DefaultValue = 0, int useIVM = -1)
|
||||
{
|
||||
try
|
||||
if (string.IsNullOrWhiteSpace(Text))
|
||||
return 0;
|
||||
|
||||
switch (true)
|
||||
{
|
||||
int IVM = useIVM > -1 ? useIVM : GetIntegerValueMode();
|
||||
Text = Text?.ToLower()?.Trim();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Text))
|
||||
return 0;
|
||||
|
||||
switch (true)
|
||||
{
|
||||
case object _ when Text.StartsWith("0x"):
|
||||
case object _ when Text.StartsWith("&h"):
|
||||
return Convert.ToInt32(Text.Substring(2), 16);
|
||||
case object _ when Text.StartsWith("$"):
|
||||
return Convert.ToInt32(Text.Substring(1), 16);
|
||||
case object _ when Text.StartsWith("0b"):
|
||||
case object _ when Text.StartsWith("&b"):
|
||||
return Convert.ToInt32(Text.Substring(2), 2);
|
||||
default:
|
||||
return Convert.ToInt32(Text);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return DefaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public static string TextFromValue(long Value, int IVM = -1, int charCount = 0)
|
||||
{
|
||||
if (IVM == -1)
|
||||
{
|
||||
IVM = GetIntegerValueMode();
|
||||
}
|
||||
|
||||
switch (IVM)
|
||||
{
|
||||
case 0:
|
||||
return Value.ToString(GetCharCountAsZeroString(charCount));
|
||||
case 1:
|
||||
return "0x" + Value.ToString("X" + (charCount > 0 ? charCount.ToString() : ""));
|
||||
case 2:
|
||||
return "&H" + Value.ToString("X" + (charCount > 0 ? charCount.ToString() : ""));
|
||||
case 3:
|
||||
return "$" + Value.ToString("X" + (charCount > 0 ? charCount.ToString() : ""));
|
||||
case object _ when Text.StartsWith("0x"):
|
||||
case object _ when Text.StartsWith("&h"):
|
||||
return Convert.ToInt32(Text.Substring(2), 16);
|
||||
case object _ when Text.StartsWith("$"):
|
||||
return Convert.ToInt32(Text.Substring(1), 16);
|
||||
case object _ when Text.StartsWith("0b"):
|
||||
case object _ when Text.StartsWith("&b"):
|
||||
return Convert.ToInt32(Text.Substring(2), 2);
|
||||
default:
|
||||
return string.Empty;
|
||||
return Convert.ToInt32(Text);
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetCharCountAsZeroString(int charCount)
|
||||
catch (Exception)
|
||||
{
|
||||
string GetCharCountAsZeroStringRet = default;
|
||||
GetCharCountAsZeroStringRet = "";
|
||||
while (GetCharCountAsZeroStringRet.Length < charCount)
|
||||
GetCharCountAsZeroStringRet += "0";
|
||||
return GetCharCountAsZeroStringRet;
|
||||
}
|
||||
|
||||
private static int GetIntegerValueMode()
|
||||
{
|
||||
var e = new WantIntegerValueModeEventArgs();
|
||||
WantIntegerValueMode?.Invoke(e);
|
||||
return e.IntegerValueMode;
|
||||
return DefaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public class WantIntegerValueModeEventArgs : EventArgs
|
||||
public static string TextFromValue(long Value, int IVM = -1, int charCount = 0)
|
||||
{
|
||||
public int IntegerValueMode { get; set; }
|
||||
if (IVM == -1)
|
||||
{
|
||||
IVM = GetIntegerValueMode();
|
||||
}
|
||||
|
||||
switch (IVM)
|
||||
{
|
||||
case 0:
|
||||
return Value.ToString(GetCharCountAsZeroString(charCount));
|
||||
case 1:
|
||||
return "0x" + Value.ToString("X" + (charCount > 0 ? charCount.ToString() : ""));
|
||||
case 2:
|
||||
return "&H" + Value.ToString("X" + (charCount > 0 ? charCount.ToString() : ""));
|
||||
case 3:
|
||||
return "$" + Value.ToString("X" + (charCount > 0 ? charCount.ToString() : ""));
|
||||
default:
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetCharCountAsZeroString(int charCount)
|
||||
{
|
||||
string GetCharCountAsZeroStringRet = default;
|
||||
GetCharCountAsZeroStringRet = "";
|
||||
while (GetCharCountAsZeroStringRet.Length < charCount)
|
||||
GetCharCountAsZeroStringRet += "0";
|
||||
return GetCharCountAsZeroStringRet;
|
||||
}
|
||||
|
||||
private static int GetIntegerValueMode()
|
||||
{
|
||||
var e = new WantIntegerValueModeEventArgs();
|
||||
WantIntegerValueMode?.Invoke(e);
|
||||
return e.IntegerValueMode;
|
||||
}
|
||||
}
|
||||
|
||||
public class WantIntegerValueModeEventArgs : EventArgs
|
||||
{
|
||||
public int IntegerValueMode { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user