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

namespace CommonHelpers {

//**************************************************************************************************
partial class HConsole {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public bool IsCharPrintable(char ch)
{
   return IsCharPrintable(ch,true);
}

public bool IsCharPrintable(char ch,bool allowSpecial)
{
   if (ch=='\0')
      return false;

   if (!( IsOnStdOutput && !(Console.OutputEncoding is UnicodeEncoding) ))
      return
         HOps.IsBetween((int)ch,0x20,0xFB17) &&
         (allowSpecial || !char.IsSurrogate(ch));
#if WIN32
   byte[] oemChars = new byte[1];
   char[] unicodeChars = new char[1];

   Win32_Console.CharToOemBuff( new char[] { ch }, oemChars, 1 );
   Win32_Console.OemToCharBuff( oemChars, unicodeChars, 1 );

   int oemCode = (int)oemChars[0];

   if ( unicodeChars[0] != ch &&
        IsOnStdOutput && !HOps.IsOneOf( Console.OutputEncoding,
#pragma warning disable SYSLIB0001 // The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.
            Encoding.Unicode, Encoding.UTF32, Encoding.UTF7, Encoding.UTF8 ) ||
#pragma warning restore SYSLIB0001
        HOps.IsOneOf( oemCode,
            HAscii.BEL, HAscii.BS, HAscii.HT, HAscii.LF, HAscii.CR ) )
      return false;
#endif // WIN32
   if ( allowSpecial )
      return true;
#if WIN32
   if ( HAscii.IsControl(oemCode,true) ||
        HOps.IsBetween(oemCode,0xB0,0xDF) ||
        oemCode==0xFF )
      return false;
#endif // WIN32
   return false;
}

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

public char CharToPrintable(char ch)
{
   return ExactDisplaying ? ch :
      ch==HUnicode.Bullet && !IsInTextWriterMode ?
         BulletChar : IsCharPrintable(ch) ? ch : NonDisplayableSubstChar;
}

public string StringToPrintable(string str)
{
   var sb=new StringBuilder();
   foreach (char ch in str)
      sb.Append( CharToPrintable(ch) );
   return sb.ToString();
}

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

public bool HasUnicodeOutput
{
   get
   {
      return IsOnStdOutput && Console.OutputEncoding is UnicodeEncoding;
   }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HConsole
//**************************************************************************************************

} // CommonHelpers
