From 38853568b4626bdbd23f582d84aea945ca3358b8 Mon Sep 17 00:00:00 2001 From: StunxFS <56417208+StunxFS@users.noreply.github.com> Date: Sun, 3 Apr 2022 12:13:43 -0400 Subject: [PATCH] encoding.csv: allow passing a custom delimiter to the `new_reader` function (#13910) --- vlib/encoding/csv/reader.v | 15 +++++++++++---- vlib/encoding/csv/reader_test.v | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/vlib/encoding/csv/reader.v b/vlib/encoding/csv/reader.v index 55bf5b8d8e..9907c85246 100644 --- a/vlib/encoding/csv/reader.v +++ b/vlib/encoding/csv/reader.v @@ -50,12 +50,19 @@ pub mut: row_pos int } -// new_reader initializes a Reader with string data to parse -pub fn new_reader(data string) &Reader { +[params] +pub struct ReaderConfig { + delimiter byte = `,` + comment byte = `#` +} + +// new_reader initializes a Reader with string data to parse and, +// optionally, a custom delimiter. +pub fn new_reader(data string, config ReaderConfig) &Reader { return &Reader{ - delimiter: `,` - comment: `#` data: data + delimiter: config.delimiter + comment: config.comment } } diff --git a/vlib/encoding/csv/reader_test.v b/vlib/encoding/csv/reader_test.v index cd54827ad3..f2350b9e6d 100644 --- a/vlib/encoding/csv/reader_test.v +++ b/vlib/encoding/csv/reader_test.v @@ -28,6 +28,34 @@ fn test_encoding_csv_reader() { assert row_count == 3 } +fn test_encoding_csv_reader_with_custom_delimiter() { + data := 'name|email|phone|other\njoe|joe@blow.com|0400000000|test\nsam|sam@likesham.com|0433000000|"test quoted field"\n#chris|chris@nomail.com|94444444|"commented row"\n' + mut csv_reader := csv.new_reader(data, delimiter: `|`) + mut row_count := 0 + for { + row := csv_reader.read() or { break } + row_count++ + if row_count == 1 { + assert row[0] == 'name' + assert row[1] == 'email' + assert row[2] == 'phone' + assert row[3] == 'other' + } else if row_count == 2 { + assert row[0] == 'joe' + assert row[1] == 'joe@blow.com' + assert row[2] == '0400000000' + assert row[3] == 'test' + } else if row_count == 3 { + assert row[0] == 'sam' + assert row[1] == 'sam@likesham.com' + assert row[2] == '0433000000' + // quoted field + assert row[3] == 'test quoted field' + } + } + assert row_count == 3 +} + fn test_line_break_lf() { lf_data := 'name,email\njoe,joe@blow.com\n' mut csv_reader := csv.new_reader(lf_data)