use io; use strings; use fmt; export type error = !(str | io::error); export fn writerecord(w: io::handle, record: []str, separator: const str = ",", quote: const rune = '"') (void | error) = { let sep = ""; for (const field .. record) { if (strings::contains(field, separator)) { if (strings::contains(field, quote)) { return "ERROR: Field contains quote character (not supported)"; }; fmt::fprintf(w, "{}{}{}{}", sep, quote, field, quote)!; } else { fmt::fprintf(w, "{}{}", sep, field)!; }; sep = separator; }; fmt::fprintln(w)!; }; export fn writerecords(w: io::handle, records: [][]str, separator: const str = ",", quote: const rune = '"') (void | error) = { for (const record .. records) { writerecord(w, record, separator, quote)?; }; };