//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#nullable enable
using System;
using System.Text;

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

//**************************************************************************************************
partial class HString {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
public static string Repeat(string str,int count)
{
   HArgChecking.VerifyNotNull( str );
   if ( count < 0 )
      throw new ArgumentException();
   var sb = new StringBuilder();
   for (int i=0; i<count; i++)
      sb.Append( str );
   return sb.ToString();
}

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

public static string? EmptyAsNull(string? str) =>
   str=="" ? null : str;
public static string? BlankAsNull(string? str) =>
   IsBlank( str ) ? null : str;

[CLSCompliant(false)]
public static string? EmptyAsNull(ref string? str) =>
   str = EmptyAsNull( str );
[CLSCompliant(false)]
public static string? BlankAsNull(ref string? str) =>
   str = BlankAsNull( str );

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

public static string? TrimOrNull(string? str) =>
   str!=null ? EmptyAsNull( str.Trim() ) : null;
public static string? TrimStartOrNull(string? str) =>
   str!=null ? EmptyAsNull( str.TrimStart() ) : null;
public static string? TrimEndOrNull(string? str) =>
   str!=null ? EmptyAsNull( str.TrimEnd() ) : null;

[CLSCompliant(false)]
public static string? TrimOrNull(ref string? str) =>
   str = TrimOrNull( str );
[CLSCompliant(false)]
public static string? TrimStartOrNull(ref string? str) =>
   str = TrimStartOrNull( str );
[CLSCompliant(false)]
public static string? TrimEndOrNull(ref string? str) =>
   str = TrimEndOrNull( str );

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

/// <exception cref="ArgumentNullException" />
public static string ToWebText(string text) =>
   ToWebText( text, true );
/// <exception cref="ArgumentNullException" />
public static string ToWebText(string text,bool trim)
{
   HArgChecking.VerifyNotNull( text );
   var sb = new StringBuilder();
   var prevCh = '\0';
   foreach (char ch in text)
   {
      if ( HOps.IsOneOf(ch,'\r','\n') )
      {
         if ( !(ch=='\n' && prevCh=='\r') )
            sb.Append('\n');
      }
      else
         sb.Append( ch );
      prevCh = ch;
   }
   return trim ? sb.ToString().Trim() : sb.ToString();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HString
//**************************************************************************************************

} // CommonHelpers (.WASM)
