 : (csv) ( -- )
   row append-if-row-not-empty
   [ (csv) ] when ;
-
-: init-vars ( -- )
-  delimiter> [ CHAR: , >delimiter ] unless ; inline 
   
 : csv-row ( stream -- row )
-  init-vars
   [ row nip ] with-input-stream ;
 
 : csv ( stream -- rows )
-  init-vars
   [ [ (csv) ] { } make ] with-input-stream ;
 
 : with-delimiter ( char quot -- )
   delimiter swap with-variable ; inline
 
-
-    
 : needs-escaping? ( cell -- ? )
-  [ "\n\"" delimiter> suffix member? ] contains? ; inline ! "
+  [ [ "\n\"" member? ] [ delimiter get = ] bi or ] contains? ; inline ! "
 
 : escape-quotes ( cell -- cell' )
   [ [ dup , CHAR: " = [ CHAR: " , ] when ] each ] "" make ; inline


: (row) ( -- sep )
  field , 
  dup delimiter> = [ drop (row) ] when ;

: row ( -- eof? array[string] )
  [ (row) ] { } make ;

! Simple CSV Parser
! Phil Dawes phil@phildawes.net

USING: kernel sequences io namespaces combinators unicode.categories ;
IN: csv

SYMBOL: delimiter

CHAR: , delimiter set-global

: delimiter> delimiter get ; inline
    
DEFER: quoted-field ( -- endchar )
    
! trims whitespace from either end of string
: trim-whitespace ( str -- str )
