Skip to content

Commit

Permalink
stopped treating single quotes like quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Masak committed Aug 20, 2009
1 parent 3e71665 commit be4e202
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
3 changes: 0 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
Some new ideas await tuits:

* Similarly, make single quotes not quote things by default. Perhaps
not even a flag is needed in this case.

* Fields with embedded double-quote characters must be enclosed within
double-quote characters, and each of the embedded double-quote
characters must be represented by a pair of double-quote characters.
Expand Down
12 changes: 4 additions & 8 deletions lib/Text/CSV.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ grammar Text::CSV::Line {
regex TOP { ^ <value> ** ',' $ }
regex value {
| <pure_text>
| \s* \' <single_quote_contents> \' \s*
| \s* \" <double_quote_contents> \" \s*
| \s* \" <quoted_contents> \" \s*
}
regex single_quote_contents { <pure_text> ** [ <[",]> | \h ] }
regex double_quote_contents { <pure_text> ** [ <[',]> | \h ] }
regex pure_text { [<!before <['",]>> .]+ }
regex quoted_contents { <pure_text> ** [ <[,]> | \h ] }
regex pure_text { [<!before <[",]>> .]+ }
}

class Text::CSV {
sub extract_text($m, :$trim) {
my $text = ($m<single_quote_contents>
// $m<double_quote_contents>
// $m).Str;
my $text = ($m<quoted_contents> // $m).Str;
return $trim ?? $text.trim !! $text;
}

Expand Down
12 changes: 8 additions & 4 deletions t/03-quotes.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ sub ok_becomes($input, $output, $description = '') {

ok_becomes q[[[foo,bar,baz
'foo','bar','baz'
'foo','bar' , 'baz']]], [ [<foo bar baz>] xx 3 ], 'single quotes';
'foo','bar' , 'baz']]],
[ [<foo bar baz>],
["'foo'", "'bar'", "'baz'"],
["'foo'", "'bar' ", " 'baz'"] ],
'single quotes carry no special significance';

ok_becomes q[[[foo,bar,baz
"foo","bar","baz"
"foo","bar" , "baz"]]], [ [<foo bar baz>] xx 3 ], 'double quotes';

dies_ok { Text::CSV.read(q[[[foo,ba'r,ba'z]]]) },
'mid-string single quotes illegal';
lives_ok { Text::CSV.read(q[[[foo,ba'r,ba'z]]]) },
'mid-string single quotes legal';
dies_ok { Text::CSV.read(q[[[foo,ba"r,ba"z]]]) },
'mid-string double quotes illegal';

is +Text::CSV.read(q[[[foo,'bar,baz']]])[0], 2, 'can single-quote commas';
is +Text::CSV.read(q[[[foo,'bar,baz']]])[0], 3, 'cannot single-quote commas';
is +Text::CSV.read(q[[[foo,"bar,baz"]]])[0], 2, 'can double-quote commas';

done_testing;
Expand Down

0 comments on commit be4e202

Please sign in to comment.