2022-11-25 01:42:17 +00:00
|
|
|
using System;
|
2019-03-21 04:50:44 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logic for converting a <see cref="string"/> between the various generation specific encoding formats.
|
|
|
|
/// </summary>
|
|
|
|
public static class StringConverter
|
2019-03-21 04:50:44 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2022-01-03 05:35:59 +00:00
|
|
|
/// Converts bytes to a string according to the input parameters.
|
2019-03-21 04:50:44 +00:00
|
|
|
/// </summary>
|
2022-01-03 05:35:59 +00:00
|
|
|
/// <param name="data">Encoded data</param>
|
|
|
|
/// <param name="generation">Generation string format</param>
|
|
|
|
/// <param name="jp">Encoding is Japanese</param>
|
|
|
|
/// <param name="isBigEndian">Encoding is Big Endian</param>
|
|
|
|
/// <returns>Decoded string.</returns>
|
|
|
|
public static string GetString(ReadOnlySpan<byte> data, int generation, bool jp, bool isBigEndian = false) => generation switch
|
2019-03-21 04:50:44 +00:00
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
3 when isBigEndian => StringConverter3GC.GetString(data),
|
|
|
|
4 when isBigEndian => StringConverter4GC.GetString(data),
|
|
|
|
|
|
|
|
1 or 2 => StringConverter12.GetString(data, jp),
|
|
|
|
3 => StringConverter3.GetString(data, jp),
|
|
|
|
4 => StringConverter4.GetString(data),
|
|
|
|
5 => StringConverter5.GetString(data),
|
|
|
|
6 => StringConverter6.GetString(data),
|
|
|
|
7 => StringConverter7.GetString(data),
|
|
|
|
8 => StringConverter8.GetString(data),
|
2022-11-25 01:42:17 +00:00
|
|
|
9 => StringConverter8.GetString(data),
|
2022-01-03 05:35:59 +00:00
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(generation)),
|
|
|
|
};
|
2021-01-15 06:50:13 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Converts bytes to a string according to the input parameters.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">Encoded data</param>
|
|
|
|
/// <param name="result">Decoded character result buffer</param>
|
|
|
|
/// <param name="generation">Generation string format</param>
|
|
|
|
/// <param name="jp">Encoding is Japanese</param>
|
|
|
|
/// <param name="isBigEndian">Encoding is Big Endian</param>
|
|
|
|
/// <returns>Decoded string.</returns>
|
|
|
|
public static int LoadString(ReadOnlySpan<byte> data, Span<char> result, int generation, bool jp, bool isBigEndian = false) => generation switch
|
|
|
|
{
|
|
|
|
3 when isBigEndian => StringConverter3GC.LoadString(data, result),
|
|
|
|
4 when isBigEndian => StringConverter4GC.LoadString(data, result),
|
|
|
|
|
|
|
|
1 or 2 => StringConverter12.LoadString(data, result, jp),
|
|
|
|
3 => StringConverter3.LoadString(data, result, jp),
|
|
|
|
4 => StringConverter4.LoadString(data, result),
|
|
|
|
5 => StringConverter5.LoadString(data, result),
|
|
|
|
6 => StringConverter6.LoadString(data, result),
|
|
|
|
7 => StringConverter7.LoadString(data, result),
|
|
|
|
8 => StringConverter8.LoadString(data, result),
|
|
|
|
9 => StringConverter8.LoadString(data, result),
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(generation)),
|
|
|
|
};
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the bytes for a Generation specific string according to the input parameters.
|
|
|
|
/// </summary>
|
2022-01-08 17:54:32 +00:00
|
|
|
/// <param name="destBuffer">Span of bytes to write encoded string data</param>
|
2022-01-03 05:35:59 +00:00
|
|
|
/// <param name="value">Decoded string.</param>
|
|
|
|
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
|
2022-01-08 17:54:32 +00:00
|
|
|
/// <param name="option">Buffer pre-formatting option</param>
|
2022-01-03 05:35:59 +00:00
|
|
|
/// <param name="generation">Generation string format</param>
|
|
|
|
/// <param name="jp">Encoding is Japanese</param>
|
|
|
|
/// <param name="isBigEndian">Encoding is Big Endian</param>
|
|
|
|
/// <param name="language">Language specific conversion (Chinese)</param>
|
2022-01-08 17:54:32 +00:00
|
|
|
/// <returns>Count of bytes written to the <see cref="destBuffer"/>.</returns>
|
2022-01-03 05:35:59 +00:00
|
|
|
public static int SetString(Span<byte> destBuffer, ReadOnlySpan<char> value, int maxLength, StringConverterOption option,
|
|
|
|
int generation, bool jp, bool isBigEndian, int language = 0) => generation switch
|
|
|
|
{
|
|
|
|
3 when isBigEndian => StringConverter3GC.SetString(destBuffer, value, maxLength, option),
|
|
|
|
4 when isBigEndian => StringConverter4GC.SetString(destBuffer, value, maxLength, option),
|
|
|
|
|
|
|
|
1 or 2 => StringConverter12.SetString(destBuffer, value, maxLength, jp, option),
|
|
|
|
3 => StringConverter3.SetString(destBuffer, value, maxLength, jp, option),
|
|
|
|
4 => StringConverter4.SetString(destBuffer, value, maxLength, option),
|
|
|
|
5 => StringConverter5.SetString(destBuffer, value, maxLength, option),
|
|
|
|
6 => StringConverter6.SetString(destBuffer, value, maxLength, option),
|
|
|
|
7 => StringConverter7.SetString(destBuffer, value, maxLength, language, option),
|
|
|
|
8 => StringConverter8.SetString(destBuffer, value, maxLength, option),
|
2022-11-25 01:42:17 +00:00
|
|
|
9 => StringConverter8.SetString(destBuffer, value, maxLength, option),
|
2022-01-03 05:35:59 +00:00
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(generation)),
|
|
|
|
};
|
2021-01-15 06:50:13 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Converts full width to single width
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="chr">Input character to sanitize.</param>
|
|
|
|
internal static char SanitizeChar(char chr) => chr switch
|
|
|
|
{
|
|
|
|
'\uE08F' => '♀',
|
|
|
|
'\uE08E' => '♂',
|
|
|
|
'\u246E' => '♀',
|
|
|
|
'\u246D' => '♂',
|
|
|
|
_ => chr,
|
|
|
|
};
|
2021-01-15 06:50:13 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Converts full width to half width when appropriate
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="chr">Input character to set back to data</param>
|
|
|
|
/// <param name="fullWidth">Checks if the overall string is full-width</param>
|
|
|
|
internal static char UnSanitizeChar(char chr, bool fullWidth = false)
|
|
|
|
{
|
|
|
|
if (fullWidth) // jp/ko/zh strings
|
|
|
|
return chr; // keep as full width
|
2019-03-21 04:50:44 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
return chr switch
|
2019-03-21 04:50:44 +00:00
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
'\u2640' => '\uE08F',
|
|
|
|
'\u2642' => '\uE08E',
|
|
|
|
_ => chr,
|
|
|
|
};
|
|
|
|
}
|
2019-03-21 04:50:44 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Converts full width to half width when appropriate, for Gen5 and prior.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="chr">Input character to set back to data</param>
|
|
|
|
internal static char UnSanitizeChar5(char chr) => chr switch
|
|
|
|
{
|
|
|
|
'\u2640' => '\u246E',
|
|
|
|
'\u2642' => '\u246D',
|
|
|
|
_ => chr,
|
|
|
|
};
|
2019-03-21 04:50:44 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
internal static bool GetIsFullWidthString(ReadOnlySpan<char> str)
|
|
|
|
{
|
|
|
|
foreach (var c in str)
|
2019-03-21 04:50:44 +00:00
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
if (c >> 12 is (0 or 0xE))
|
|
|
|
continue;
|
|
|
|
if (c is '\u2640' or '\u2642') // ♀♂
|
|
|
|
continue;
|
|
|
|
return true;
|
2019-03-21 04:50:44 +00:00
|
|
|
}
|
2022-01-03 05:35:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-21 04:50:44 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
public static bool HasEastAsianScriptCharacters(ReadOnlySpan<char> str)
|
|
|
|
{
|
|
|
|
foreach (var c in str)
|
2021-04-19 01:29:02 +00:00
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
if (c is >= '\u4E00' and <= '\u9FFF')
|
2021-04-19 01:29:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-01-03 05:35:59 +00:00
|
|
|
return false;
|
2019-03-21 04:50:44 +00:00
|
|
|
}
|
|
|
|
}
|