CSV.parse can't read back what CSV.generate wrote when a quoted field contains \r

Offen
#389 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
57/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
ruby
Bereich
backend, databases

Rechercherichtung

Start by reading the row_sep documentation and tracing how row_sep: :auto detects separators when quoted fields contain \r; the report reproduces the issue with CSV.parse_line and CSV.parse. Check existing tests for automatic row separator detection and round-tripping generated CSV. Done means detection no longer misreads this generated input, or the documentation clearly explains the limitation.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

When a field contains a bare \r, CSV writes it quoted with the default "\n" row separator, but reading it back with the default row_sep: :auto either raises or silently merges rows:

require "csv"

CSV.parse_line(CSV.generate_line(["a\rb"]))
# => CSV::MalformedCSVError: Any value after quoted field isn't allowed in line 1.

csv = CSV.generate { |c| c << ["a\rb", "c"] << ["d", "e"] }
# => "\"a\rb\",c\nd,e\n"
CSV.parse(csv)
# => [["a\rb", "c\nd", "e\n"]]   # two rows become one, with no error

CSV.parse(csv, row_sep: "\n")
# => [["a\rb", "c"], ["d", "e"]]

Auto-detection picks the first \r/\n it finds, which here is inside the quoted field, so "\r" is used as the row separator. I see this is documented in the row_sep docs ("The sequence will be selected even if it occurs in a quoted field, assuming that you would have the same line endings there"). But in this case the data was produced by CSV itself with default options, and the second example silently returns wrong rows rather than raising.

Would it make sense for auto-detection to skip over quoted fields, at least when the sample contains a quote character? If you'd rather keep the current behavior, a sentence in the row_sep docs about round-tripping data that contains \r would also help.

Checked with csv 3.3.8 (current main) and 3.3.5 (Ruby 3.3).

Vorherrschende Sprache
Ruby
Sterne
221
Forks
142
Ø Merge
1 T. 1 Std.
Gemergte PRs (30 T.)
2

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus ruby/csv

Alle Issues in ruby/csv

Ähnliche Issues

Weitere Issues zu Ruby

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.