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

未關閉
#389 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
57/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
ruby
領域
backend, databases

研究方向

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.

由索引模型根據 Issue 內容生成。

描述

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).

主要語言
Ruby
星號
221
分支
142
平均合併
1 天 1 小時
30 天內合併 PR
2

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

ruby/csv 的其他 Issue

查看 ruby/csv 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。