//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// UserRTX.NET satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Globalization;

#if !WASM
namespace user_rtx { using u = user_rtx;
#else
namespace user_rtx.wasm { using u = user_rtx.wasm;
#endif

/// <summary>
/// Helpers for “System.Char”
/// </summary>
public static partial class chr {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static bool IsDecimalDigit( char ch ) =>
   u.val.IsBetween( ch, '0', '9' );

public static bool IsHexDigit( char ch ) =>
   IsDecimalDigit( ch ) ||
   u.val.IsBetween( ch, 'a', 'f' ) ||
   u.val.IsBetween( ch, 'A', 'F' );

//--------------------------------------------------------------------------------------------------

public static bool IsEnglishLetter( char ch ) =>
   u.val.IsBetween( ch, 'A', 'Z' ) || u.val.IsBetween( ch, 'a', 'z' );

public static bool IsRussianLetter( char ch ) =>
   u.val.IsBetween( ch, 'А', 'Я' ) ||
   u.val.IsBetween( ch, 'а', 'я' ) ||
   u.val.IsOneOf( ch, 'Ё', 'ё' );

//--------------------------------------------------------------------------------------------------

public static bool IsNativeLetter( char ch ) =>
   IsNativeLetter( ch, CultureInfo.CurrentCulture );

public static bool IsNativeLetter( char ch, CultureInfo? culture ) =>
   IsNativeLetterX( ch, culture ) ?? false;

public static bool? IsNativeLetterX( char ch, CultureInfo? culture )
{
   var ci = culture ?? CultureInfo.InvariantCulture;
   var cultureNeutralName = u.culture.ToNeutral( ci ).Name;

   switch ( cultureNeutralName )
   {
   case "en": return IsEnglishLetter( ch );
   } // switch

   switch ( ci.Name )
   {
   case "ru-RU": return IsRussianLetter( ch );
   } // switch

   return null;
}

//--------------------------------------------------------------------------------------------------

/// <exception cref="NotSupportedException" />
[u.strict_result]
public static bool StrictIsNativeLetter( char ch ) =>
   StrictIsNativeLetter( ch, CultureInfo.CurrentCulture );

/// <exception cref="NotSupportedException" />
[u.strict_result]
public static bool StrictIsNativeLetter( char ch, CultureInfo? culture ) =>
   IsNativeLetterX( ch, culture ) ??
      throw u.culture.NotSupportedOperationException( culture );

//==================================================================================================
#if WIN32
public static bool IsInSystemACP( char ch )
{
   int usedDefaultChar;
   var multiByteStrBuff = new byte[ 8 ];
   return w32_core.WideCharToMultiByte(
      w32_core.CP_ACP, w32_core.WC_NO_BEST_FIT_CHARS,
      ch.ToString(), -1,
      multiByteStrBuff, multiByteStrBuff.Length,
      IntPtr.Zero, out usedDefaultChar ) != 0 &&
      usedDefaultChar == 0;
}
#endif // WIN32
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // chr

} // user_rtx (.wasm)
