2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
2019-09-19 02:58:23 +00:00
|
|
|
using System.Collections.Generic;
|
2019-03-21 05:13:09 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2016-07-09 22:30:12 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static partial class Util
|
|
|
|
{
|
|
|
|
/// <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>
|
|
|
|
public static int ToInt32(ReadOnlySpan<char> value)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
if (value.Length == 0)
|
|
|
|
return result;
|
2016-07-09 22:30:12 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
bool negative = false;
|
|
|
|
foreach (var c in value)
|
2016-07-09 22:30:12 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (char.IsAsciiDigit(c))
|
2019-02-09 02:29:26 +00:00
|
|
|
{
|
2020-01-02 19:11:31 +00:00
|
|
|
result *= 10;
|
2022-06-18 18:04:24 +00:00
|
|
|
result += c;
|
|
|
|
result -= '0';
|
|
|
|
}
|
|
|
|
else if (c == '-' && result == 0)
|
|
|
|
{
|
|
|
|
negative = true;
|
2019-02-09 02:29:26 +00:00
|
|
|
}
|
2016-07-09 22:30:12 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return negative ? -result : result;
|
|
|
|
}
|
2016-07-09 22:30:12 +00:00
|
|
|
|
2022-06-18 18:04:24 +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>
|
|
|
|
public static uint ToUInt32(ReadOnlySpan<char> value)
|
|
|
|
{
|
|
|
|
uint result = 0;
|
|
|
|
if (value.Length == 0)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
foreach (var c in value)
|
2016-07-09 22:30:12 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (!char.IsAsciiDigit(c))
|
2022-06-18 18:04:24 +00:00
|
|
|
continue;
|
|
|
|
result *= 10;
|
|
|
|
result += (uint)(c - '0');
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-02-09 02:29:26 +00:00
|
|
|
|
2022-06-18 18:04:24 +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(ReadOnlySpan<char> value)
|
|
|
|
{
|
|
|
|
uint result = 0;
|
|
|
|
if (value.Length == 0)
|
2019-02-09 02:29:26 +00:00
|
|
|
return result;
|
2020-02-13 02:10:03 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var c in value)
|
2020-02-13 02:10:03 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (char.IsAsciiDigit(c))
|
2020-02-13 02:10:03 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
result <<= 4;
|
|
|
|
result += (uint)(c - '0');
|
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
else if (char.IsAsciiHexDigitUpper(c))
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
result <<= 4;
|
|
|
|
result += (uint)(c - 'A' + 10);
|
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
else if (char.IsAsciiHexDigitLower(c))
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
result <<= 4;
|
|
|
|
result += (uint)(c - 'a' + 10);
|
2020-02-13 02:10:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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(ReadOnlySpan<char> value)
|
|
|
|
{
|
|
|
|
ulong result = 0;
|
|
|
|
if (value.Length == 0)
|
|
|
|
return result;
|
2016-07-09 22:30:12 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var c in value)
|
2019-06-09 02:56:11 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (char.IsAsciiDigit(c))
|
2021-05-10 06:33:54 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
result <<= 4;
|
|
|
|
result += (uint)(c - '0');
|
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
else if (char.IsAsciiHexDigitUpper(c))
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
result <<= 4;
|
|
|
|
result += (uint)(c - 'A' + 10);
|
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
else if (char.IsAsciiHexDigitLower(c))
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
result <<= 4;
|
|
|
|
result += (uint)(c - 'a' + 10);
|
2021-05-10 06:33:54 +00:00
|
|
|
}
|
2019-06-09 02:56:11 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
2019-06-09 02:56:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static byte[] GetBytesFromHexString(ReadOnlySpan<char> seed)
|
|
|
|
{
|
|
|
|
byte[] result = new byte[seed.Length / 2];
|
|
|
|
for (int i = 0; i < result.Length; i++)
|
2019-06-09 02:56:11 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var slice = seed.Slice(i * 2, 2);
|
|
|
|
result[^(i+1)] = (byte)GetHexValue(slice);
|
2019-06-09 02:56:11 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
2019-06-09 02:56:11 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private const string HexChars = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
public static string GetHexStringFromBytes(ReadOnlySpan<byte> data)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
System.Diagnostics.Debug.Assert(data.Length is (4 or 8 or 12 or 16));
|
|
|
|
Span<char> result = stackalloc char[data.Length * 2];
|
|
|
|
for (int i = 0; i < data.Length; i++)
|
|
|
|
{
|
|
|
|
// Write tuples from the opposite side of the result buffer.
|
|
|
|
var offset = (data.Length - i - 1) * 2;
|
|
|
|
result[offset + 0] = HexChars[data[i] >> 4];
|
|
|
|
result[offset + 1] = HexChars[data[i] & 0xF];
|
|
|
|
}
|
|
|
|
return new string(result);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-09-19 02:58:23 +00:00
|
|
|
|
2022-06-18 18:04:24 +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>
|
2023-01-22 04:02:33 +00:00
|
|
|
public static string GetOnlyHex(ReadOnlySpan<char> str)
|
2022-08-18 06:48:37 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (str.IsWhiteSpace())
|
2022-08-18 06:48:37 +00:00
|
|
|
return string.Empty;
|
2023-01-22 04:02:33 +00:00
|
|
|
|
|
|
|
int ctr = 0;
|
|
|
|
Span<char> result = stackalloc char[str.Length];
|
2022-08-18 06:48:37 +00:00
|
|
|
foreach (var c in str)
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (char.IsAsciiHexDigit(c))
|
|
|
|
result[ctr++] = c;
|
2022-08-18 06:48:37 +00:00
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
return new string(result[..ctr]);
|
2022-08-18 06:48:37 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a new string with each word converted to its appropriate title case.
|
|
|
|
/// </summary>
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <param name="span">Input string to modify</param>
|
|
|
|
public static string ToTitleCase(ReadOnlySpan<char> span)
|
2022-08-18 06:48:37 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (span.IsEmpty)
|
2022-08-18 06:48:37 +00:00
|
|
|
return string.Empty;
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
Span<char> result = stackalloc char[span.Length];
|
2022-08-18 06:48:37 +00:00
|
|
|
// Add each word to the string builder. Continue from the first index that isn't a space.
|
|
|
|
// Add the first character as uppercase, then add each successive character as lowercase.
|
|
|
|
bool first = true;
|
2023-01-22 04:02:33 +00:00
|
|
|
for (var i = 0; i < span.Length; i++)
|
2022-08-18 06:48:37 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
char c = span[i];
|
2022-08-18 06:48:37 +00:00
|
|
|
if (char.IsWhiteSpace(c))
|
|
|
|
{
|
|
|
|
first = true;
|
|
|
|
}
|
|
|
|
else if (first)
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
c = char.ToUpper(c);
|
2022-08-18 06:48:37 +00:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
c = char.ToLower(c);
|
2022-08-18 06:48:37 +00:00
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
result[i] = c;
|
2022-08-18 06:48:37 +00:00
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
return new string(result);
|
2022-08-18 06:48:37 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
/// <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[..index];
|
|
|
|
}
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public static Dictionary<string, int>[] GetMultiDictionary(IReadOnlyList<IReadOnlyList<string>> nameArray, int start)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var result = new Dictionary<string, int>[nameArray.Count];
|
|
|
|
for (int i = 0; i < result.Length; i++)
|
2022-08-27 06:43:36 +00:00
|
|
|
result[i] = GetDictionary(nameArray[i], start);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Dictionary<string, int> GetDictionary(IReadOnlyList<string> names, int start)
|
|
|
|
{
|
|
|
|
var result = new Dictionary<string, int>(names.Count - start);
|
|
|
|
for (int i = start; i < names.Count; i++)
|
|
|
|
result.Add(names[i], i);
|
2022-06-18 18:04:24 +00:00
|
|
|
return result;
|
2016-07-09 22:30:12 +00:00
|
|
|
}
|
|
|
|
}
|