I am attempting to implement a converter which can convert List<string>
type markdown table into two dimensional array string[,]
.
The experimental implementation
public static class Converter
{
public static string[,] ToTwoDimArray(in List<string> input)
{
return ConcatenateVertical(GetTitleRow(input), GetContents(input));
}
private static string[,] ConcatenateVertical(in string[] source, in string[] newRow)
{
int columnLength = source.GetLength(0);
string[,] output = new string[2, columnLength];
for (int columnIndex = 0; columnIndex < columnLength; columnIndex++)
{
output[0, columnIndex] = source[columnIndex];
}
for (int columnIndex = 0; columnIndex < columnLength; columnIndex++)
{
output[1, columnIndex] = newRow[columnIndex];
}
return output;
}
private static string[,] ConcatenateVertical(in string[,] source, in string[] newRow)
{
int columnLength = source.GetLength(1);
int rowLength = source.GetLength(0) + 1;
string[,] output = new string[rowLength, columnLength];
for (int rowIndex = 0; rowIndex < rowLength - 1; rowIndex++)
{
for (int columnIndex = 0; columnIndex < columnLength; columnIndex++)
{
output[rowIndex, columnIndex] = source[rowIndex, columnIndex];
}
}
for (int columnIndex = 0; columnIndex < columnLength; columnIndex++)
{
output[rowLength - 1, columnIndex] = newRow[columnIndex];
}
return output;
}
private static string[,] ConcatenateVertical(in string[] source, in string[,] newRows)
{
if (source.GetLength(0) != newRows.GetLength(1))
{
throw new ArgumentException("Width isn't match", nameof(source));
}
int columnLength = source.GetLength(0);
int rowLength = newRows.GetLength(0) + 1;
string[,] output = new string[rowLength, columnLength];
for (int columnIndex = 0; columnIndex < columnLength; columnIndex++)
{
output[0, columnIndex] = source[columnIndex];
}
for (int rowIndex = 1; rowIndex < rowLength; rowIndex++)
{
for (int columnIndex = 0; columnIndex < columnLength; columnIndex++)
{
output[rowIndex, columnIndex] = newRows[rowIndex - 1, columnIndex];
}
}
return output;
}
private static string[,] GetContents(in List<string> input)
{
int columnLength = GetTitleRow(input).Length;
int rowLength = input.Count - 2;
string[,] output = new string[rowLength, columnLength];
for (int rowIndex = 2; rowIndex < input.Count; rowIndex++)
{
var rowData = GetRow(input, rowIndex);
for (int columnIndex = 0; columnIndex < columnLength; columnIndex++)
{
output[rowIndex - 2, columnIndex] = rowData[columnIndex];
}
}
return output;
}
private static string[] GetTitleRow(in List<string> input)
{
return GetRow(input, 0);
}
private static string[] GetRow(in List<string> input, in int rowIndex)
{
return RowConstructor(input[rowIndex]);
}
private static string[] RowConstructor(in string input)
{
char[] charsToTrim = { ' ' };
return Array.ConvertAll(input[1..^1].Split('|').ToArray(), element => element.Trim(charsToTrim));
}
}
Test cases
System.Collections.Generic.List<string> strings = new System.Collections.Generic.List<string>();
strings.Add("|1|2|3|4|");
strings.Add("|:-:|:-:|:-:|:-:|");
strings.Add("|5|6|7|8|");
strings.Add("|9|10|11|12|");
strings.Add("|13|14|15|16|");
strings.Add("|17|18|19|20|");
strings.Add("|21|22|23|24|");
strings.Add("|25|26|27|28|");
strings.Add("|29|30|31|32|");
strings.Add("|33|34|35|36|");
strings.Add("|37|38|39|40|");
string[,] result = Converter.ToTwoDimArray(strings);
int columnLength = result.GetLength(1);
int rowLength = result.GetLength(0);
for (int rowIndex = 0; rowIndex < rowLength; rowIndex++)
{
for (int columnIndex = 0; columnIndex < columnLength; columnIndex++)
{
Console.Write($"{result[rowIndex, columnIndex]}\t");
}
Console.WriteLine();
}
The output of the above test:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
25 26 27 28
29 30 31 32
33 34 35 36
37 38 39 40
If there is any possible improvement, please let me know.
:-:
but each is left aligned. This example seems too trivial to catch any problems. For instance, what about determining the width of each column? Here you have 2 digits so that's trivial. What if you the 2nd column had something with 8 characters and the last column has something with 10 characters? \$\endgroup\$string[,]
type structure. Because the data structure used here is onlystring[,]
, the alignment hasn't been recorded. In the listed example, I am trying to print the constructedstring[,]
result and this is nothing about the alignment:-:
\$\endgroup\$