Skip to content

Commit

Permalink
Allow for escaping the delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
leaty committed May 31, 2021
1 parent 40ea4c4 commit 91998f6
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions csv-core/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,15 @@ enum NfaState {
InQuotedField = 3,
InEscapedQuote = 4,
InDoubleEscapedQuote = 5,
InComment = 6,
InEscapeSequence = 6,
InComment = 7,
// All states below are "final field" states.
// Namely, they indicate that a field has been parsed.
EndFieldDelim = 7,
EndFieldDelim = 8,
// All states below are "final record" states.
// Namely, they indicate that a record has been parsed.
EndRecord = 8,
CRLF = 9,
EndRecord = 9,
CRLF = 10,
}

/// A list of NFA states that have an explicit representation in the DFA.
Expand Down Expand Up @@ -805,9 +806,9 @@ impl Reader {
self.dfa.classes.add(self.delimiter);
if self.quoting {
self.dfa.classes.add(self.quote);
if let Some(escape) = self.escape {
self.dfa.classes.add(escape);
}
}
if let Some(escape) = self.escape {
self.dfa.classes.add(escape);
}
if let Some(comment) = self.comment {
self.dfa.classes.add(comment);
Expand Down Expand Up @@ -970,7 +971,7 @@ impl Reader {
match state {
End | StartRecord | EndRecord | InComment | CRLF => End,
StartField | EndFieldDelim | EndFieldTerm | InField
| InQuotedField | InEscapedQuote | InDoubleEscapedQuote
| InQuotedField | InEscapedQuote | InDoubleEscapedQuote | InEscapeSequence
| InRecordTerm => EndRecord,
}
}
Expand Down Expand Up @@ -1018,6 +1019,8 @@ impl Reader {
(EndFieldDelim, NfaInputAction::Discard)
} else if self.term.equals(c) {
(EndFieldTerm, NfaInputAction::Epsilon)
} else if !self.quoting && self.escape == Some(c) {
(InEscapeSequence, NfaInputAction::Discard)
} else {
(InField, NfaInputAction::CopyToOutput)
}
Expand All @@ -1043,6 +1046,7 @@ impl Reader {
(InField, NfaInputAction::CopyToOutput)
}
}
InEscapeSequence => (InField, NfaInputAction::CopyToOutput),
InComment => {
if b'\n' == c {
(StartRecord, NfaInputAction::Discard)
Expand Down

0 comments on commit 91998f6

Please sign in to comment.