I am attempting to implement a converter which can convert two dimensional array (such as string[,]
) into markdown table.
The experimental implementation
The experimental implementation of Converter
class is as below.
public static class Converter
{
/// <summary>
/// Align formats
/// </summary>
public enum Align
{
Right,
Center,
Left,
Default
}
private static TOutput[,] ConvertAll<TInput, TOutput>(TInput[,] array, Converter<TInput, TOutput> converter)
{
// null check
if (ReferenceEquals(array, null))
{
throw new ArgumentNullException($"{nameof(array)} is null");
}
if (ReferenceEquals(converter, null))
{
throw new ArgumentNullException($"{nameof(converter)} is null");
}
var output = new TOutput[array.GetLongLength(0), array.GetLongLength(1)];
for (long row = 0; row < array.GetLongLength(0); row++)
{
for (long column = 0; column < array.GetLongLength(1); column++)
{
output[row, column] = converter(array[row, column]);
}
}
return output;
}
/// <summary>
/// ToMarkdownTable method with TInput type array
/// </summary>
/// <param name="input">Input array</param>
/// <param name="align">Align format of all columns</param>
/// <returns></returns>
public static string[] ToMarkdownTable<TInput>(TInput[,] input, Align align = Align.Default)
{
return ToMarkdownTable(ConvertAll(input, element => element.ToString()), align);
}
/// <summary>
/// ToMarkdownTable method with TInput type array and the parameter for assigning align format of each column
/// </summary>
/// <param name="input">Input array</param>
/// <param name="aligns">Align format of each column</param>
/// <returns></returns>
public static string[] ToMarkdownTable<TInput>(TInput[,] input, Align[] aligns)
{
return ToMarkdownTable(ConvertAll(input, element => element.ToString()), aligns);
}
/// <summary>
/// ToMarkdownTable method
/// </summary>
/// <param name="input">Input array</param>
/// <param name="align">Align format of all columns</param>
/// <returns></returns>
public static string[] ToMarkdownTable(string[,] input, Align align = Align.Default)
{
Align[] format = new Align[input.GetLongLength(1)];
Array.Fill(format, align);
return ToMarkdownTable(input, format);
}
/// <summary>
/// ToMarkdownTable method with assigning align format of each column
/// </summary>
/// <param name="input">Input array</param>
/// <param name="aligns">Align format of each column</param>
/// <returns></returns>
public static string[] ToMarkdownTable(string[,] input, Align[] aligns)
{
// Null check
if (ReferenceEquals(input, null))
{
throw new ArgumentNullException($"{nameof(input)} is null");
}
if (ReferenceEquals(input, null))
{
throw new ArgumentNullException($"{nameof(aligns)} is null");
}
long width = input.GetLongLength(1);
long height = input.GetLongLength(0);
string[] output = new string[height + 1];
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append('|');
for (int x = 0; x < width; x++)
{
stringBuilder.Append(input[0, x]);
stringBuilder.Append('|');
}
output[0] = stringBuilder.ToString();
stringBuilder.Clear();
stringBuilder.Append('|');
for (int x = 0; x < width; x++)
{
switch (aligns[x])
{
case Align.Right:
stringBuilder.Append("-:|");
break;
case Align.Center:
stringBuilder.Append(":-:|");
break;
case Align.Left:
stringBuilder.Append(":-|");
break;
case Align.Default:
stringBuilder.Append("-|");
break;
default:
stringBuilder.Append("-|");
break;
}
}
output[1] = stringBuilder.ToString();
stringBuilder.Clear();
for (int y = 1; y < height; y++)
{
stringBuilder.Append('|');
for (int x = 0; x < width; x++)
{
stringBuilder.Append(input[y, x]);
stringBuilder.Append('|');
}
output[y + 1] = stringBuilder.ToString();
stringBuilder.Clear();
}
return output;
}
}
Test cases
The test cases of Converter.ToMarkdownTable
methods include string
, sbyte
, byte
, short
, ushort
, char
and int
type two dimensional arrays input.
// string type two dimensional array case
Console.WriteLine("string type two dimensional array case");
string[,] test_string = { { "0", "1", "1", "1" },
{ "2", "3", "1", "1" },
{ "0", "1", "1", "1" }};
var format = new Converter.Align[test_string.GetLongLength(1)];
for (int i = 0; i < test_string.GetLongLength(1); i++)
{
format[i] = Converter.Align.Center;
}
var MarkdownTable = Converter.ToMarkdownTable(test_string, Converter.Align.Center);
for (int i = 0; i < MarkdownTable.Length; i++)
{
Console.WriteLine(MarkdownTable[i]);
}
Console.WriteLine();
// sbyte type two dimensional array case
Console.WriteLine("sbyte type two dimensional array case");
sbyte[,] test_sbyte = { { 0, 1, 1, 1 },
{ 2, 3, 1, 1 },
{ 0, 1, 1, 1 }};
var MarkdownTable_sbyte = Converter.ToMarkdownTable(test_sbyte, Converter.Align.Center);
for (int i = 0; i < MarkdownTable.Length; i++)
{
Console.WriteLine(MarkdownTable[i]);
}
Console.WriteLine();
// byte type two dimensional array case
Console.WriteLine("byte type two dimensional array case");
byte[,] test_byte = { { 0, 1, 1, 1 },
{ 2, 3, 1, 1 },
{ 0, 1, 1, 1 }};
var MarkdownTable_byte = Converter.ToMarkdownTable(test_byte, Converter.Align.Center);
for (int i = 0; i < MarkdownTable.Length; i++)
{
Console.WriteLine(MarkdownTable[i]);
}
Console.WriteLine();
// short type two dimensional array case
Console.WriteLine("short type two dimensional array case");
short[,] test_short = { { 0, 1, 1, 1 },
{ 2, 3, 1, 1 },
{ 0, 1, 1, 1 }};
var MarkdownTable_short = Converter.ToMarkdownTable(test_short, Converter.Align.Center);
for (int i = 0; i < MarkdownTable.Length; i++)
{
Console.WriteLine(MarkdownTable[i]);
}
Console.WriteLine();
// ushort type two dimensional array case
Console.WriteLine("ushort type two dimensional array case");
ushort[,] test_ushort = { { 0, 1, 1, 1 },
{ 2, 3, 1, 1 },
{ 0, 1, 1, 1 }};
var MarkdownTable_ushort = Converter.ToMarkdownTable(test_ushort, Converter.Align.Center);
for (int i = 0; i < MarkdownTable.Length; i++)
{
Console.WriteLine(MarkdownTable[i]);
}
Console.WriteLine();
// char type two dimensional array case
Console.WriteLine("char type two dimensional array case");
char[,] test_char = { { '0', '1', '1', '1' },
{ '2', '3', '1', '1' },
{ '0', '1', '1', '1' }};
var MarkdownTable_char = Converter.ToMarkdownTable(test_char, Converter.Align.Center);
for (int i = 0; i < MarkdownTable.Length; i++)
{
Console.WriteLine(MarkdownTable[i]);
}
Console.WriteLine();
// int type two dimensional array case
Console.WriteLine("int type two dimensional array case");
int[,] test_int = { { 0, 1, 1, 1 },
{ 2, 3, 1, 1 },
{ 0, 1, 1, 1 }};
var MarkdownTable_int = Converter.ToMarkdownTable(test_int, Converter.Align.Center);
for (int i = 0; i < MarkdownTable.Length; i++)
{
Console.WriteLine(MarkdownTable[i]);
}
Console.WriteLine();
The output of the above tests:
string type two dimensional array case
|0|1|1|1|
|:-:|:-:|:-:|:-:|
|2|3|1|1|
|0|1|1|1|
sbyte type two dimensional array case
|0|1|1|1|
|:-:|:-:|:-:|:-:|
|2|3|1|1|
|0|1|1|1|
byte type two dimensional array case
|0|1|1|1|
|:-:|:-:|:-:|:-:|
|2|3|1|1|
|0|1|1|1|
short type two dimensional array case
|0|1|1|1|
|:-:|:-:|:-:|:-:|
|2|3|1|1|
|0|1|1|1|
ushort type two dimensional array case
|0|1|1|1|
|:-:|:-:|:-:|:-:|
|2|3|1|1|
|0|1|1|1|
char type two dimensional array case
|0|1|1|1|
|:-:|:-:|:-:|:-:|
|2|3|1|1|
|0|1|1|1|
int type two dimensional array case
|0|1|1|1|
|:-:|:-:|:-:|:-:|
|2|3|1|1|
|0|1|1|1|
If there is any possible improvement, please let me know.