//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// 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.Byte” value type
/// </summary>
public static class uint8 {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public const int StdMaxWidth = 3;

//==================================================================================================

public static string ToNormalStr( byte value ) =>
   value.ToString( "G", NumberFormatInfo.InvariantInfo );

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

/// <exception cref="ArgumentOutOfRangeException" />
public static string ToFixedWidthStr( byte value,
                                      int width=StdMaxWidth, char? overflowChar='*' ) =>
   ToFixedWidthStrX( value, width, overflowChar, ' ' );

/// <exception cref="ArgumentOutOfRangeException" />
public static string ToDigitalStr( byte value, int width=StdMaxWidth, char? overflowChar='*' ) =>
   ToFixedWidthStrX( value, width, overflowChar, '0' );

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

public static string ToHexStr( byte value ) =>
   ToHexStr( value, false );

public static string ToHexStr( byte value, bool withPrefix ) =>
   u.hex.Format( value, 2, withPrefix );

//==================================================================================================

public static byte HighWord(byte value) =>
   (byte)( value >> 8 );

public static byte LowWord(byte value) =>
   (byte)( value & 0xFFu );

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

/// <exception cref="ArgumentOutOfRangeException" />
public static byte Flag( int bitNo ) =>
   (byte)( 1u << u.arg_check.InRange( bitNo, 0, 7, nameof(bitNo) ) );

//==================================================================================================

/// <exception cref="ArgumentOutOfRangeException" />
public static string ToFixedWidthStrX( byte value, [u.positive] int width,
                                       char? overflowChar, char padChar )
{
   u.arg_check.Positive( width, nameof(width) );
   var str = ToNormalStr( value );
   if ( str.Length == width || str.Length > width && overflowChar == null )
      return str;
   return str.Length > width ?
      overflowChar + (width > 1 ? str[ (str.Length-width+1).. ] : "") :
      str.PadLeft( width, padChar );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // uint8

} // user_rtx (.wasm)
