]> gitweb.factorcode.org Git - factor.git/commitdiff
Add csv>string and string>csv
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 26 Feb 2010 00:30:40 +0000 (18:30 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Sat, 27 Feb 2010 13:58:38 +0000 (07:58 -0600)
basis/csv/csv-docs.factor
basis/csv/csv.factor

index 1f05ab639bd8664b2296bb4497ebcb92ffb54fff..1796a029de7d407ebc18e5324b39fb8da4622afc 100644 (file)
@@ -21,6 +21,20 @@ HELP: csv>file
 }
 { $description "Writes a comma-separated-value structure to a file." } ;
 
+HELP: string>csv
+{ $values
+    { "string" string }
+    { "csv" "csv" }
+}
+{ $description "Parses a string into a sequence of comma-separated-value fields." } ;
+
+HELP: csv>string
+{ $values
+    { "rows" "a sequence of sequences of strings" }
+    { "string" string }
+}
+{ $description "Writes a comma-separated-value structure to a string." } ;
+
 HELP: csv-row
 { $values { "stream" "an input stream" }
           { "row" "an array of fields" } } 
@@ -42,6 +56,10 @@ ARTICLE: "csv" "Comma-separated-values parsing and writing"
 { $subsections file>csv }
 "Writing a csv file:"
 { $subsections csv>file }
+"Reading a string to csv:"
+{ $subsections string>csv }
+"Writing csv to a string:"
+{ $subsections csv>string }
 "Changing the delimiter from a comma:"
 { $subsections with-delimiter }
 "Reading from a stream:"
index 23416d6912aa6899efa3eff7f739fd3d599966d9..1aeb2e1d193ecc4488a504e03cda73d49b71f8c9 100644 (file)
@@ -1,7 +1,8 @@
 ! Copyright (C) 2007, 2008 Phil Dawes
 ! See http://factorcode.org/license.txt for BSD license.
 USING: kernel sequences io namespaces make combinators
-unicode.categories io.files combinators.short-circuit ;
+unicode.categories io.files combinators.short-circuit
+io.streams.string ;
 IN: csv
 
 SYMBOL: delimiter
@@ -65,6 +66,9 @@ PRIVATE>
     [ [ (csv) ] { } make ] with-input-stream
     dup last { "" } = [ but-last ] when ;
 
+: string>csv ( string -- csv )
+    <string-reader> csv ;
+
 : file>csv ( path encoding -- csv )
     <file-reader> csv ;
 
@@ -96,8 +100,18 @@ PRIVATE>
 : write-row ( row -- )
     [ delimiter get write1 ]
     [ escape-if-required write ] interleave nl ; inline
+
+<PRIVATE
+
+: (write-csv) ( rows -- )
+    [ write-row ] each ;
     
+PRIVATE>
+
 : write-csv ( rows stream -- )
-    [ [ write-row ] each ] with-output-stream ;
+    [ (write-csv) ] with-output-stream ;
 
+: csv>string ( csv -- string )
+    [ (write-csv) ] with-string-writer ;
+    
 : csv>file ( rows path encoding -- ) <file-writer> write-csv ;