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

public const int
   StdMaxWidth = 11,
   StdMaxWidthWithoutSign = 10;

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

public static string ToNormalStr( Int32 value )
{
   return value.ToString( "G", NumberFormatInfo.InvariantInfo );
}

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

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

/// <exception cref="ArgumentOutOfRangeException" />
public static string ToDigitalStr( Int32 value,
                                   int width = StdMaxWidthWithoutSign,
                                   char? overflowChar = '*',
                                   char badValueChar = '#' )
{
   u.arg_check.Positive( width, nameof(width) );
   return value >= 0 ?
      ToFixedWidthStrX( value, width, overflowChar, '0' ) :
      new string( badValueChar, width );
}

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

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

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

/// <exception cref="ArgumentOutOfRangeException" />
public static string ToFixedWidthStrX( Int32 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 );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // int32

} // user_rtx (.wasm)
