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)
49 StringBuilder sb =
new StringBuilder();
64 Fields.
Add(
string.Empty);
65 else if (ch ==
'\r' || ch ==
'\n')
84 Fields.
Add(sb.ToString());
89 else if (ch ==
'\r' || ch ==
'\n')
91 Fields.
Add(sb.ToString());
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());
198 if (Fields.
Count > 0)
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)
301 if (ElementToString is
null)
302 throw new ArgumentNullException(nameof(ElementToString));
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++)
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.
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 types of elements.
Base class for all nodes in a parsed script tree.
override string ToString()
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.