13 public static class TSV
15 #region Encoding/Decoding
22 public static string[][]
Parse(
string Tsv)
26 string[][] Result =
Parse(Tsv, ref Pos, Len);
29 while (Pos < Len && ((ch = Tsv[Pos]) <=
' ' || ch == 160))
33 throw new Exception(
"Unexpected content at end of string.");
38 private static string[][]
Parse(
string Tsv, ref
int Pos,
int Len)
42 StringBuilder sb =
new StringBuilder();
57 Fields.
Add(
string.Empty);
58 else if (ch ==
'\r' || ch ==
'\n')
77 Fields.
Add(sb.ToString());
82 else if (ch ==
'\r' || ch ==
'\n')
84 Fields.
Add(sb.ToString());
162 i = JSON.HexDigit(ch);
168 i |= JSON.HexDigit(ch);
174 i |= JSON.HexDigit(ch);
180 i |= JSON.HexDigit(ch);
189 Fields.
Add(sb.ToString());
191 if (Fields.
Count > 0)
202 public static string Encode(
string[][] Records)
204 return Encode(Records,
true);
214 public static string Encode(
string[][] Records,
bool QuoteIllegalCharacters)
216 StringBuilder sb =
new StringBuilder();
219 foreach (
string[] Record
in Records)
223 foreach (
string Field
in Record)
226 bool Control =
false;
237 foreach (
char ch
in Field)
239 if (ch ==
'\t' && QuoteIllegalCharacters)
241 else if (ch ==
'"' && QuoteIllegalCharacters)
245 if (!QuoteIllegalCharacters)
246 throw new InvalidOperationException(
"String is not properly quoted.");
252 if (Tab || Quote || Control)
254 string Escaped = Field;
257 Escaped = Escaped.Replace(
"\"",
"\\\"");
262 Replace(
"\a",
"\\a").
263 Replace(
"\b",
"\\b").
264 Replace(
"\f",
"\\f").
265 Replace(
"\n",
"\\n").
266 Replace(
"\r",
"\\r").
267 Replace(
"\t",
"\\t").
268 Replace(
"\v",
"\\v");
282 return sb.ToString();
292 return Encode(Matrix, (E) =>
294 if (E.AssociatedObjectValue is
string s)
296 else if (E.AssociatedObjectValue is
double d)
313 if (ElementToString is
null)
314 throw new ArgumentNullException(nameof(ElementToString));
321 if (!(M.ColumnNames is
null))
322 Records.
Add(M.ColumnNames);
325 int Row, NrRows = Matrix.
Rows;
326 int Column, NrColumns = Matrix.
Columns;
328 for (Row = 0; Row < NrRows; Row++)
330 for (Column = 0; Column < NrColumns; Column++)
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.
A chunked list is a linked list of chunks of objects of type T .
void Clear()
Clears the collection.
int Count
Number of elements in collection.
void Add(T Item)
Adds an item to the collection.
T[] ToArray()
Returns an array containing all elements of the collection.
Base class for all nodes in a parsed script tree.
override string ToString()
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.