Skip to content

Commit

Permalink
csv-core: fix comment handling
Browse files Browse the repository at this point in the history
This commit fixes a bug in how comments were interpreted. In particular,
this causes the behavior to match the documentation, which is correct.
Comments should only be allowed at the beginning of a record, but the
parser permitted them to also appear at the beginning of a field. This
corrects the implementation by simply moving the transition from
StartField to StartRecord, and then adds a couple regression tests.

Fixes #137
  • Loading branch information
BurntSushi committed Dec 15, 2018
1 parent 3a4ed9a commit a5745ba
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions csv-core/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,8 @@ impl Reader {
StartRecord => {
if self.term.equals(c) {
(StartRecord, NfaInputAction::Discard)
} else if self.comment == Some(c) {
(InComment, NfaInputAction::Discard)
} else {
(StartField, NfaInputAction::Epsilon)
}
Expand All @@ -1004,8 +1006,6 @@ impl Reader {
(EndFieldDelim, NfaInputAction::Discard)
} else if self.term.equals(c) {
(EndFieldTerm, NfaInputAction::Epsilon)
} else if self.comment == Some(c) {
(InComment, NfaInputAction::Discard)
} else {
(InField, NfaInputAction::CopyToOutput)
}
Expand Down Expand Up @@ -1623,6 +1623,10 @@ mod tests {
"foo\n# hi\nbar\n",
csv![["foo"], ["# hi"], ["bar"]],
|b: &mut ReaderBuilder| { b.comment(Some(b'\n')); });
parses_to!(comment_4, "foo,b#ar,baz", csv![["foo", "b#ar", "baz"]],
|b: &mut ReaderBuilder| { b.comment(Some(b'#')); });
parses_to!(comment_5, "foo,#bar,baz", csv![["foo", "#bar", "baz"]],
|b: &mut ReaderBuilder| { b.comment(Some(b'#')); });

macro_rules! assert_read {
(
Expand Down

0 comments on commit a5745ba

Please sign in to comment.