From 4da35965e4ba31cacd90776bffbf36d7f585c645 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Fri, 15 Nov 2024 01:14:07 +0100 Subject: tsv -> format::tsv and add tests --- format/tsv/writer.ha | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 format/tsv/writer.ha (limited to 'format/tsv/writer.ha') diff --git a/format/tsv/writer.ha b/format/tsv/writer.ha new file mode 100644 index 0000000..f1f045b --- /dev/null +++ b/format/tsv/writer.ha @@ -0,0 +1,63 @@ +use io; +use strings; +use fmt; +use memio; + +// Writes a slice strings to a handle in TSV format. Existing tabs in the record +// are removed. +export fn writerecord(w: io::handle, record: []str) (void | io::error) = { + let sep = ""; + for (const field .. record) { + const pfield = strings::replace(field, "\t", ""); + defer free(pfield); + fmt::fprintf(w, "{}{}", sep, pfield)!; + sep = "\t"; + }; + fmt::fprintln(w)!; +}; + +// Writes a slice of string slices to a handle in TSV format. Existing tabs in +// the records are removed. +export fn writerecords(w: io::handle, records: [][]str) (void | io::error) = { + for (const record .. records) { + writerecord(w, record)?; + }; +}; + +@test fn writenormal() void = { + const expected = "col1\tcol2\tcol3 +1\t2\t3 +4\t5\t6\n"; + const input: [][]str = [ + ["col1", "col2", "col3"], + ["1", "2", "3"], + ["4", "5", "6"], + ]; + const st = memio::dynamic(); + defer io::close(&st)!; + writerecords(&st, input)!; + const actual = memio::string(&st)!; + fmt::errorfln("expected: {}EOF", expected)!; + fmt::errorln()!; + fmt::errorfln("actual: {}EOF", actual)!; + assert(actual == expected); +}; + +@test fn writetabs() void = { + const expected = "col1\tcol2\tcol3 +1\t2\t3 +4\t5\t6\n"; + const input: [][]str = [ + ["col1\t", "co\tl2", "col3"], + ["1", "2", "\t3"], + ["4\t", "\t\t5\t", "6"], + ]; + const st = memio::dynamic(); + defer io::close(&st)!; + writerecords(&st, input)!; + const actual = memio::string(&st)!; + fmt::errorfln("expected: {}EOF", expected)!; + fmt::errorln()!; + fmt::errorfln("actual: {}EOF", actual)!; + assert(actual == expected); +}; -- cgit v1.2.3