Files
pipelines/app/lib/features/artifacts/domain/artifact_table.dart
T

88 lines
2.4 KiB
Dart

/// A delimited text artifact (CSV/TSV) parsed for the viewer's table preview.
///
/// RFC 4180 quoting: a field in double quotes may contain the delimiter, line
/// breaks and doubled quotes (`""` is one quote). Rows may be ragged; the table
/// pads short rows when drawn rather than inventing values here.
class ArtifactTable {
const ArtifactTable({required this.rows, required this.totalRows});
/// At most [artifactTableRowLimit] parsed rows, the first being the header.
final List<List<String>> rows;
/// Every record in the file, so a capped preview can say what it omits.
final int totalRows;
bool get truncated => totalRows > rows.length;
int get columnCount =>
rows.fold(0, (widest, row) => row.length > widest ? row.length : widest);
}
/// Rows drawn in the viewer; larger files say how many were omitted.
const int artifactTableRowLimit = 500;
/// Parses [text] separated by [delimiter] (`,` for CSV, a tab for TSV).
ArtifactTable parseArtifactTable(
String text, {
String delimiter = ',',
int limit = artifactTableRowLimit,
}) {
final rows = <List<String>>[];
var total = 0;
var row = <String>[];
final field = StringBuffer();
var quoted = false;
var fieldStarted = false;
void endField() {
row.add(field.toString());
field.clear();
fieldStarted = false;
}
void endRow() {
endField();
// A trailing line break does not start an empty record.
if (row.length == 1 && row.first.isEmpty) {
row = <String>[];
return;
}
total++;
if (rows.length < limit) rows.add(row);
row = <String>[];
}
for (var i = 0; i < text.length; i++) {
final char = text[i];
if (quoted) {
if (char == '"') {
if (i + 1 < text.length && text[i + 1] == '"') {
field.write('"');
i++;
} else {
quoted = false;
}
} else {
field.write(char);
}
continue;
}
if (char == '"' && !fieldStarted) {
quoted = true;
fieldStarted = true;
} else if (char == delimiter) {
endField();
} else if (char == '\n') {
endRow();
} else if (char == '\r') {
if (i + 1 < text.length && text[i + 1] == '\n') i++;
endRow();
} else {
field.write(char);
fieldStarted = true;
}
}
if (fieldStarted || field.isNotEmpty || row.isNotEmpty) endRow();
return ArtifactTable(rows: rows, totalRows: total);
}