Compare commits
10 Commits
a7e6f1c0e9
...
8e3332b484
Author | SHA1 | Date | |
---|---|---|---|
|
8e3332b484 | ||
|
32393e704d | ||
|
d7720d8cde | ||
|
aff4b353bb | ||
|
203458fdf7 | ||
|
4148475031 | ||
|
e38baa9167 | ||
|
a66f6a1368 | ||
|
96af5ae82c | ||
|
11c7daec8e |
613
SaneTsv.cs
613
SaneTsv.cs
@ -43,9 +43,6 @@ public class SaneTsv
|
||||
public static readonly byte[] TrueEncoded = Encoding.UTF8.GetBytes("TRUE");
|
||||
public static readonly byte[] FalseEncoded = Encoding.UTF8.GetBytes("FALSE");
|
||||
|
||||
// TODO: We need to be able to update all these in tandem somehow
|
||||
public string[] ColumnNames { get; protected set; }
|
||||
public Type[] ColumnTypes { get; protected set; }
|
||||
protected static bool? _littleEndian = null;
|
||||
public static bool LittleEndian
|
||||
{
|
||||
@ -71,11 +68,9 @@ public class SaneTsv
|
||||
|
||||
public static CommentedTsv<T> ParseCommentedTsv<T>(byte[] inputBuffer) where T : CommentedTsvRecord, new()
|
||||
{
|
||||
// TODO: add the file comment?
|
||||
return (CommentedTsv<T>)Parse<T>(inputBuffer, FormatType.COMMENTED_TSV);
|
||||
}
|
||||
|
||||
// TODO: Have parsing errors include line / column #
|
||||
protected static Tsv<T> Parse<T>(byte[] inputBuffer, FormatType format) where T : TsvRecord, new()
|
||||
{
|
||||
Tsv<T> parsed;
|
||||
@ -89,22 +84,22 @@ public class SaneTsv
|
||||
}
|
||||
parsed.Records = new List<T>();
|
||||
|
||||
var headerTypes = new List<Type>();
|
||||
var headerNames = new List<string>();
|
||||
var headerPropertyInfos = new List<PropertyInfo>();
|
||||
var columnTypes = new List<Type>();
|
||||
var columnNames = new List<string>();
|
||||
var columnPropertyInfos = new List<PropertyInfo>();
|
||||
int columnCount = 0;
|
||||
|
||||
foreach (PropertyInfo property in typeof(T).GetProperties())
|
||||
{
|
||||
TypedTsvColumnAttribute attribute = (TypedTsvColumnAttribute)Attribute.GetCustomAttribute(property, typeof(TypedTsvColumnAttribute));
|
||||
TsvColumnAttribute attribute = (TsvColumnAttribute)Attribute.GetCustomAttribute(property, typeof(TsvColumnAttribute));
|
||||
if (attribute == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
headerNames.Add(attribute.ColumnName ?? property.Name);
|
||||
headerTypes.Add(attribute.ColumnType ?? GetColumnFromType(property.PropertyType));
|
||||
headerPropertyInfos.Add(property);
|
||||
columnNames.Add(attribute.ColumnName ?? property.Name);
|
||||
columnTypes.Add(attribute.ColumnType ?? GetColumnFromType(property.PropertyType));
|
||||
columnPropertyInfos.Add(property);
|
||||
// TODO: Check that the property type and given column type are compatible
|
||||
columnCount++;
|
||||
}
|
||||
@ -160,12 +155,6 @@ public class SaneTsv
|
||||
fields.Add(fieldBytes.ToArray());
|
||||
fieldBytes.Clear();
|
||||
|
||||
if (numFields < 0)
|
||||
{
|
||||
// This is the header
|
||||
|
||||
numFields = fields.Count;
|
||||
|
||||
int numTypesBlank = 0;
|
||||
|
||||
for (int j = 0; j < fields.Count; j++)
|
||||
@ -177,7 +166,7 @@ public class SaneTsv
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception($"Header {fields.Count} is not valid UTF-8", e);
|
||||
throw new Exception($"Header field {fields.Count} is not valid UTF-8", e);
|
||||
}
|
||||
|
||||
string columnTypeString;
|
||||
@ -186,7 +175,7 @@ public class SaneTsv
|
||||
{
|
||||
if (format == FormatType.SIMPLE_TSV)
|
||||
{
|
||||
throw new Exception($"Header {fields.Count} contain ':', which is not allowed for column names");
|
||||
throw new Exception($"Header field {j} contains ':', which is not allowed for column names");
|
||||
}
|
||||
columnTypeString = columnString.Split(":").Last();
|
||||
columnName = columnString.Substring(0, columnString.Length - columnTypeString.Length - 1);
|
||||
@ -195,7 +184,7 @@ public class SaneTsv
|
||||
{
|
||||
if (format > FormatType.SIMPLE_TSV)
|
||||
{
|
||||
throw new Exception($"Header {fields.Count} has no type");
|
||||
throw new Exception($"Header field {fields.Count} has no type");
|
||||
}
|
||||
columnTypeString = "";
|
||||
columnName = columnString;
|
||||
@ -246,17 +235,16 @@ public class SaneTsv
|
||||
throw new Exception($"Invalid type '{columnTypeString}' for column {j}");
|
||||
}
|
||||
|
||||
// TODO: Check column name uniqueness
|
||||
// TODO: Allow lax parsing (only worry about parsing columns that are given in the specifying type
|
||||
|
||||
if (headerNames[j] != columnName)
|
||||
if (columnNames[j] != columnName)
|
||||
{
|
||||
throw new Exception($"Column {j} has name {columnName}, but expected {headerNames[j]}");
|
||||
throw new Exception($"Column {j} has name {columnName}, but expected {columnNames[j]}");
|
||||
}
|
||||
|
||||
if (headerTypes[j] != type)
|
||||
if (columnTypes[j] != type)
|
||||
{
|
||||
throw new Exception($"Column {j} has type {type}, but expected {headerTypes[j]}");
|
||||
throw new Exception($"Column {j} has type {type}, but expected {columnTypes[j]}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -273,27 +261,13 @@ public class SaneTsv
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fields.Clear();
|
||||
}
|
||||
else if (numFields != fields.Count)
|
||||
{
|
||||
throw new Exception($"Expected {numFields} fields on line {line}, but found {fields.Count}");
|
||||
}
|
||||
else
|
||||
{
|
||||
string comment = null;
|
||||
if (currentComment.Length > 0)
|
||||
{
|
||||
comment = currentComment.ToString();
|
||||
currentComment.Clear();
|
||||
}
|
||||
parsed.Records.Add(ParseCurrentRecord<T>(headerTypes.ToArray(), headerPropertyInfos.ToArray(), fields, comment, line));
|
||||
fields.Clear();
|
||||
}
|
||||
|
||||
line++;
|
||||
currentLineStart = i + 1;
|
||||
|
||||
// Done parsing header
|
||||
break;
|
||||
}
|
||||
else if (inputBuffer[i] == '#')
|
||||
{
|
||||
@ -330,15 +304,123 @@ public class SaneTsv
|
||||
}
|
||||
}
|
||||
|
||||
fields.Add(fieldBytes.ToArray());
|
||||
|
||||
if (numFields == 0)
|
||||
// TODO: need to figure out where the crossover is
|
||||
// Complication: it probably depends on processor count
|
||||
if (inputBuffer.Length < 10000)
|
||||
{
|
||||
throw new Exception("Found 0 fields on last line. Possibly because of extra \\n after last record");
|
||||
parsed.Records.AddRange(Parse<T>(inputBuffer, format, columnPropertyInfos.ToArray(), columnTypes.ToArray(), currentLineStart - 1, inputBuffer.Length));
|
||||
return parsed;
|
||||
}
|
||||
if (numFields != fields.Count)
|
||||
else
|
||||
{
|
||||
throw new Exception($"Expected {numFields} fields on line {parsed.Records.Count + 2}, but found {fields.Count}");
|
||||
int parseStart = currentLineStart;
|
||||
int tasks = Environment.ProcessorCount - 1;
|
||||
int splitCount = (inputBuffer.Length - parseStart) / tasks;
|
||||
T[][] parsedValues = new T[tasks][];
|
||||
Parallel.For(0, tasks, i =>
|
||||
{
|
||||
int startIndex = i * splitCount + parseStart - 1;
|
||||
int endIndex;
|
||||
if (i == tasks - 1)
|
||||
{
|
||||
endIndex = inputBuffer.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
endIndex = (i + 1) * splitCount + parseStart;
|
||||
}
|
||||
|
||||
parsedValues[i] = Parse<T>(inputBuffer, format, columnPropertyInfos.ToArray(), columnTypes.ToArray(), startIndex, endIndex);
|
||||
});
|
||||
|
||||
// TODO: Handle relative line numbers
|
||||
for (int i = 0; i < tasks; i++)
|
||||
{
|
||||
parsed.Records.AddRange(parsedValues[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
// This approach is slightly different than others. We skip the record that startIndex is in and
|
||||
// include the record that endIndex is in. We do this because in order to include the record
|
||||
// startIndex is in we'd have to go back to the start of the record's comment, and to know
|
||||
// exactly where that comment started we'd have to go back to the start of the record before that
|
||||
// (not including that other record's comment).
|
||||
protected static T[] Parse<T>(byte[] inputBuffer, FormatType format, PropertyInfo[] columnPropertyInfos, Type[] columnTypes, int startIndex, int endIndex) where T : TsvRecord, new()
|
||||
{
|
||||
var fieldBytes = new List<byte>();
|
||||
var fields = new List<byte[]>();
|
||||
var currentComment = new StringBuilder();
|
||||
List<T> parsed = new List<T>();
|
||||
bool parsingLastRecord = false;
|
||||
|
||||
int relativeLine = 0;
|
||||
|
||||
int i = startIndex;
|
||||
while (i < inputBuffer.Length - 1 && inputBuffer[i] != '\n' && inputBuffer[i + 1] != '#')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i >= inputBuffer.Length - 1)
|
||||
{
|
||||
return Array.Empty<T>();
|
||||
}
|
||||
|
||||
// Start parsing after \n
|
||||
i++;
|
||||
|
||||
int currentLineStart = i;
|
||||
|
||||
for (; i < inputBuffer.Length && (i < endIndex || parsingLastRecord); i++)
|
||||
{
|
||||
if (inputBuffer[i] == '\\')
|
||||
{
|
||||
if (i + 1 == inputBuffer.Count())
|
||||
{
|
||||
throw new Exception($"Found '\\' at end of input");
|
||||
}
|
||||
if (inputBuffer[i + 1] == 'n')
|
||||
{
|
||||
fieldBytes.Add((byte)'\n');
|
||||
i++;
|
||||
}
|
||||
else if (inputBuffer[i + 1] == '\\')
|
||||
{
|
||||
fieldBytes.Add((byte)'\\');
|
||||
i++;
|
||||
}
|
||||
else if (inputBuffer[i + 1] == 't')
|
||||
{
|
||||
fieldBytes.Add((byte)'\t');
|
||||
i++;
|
||||
}
|
||||
else if (inputBuffer[i + 1] == '#')
|
||||
{
|
||||
fieldBytes.Add((byte)'#');
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Expected 'n', 't', '#', or '\\' after '\\' at line {relativeLine} column {i - currentLineStart}");
|
||||
}
|
||||
}
|
||||
else if (inputBuffer[i] == '\t')
|
||||
{
|
||||
// end of field
|
||||
fields.Add(fieldBytes.ToArray());
|
||||
fieldBytes.Clear();
|
||||
}
|
||||
else if (inputBuffer[i] == '\n')
|
||||
{
|
||||
fields.Add(fieldBytes.ToArray());
|
||||
fieldBytes.Clear();
|
||||
|
||||
if (columnTypes.Length != fields.Count)
|
||||
{
|
||||
throw new Exception($"Expected {columnTypes.Length} fields on line {relativeLine}, but found {fields.Count}");
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -348,11 +430,83 @@ public class SaneTsv
|
||||
comment = currentComment.ToString();
|
||||
currentComment.Clear();
|
||||
}
|
||||
parsed.Records.Add(ParseCurrentRecord<T>(headerTypes.ToArray(), headerPropertyInfos.ToArray(), fields, comment, line));
|
||||
parsed.Add(ParseCurrentRecord<T>(columnTypes.ToArray(), columnPropertyInfos.ToArray(), fields, comment, relativeLine));
|
||||
fields.Clear();
|
||||
}
|
||||
|
||||
return parsed;
|
||||
parsingLastRecord = false;
|
||||
relativeLine++;
|
||||
currentLineStart = i + 1;
|
||||
}
|
||||
else if (inputBuffer[i] == '#')
|
||||
{
|
||||
if (i == currentLineStart && format >= FormatType.COMMENTED_TSV)
|
||||
{
|
||||
int j = i;
|
||||
for (; j < inputBuffer.Length && inputBuffer[j] != '\n'; j++) { }
|
||||
if (j < inputBuffer.Length)
|
||||
{
|
||||
var commentBytes = new byte[j - i - 1];
|
||||
Array.Copy(inputBuffer, i + 1, commentBytes, 0, j - i - 1);
|
||||
if (currentComment.Length > 0)
|
||||
{
|
||||
currentComment.Append('\n');
|
||||
}
|
||||
currentComment.Append(Encoding.UTF8.GetString(commentBytes));
|
||||
i = j;
|
||||
currentLineStart = i + 1;
|
||||
relativeLine++;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Comments at end of file are not allowed");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Found unescaped '#' at line {relativeLine}, column {i - currentLineStart}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fieldBytes.Add(inputBuffer[i]);
|
||||
}
|
||||
|
||||
if (i == endIndex - 1)
|
||||
{
|
||||
parsingLastRecord = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (endIndex < inputBuffer.Length)
|
||||
{
|
||||
return parsed.ToArray();
|
||||
}
|
||||
|
||||
fields.Add(fieldBytes.ToArray());
|
||||
|
||||
if (fields.Count == 0)
|
||||
{
|
||||
// TODO
|
||||
throw new Exception("Not sure when this will happen. THis might actuall be fine");
|
||||
}
|
||||
if (fields.Count != columnTypes.Length)
|
||||
{
|
||||
throw new Exception($"Expected {columnTypes} fields on line {relativeLine}, but found {fields.Count}");
|
||||
}
|
||||
else
|
||||
{
|
||||
string comment = null;
|
||||
if (currentComment.Length > 0)
|
||||
{
|
||||
comment = currentComment.ToString();
|
||||
currentComment.Clear();
|
||||
}
|
||||
parsed.Add(ParseCurrentRecord<T>(columnTypes.ToArray(), columnPropertyInfos.ToArray(), fields, comment, relativeLine));
|
||||
fields.Clear();
|
||||
}
|
||||
|
||||
return parsed.ToArray();
|
||||
}
|
||||
|
||||
protected static T ParseCurrentCommentedRecord<T>(Type[] columnTypes, PropertyInfo[] properties, List<byte[]> fields, string comment, int line) where T : CommentedTsvRecord, new()
|
||||
@ -548,103 +702,6 @@ public class SaneTsv
|
||||
}
|
||||
|
||||
public static byte[] SerializeSimpleTsv(IList<string> header, IList<IList<string>> data)
|
||||
{
|
||||
var escapedString = new StringBuilder();
|
||||
|
||||
// Serialize header
|
||||
for (int i = 0; i < header.Count; i++)
|
||||
{
|
||||
if (header[i].Contains(':'))
|
||||
{
|
||||
throw new Exception($"Column {i} contains the character ':'");
|
||||
}
|
||||
|
||||
for (int j = i + 1; j < header.Count; j++)
|
||||
{
|
||||
if (header[i] == header[j])
|
||||
{
|
||||
throw new Exception("Column names in header must be unique");
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < header[i].Count(); j++)
|
||||
{
|
||||
if (header[i][j] == '\n')
|
||||
{
|
||||
escapedString.Append("\\n");
|
||||
}
|
||||
else if (header[i][j] == '\t')
|
||||
{
|
||||
escapedString.Append("\\t");
|
||||
}
|
||||
else if (header[i][j] == '\\')
|
||||
{
|
||||
escapedString.Append("\\\\");
|
||||
}
|
||||
else if (header[i][j] == '#')
|
||||
{
|
||||
escapedString.Append("\\#");
|
||||
}
|
||||
else
|
||||
{
|
||||
escapedString.Append(header[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
if (i == header.Count - 1)
|
||||
{
|
||||
escapedString.Append('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
escapedString.Append('\t');
|
||||
}
|
||||
}
|
||||
|
||||
// Serialize data
|
||||
for (int i = 0; i < data.Count; i++)
|
||||
{
|
||||
for (int j = 0; j < data[i].Count; j++)
|
||||
{
|
||||
for (int k = 0; k < data[i][j].Length; k++)
|
||||
{
|
||||
if (data[i][j][k] == '\n')
|
||||
{
|
||||
escapedString.Append("\\n");
|
||||
}
|
||||
else if (data[i][j][k] == '\t')
|
||||
{
|
||||
escapedString.Append("\\t");
|
||||
}
|
||||
else if (data[i][j][k] == '\\')
|
||||
{
|
||||
escapedString.Append("\\\\");
|
||||
}
|
||||
else if (data[i][j][k] == '#')
|
||||
{
|
||||
escapedString.Append("\\#");
|
||||
}
|
||||
else
|
||||
{
|
||||
escapedString.Append(data[i][j][k]);
|
||||
}
|
||||
}
|
||||
|
||||
if (j < data[i].Count - 1)
|
||||
{
|
||||
escapedString.Append('\t');
|
||||
}
|
||||
else if (i < data.Count - 1)
|
||||
{
|
||||
escapedString.Append('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetBytes(escapedString.ToString());
|
||||
}
|
||||
|
||||
public static byte[] SerializeSimpleTsvParallel(IList<string> header, IList<IList<string>> data)
|
||||
{
|
||||
var serialized = new List<byte>();
|
||||
var escapedString = new StringBuilder();
|
||||
@ -705,7 +762,7 @@ public class SaneTsv
|
||||
// Complication: it probably depends on processor count
|
||||
if (data.Count < 100)
|
||||
{
|
||||
serialized.AddRange(Encoding.UTF8.GetBytes(SerializeSimpleTsvParallel(data, 0, data.Count)));
|
||||
serialized.AddRange(Encoding.UTF8.GetBytes(SerializeSimpleTsv(data, 0, data.Count)));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -723,7 +780,7 @@ public class SaneTsv
|
||||
{
|
||||
endIndex = (i + 1) * splitCount;
|
||||
}
|
||||
string escapedString = SerializeSimpleTsvParallel(data, i * splitCount, endIndex);
|
||||
string escapedString = SerializeSimpleTsv(data, i * splitCount, endIndex);
|
||||
bytes[i] = Encoding.UTF8.GetBytes(escapedString);
|
||||
});
|
||||
|
||||
@ -736,7 +793,7 @@ public class SaneTsv
|
||||
return serialized.ToArray();
|
||||
}
|
||||
|
||||
public static string SerializeSimpleTsvParallel(IList<IList<string>> data, int startIndex, int endIndex)
|
||||
public static string SerializeSimpleTsv(IList<IList<string>> data, int startIndex, int endIndex)
|
||||
{
|
||||
var escapedString = new StringBuilder();
|
||||
|
||||
@ -783,162 +840,10 @@ public class SaneTsv
|
||||
return escapedString.ToString();
|
||||
}
|
||||
|
||||
|
||||
public static (string[] columns, string[][] data) ParseSimpleTsv(byte[] inputBuffer)
|
||||
{
|
||||
string[] columnNames = null;
|
||||
|
||||
var fieldBytes = new List<byte>();
|
||||
var fields = new List<byte[]>();
|
||||
var records = new List<string[]>();
|
||||
|
||||
int numFields = -1;
|
||||
int line = 1;
|
||||
int currentLineStart = 0;
|
||||
for (int i = 0; i < inputBuffer.Count(); i++)
|
||||
{
|
||||
if (inputBuffer[i] == '\\')
|
||||
{
|
||||
if (i + 1 == inputBuffer.Count())
|
||||
{
|
||||
throw new Exception($"Found '\\' at end of input");
|
||||
}
|
||||
if (inputBuffer[i + 1] == 'n')
|
||||
{
|
||||
fieldBytes.Add((byte)'\n');
|
||||
i++;
|
||||
}
|
||||
else if (inputBuffer[i + 1] == '\\')
|
||||
{
|
||||
fieldBytes.Add((byte)'\\');
|
||||
i++;
|
||||
}
|
||||
else if (inputBuffer[i + 1] == 't')
|
||||
{
|
||||
fieldBytes.Add((byte)'\t');
|
||||
i++;
|
||||
}
|
||||
else if (inputBuffer[i + 1] == '#')
|
||||
{
|
||||
fieldBytes.Add((byte)'#');
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Expected 'n', 't', '#', or '\\' after '\\' at line {line} column {i - currentLineStart}");
|
||||
}
|
||||
}
|
||||
else if (inputBuffer[i] == '\t')
|
||||
{
|
||||
// end of field
|
||||
fields.Add(fieldBytes.ToArray());
|
||||
fieldBytes.Clear();
|
||||
}
|
||||
else if (inputBuffer[i] == '\n')
|
||||
{
|
||||
fields.Add(fieldBytes.ToArray());
|
||||
fieldBytes.Clear();
|
||||
|
||||
if (numFields < 0)
|
||||
{
|
||||
// This is the header
|
||||
|
||||
numFields = fields.Count;
|
||||
|
||||
columnNames = new string[numFields];
|
||||
|
||||
for (int j = 0; j < fields.Count; j++)
|
||||
{
|
||||
string columnString;
|
||||
try
|
||||
{
|
||||
columnString = Encoding.UTF8.GetString(fields[j]);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception($"Column name {fields.Count} is not valid UTF-8", e);
|
||||
}
|
||||
|
||||
if (columnString.Contains(':'))
|
||||
{
|
||||
throw new Exception($"Header {fields.Count} contain ':', which is not allowed for column names");
|
||||
}
|
||||
|
||||
columnNames[j] = columnString;
|
||||
}
|
||||
|
||||
fields.Clear();
|
||||
}
|
||||
else if (numFields != fields.Count)
|
||||
{
|
||||
throw new Exception($"Expected {numFields} fields on line {line}, but found {fields.Count}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var fieldStrings = new string[fields.Count];
|
||||
for (int j = 0; j < fields.Count; j++)
|
||||
{
|
||||
try
|
||||
{
|
||||
fieldStrings[j] = Encoding.UTF8.GetString(fields[j]);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception($"Line {line}, column {j} is not valid UTF-8", e);
|
||||
}
|
||||
}
|
||||
records.Add(fieldStrings);
|
||||
fields.Clear();
|
||||
}
|
||||
|
||||
line++;
|
||||
currentLineStart = i + 1;
|
||||
}
|
||||
else if (inputBuffer[i] == '#')
|
||||
{
|
||||
throw new Exception($"Found unescaped '#' at line {line}, column {i - currentLineStart}");
|
||||
}
|
||||
else
|
||||
{
|
||||
fieldBytes.Add(inputBuffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
fields.Add(fieldBytes.ToArray());
|
||||
|
||||
if (numFields == 0)
|
||||
{
|
||||
throw new Exception("Found 0 fields on last line. Possibly because of extra \\n after last record");
|
||||
}
|
||||
if (numFields != fields.Count)
|
||||
{
|
||||
throw new Exception($"Expected {numFields} fields on line {line}, but found {fields.Count}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var fieldStrings = new string[fields.Count];
|
||||
for (int j = 0; j < fields.Count; j++)
|
||||
{
|
||||
try
|
||||
{
|
||||
fieldStrings[j] = Encoding.UTF8.GetString(fields[j]);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception($"Line {line}, column {j} is not valid UTF-8", e);
|
||||
}
|
||||
}
|
||||
records.Add(fieldStrings);
|
||||
fields.Clear();
|
||||
}
|
||||
|
||||
return (columnNames, records.ToArray());
|
||||
}
|
||||
|
||||
public static (string[] columns, string[][] data) ParseSimpleTsvParallel(byte[] inputBuffer)
|
||||
{
|
||||
string[] columnNames = null;
|
||||
var headers = new List<byte[]>();
|
||||
var headerFields = new List<byte[]>();
|
||||
var fieldBytes = new List<byte>();
|
||||
int startOfData = -1;
|
||||
for (int i = 0; i < inputBuffer.Count(); i++)
|
||||
@ -977,33 +882,33 @@ public class SaneTsv
|
||||
else if (inputBuffer[i] == '\t')
|
||||
{
|
||||
// end of field
|
||||
headers.Add(fieldBytes.ToArray());
|
||||
headerFields.Add(fieldBytes.ToArray());
|
||||
fieldBytes.Clear();
|
||||
}
|
||||
else if (inputBuffer[i] == '\n')
|
||||
{
|
||||
// This is the end of the header
|
||||
headers.Add(fieldBytes.ToArray());
|
||||
headerFields.Add(fieldBytes.ToArray());
|
||||
startOfData = i + 1;
|
||||
|
||||
columnNames = new string[headers.Count];
|
||||
columnNames = new string[headerFields.Count];
|
||||
fieldBytes.Clear();
|
||||
|
||||
for (int j = 0; j < headers.Count; j++)
|
||||
for (int j = 0; j < headerFields.Count; j++)
|
||||
{
|
||||
string columnString;
|
||||
try
|
||||
{
|
||||
columnString = Encoding.UTF8.GetString(headers[j]);
|
||||
columnString = Encoding.UTF8.GetString(headerFields[j]);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception($"Column {headers.Count} name is not valid UTF-8", e);
|
||||
throw new Exception($"Column {headerFields.Count} name is not valid UTF-8", e);
|
||||
}
|
||||
|
||||
if (columnString.Contains(':'))
|
||||
{
|
||||
throw new Exception($"Header {headers.Count} contain ':', which is not allowed for column names");
|
||||
throw new Exception($"Header field {headerFields.Count} contain ':', which is not allowed for column names");
|
||||
}
|
||||
|
||||
columnNames[j] = columnString;
|
||||
@ -1022,16 +927,16 @@ public class SaneTsv
|
||||
}
|
||||
}
|
||||
|
||||
return (columnNames, ParseSimpleTsvParallel(inputBuffer, columnNames.Length, startOfData, inputBuffer.Length));
|
||||
return (columnNames, ParseSimpleTsv(inputBuffer, columnNames.Length, startOfData, inputBuffer.Length));
|
||||
}
|
||||
|
||||
public static string[][] ParseSimpleTsvParallel(byte[] inputBuffer, int numFields, int startIndex, int endIndex)
|
||||
public static string[][] ParseSimpleTsv(byte[] inputBuffer, int numFields, int startIndex, int endIndex)
|
||||
{
|
||||
var fieldBytes = new List<byte>();
|
||||
var fields = new List<byte[]>();
|
||||
var records = new List<string[]>();
|
||||
|
||||
int line = 1;
|
||||
int line = 2;
|
||||
int currentLineStart = 0;
|
||||
|
||||
// Go back to the start of the current line
|
||||
@ -1125,15 +1030,22 @@ public class SaneTsv
|
||||
|
||||
fields.Add(fieldBytes.ToArray());
|
||||
|
||||
if (fields.Count == 0)
|
||||
if (fields.Count == 0 && endIndex == inputBuffer.Length)
|
||||
{
|
||||
throw new Exception("Found 0 fields on last line. Possibly because of extra \\n after last record");
|
||||
}
|
||||
if (numFields != fields.Count)
|
||||
{
|
||||
if (endIndex == inputBuffer.Length)
|
||||
{
|
||||
throw new Exception($"Expected {numFields} fields on line {line}, but found {fields.Count}");
|
||||
}
|
||||
else
|
||||
{
|
||||
return records.ToArray();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var fieldStrings = new string[fields.Count];
|
||||
for (int j = 0; j < fields.Count; j++)
|
||||
@ -1265,15 +1177,16 @@ public class SaneTsv
|
||||
return SerializeTsv<T>(data, FormatType.COMMENTED_TSV);
|
||||
}
|
||||
|
||||
protected static byte[] SerializeTsv<T>(IList<T> data, FormatType tsvFormat) where T : TsvRecord
|
||||
protected static byte[] SerializeTsv<T>(IList<T> data, FormatType tsvFormat)
|
||||
{
|
||||
var bytes = new List<byte>();
|
||||
|
||||
var headerTypes = new List<Type>();
|
||||
var headerNames = new List<string>();
|
||||
var headerPropertyInfos = new List<PropertyInfo>();
|
||||
var columnTypes = new List<Type>();
|
||||
var columnNames = new List<string>();
|
||||
var columnPropertyInfos = new List<PropertyInfo>();
|
||||
int columnCount = 0;
|
||||
|
||||
// Serialize header
|
||||
foreach (PropertyInfo property in typeof(T).GetProperties())
|
||||
{
|
||||
TsvColumnAttribute attribute = (TsvColumnAttribute)Attribute.GetCustomAttribute(property, typeof(TsvColumnAttribute));
|
||||
@ -1283,30 +1196,30 @@ public class SaneTsv
|
||||
}
|
||||
|
||||
string headerName = attribute.ColumnName ?? property.Name;
|
||||
headerNames.Add(headerName);
|
||||
columnNames.Add(headerName);
|
||||
Type headerType = attribute.ColumnType ?? GetColumnFromType(property.PropertyType);
|
||||
if (tsvFormat == FormatType.SIMPLE_TSV && headerType != typeof(StringType))
|
||||
{
|
||||
throw new Exception($"Serializing Simple TSV requires all columns be of type string, but column '{headerName}' has type '{headerType}'");
|
||||
}
|
||||
headerTypes.Add(headerType);
|
||||
headerPropertyInfos.Add(property);
|
||||
columnTypes.Add(headerType);
|
||||
columnPropertyInfos.Add(property);
|
||||
// TODO: Check that the property type and given column type are compatible
|
||||
columnCount++;
|
||||
}
|
||||
|
||||
// Serialize header
|
||||
for (int i = 0; i < headerNames.Count; i++)
|
||||
for (int i = 0; i < columnNames.Count; i++)
|
||||
{
|
||||
for (int j = i + 1; j < headerNames.Count; j++)
|
||||
for (int j = i + 1; j < columnNames.Count; j++)
|
||||
{
|
||||
if (headerNames[i] == headerNames[j])
|
||||
if (columnNames[i] == columnNames[j])
|
||||
{
|
||||
throw new Exception("Column names in header must be unique");
|
||||
}
|
||||
}
|
||||
|
||||
byte[] nameEncoded = Encoding.UTF8.GetBytes(headerNames[i]);
|
||||
byte[] nameEncoded = Encoding.UTF8.GetBytes(columnNames[i]);
|
||||
|
||||
for (int j = 0; j < nameEncoded.Length; j++)
|
||||
{
|
||||
@ -1341,15 +1254,15 @@ public class SaneTsv
|
||||
bytes.Add((byte)':');
|
||||
try
|
||||
{
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(GetNameFromColumn(headerTypes[i])));
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(GetNameFromColumn(columnTypes[i])));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception($"Invalid header type for column {i}", e);
|
||||
throw new Exception($"Invalid column type for column {i}", e);
|
||||
}
|
||||
}
|
||||
|
||||
if (i == headerNames.Count - 1)
|
||||
if (i == columnNames.Count - 1)
|
||||
{
|
||||
bytes.Add((byte)'\n');
|
||||
}
|
||||
@ -1359,12 +1272,20 @@ public class SaneTsv
|
||||
}
|
||||
}
|
||||
|
||||
// Serialize data
|
||||
SerializeTsv<T>(data, bytes, columnPropertyInfos.ToArray(), columnTypes.ToArray(), tsvFormat, 0, data.Count);
|
||||
|
||||
return bytes.ToArray();
|
||||
}
|
||||
|
||||
protected static void SerializeTsv<T>(IList<T> data, List<byte> bytes, PropertyInfo[] columnPropertyInfos, Type[] columnTypes, FormatType tsvFormat, int startIndex, int endIndex)
|
||||
{
|
||||
// Serialize data
|
||||
for (int i = 0; i < data.Count; i++)
|
||||
{
|
||||
for (int j = 0; j < columnCount; j++)
|
||||
for (int j = 0; j < columnTypes.Length; j++)
|
||||
{
|
||||
object datum = headerPropertyInfos[j].GetValue(data[i]);
|
||||
object datum = columnPropertyInfos[j].GetValue(data[i]);
|
||||
|
||||
try
|
||||
{
|
||||
@ -1372,16 +1293,16 @@ public class SaneTsv
|
||||
// Some fields definitely don't need escaping, so we add them directly to bytes
|
||||
bool skipEscaping = false;
|
||||
|
||||
if (headerTypes[j] == typeof(StringType))
|
||||
if (columnTypes[j] == typeof(StringType))
|
||||
{
|
||||
fieldEncoded = Encoding.UTF8.GetBytes((string)datum);
|
||||
}
|
||||
else if (headerTypes[j] == typeof(BooleanType))
|
||||
else if (columnTypes[j] == typeof(BooleanType))
|
||||
{
|
||||
bytes.AddRange((bool)datum ? TrueEncoded : FalseEncoded);
|
||||
skipEscaping = true;
|
||||
}
|
||||
else if (headerTypes[j] == typeof(Float32Type))
|
||||
else if (columnTypes[j] == typeof(Float32Type))
|
||||
{
|
||||
if (datum is float f)
|
||||
{
|
||||
@ -1405,7 +1326,7 @@ public class SaneTsv
|
||||
}
|
||||
skipEscaping = true;
|
||||
}
|
||||
else if (headerTypes[j] == typeof(Float32LEType))
|
||||
else if (columnTypes[j] == typeof(Float32LEType))
|
||||
{
|
||||
if (LittleEndian)
|
||||
{
|
||||
@ -1421,7 +1342,7 @@ public class SaneTsv
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (headerTypes[j] == typeof(Float64Type))
|
||||
else if (columnTypes[j] == typeof(Float64Type))
|
||||
{
|
||||
if (datum is double d)
|
||||
{
|
||||
@ -1445,7 +1366,7 @@ public class SaneTsv
|
||||
}
|
||||
skipEscaping = true;
|
||||
}
|
||||
else if (headerTypes[j] == typeof(Float64LEType))
|
||||
else if (columnTypes[j] == typeof(Float64LEType))
|
||||
{
|
||||
if (LittleEndian)
|
||||
{
|
||||
@ -1461,33 +1382,33 @@ public class SaneTsv
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (headerTypes[j] == typeof(UInt32Type))
|
||||
else if (columnTypes[j] == typeof(UInt32Type))
|
||||
{
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((UInt32)datum).ToString()));
|
||||
skipEscaping = true;
|
||||
}
|
||||
else if (headerTypes[j] == typeof(UInt64Type))
|
||||
else if (columnTypes[j] == typeof(UInt64Type))
|
||||
{
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((UInt64)datum).ToString()));
|
||||
skipEscaping = true;
|
||||
}
|
||||
else if (headerTypes[j] == typeof(Int32Type))
|
||||
else if (columnTypes[j] == typeof(Int32Type))
|
||||
{
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((Int32)datum).ToString()));
|
||||
skipEscaping = true;
|
||||
}
|
||||
else if (headerTypes[j] == typeof(Int64Type))
|
||||
else if (columnTypes[j] == typeof(Int64Type))
|
||||
{
|
||||
bytes.AddRange(Encoding.UTF8.GetBytes(((Int64)datum).ToString()));
|
||||
skipEscaping = true;
|
||||
}
|
||||
else if (headerTypes[j] == typeof(BinaryType))
|
||||
else if (columnTypes[j] == typeof(BinaryType))
|
||||
{
|
||||
fieldEncoded = (byte[])datum;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Unexpected column type {headerTypes[j]} for column {j}");
|
||||
throw new Exception($"Unexpected column type {columnTypes[j]} for column {j}");
|
||||
}
|
||||
|
||||
if (!skipEscaping)
|
||||
@ -1521,7 +1442,7 @@ public class SaneTsv
|
||||
}
|
||||
}
|
||||
|
||||
if (j < columnCount - 1)
|
||||
if (j < columnTypes.Length - 1)
|
||||
{
|
||||
bytes.Add((byte)'\t');
|
||||
}
|
||||
@ -1532,12 +1453,10 @@ public class SaneTsv
|
||||
}
|
||||
catch (InvalidCastException e)
|
||||
{
|
||||
throw new Exception($"Record {i}, field {j} expected type compatible with {GetNameFromColumn(headerTypes[j])}", e);
|
||||
throw new Exception($"Record {i}, field {j} expected type compatible with {GetNameFromColumn(columnTypes[j])}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bytes.ToArray();
|
||||
}
|
||||
|
||||
public class SimpleTsvRecord
|
||||
|
@ -1,7 +1,8 @@
|
||||
using NathanMcRae;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
internal class Program
|
||||
internal class Program : SaneTsv
|
||||
{
|
||||
public class TestRecord : SaneTsv.TsvRecord
|
||||
{
|
||||
@ -80,6 +81,18 @@ internal class Program
|
||||
public string Column3 { get; set; }
|
||||
}
|
||||
|
||||
public class BoolTestRecord3 : SaneTsv.CommentedTsvRecord
|
||||
{
|
||||
[SaneTsv.TsvColumn("column1")]
|
||||
public string Column1 { get; set; }
|
||||
|
||||
[SaneTsv.TsvColumn]
|
||||
public string column2 { get; set; }
|
||||
|
||||
[SaneTsv.TsvColumn("columnthree\nyep")]
|
||||
public string Column3 { get; set; }
|
||||
}
|
||||
|
||||
public class SerdeTestRecord : SaneTsv.CommentedTsvRecord
|
||||
{
|
||||
[SaneTsv.TypedTsvColumn("column1")]
|
||||
@ -349,45 +362,26 @@ internal class Program
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Check parallel serialization";
|
||||
string testName = "With and without file comment";
|
||||
|
||||
int N = 100000;
|
||||
var records = new StringTestRecord[N];
|
||||
var rand = new Random(1);
|
||||
string testString1 = "#This is a file comment\n" +
|
||||
"#One more file comment line\n" +
|
||||
"column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\n#This is a comment" +
|
||||
"\n#Another comment line" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
records[i] = new StringTestRecord()
|
||||
{
|
||||
Column1 = rand.Next().ToString(),
|
||||
column2 = rand.Next().ToString(),
|
||||
Column3 = rand.Next().ToString(),
|
||||
};
|
||||
}
|
||||
string testString2 = "column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\n#This is a comment" +
|
||||
"\n#Another comment line" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
string[][] recordStrings = records.Select(record => new string[] { record.Column1, record.column2, record.Column3 }).ToArray();
|
||||
CommentedTsv<BoolTestRecord2> parsed = SaneTsv.ParseCommentedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
CommentedTsv<BoolTestRecord2> parsed2 = SaneTsv.ParseCommentedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString2));
|
||||
|
||||
DateTime lastTime = DateTime.Now;
|
||||
byte[] serialized1 = SaneTsv.SerializeSimpleTsv(new string[] { "column1", "column2", "columnthree\nyep" }, recordStrings);
|
||||
TimeSpan unparallelTime = DateTime.Now - lastTime;
|
||||
lastTime = DateTime.Now;
|
||||
byte[] serialized2 = SaneTsv.SerializeSimpleTsvParallel(new string[] { "column1", "column2", "columnthree\nyep" }, recordStrings);
|
||||
TimeSpan parallelTime = DateTime.Now - lastTime;
|
||||
|
||||
Console.WriteLine($"Unparallel serialization time: {unparallelTime}");
|
||||
Console.WriteLine($"Parallel serialization time: {parallelTime}");
|
||||
|
||||
bool matching = true;
|
||||
for (int i = 0; i < Math.Min(serialized1.Length, serialized2.Length); i++)
|
||||
{
|
||||
if (serialized1[i] != serialized2[i])
|
||||
{
|
||||
matching = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (matching)
|
||||
if (parsed.FileComment == "This is a file comment\nOne more file comment line" && parsed2.FileComment == null)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
}
|
||||
@ -398,63 +392,370 @@ internal class Program
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Check parallel parsing";
|
||||
string testName = "With and without types";
|
||||
|
||||
int N = 100000;
|
||||
var records = new StringTestRecord[N];
|
||||
var rand = new Random(1);
|
||||
string testString1 = "column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\nTRUE\tvalue\\\\twoo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
try
|
||||
{
|
||||
records[i] = new StringTestRecord()
|
||||
Tsv<BoolTestRecord2> parsed = SaneTsv.ParseTypedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Passed {testName} 1A");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Column1 = rand.Next().ToString(),
|
||||
column2 = rand.Next().ToString(),
|
||||
Column3 = rand.Next().ToString(),
|
||||
};
|
||||
Console.WriteLine($"Failed {testName} 1A");
|
||||
}
|
||||
|
||||
byte[] serialized = SaneTsv.SerializeSimpleTsv<StringTestRecord>(records);
|
||||
|
||||
DateTime lastTime = DateTime.Now;
|
||||
(string[] headers2, string[][] data2) = SaneTsv.ParseSimpleTsv(serialized);
|
||||
TimeSpan unparallelTime = DateTime.Now - lastTime;
|
||||
lastTime = DateTime.Now;
|
||||
(string[] headers, string[][] data) = SaneTsv.ParseSimpleTsvParallel(serialized);
|
||||
TimeSpan parallelTime = DateTime.Now - lastTime;
|
||||
|
||||
Console.WriteLine($"Unparallel serialization time: {unparallelTime}");
|
||||
Console.WriteLine($"Parallel serialization time: {parallelTime}");
|
||||
|
||||
bool matching = true;
|
||||
for (int j = 0; j < Math.Min(headers2.Length, headers.Length); j++)
|
||||
try
|
||||
{
|
||||
if (headers[j] != headers2[j])
|
||||
Tsv<BoolTestRecord2> parsed2 = SaneTsv.ParseSimpleTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 1B");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
matching = false;
|
||||
break;
|
||||
Console.WriteLine($"Passed {testName} 1B");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
(string[] columns, string[][] data) = SaneTsv.ParseSimpleTsv(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 1C");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1C");
|
||||
}
|
||||
|
||||
string testString2 = "column1\tcolumn2\tcolumnthree\\nyep" +
|
||||
"\nTRUE\tvalue\\\\twoo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord2> parsed = SaneTsv.ParseTypedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString2));
|
||||
Console.WriteLine($"Failed {testName} 2A");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 2A");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord2> parsed2 = SaneTsv.ParseSimpleTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 2B");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 2B");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
(string[] columns, string[][] data) = SaneTsv.ParseSimpleTsv(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 2C");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 2C");
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < Math.Min(data.Length, data2.Length) && matching; i++)
|
||||
{
|
||||
for (int j = 0; j < data[0].Length; j++)
|
||||
string testName = "With and without line comment";
|
||||
|
||||
string testString1 = "column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\n#This is a comment" +
|
||||
"\n#Another comment line" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther";
|
||||
|
||||
try
|
||||
{
|
||||
if (data[i][j] != data2[i][j])
|
||||
{
|
||||
matching = false;
|
||||
break;
|
||||
CommentedTsv<BoolTestRecord2> parsed = SaneTsv.ParseCommentedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Passed {testName} 1A");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Failed {testName} 1A");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord2> parsed = SaneTsv.ParseTypedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 1B");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1B");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord2> parsed2 = SaneTsv.ParseSimpleTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 1C");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1C");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
(string[] columns, string[][] data) = SaneTsv.ParseSimpleTsv(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 1D");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1D");
|
||||
}
|
||||
}
|
||||
|
||||
if (matching)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName}");
|
||||
string testName = "End of file comment";
|
||||
|
||||
string testString1 = "column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther" +
|
||||
"\n# Hey, you're not supposed to have comments at the end of the tsv!";
|
||||
|
||||
try
|
||||
{
|
||||
CommentedTsv<BoolTestRecord2> parsed = SaneTsv.ParseCommentedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 1A");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1A");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord2> parsed = SaneTsv.ParseTypedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 1B");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1B");
|
||||
}
|
||||
|
||||
string testString2 = "column1\tcolumn2\tcolumnthree\\nyep" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther" +
|
||||
"\n# Hey, you're not supposed to have comments at the end of the tsv!";
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord3> parsed3 = SaneTsv.ParseSimpleTsv<BoolTestRecord3>(Encoding.UTF8.GetBytes(testString2));
|
||||
Console.WriteLine($"Failed {testName} 1C");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1C");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
(string[] columns, string[][] data) = SaneTsv.ParseSimpleTsv(Encoding.UTF8.GetBytes(testString2));
|
||||
Console.WriteLine($"Failed {testName} 1D");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1D");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "Partial parsing";
|
||||
|
||||
string line1 = "column1\tcolumn2\tcolumnthree\\nyep";
|
||||
string line2 = "\nTRUE\tvalue\\\\t\0woo\tvaluetrhee";
|
||||
string line3 = "\nFALSE\tnother\tno\\ther";
|
||||
|
||||
byte[] inputBuffer = Encoding.UTF8.GetBytes(line1 + line2 + line3);
|
||||
|
||||
var headerTypes = new List<Type>();
|
||||
var headerNames = new List<string>();
|
||||
var headerPropertyInfos = new List<PropertyInfo>();
|
||||
int columnCount = 0;
|
||||
|
||||
foreach (PropertyInfo property in typeof(BoolTestRecord3).GetProperties())
|
||||
{
|
||||
TsvColumnAttribute attribute = (TsvColumnAttribute)Attribute.GetCustomAttribute(property, typeof(TsvColumnAttribute));
|
||||
if (attribute == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
headerNames.Add(attribute.ColumnName ?? property.Name);
|
||||
headerTypes.Add(attribute.ColumnType ?? GetColumnFromType(property.PropertyType));
|
||||
headerPropertyInfos.Add(property);
|
||||
// TODO: Check that the property type and given column type are compatible
|
||||
columnCount++;
|
||||
}
|
||||
|
||||
BoolTestRecord3[] records = SaneTsv.Parse<BoolTestRecord3>(inputBuffer,
|
||||
FormatType.SIMPLE_TSV,
|
||||
headerPropertyInfos.ToArray(),
|
||||
headerTypes.ToArray(),
|
||||
line1.Length + line2.Length + 1,
|
||||
inputBuffer.Length);
|
||||
|
||||
if (records.Length == 0 )
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Failed {testName}");
|
||||
Console.WriteLine($"Failed {testName} 1");
|
||||
}
|
||||
|
||||
BoolTestRecord3[] records2 = SaneTsv.Parse<BoolTestRecord3>(inputBuffer,
|
||||
FormatType.SIMPLE_TSV,
|
||||
headerPropertyInfos.ToArray(),
|
||||
headerTypes.ToArray(),
|
||||
line1.Length,
|
||||
line1.Length + 3);
|
||||
|
||||
if (records2[0].Column3 == "valuetrhee")
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 2");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Failed {testName} 2");
|
||||
}
|
||||
|
||||
string[][] data = SaneTsv.ParseSimpleTsv(inputBuffer, 3, line1.Length + line2.Length + 1, inputBuffer.Length);
|
||||
|
||||
if (data[0][1] == "nother")
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 3");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Failed {testName} 3");
|
||||
}
|
||||
|
||||
string[][] data2 = SaneTsv.ParseSimpleTsv(inputBuffer, 3, line1.Length, line1.Length + 3);
|
||||
|
||||
if (data2.Length == 0)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 4");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Failed {testName} 4");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "End of file \\n";
|
||||
|
||||
string testString1 = "column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther" +
|
||||
"\n";
|
||||
|
||||
try
|
||||
{
|
||||
CommentedTsv<BoolTestRecord2> parsed = SaneTsv.ParseCommentedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 1A");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1A");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord2> parsed = SaneTsv.ParseTypedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 1B");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1B");
|
||||
}
|
||||
|
||||
string testString2 = "column1\tcolumn2\tcolumnthree\\nyep" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther" +
|
||||
"\n";
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord3> parsed3 = SaneTsv.ParseSimpleTsv<BoolTestRecord3>(Encoding.UTF8.GetBytes(testString2));
|
||||
Console.WriteLine($"Failed {testName} 1C");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1C");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
(string[] columns, string[][] data) = SaneTsv.ParseSimpleTsv(Encoding.UTF8.GetBytes(testString2));
|
||||
Console.WriteLine($"Failed {testName} 1D");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1D");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
string testName = "End of file partial record";
|
||||
|
||||
string testString1 = "column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther" +
|
||||
"\nTRUE\t";
|
||||
|
||||
try
|
||||
{
|
||||
CommentedTsv<BoolTestRecord2> parsed = SaneTsv.ParseCommentedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 1A");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1A");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord2> parsed = SaneTsv.ParseTypedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
|
||||
Console.WriteLine($"Failed {testName} 1B");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1B");
|
||||
}
|
||||
|
||||
string testString2 = "column1\tcolumn2\tcolumnthree\\nyep" +
|
||||
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
|
||||
"\nFALSE\tnother\tno\\ther" +
|
||||
"\nTRUE\t";
|
||||
|
||||
try
|
||||
{
|
||||
Tsv<BoolTestRecord3> parsed3 = SaneTsv.ParseSimpleTsv<BoolTestRecord3>(Encoding.UTF8.GetBytes(testString2));
|
||||
Console.WriteLine($"Failed {testName} 1C");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1C");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
(string[] columns, string[][] data) = SaneTsv.ParseSimpleTsv(Encoding.UTF8.GetBytes(testString2));
|
||||
Console.WriteLine($"Failed {testName} 1D");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Passed {testName} 1D");
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user