]> gitweb.factorcode.org Git - factor.git/commitdiff
csv: need to handle \r because windows lines ends with \r\n
authorBjörn Lindqvist <bjourne@gmail.com>
Thu, 25 Sep 2014 15:06:01 +0000 (17:06 +0200)
committerDoug Coleman <doug.coleman@gmail.com>
Thu, 25 Sep 2014 17:00:07 +0000 (10:00 -0700)
basis/csv/csv-tests.factor
basis/csv/csv.factor

index 844a77d5e73a4314f512a5fc1ea2b003eb6ed436..5beefe283c156bc884b21ec36e600a232a8e0e9a 100644 (file)
@@ -4,11 +4,11 @@ io.directories ;
 IN: csv.tests
 
 ! I like to name my unit tests
-: named-unit-test ( name output input -- ) 
+: named-unit-test ( name output input -- )
     unit-test drop ; inline
 
 "Fields are separated by commas"
-[ { { "1997" "Ford" "E350" } } ] 
+[ { { "1997" "Ford" "E350" } } ]
 [ "1997,Ford,E350" string>csv ] named-unit-test
 
 "ignores whitespace before and after elements. n.b.specifically prohibited by RFC 4180, which states, 'Spaces are considered part of a field and should not be ignored.'"
@@ -21,7 +21,7 @@ IN: csv.tests
 
 "double quotes mean escaped in quotes"
 [ { { "1997" "Ford" "E350" "Super \"luxurious\" truck" } } ]
-[ "1997,Ford,E350,\"Super \"\"luxurious\"\" truck\"" 
+[ "1997,Ford,E350,\"Super \"\"luxurious\"\" truck\""
     string>csv ] named-unit-test
 
 "Fields with embedded line breaks must be delimited by double-quote characters."
@@ -39,10 +39,10 @@ IN: csv.tests
 [ "\"1997\",\"Ford\",\"E350\"" string>csv ] named-unit-test
 
 "The first record in a csv file may contain column names in each of the fields."
-[ { { "Year" "Make" "Model" } 
+[ { { "Year" "Make" "Model" }
     { "1997" "Ford" "E350" }
     { "2000" "Mercury" "Cougar" } } ]
-[ "Year,Make,Model\n1997,Ford,E350\n2000,Mercury,Cougar" 
+[ "Year,Make,Model\n1997,Ford,E350\n2000,Mercury,Cougar"
     string>csv ] named-unit-test
 
 
@@ -102,3 +102,10 @@ IN: csv.tests
 { { { "as,d\"f" "asdf" } } } [ "\"as,\"d\"\"\"\"f,asdf" string>csv ] unit-test
 
 [ { } ] [ "" string>csv ] unit-test
+
+[
+    { { "Year" "Make" "Model" }
+      { "1997" "Ford" "E350" }
+    }
+]
+[ "Year,Make,\"Model\"\r\n1997,Ford,E350" string>csv ] unit-test
index 350ee0b02e91ad39289664c45f56ddc00ad75a77..008c0b2e386f8ffa58bbedc618f44948d42af2d5 100644 (file)
@@ -12,7 +12,7 @@ CHAR: , delimiter set-global
 <PRIVATE
 
 MEMO: field-delimiters ( delimiter -- field-seps quote-seps )
-    [ "\n" swap prefix ] [ "\"\n" swap prefix ] bi ; inline
+    [ "\r\n" swap prefix ] [ "\r\"\n" swap prefix ] bi ; inline
 
 DEFER: quoted-field,
 
@@ -21,7 +21,8 @@ DEFER: quoted-field,
     [ nip ] [
         {
             { CHAR: "    [ [ CHAR: " , ] when quoted-field, ] }
-            { CHAR: \n   [ ] } ! Error: newline inside string?
+            { CHAR: \n   [ ] } ! Error: cr inside string?
+            { CHAR: \r   [ ] } ! Error: lf inside string?
             [ [ , drop f maybe-escaped-quote ] when* ]
         } case
      ] if ; inline recursive
@@ -85,7 +86,7 @@ PRIVATE>
 <PRIVATE
 
 : needs-escaping? ( cell delimiter -- ? )
-    '[ dup "\n\"" member? [ drop t ] [ _ = ] if ] any? ; inline
+    '[ dup "\n\"\r" member? [ drop t ] [ _ = ] if ] any? ; inline
 
 : escape-quotes ( cell stream -- )
     CHAR: " over stream-write1 swap [