diff --git a/basis/csv/csv-docs.factor b/basis/csv/csv-docs.factor index 1f05ab639b..1796a029de 100644 --- a/basis/csv/csv-docs.factor +++ b/basis/csv/csv-docs.factor @@ -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:" diff --git a/basis/csv/csv.factor b/basis/csv/csv.factor index 23416d6912..1aeb2e1d19 100644 --- a/basis/csv/csv.factor +++ b/basis/csv/csv.factor @@ -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 ) + csv ; + : file>csv ( path encoding -- csv ) csv ; @@ -96,8 +100,18 @@ PRIVATE> : write-row ( row -- ) [ delimiter get write1 ] [ escape-if-required write ] interleave nl ; inline - -: write-csv ( rows stream -- ) - [ [ write-row ] each ] with-output-stream ; + + +: write-csv ( rows stream -- ) + [ (write-csv) ] with-output-stream ; + +: csv>string ( csv -- string ) + [ (write-csv) ] with-string-writer ; + : csv>file ( rows path encoding -- ) write-csv ;