Support RFC4180 better (drop support for final newline)

This commit is contained in:
Marten/Qqwy 2022-07-16 23:47:57 +02:00
parent ac16db641a
commit c40d03fe22
No known key found for this signature in database
GPG key ID: FACEF83266BDAF72
2 changed files with 4 additions and 17 deletions

View file

@ -13,7 +13,7 @@ interface Parser.CSV
f64 f64
] ]
imports [ imports [
Parser.Core.{Parser, parse, buildPrimitiveParser, fail, const, alt, map, map2, apply, many, maybe, oneorMore, sepBy1, between, ignore, flatten}, Parser.Core.{Parser, parse, buildPrimitiveParser, fail, const, alt, map, map2, apply, many, maybe, oneorMore, sepBy1, between, ignore, flatten, sepBy},
Parser.Str.{RawStr, parseStrPartial, oneOf, codepoint, codepointSatisfies, scalar, digits, strFromRaw} Parser.Str.{RawStr, parseStrPartial, oneOf, codepoint, codepointSatisfies, scalar, digits, strFromRaw}
] ]
@ -21,10 +21,9 @@ interface Parser.CSV
## ##
## For simplicity's sake, the following things are not yet supported: ## For simplicity's sake, the following things are not yet supported:
## - CSV files with headings ## - CSV files with headings
## - A file not ending in a final CRLF ("\r\n").
## ##
## The following however *is* supported ## The following however *is* supported
## - A simple LF ("\n") instead of CRLF ("\r\n") to separate records (and at the end). ## - A simple LF ("\n") instead of CRLF ("\r\n") to separate records.
CSV : List CSVRecord CSV : List CSVRecord
CSVRecord : List CSVField CSVRecord : List CSVField
@ -150,19 +149,7 @@ parseStrToCSVRecord = \input ->
# The following are parsers to turn strings into CSV structures # The following are parsers to turn strings into CSV structures
file : Parser RawStr CSV file : Parser RawStr CSV
file = many recordNewline file = sepBy csvRecord endOfLine
# The following compiles 6x slower, but follows the RFC to the letter (allowing the final CRLF to be omitted)
# file : Parser RawStr CSV
# file = map2 (many recordNewline) (maybe csvRecord) \records, finalRecord ->
# when finalRecord is
# Err Nothing ->
# records
# Ok val ->
# List.append records val
recordNewline : Parser RawStr CSVRecord
recordNewline = map2 csvRecord endOfLine (\rec, _ -> rec)
csvRecord : Parser RawStr CSVRecord csvRecord : Parser RawStr CSVRecord
csvRecord = sepBy1 csvField comma csvRecord = sepBy1 csvField comma

View file

@ -3,7 +3,7 @@ app "main"
imports [Parser.Core.{Parser, map, apply}, Parser.Str.{RawStr}, Parser.CSV.{CSV, record, field, string, nat}] imports [Parser.Core.{Parser, map, apply}, Parser.Str.{RawStr}, Parser.CSV.{CSV, record, field, string, nat}]
provides [main] to pf provides [main] to pf
input = "Airplane!,1980,\"Robert Hays,Julie Hagerty\"\r\nCaddyshack,1980,\"Chevy Chase,Rodney Dangerfield,Ted Knight,Michael O'Keefe,Bill Murray\"\r\n" input = "Airplane!,1980,\"Robert Hays,Julie Hagerty\"\r\nCaddyshack,1980,\"Chevy Chase,Rodney Dangerfield,Ted Knight,Michael O'Keefe,Bill Murray\""
main = main =
when Parser.CSV.parseStr movieInfoParser input is when Parser.CSV.parseStr movieInfoParser input is
Ok movies -> Ok movies ->