From 76dd45d61be6e4f5f174c59298fca7bc8596e8bb Mon Sep 17 00:00:00 2001 From: Vibhu Mohindra Date: Mon, 18 Aug 2025 13:40:53 +0200 Subject: [PATCH] Fix empty field handling in multi-record file RFC 4180 section 2.4 allows one or more fields (i.e. not zero). Given a three-record CSV file with one field per record, it's reasonable for it to have a blank second line indicating that the second record's only field is empty. Presently, this library reads that line as having no fields. There is no need for it to, since the RFC doesn't allow records with no fields. This patch fixes the library so that it correctly reads that second line as having one empty field. This change is not backwards compatible. The earlier specification in test-happy-ending is for a file with nothing but three spaces to be read as having one record with no fields. I've changed that test specification to say that such a file will now be read in as having one record with one empty field. --- csv.lisp | 7 +------ test.lisp | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/csv.lisp b/csv.lisp index da26e34..39fdc51 100644 --- a/csv.lisp +++ b/csv.lisp @@ -238,12 +238,7 @@ Be careful to not skip a separator, as it could be e.g. a tab!" (setf had-quotes nil) (when *skip-whitespace* (accept-spaces stream)) - (cond - ((and (null fields) - (or (accept-eol stream) (accept-eof stream))) - (done)) - (t - (do-field-start)))) + (do-field-start)) (do-field-start () (cond ((accept-separator stream) diff --git a/test.lisp b/test.lisp index 3bba1bf..e92514a 100644 --- a/test.lisp +++ b/test.lisp @@ -61,8 +61,25 @@ ") (test-read-write " " - '(()) + '(("")) +lf+) (test-both "" '())) + +(deftest test-empty-field () + (test-read + "a + +c" + '(("a") + ("") + ("c"))) + (test-read + "a +" + '(("a"))) + (test-read + "a + " + '(("a") (""))))