CSV: fixed case where there's whitespace padding inside quotes. tests now run ok
parent
634754d0b0
commit
39c578ee56
|
@ -10,34 +10,35 @@ IN: csv
|
||||||
|
|
||||||
DEFER: quoted-field
|
DEFER: quoted-field
|
||||||
|
|
||||||
|
! trims whitespace from either end of string
|
||||||
|
: trim-whitespace ( str -- str )
|
||||||
|
[ blank? ] trim ; inline
|
||||||
|
|
||||||
|
: skip-to-field-end ( -- endchar )
|
||||||
|
",\n" read-until nip ; inline
|
||||||
|
|
||||||
: not-quoted-field ( -- endchar )
|
: not-quoted-field ( -- endchar )
|
||||||
",\"\n" read-until ! "
|
",\"\n" read-until ! "
|
||||||
dup
|
dup
|
||||||
{ { CHAR: " [ drop drop quoted-field ] } ! "
|
{ { CHAR: " [ drop drop quoted-field ] } ! "
|
||||||
{ CHAR: , [ swap % ] }
|
{ CHAR: , [ swap trim-whitespace % ] }
|
||||||
{ CHAR: \n [ swap % ] }
|
{ CHAR: \n [ swap trim-whitespace % ] }
|
||||||
{ f [ swap % ] } ! eof
|
{ f [ swap trim-whitespace % ] } ! eof
|
||||||
} case ;
|
} case ;
|
||||||
|
|
||||||
: maybe-escaped-quote ( -- endchar )
|
: maybe-escaped-quote ( -- endchar )
|
||||||
read1
|
read1 dup
|
||||||
dup
|
{ { CHAR: " [ , quoted-field ] } ! " is an escaped quote
|
||||||
{ { CHAR: " [ , quoted-field ] } ! " is an escaped quote
|
{ CHAR: , [ ] } ! end of quoted field
|
||||||
{ CHAR: \s [ drop not-quoted-field ] }
|
[ 2drop skip-to-field-end ] ! end of quoted field + padding
|
||||||
{ CHAR: \t [ drop not-quoted-field ] }
|
|
||||||
[ drop ]
|
|
||||||
} case ;
|
} case ;
|
||||||
|
|
||||||
! trims whitespace from either end of string
|
|
||||||
: trim-whitespace ( str -- str )
|
|
||||||
[ blank? ] trim ; inline
|
|
||||||
|
|
||||||
: quoted-field ( -- endchar )
|
: quoted-field ( -- endchar )
|
||||||
"\"" read-until ! "
|
"\"" read-until ! "
|
||||||
drop % maybe-escaped-quote ;
|
drop % maybe-escaped-quote ;
|
||||||
|
|
||||||
: field ( -- sep string )
|
: field ( -- sep string )
|
||||||
[ not-quoted-field ] "" make trim-whitespace ;
|
[ not-quoted-field ] "" make ; ! trim-whitespace
|
||||||
|
|
||||||
: (row) ( -- sep )
|
: (row) ( -- sep )
|
||||||
field ,
|
field ,
|
||||||
|
|
Loading…
Reference in New Issue