You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
636 B
Dart
25 lines
636 B
Dart
// lib/shared/csv.dart
|
|
class CsvBuilder {
|
|
final StringBuffer _buf = StringBuffer();
|
|
final String delimiter;
|
|
final String eol;
|
|
|
|
CsvBuilder({this.delimiter = ';', this.eol = '\n'});
|
|
|
|
static String _escape(String? v, String delimiter) {
|
|
final s = v ?? '';
|
|
final mustQuote =
|
|
s.contains(delimiter) || s.contains('\n') || s.contains('"');
|
|
var out = s.replaceAll('"', '""');
|
|
return mustQuote ? '"$out"' : out;
|
|
}
|
|
|
|
void addRow(Iterable<dynamic> cols) {
|
|
_buf.writeln(
|
|
cols.map((c) => _escape(c?.toString(), delimiter)).join(delimiter));
|
|
}
|
|
|
|
@override
|
|
String toString() => _buf.toString();
|
|
}
|