scream/src/Program.cs

140 lines
No EOL
4.7 KiB
C#

using System.Text;
namespace scream
{
/// <summary>
/// Implements the Scream Cipher, encoding messages using diacritics on the letter 'A'.
/// </summary>
class Program
{
/// <summary>
/// The base character 'A' used for encoding messages with diacritics.
/// </summary>
private const char BASE_CHARACTER = 'A';
/// <summary>
/// The starting ASCII value of printable characters.
/// </summary>
private const int ASCII_START = 0x20;
/// <summary>
/// The ending ASCII value of printable characters.
/// </summary>
/// <remarks>
/// Not used, but included for reference
/// </remarks>
private const int ASCII_END = 0x7E;
/// <summary>
/// The starting Unicode code point for diacritic marks.
/// </summary>
private const int DIACRITIC_START = 0x300;
/// <summary>
/// The ending Unicode code point for diacritic marks.
/// </summary>
/// <remarks>
/// Not used, but included for reference
/// </remarks>
private const int DIACRITIC_END = 0x36F; // Not needed, but here for reference.
/// <summary> Main entry-point for this application. </summary>
/// <param name="args"> The component parts of the message. </param>
static void Main(string[] args)
{
if (args[0].Equals("-q") || args[0].Equals("--quiet"))
{
QuietDown(args);
}
else if (args.Length > 0)
{
var scream = Scream(args);
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine(scream);
}
else
{
return;
}
}
/// <summary> Quiets the given message as defined by the string arry. </summary>
/// <param name="messageComponents"> The component parts of the message. </param>
/// <returns> A string representing the quiet message. </returns>
private static string QuietDown(string[] messageComponents)
{
var concat = string.Concat(messageComponents.Skip(1));
StringBuilder sb = new StringBuilder();
foreach (var c in concat)
{
var index = GetAsciiIndex(c);
var ascii = GetAscii(index);
sb.Append(ascii);
}
return sb.ToString();
}
/// <summary> Screams the given message as defined by the string array. </summary>
/// <param name="messageComponents"> The component parts of the message. </param>
/// <returns> A string representing the screamed message. </returns>
private static string Scream(string[] messageComponents)
{
var concat = string.Concat(messageComponents);
StringBuilder sb = new StringBuilder();
foreach (var c in concat)
{
var index = GetDiacriticIndex(c);
var diacritic = GetDiacritic(index);
sb.Append(BASE_CHARACTER);
sb.Append(diacritic);
}
return sb.ToString();
}
/// <summary> Gets the zero-based index of the diacritic character. </summary>
/// <param name="character"> The diacritic character to index. </param>
/// <returns> The diacritic character index. </returns>
private static int GetDiacriticIndex(char character)
{
var index = character - ASCII_START;
return index;
}
/// <summary> Gets the zero-based index of the viewable ASCII character. </summary>
/// <param name="character"> The ASCII character to index. </param>
/// <returns> The ASCII character index. </returns>
private static int GetAsciiIndex(char character)
{
var index = character - DIACRITIC_START;
return index;
}
/// <summary> Gets a diacritic character given an index offset. </summary>
/// <param name="index"> Zero-based index of the Diacritic character. </param>
/// <returns> The diacritic character. </returns>
private static char GetDiacritic(int index)
{
var diacritic = (char)(DIACRITIC_START + index);
return diacritic;
}
/// <summary> Gets an ASCII character given an index offset. </summary>
/// <param name="index"> Zero-based index of the ASCII character. </param>
/// <returns> The ASCII character. </returns>
private static char GetAscii(int index)
{
var character = (char)(ASCII_START + index);
return character;
}
}
}