
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;


namespace Rokojori
{
  public static class Text
  {
    public static string EscapeAsPathForCommandLine( this string path )
    {
      var escaped = path.Replace( "\"", "\\\"" );

      return $"\"{escaped}\"";
    }

    public static string EnsureEndsWith( this string path, string ending )
    {
      if ( ! path.EndsWith( ending ) )
      {
        path += ending;
      }

      return path;
    }

    public static string Replace( this string source, Regex regex, string replacement )
    {
      return regex.Replace( source, replacement );
    }

    public static string SubstringAfterMatching( this string source, string match )
    {
      var index = source.IndexOf( match );

      return index == -1 ? source : source.Substring( index + match.Length );
    }

    public static long ToLong( this string source )
    {
      return long.Parse( source );
    } 

    public static string ReplaceStart( this string source, string start, string replacement = "" )
    {
      if ( source.StartsWith( start ) )
      {
        return replacement + source.Substring( start.Length );
      }

      return source;
    }

    public static string ReplaceEnd( this string source, string ending, string replacement = "" )
    {
      if ( source.EndsWith( ending ) )
      {
        return source.Substring( 0, source.Length - ending.Length ) + replacement;
      }

      return source;
    }

    public static string ExtractCommonStart( this string source, string other )
    {
      for ( int i = 0; i < source.Length && i < other.Length; i++ )
      {
        if ( source[ i ] != other[ i ] )
        {
          return source.Substring( 0, i );
        }
      }

      if ( source.Length < other.Length )
      {
        return source;
      }

      return other;
    }

   
    
    
  }
}