2using System.Collections.Generic;
12 public static class TSV
14 #region Encoding/Decoding
21 public static string[][]
Parse(
string Tsv)
25 string[][] Result =
Parse(Tsv, ref Pos, Len);
28 while (Pos < Len && ((ch = Tsv[Pos]) <=
' ' || ch == 160))
32 throw new Exception(
"Unexpected content at end of string.");
37 private static string[][]
Parse(
string Tsv, ref
int Pos,
int Len)
39 List<string[]> Records =
new List<string[]>();
40 List<string> Fields =
new List<string>();
41 StringBuilder sb =
new StringBuilder();
56 Fields.Add(
string.Empty);
57 else if (ch ==
'\r' || ch ==
'\n')
61 Records.Add(Fields.ToArray());
76 Fields.Add(sb.ToString());
81 else if (ch ==
'\r' || ch ==
'\n')
83 Fields.Add(sb.ToString());
88 Records.Add(Fields.ToArray());
161 i = JSON.HexDigit(ch);
167 i |= JSON.HexDigit(ch);
173 i |= JSON.HexDigit(ch);
179 i |= JSON.HexDigit(ch);
188 Fields.Add(sb.ToString());
190 if (Fields.Count > 0)
191 Records.Add(Fields.ToArray());
193 return Records.ToArray();
201 public static string Encode(
string[][] Records)
203 return Encode(Records,
true);
213 public static string Encode(
string[][] Records,
bool QuoteIllegalCharacters)
215 StringBuilder sb =
new StringBuilder();
218 foreach (
string[] Record
in Records)
222 foreach (
string Field
in Record)
225 bool Control =
false;
236 foreach (
char ch
in Field)
238 if (ch ==
'\t' && QuoteIllegalCharacters)
240 else if (ch ==
'"' && QuoteIllegalCharacters)
244 if (!QuoteIllegalCharacters)
245 throw new InvalidOperationException(
"String is not properly quoted.");
251 if (Tab || Quote || Control)
253 string Escaped = Field;
256 Escaped = Escaped.Replace(
"\"",
"\\\"");
261 Replace(
"\a",
"\\a").
262 Replace(
"\b",
"\\b").
263 Replace(
"\f",
"\\f").
264 Replace(
"\n",
"\\n").
265 Replace(
"\r",
"\\r").
266 Replace(
"\t",
"\\t").
267 Replace(
"\v",
"\\v");
281 return sb.ToString();
291 return Encode(Matrix, (E) =>
293 if (E.AssociatedObjectValue is
string s)
295 else if (E.AssociatedObjectValue is
double d)
298 return E.AssociatedObjectValue?.ToString();
312 if (ElementToString is
null)
313 throw new ArgumentNullException(nameof(ElementToString));
315 List<string[]> Records =
new List<string[]>();
316 List<string> Fields =
new List<string>();
320 if (!(M.ColumnNames is
null))
321 Records.Add(M.ColumnNames);
324 int Row, NrRows = Matrix.
Rows;
325 int Column, NrColumns = Matrix.
Columns;
327 for (Row = 0; Row < NrRows; Row++)
329 for (Column = 0; Column < NrColumns; Column++)
330 Fields.Add(ElementToString(Matrix.
GetElement(Column, Row)));
332 Records.Add(Fields.ToArray());
336 return Encode(Records.ToArray(), QuoteIllegalCharacters);
Helps with parsing of commong data types.
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Helps with common TSV-related tasks. (TSV=TAB Separated Values)
static string Encode(string[][] Records)
Encodes records as a Comma-separated values string.
static string Encode(IMatrix Matrix, ToString ElementToString, bool QuoteIllegalCharacters)
Encodes a matrix as a Comma-separated values string.
static string Encode(string[][] Records, bool QuoteIllegalCharacters)
Encodes records as a Comma-separated values string.
static string[][] Parse(string Tsv)
Parses a TSV string.
static string Encode(IMatrix Matrix)
Encodes a matrix as a Comma-separated values string.
Basic interface for matrices.
int Columns
Number of columns.
IElement GetElement(int Column, int Row)
Gets an element of the matrix.
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.