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

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

static partial class fatal_impl {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static string TXT( string multilineStr ) =>
   AppendText( new StringBuilder(), multilineStr ).ToString();

public static string FMT( string messageFmt, params object?[] args ) =>
   TXT( string.Format( messageFmt, args ) );

[u.returns_source]
public static StringBuilder AppendText( StringBuilder sb, string multilineStr ) =>
   AppendText( sb, true, multilineStr );
[u.returns_source]
public static StringBuilder AppendText( StringBuilder sb,
                                        [u.def_true] bool insertNewLineIfNotEmpty,
                                        string multilineStr )
{
   if ( insertNewLineIfNotEmpty )
      AppendLine( sb );
   foreach ( char ch in multilineStr )
      if ( ch == '\n' )
         sb.AppendLine();
      else
         if ( ch != '\r' )
            sb.Append( ch );
   return sb;
}

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

public static StringBuilder AsSingleLine( string multilineStr )
{
   var sb = new StringBuilder();
   foreach ( char ch in multilineStr )
      if ( ch != '\r' )
         sb.Append( ch != '\n' ? ch : ' ' );
   return sb;
}

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

public static bool AppendLine( StringBuilder sb )
{
   if ( sb.Length == 0 )
      return false;
   sb.AppendLine();
   return true;
}

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

public static void AppendLine( StringBuilder sb, string str ) =>
   AppendLine( sb, false, str );

public static void AppendLine( StringBuilder sb, bool separate, string str )
{
   if ( sb.Length > 0 ) {
      AppendLine( sb );
      if ( separate )
         AppendLine( sb );
   }
   sb.Append( TXT( str ) );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // fatal_impl

} // user_rtx (.wasm)
