2using System.Collections.Generic;
20 public static class CSV
22 #region Encoding/Decoding
29 public static string[][]
Parse(
string Csv)
33 string[][] Result =
Parse(Csv, ref Pos, Len);
36 while (Pos < Len && ((ch = Csv[Pos]) <=
' ' || ch == 160))
40 throw new Exception(
"Unexpected content at end of string.");
45 private static string[][]
Parse(
string Csv, ref
int Pos,
int Len)
47 List<string[]> Records =
new List<string[]>();
48 List<string>
Fields =
new List<string>();
49 StringBuilder sb =
new StringBuilder();
65 else if (ch ==
'\r' || ch ==
'\n')
69 Records.Add(
Fields.ToArray());
89 else if (ch ==
'\r' || ch ==
'\n')
96 Records.Add(
Fields.ToArray());
169 i = JSON.HexDigit(ch);
175 i |= JSON.HexDigit(ch);
181 i |= JSON.HexDigit(ch);
187 i |= JSON.HexDigit(ch);
196 Fields.Add(sb.ToString());
199 Records.Add(
Fields.ToArray());
201 return Records.ToArray();
209 public static string Encode(
string[][] Records)
211 StringBuilder sb =
new StringBuilder();
214 foreach (
string[] Record
in Records)
218 foreach (
string Field
in Record)
221 bool Control =
false;
232 foreach (
char ch
in Field)
242 if (Comma || Quote || Control)
244 string Escaped = Field;
247 Escaped = Escaped.Replace(
"\"",
"\\\"");
252 Replace(
"\a",
"\\a").
253 Replace(
"\b",
"\\b").
254 Replace(
"\f",
"\\f").
255 Replace(
"\n",
"\\n").
256 Replace(
"\r",
"\\r").
257 Replace(
"\t",
"\\t").
258 Replace(
"\v",
"\\v");
272 return sb.ToString();
282 return Encode(Matrix, (E) =>
284 if (E.AssociatedObjectValue is
string s)
286 else if (E.AssociatedObjectValue is
double d)
289 return E.AssociatedObjectValue?.ToString();
301 if (ElementToString is
null)
302 throw new ArgumentNullException(nameof(ElementToString));
304 List<string[]> Records =
new List<string[]>();
305 List<string>
Fields =
new List<string>();
309 if (!(M.ColumnNames is
null))
310 Records.Add(M.ColumnNames);
313 int Row, NrRows = Matrix.
Rows;
314 int Column, NrColumns = Matrix.
Columns;
316 for (Row = 0; Row < NrRows; Row++)
318 for (Column = 0; Column < NrColumns; Column++)
321 Records.Add(
Fields.ToArray());
325 return Encode(Records.ToArray());
Helps with common CSV-related tasks. (CSV=Comma Separated Values)
static string[][] Parse(string Csv)
Parses a CSV string.
static string Encode(string[][] Records)
Encodes records as a Comma-separated values string.
static string Encode(IMatrix Matrix, ToString ElementToString)
Encodes a matrix as a Comma-separated values string.
static string Encode(IMatrix Matrix)
Encodes a matrix as a Comma-separated values string.
Helps with parsing of commong data types.
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Base class for all types of elements.
Extract the fields of a type or an object.
Basic interface for all types of elements.
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.