Skip to content

docs: Add entry for backslash_quotes liberal parsing #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions doc/csv/options/parsing/liberal_parsing.rdoc
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
====== Option +liberal_parsing+

Specifies the boolean value that determines whether
Specifies the boolean or hash value that determines whether
CSV will attempt to parse input not conformant with RFC 4180,
such as double quotes in unquoted fields.

Default value:
CSV::DEFAULT_OPTIONS.fetch(:liberal_parsing) # => false

For examples in this section:
For the next two examples:
str = 'is,this "three, or four",fields'

Without +liberal_parsing+:
Expand All @@ -17,3 +17,22 @@ Without +liberal_parsing+:
With +liberal_parsing+:
ary = CSV.parse_line(str, liberal_parsing: true)
ary # => ["is", "this \"three", " or four\"", "fields"]

Use the +backslash_quote+ sub-option to parse values that use
a backslash to escape a double-quote character. This
causes the parser to treat <code>\"</code> as if it were
<code>""</code>.

For the next two examples:
str = 'Show,"Harry \"Handcuff\" Houdini, the one and only","Tampa Theater"'

With +liberal_parsing+, but without the +backslash_quote+ sub-option:
# Incorrect interpretation of backslash; incorrectly interprets the quoted comma as a field separator.
ary = CSV.parse_line(str, liberal_parsing: true)
ary # => ["Show", "\"Harry \\\"Handcuff\\\" Houdini", " the one and only\"", "Tampa Theater"]
puts ary[1] # => "Harry \"Handcuff\" Houdini

With +liberal_parsing+ and its +backslash_quote+ sub-option:
ary = CSV.parse_line(str, liberal_parsing: { backslash_quote: true })
ary # => ["Show", "Harry \"Handcuff\" Houdini, the one and only", "Tampa Theater"]
puts ary[1] # => Harry "Handcuff" Houdini, the one and only