From 71e016a3aa1adfd0613cdcb19739e620fc53ef0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Thu, 30 Mar 2023 13:41:21 +0200 Subject: [PATCH] Splitting CSV into header and body. --- csv-simple.scm | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/csv-simple.scm b/csv-simple.scm index ea921fa..35f0c7a 100644 --- a/csv-simple.scm +++ b/csv-simple.scm @@ -29,6 +29,7 @@ csv-simple ( csv-parse + csv-split-header csv-simple-tests! ) @@ -86,6 +87,19 @@ (csv-parse-line (make-csv-line-parser separator string-delimiter))) (map csv-parse-line lines))) + ;; Splits CSV into header and body based on the first empty row. + (define (csv-split-header csv) + (let loop ((body csv) + (rhead '())) + (if (null? body) + (list (reverse rhead) '()) + (let ((row (car body))) + (if (null? row) + (list (reverse rhead) + (cdr body)) + (loop (cdr body) + (cons row rhead))))))) + ;; Module self-tests (define (csv-simple-tests!) (run-tests @@ -96,6 +110,12 @@ (test-equal? csv-parse-line ((make-csv-line-parser ";" "\"") "test;\"2;quoted\";3") '("test" "2;quoted" "3")) + (test-equal? csv-split-header + (csv-split-header '((1 2) () (3 4))) + '(((1 2)) ((3 4)))) + (test-equal? csv-split-header + (csv-split-header '((1 2) (5 6) (3 4))) + '(((1 2) (5 6) (3 4)) ())) )) )