2016-07-09 22:30:12 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-07-09 22:30:12 +00:00
|
|
|
|
{
|
2018-05-12 19:28:48 +00:00
|
|
|
|
public static partial class Util
|
2016-07-09 22:30:12 +00:00
|
|
|
|
{
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static int ToInt32(string value)
|
2016-07-09 22:30:12 +00:00
|
|
|
|
{
|
|
|
|
|
string val = value?.Replace(" ", "").Replace("_", "").Trim();
|
2016-07-22 05:45:20 +00:00
|
|
|
|
return string.IsNullOrWhiteSpace(val) ? 0 : int.Parse(val);
|
2016-07-09 22:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static uint ToUInt32(string value)
|
2016-07-09 22:30:12 +00:00
|
|
|
|
{
|
|
|
|
|
string val = value?.Replace(" ", "").Replace("_", "").Trim();
|
2016-07-22 05:45:20 +00:00
|
|
|
|
return string.IsNullOrWhiteSpace(val) ? 0 : uint.Parse(val);
|
2016-07-09 22:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static uint GetHexValue(string s)
|
2016-07-09 22:30:12 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
string str = GetOnlyHex(s);
|
2016-07-22 05:45:20 +00:00
|
|
|
|
return string.IsNullOrWhiteSpace(str) ? 0 : Convert.ToUInt32(str, 16);
|
2016-07-09 22:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-07 18:20:05 +00:00
|
|
|
|
private static bool IsHex(char c) => (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
|
2018-07-26 21:55:45 +00:00
|
|
|
|
private static string TitleCase(string word) => char.ToUpper(word[0]) + word.Substring(1, word.Length - 1).ToLower();
|
2017-05-12 04:34:18 +00:00
|
|
|
|
|
2018-07-07 18:20:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Filters the string down to only valid hex characters, returning a new string.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str">Input string to filter</param>
|
|
|
|
|
public static string GetOnlyHex(string str) => string.IsNullOrWhiteSpace(str) ? string.Empty : string.Concat(str.Where(IsHex));
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a new string with each word converted to its appropriate title case.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="str">Input string to modify</param>
|
|
|
|
|
public static string ToTitleCase(string str) => string.IsNullOrWhiteSpace(str) ? string.Empty : string.Join(" ", str.Split(' ').Select(TitleCase));
|
2016-07-09 22:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|