]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/csv/csv.factor
factor: trim using lists
[factor.git] / basis / csv / csv.factor
index 7eba910e1b123b8f65f9fc26cd126799dc8341bb..e615493a45ad4441dcd881fdcf0a629f333af00d 100644 (file)
@@ -1,8 +1,7 @@
 ! 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
@@ -17,10 +16,10 @@ MEMO: field-delimiters ( delimiter -- field-seps quote-seps )
 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* ]
@@ -35,19 +34,19 @@ DEFER: quoted-field,
 
 : ?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
@@ -61,7 +60,7 @@ DEFER: quoted-field,
 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 )
@@ -89,10 +88,10 @@ PRIVATE>
     '[ 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