PKHeX/PKHeX.Core/Util/StringUtil.cs
Kurt 88830e0d00
Update from .NET Framework 4.6 to .NET 7 (#3729)
Updates from net46->net7, dropping support for mono in favor of using the latest runtime (along with the performance/API improvements). Releases will be posted as 64bit only for now.

Refactors a good amount of internal API methods to be more performant and more customizable for future updates & fixes.

Adds functionality for Batch Editor commands to `>`, `<` and <=/>=

TID/SID properties renamed to TID16/SID16 for clarity; other properties exposed for Gen7 / display variants.

Main window has a new layout to account for DPI scaling (8 point grid)

Fixed: Tatsugiri and Paldean Tauros now output Showdown form names as Showdown expects
Changed: Gen9 species now interact based on the confirmed National Dex IDs (closes #3724)
Fixed: Pokedex set all no longer clears species with unavailable non-base forms (closes #3720)
Changed: Hyper Training suggestions now apply for level 50 in SV. (closes #3714)
Fixed: B2/W2 hatched egg met locations exclusive to specific versions are now explicitly checked (closes #3691)
Added: Properties for ribbon/mark count (closes #3659)
Fixed: Traded SV eggs are now checked correctly (closes #3692)
2023-01-21 20:02:33 -08:00

98 lines
3.2 KiB
C#

using System;
using System.Globalization;
namespace PKHeX.Core;
/// <summary>
/// Utility for searching strings within arrays or within another string.
/// </summary>
public static class StringUtil
{
private static readonly CompareInfo CompareInfo = CultureInfo.CurrentCulture.CompareInfo;
/// <summary>
/// Finds the index of the string within the array by ignoring casing, spaces, and punctuation.
/// </summary>
/// <param name="arr">Array of strings to search in</param>
/// <param name="value">Value to search for</param>
/// <returns>Index within <see cref="arr"/></returns>
public static int FindIndexIgnoreCase(string[] arr, ReadOnlySpan<char> value)
{
for (int i = 0; i < arr.Length; i++)
{
if (IsMatchIgnoreCase(arr[i], value))
return i;
}
return -1;
}
public static bool IsMatchIgnoreCase(ReadOnlySpan<char> string1, ReadOnlySpan<char> string2)
{
if (string1.Length != string2.Length)
return false;
const CompareOptions options = CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreWidth;
var compare = CompareInfo.Compare(string1, string2, options);
return compare == 0;
}
/// <summary>
/// Gets the <see cref="nth"/> string entry within the input <see cref="line"/>, based on the <see cref="separator"/> and <see cref="start"/> position.
/// </summary>
public static string GetNthEntry(ReadOnlySpan<char> line, int nth, int start, char separator = ',')
{
if (nth != 1)
start = line.IndexOfNth(separator, nth - 1, start + 1);
var end = line.IndexOfNth(separator, 1, start + 1);
if (end == -1)
return new string(line[(start + 1)..]);
return new string(line[(start + 1)..end]);
}
private static int IndexOfNth(this ReadOnlySpan<char> s, char t, int n, int start)
{
int count = 0;
for (int i = start; i < s.Length; i++)
{
if (s[i] != t)
continue;
if (++count == n)
return i;
}
return -1;
}
/// <summary>
/// Converts an all-caps hex string to a byte array.
/// </summary>
public static byte[] ToByteArray(this string toTransform)
{
var result = new byte[toTransform.Length / 2];
for (int i = 0; i < result.Length; i++)
{
var ofs = i << 1;
var _0 = toTransform[ofs + 0];
var _1 = toTransform[ofs + 1];
result[i] = DecodeTuple(_0, _1);
}
return result;
}
private static byte DecodeTuple(char _0, char _1)
{
byte result;
if (char.IsAsciiDigit(_0))
result = (byte)((_0 - '0') << 4);
else if (char.IsAsciiHexDigitUpper(_0))
result = (byte)((_0 - 'A' + 10) << 4);
else
throw new ArgumentOutOfRangeException(nameof(_0));
if (char.IsAsciiDigit(_1))
result |= (byte)(_1 - '0');
else if (char.IsAsciiHexDigitUpper(_1))
result |= (byte)(_1 - 'A' + 10);
else
throw new ArgumentOutOfRangeException(nameof(_1));
return result;
}
}