2019-06-09 02:56:11 +00:00
|
|
|
|
using System;
|
2019-09-19 02:58:23 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-06-09 02:56:11 +00:00
|
|
|
|
using System.Linq;
|
2019-03-21 05:13:09 +00:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2016-07-09 22:30:12 +00:00
|
|
|
|
|
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
|
|
|
|
{
|
2019-02-09 02:29:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parses the string into an <see cref="int"/>, skipping all characters except for valid digits.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">String to parse</param>
|
|
|
|
|
/// <returns>Parsed value</returns>
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static int ToInt32(string value)
|
2016-07-09 22:30:12 +00:00
|
|
|
|
{
|
2019-02-09 02:29:26 +00:00
|
|
|
|
int result = 0;
|
|
|
|
|
if (string.IsNullOrEmpty(value))
|
|
|
|
|
return result;
|
|
|
|
|
|
2020-01-02 19:11:31 +00:00
|
|
|
|
bool negative = false;
|
|
|
|
|
foreach (var c in value)
|
2019-02-09 02:29:26 +00:00
|
|
|
|
{
|
|
|
|
|
if (IsNum(c))
|
|
|
|
|
{
|
|
|
|
|
result *= 10;
|
|
|
|
|
result += c;
|
|
|
|
|
result -= '0';
|
|
|
|
|
}
|
2020-01-02 19:11:31 +00:00
|
|
|
|
else if (c == '-' && result == 0)
|
2019-02-09 02:29:26 +00:00
|
|
|
|
{
|
2020-01-02 19:11:31 +00:00
|
|
|
|
negative = true;
|
2019-02-09 02:29:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-02 19:11:31 +00:00
|
|
|
|
return negative ? -result : result;
|
2016-07-09 22:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 02:29:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parses the string into a <see cref="uint"/>, skipping all characters except for valid digits.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">String to parse</param>
|
|
|
|
|
/// <returns>Parsed value</returns>
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static uint ToUInt32(string value)
|
2016-07-09 22:30:12 +00:00
|
|
|
|
{
|
2019-02-09 02:29:26 +00:00
|
|
|
|
uint result = 0;
|
|
|
|
|
if (string.IsNullOrEmpty(value))
|
|
|
|
|
return result;
|
|
|
|
|
|
2020-01-02 19:11:31 +00:00
|
|
|
|
foreach (var c in value)
|
2019-02-09 02:29:26 +00:00
|
|
|
|
{
|
2020-01-02 19:11:31 +00:00
|
|
|
|
if (!IsNum(c))
|
|
|
|
|
continue;
|
|
|
|
|
result *= 10;
|
|
|
|
|
result += c;
|
|
|
|
|
result -= '0';
|
2019-02-09 02:29:26 +00:00
|
|
|
|
}
|
|
|
|
|
return result;
|
2016-07-09 22:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 02:29:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parses the hex string into a <see cref="uint"/>, skipping all characters except for valid digits.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">Hex String to parse</param>
|
|
|
|
|
/// <returns>Parsed value</returns>
|
|
|
|
|
public static uint GetHexValue(string value)
|
2016-07-09 22:30:12 +00:00
|
|
|
|
{
|
2019-02-09 02:29:26 +00:00
|
|
|
|
uint result = 0;
|
|
|
|
|
if (string.IsNullOrEmpty(value))
|
|
|
|
|
return result;
|
|
|
|
|
|
2020-01-02 19:11:31 +00:00
|
|
|
|
foreach (var c in value)
|
2019-02-09 02:29:26 +00:00
|
|
|
|
{
|
2019-02-10 18:19:05 +00:00
|
|
|
|
if (IsNum(c))
|
2019-02-09 02:29:26 +00:00
|
|
|
|
{
|
|
|
|
|
result <<= 4;
|
2019-02-10 18:19:05 +00:00
|
|
|
|
result += (uint)(c - '0');
|
|
|
|
|
}
|
|
|
|
|
else if (IsHexUpper(c))
|
|
|
|
|
{
|
|
|
|
|
result <<= 4;
|
|
|
|
|
result += (uint)(c - 'A' + 10);
|
|
|
|
|
}
|
|
|
|
|
else if (IsHexLower(c))
|
|
|
|
|
{
|
|
|
|
|
result <<= 4;
|
|
|
|
|
result += (uint)(c - 'a' + 10);
|
2019-02-09 02:29:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2016-07-09 22:30:12 +00:00
|
|
|
|
}
|
2020-02-13 02:10:03 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parses the hex string into a <see cref="ulong"/>, skipping all characters except for valid digits.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">Hex String to parse</param>
|
|
|
|
|
/// <returns>Parsed value</returns>
|
|
|
|
|
public static ulong GetHexValue64(string value)
|
|
|
|
|
{
|
|
|
|
|
ulong result = 0;
|
|
|
|
|
if (string.IsNullOrEmpty(value))
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
foreach (var c in value)
|
|
|
|
|
{
|
|
|
|
|
if (IsNum(c))
|
|
|
|
|
{
|
|
|
|
|
result <<= 4;
|
|
|
|
|
result += (uint)(c - '0');
|
|
|
|
|
}
|
|
|
|
|
else if (IsHexUpper(c))
|
|
|
|
|
{
|
|
|
|
|
result <<= 4;
|
|
|
|
|
result += (uint)(c - 'A' + 10);
|
|
|
|
|
}
|
|
|
|
|
else if (IsHexLower(c))
|
|
|
|
|
{
|
|
|
|
|
result <<= 4;
|
|
|
|
|
result += (uint)(c - 'a' + 10);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2016-07-09 22:30:12 +00:00
|
|
|
|
|
2019-06-09 02:56:11 +00:00
|
|
|
|
public static byte[] GetBytesFromHexString(string seed)
|
|
|
|
|
{
|
|
|
|
|
return Enumerable.Range(0, seed.Length)
|
|
|
|
|
.Where(x => x % 2 == 0)
|
|
|
|
|
.Select(x => Convert.ToByte(seed.Substring(x, 2), 16))
|
|
|
|
|
.Reverse().ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetHexStringFromBytes(byte[] data, int offset, int length)
|
|
|
|
|
{
|
|
|
|
|
data = data.Skip(offset).Take(length).Reverse().ToArray();
|
|
|
|
|
return BitConverter.ToString(data).Replace("-", string.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-02 19:11:31 +00:00
|
|
|
|
private static bool IsNum(char c) => (uint)(c - '0') <= 9;
|
|
|
|
|
private static bool IsHexUpper(char c) => (uint)(c - 'A') <= 5;
|
|
|
|
|
private static bool IsHexLower(char c) => (uint)(c - 'a') <= 5;
|
2019-02-10 18:19:05 +00:00
|
|
|
|
private static bool IsHex(char c) => IsNum(c) || IsHexUpper(c) || IsHexLower(c);
|
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));
|
2019-03-21 05:13:09 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Trims a string at the first instance of a 0xFFFF terminator.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">String to trim.</param>
|
|
|
|
|
/// <returns>Trimmed string.</returns>
|
|
|
|
|
public static string TrimFromFFFF(string input) => TrimFromFirst(input, (char)0xFFFF);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Trims a string at the first instance of a 0x0000 terminator.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">String to trim.</param>
|
|
|
|
|
/// <returns>Trimmed string.</returns>
|
|
|
|
|
public static string TrimFromZero(string input) => TrimFromFirst(input, '\0');
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
private static string TrimFromFirst(string input, char c)
|
|
|
|
|
{
|
|
|
|
|
int index = input.IndexOf(c);
|
|
|
|
|
return index < 0 ? input : input.Substring(0, index);
|
|
|
|
|
}
|
2019-09-19 02:58:23 +00:00
|
|
|
|
|
|
|
|
|
public static Dictionary<string, int>[] GetMultiDictionary(IReadOnlyList<IReadOnlyList<string>> nameArray)
|
|
|
|
|
{
|
|
|
|
|
var result = new Dictionary<string, int>[nameArray.Count];
|
|
|
|
|
for (int i = 0; i < result.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var dict = result[i] = new Dictionary<string, int>();
|
|
|
|
|
var names = nameArray[i];
|
|
|
|
|
for (int j = 0; j < names.Count; j++)
|
|
|
|
|
dict.Add(names[j], j);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2016-07-09 22:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|