From 17cfe8242b4c920f713edf460d44939da69e05cd Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 24 Jan 2025 21:14:59 -0800 Subject: get-csv: bugfix: return nil on EOF. * stream.c (get_csv): Let's add a new state init. If get_char returns nil and we are in the init state, let's bail to a nil return. While we are at it, let's not allocate the record or string until we read at least one character. If we read a character in the init state, let's allocate those two objects, and then change to the rfield state and fall through to it to handle the character. * tests/010/csv.tl: Fix one incorrect test: (tocsv "") now returns nil, as it should. Add tests for multiple record extraction, also covering missing line termination on the last record as well as CR-LF termination. * txr.1: Documented nil return conditions. --- tests/010/csv.tl | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'tests/010/csv.tl') diff --git a/tests/010/csv.tl b/tests/010/csv.tl index 6c93b6c8..d8561c8c 100644 --- a/tests/010/csv.tl +++ b/tests/010/csv.tl @@ -13,7 +13,7 @@ data expected)))) (mtest-csv - "" #("") + "" nil "," #("" "") ",," #("" "" "")) @@ -183,3 +183,25 @@ #("a\nb" "c#d" "e,f") "#a\nb#,#c##d#,#e,f#\n" #("a\n#,b") "#a\n##,b#\n" #("a#\n,b\n") "#a##\n,b\n#\n") + +(mtest + (with-in-string-stream (s "a,b,c\nd,e,f\r\n") (list (get-csv s) (get-csv s) (get-csv s))) + (#("a" "b" "c") #("d" "e" "f") nil) + (with-in-string-stream (s "a,b,c\nd,e,f\n") (list (get-csv s) (get-csv s) (get-csv s))) + (#("a" "b" "c") #("d" "e" "f") nil) + (with-in-string-stream (s "a,b,c\nd,e,f") (list (get-csv s) (get-csv s) (get-csv s))) + (#("a" "b" "c") #("d" "e" "f") nil) + (with-in-string-stream (s "a,b,c\r\n") (list (get-csv s) (get-csv s) (get-csv s))) + (#("a" "b" "c") nil nil) + (with-in-string-stream (s "a,b,c\n") (list (get-csv s) (get-csv s) (get-csv s))) + (#("a" "b" "c") nil nil) + (with-in-string-stream (s "a,b,c") (list (get-csv s) (get-csv s) (get-csv s))) + (#("a" "b" "c") nil nil) + (with-in-string-stream (s "") (list (get-csv s) (get-csv s) (get-csv s))) + (nil nil nil) + (with-in-string-stream (s "\r") (list (get-csv s) (get-csv s) (get-csv s))) + (#("\r") nil nil) + (with-in-string-stream (s "\r\n") (list (get-csv s) (get-csv s) (get-csv s))) + (#("") nil nil) + (with-in-string-stream (s "\n") (list (get-csv s) (get-csv s) (get-csv s))) + (#("") nil nil)) -- cgit v1.2.3