//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// 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>
[Flags]
public enum HStringSplitOptions
{
   Auto = 0,

   TrimElementStart          = 1 << 0,
   TrimElementEnd            = 1 << 1,
   RemoveEmptyEntries        = 1 << 2,
   AlwaysReturnNonEmptyArray = 1 << 3,

   TrimElement =
      TrimElementStart | TrimElementEnd,
   WhiteSpaceSeparatorDefaults =
      RemoveEmptyEntries,
   NonWhiteSpaceSeparatorDefaults =
      TrimElement
}
//**************************************************************************************************


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

public static string[] Split(string str)
{
   return Split(str,(char[])null);
}

public static string[] Split(string str,HStringSplitOptions options)
{
   return Split(str,(char[])null,options);
}

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

public static string[] Split(string str,char separator)
{
   return Split(str,new char[]{separator});
}

public static string[] Split(string str,char separator,HStringSplitOptions options)
{
   return Split(str,new char[]{separator},options);
}

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

public static string[] Split(string str,[HOptional] string separators)
{
   return Split(str,ToCharArray(separators));
}

public static string[] Split(string str,[HOptional] string separators,HStringSplitOptions options)
{
   return Split(str,ToCharArray(separators),options);
}

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

public static string[] Split(string str,[HOptional] char[] separators)
{
   return Split(str,separators,HStringSplitOptions.Auto);
}

public static string[] Split(string str,[HOptional] char[] separators,HStringSplitOptions options)
{
   HArgChecking.VerifyNotNull( str );

   if (options==HStringSplitOptions.Auto)
      options |=
         separators==null ||
         HArray.Exists( separators, ch => char.IsWhiteSpace( ch ) ) ?
            HStringSplitOptions.WhiteSpaceSeparatorDefaults :
            HStringSplitOptions.NonWhiteSpaceSeparatorDefaults;
   var
      list = new List<string>();
   int
      len = str.Length,
      i = 0;

   while (i<len)
   {
      var sb = new StringBuilder();
      bool hasSeparator = false;

      while (i<len)
      {
         char ch = str[i];
         if ( separators!=null && HArray.Contains(separators,ch) ||
              separators==null && char.IsWhiteSpace(ch) )
         {
            hasSeparator = true;
            break;
         }
         sb.Append( ch );
         i++;
      }

      string element = sb.ToString();

      if ( (options & HStringSplitOptions.TrimElementStart)!=0 &&
           (options & HStringSplitOptions.TrimElementEnd  )!=0 )
         element = element.Trim();
      else
         if ((options & HStringSplitOptions.TrimElementStart)!=0)
            element = element.TrimStart();
         else
            if ((options & HStringSplitOptions.TrimElementEnd)!=0)
               element = element.TrimEnd();

      if (!(
         element=="" &&
         (options & HStringSplitOptions.RemoveEmptyEntries)!=0
      ))
         list.Add( element );

      if ( i==len-1 && hasSeparator &&
           (options & HStringSplitOptions.RemoveEmptyEntries)==0 )
         list.Add("");

      i++;
   }

   if ( list.Count==0 &&
        (options & HStringSplitOptions.AlwaysReturnNonEmptyArray)!=0 )
      list.Add( "" );

   return list.ToArray();
}

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

} // CommonHelpers (.WASM)
