//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Text;
using System.Collections.Generic;

#if !WASM
namespace CommonHelpers {
#else
namespace CommonHelpers.WASM {
#endif

//**************************************************************************************************
/// <summary>
/// </summary>
public static partial class HTextBuilder {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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(HString.ConvertAsText(str));
}

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

public static void AppendLine(StringBuilder sb,string format,params object[] parameters)
{
   AppendLine(sb,false,format,parameters);
}

public static void AppendLine( StringBuilder sb, bool separate,
                               string format, params object[] parameters )
{
   AppendLine(sb,separate,string.Format(format,parameters));
}

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

/// <exception cref="ArgumentNullException" />
public static string CombineLines(params string[] strings)
{
   return CombineLines(false,strings);
}

/// <exception cref="ArgumentNullException" />
public static string CombineLines(bool separate,params string[] strings)
{
   HArgChecking.VerifyNotNull( strings );
   var sb = new StringBuilder();
   foreach (string str in strings)
      AppendLine( sb, separate, str );
   return sb.ToString();
}

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

public static StringBuilder ConvertAsText(string multilineStr)
{
   return ConvertAsText(new StringBuilder(multilineStr));
}

public static StringBuilder ConvertAsText(StringBuilder multilineBld)
{
   var sb=new StringBuilder();
   for (int i=0;i<multilineBld.Length;i++)
   {
      char ch=multilineBld[i];
      if (ch==HAscii.NL)
         sb.AppendLine();
      else
         if (ch!=HAscii.CR)
            sb.Append(ch);
   }
   return sb;
}

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

public static List<string> ToLines(string multilineStr)
{
   return ToLines(new StringBuilder(multilineStr));
}

public static List<string> ToLines(string multilineStrFmt,params object[] parameters)
{
   return ToLines(string.Format(multilineStrFmt,parameters));
}

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

public static List<string> ToLines(StringBuilder multilineBld)
{
   var list = new List<string>();
   var sb=new StringBuilder();
   for (int i=0;i<multilineBld.Length;i++)
   {
      char ch=multilineBld[i];
      if (ch==HAscii.NL)
      {
         list.Add( sb.ToString() );
         sb=new StringBuilder();
      }
      else
         if (ch!=HAscii.CR)
            sb.Append(ch);
   }
   if ( sb.Length > 0 )
      list.Add( sb.ToString() );
   return list;
}

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

static HTextBuilder()
{
   CommonHelpers_Core.Access();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HTextBuilder
//**************************************************************************************************

} // CommonHelpers (.WASM)
