 
 <PRIVATE
 
-MEMO: field-delimiters ( delimiter -- field-end quoted-field )
-    [ "\n" swap suffix ] [ "\"\n" swap suffix ] bi ; inline
+MEMO: field-delimiters ( delimiter -- field-seps quote-seps )
+    [ "\n" swap prefix ] [ "\"\n" swap prefix ] bi ; inline
 
-DEFER: quoted-field
+DEFER: quoted-field,
 
-: maybe-escaped-quote ( delimeter stream quoted? -- delimiter stream endchar/f )
+: maybe-escaped-quote ( delimeter stream quoted? -- delimiter stream sep/f )
     2over stream-read1 swap over =
     [ nip ] [
         {
-            { CHAR: "    [ [ CHAR: " , ] when quoted-field ] }
+            { CHAR: "    [ [ CHAR: " , ] when quoted-field, ] }
             { CHAR: \n   [ ] } ! Error: newline inside string?
             [ [ , drop f maybe-escaped-quote ] when* ]
         } case
      ] if ; inline recursive
 
-: quoted-field ( delimiter stream -- delimiter stream endchar/f )
+: quoted-field, ( delimiter stream -- delimiter stream sep/f )
     "\"" over stream-read-until drop % t maybe-escaped-quote ;
+
+: quoted-field ( delimiter stream -- sep/f field )
+    [ quoted-field, 2nip ] "" make ;
 
 : ?trim ( string -- string' )
     dup length [ drop "" ] [

    CHAR: " over stream-write1 swap [
        [ over stream-write1 ]
        [ dup CHAR: " = [ over stream-write1 ] [ drop ] if ] bi
    ] each CHAR: " swap stream-write1 ; inline

: escape-if-required ( cell delimiter stream -- )
    [ dupd needs-escaping? ] dip

! 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
