! Copyright (C) 2007, 2008 Phil Dawes, 2013 John Benediktsson
! See http://factorcode.org/license.txt for BSD license.
-USING: combinators fry io io.files io.streams.string kernel
-make math memoize namespaces sbufs sequences sequences.private
-unicode ;
+USING: combinators io io.files io.streams.string kernel make
+math namespaces sequences sequences.private unicode ;
IN: csv
SYMBOL: delimiter
DEFER: quoted-field,
: maybe-escaped-quote ( delimeter stream quoted? -- delimiter stream sep/f )
- 2over stream-read1 swap over =
+ 2over stream-read1 tuck =
[ nip ] [
{
- { CHAR: " [ [ CHAR: " , ] when quoted-field, ] }
+ { CHAR: \" [ [ CHAR: \" , ] when quoted-field, ] }
{ CHAR: \n [ ] } ! Error: cr inside string?
{ CHAR: \r [ ] } ! Error: lf inside string?
[ [ , drop f maybe-escaped-quote ] when* ]
: ?trim ( string -- string' )
dup length [ drop "" ] [
- over first-unsafe blank?
- [ drop t ] [ 1 - over nth-unsafe blank? ] if
- [ [ blank? ] trim ] when
+ over first-unsafe unicode:blank?
+ [ drop t ] [ 1 - over nth-unsafe unicode:blank? ] if
+ [ [ unicode:blank? ] trim ] when
] if-zero ; inline
: continue-field ( delimiter stream field-seps seq -- sep/f field )
swap rot stream-read-until [ "\"" glue ] dip
- swap ?trim [ drop ] 2dip ; inline
+ swap ?trim nipd ; inline
: field ( delimiter stream field-seps quote-seps -- sep/f field )
- pick stream-read-until dup CHAR: " = [
+ pick stream-read-until dup CHAR: \" = [
drop [ drop quoted-field ] [ continue-field ] if-empty
- ] [ [ 3drop ] 2dip swap ?trim ] if ;
+ ] [ 3nipd swap ?trim ] if ;
: (stream-read-row) ( delimiter stream field-end quoted-field -- sep/f fields )
[ [ dup '[ dup _ = ] ] keep ] 3dip
PRIVATE>
: stream-read-row ( stream -- row )
- delimiter get swap over field-delimiters
+ delimiter get tuck field-delimiters
(stream-read-row) nip ; inline
: read-row ( -- row )
'[ dup "\n\"\r" member? [ drop t ] [ _ = ] if ] any? ; inline
: escape-quotes ( cell stream -- )
- CHAR: " over stream-write1 swap [
+ CHAR: \" over stream-write1 swap [
[ over stream-write1 ]
- [ dup CHAR: " = [ over stream-write1 ] [ drop ] if ] bi
- ] each CHAR: " swap stream-write1 ;
+ [ dup CHAR: \" = [ over stream-write1 ] [ drop ] if ] bi
+ ] each CHAR: \" swap stream-write1 ;
: escape-if-required ( cell delimiter stream -- )
[ dupd needs-escaping? ] dip