rust 54 lines · 9 steps

Parsing a CSV line with a state machine

A four-state finite state machine walks a line character by character to split fields while respecting quotes and escaped quotes.

Explained by highlit
1#[derive(Debug, PartialEq)]
2enum State {
3 FieldStart,
4 InUnquoted,
5 InQuoted,
6 QuoteInQuoted,
7}
8 
9pub fn parse_csv_line(line: &str) -> Vec<String> {
10 let mut fields = Vec::new();
11 let mut current = String::new();
12 let mut state = State::FieldStart;
13 
14 for ch in line.chars() {
15 match state {
16 State::FieldStart => match ch {
17 '"' => state = State::InQuoted,
18 ',' => fields.push(std::mem::take(&mut current)),
19 _ => {
20 current.push(ch);
21 state = State::InUnquoted;
22 }
23 },
24 State::InUnquoted => match ch {
25 ',' => {
26 fields.push(std::mem::take(&mut current));
27 state = State::FieldStart;
28 }
29 _ => current.push(ch),
30 },
31 State::InQuoted => match ch {
32 '"' => state = State::QuoteInQuoted,
33 _ => current.push(ch),
34 },
35 State::QuoteInQuoted => match ch {
36 '"' => {
37 current.push('"');
38 state = State::InQuoted;
39 }
40 ',' => {
41 fields.push(std::mem::take(&mut current));
42 state = State::FieldStart;
43 }
44 _ => {
45 current.push(ch);
46 state = State::InUnquoted;
47 }
48 },
49 }
50 }
51 
52 fields.push(current);
53 fields
54}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Modeling parsing as explicit states makes tricky edge cases like quoted commas and escaped quotes tractable.
  2. 2std::mem::take swaps a value out and leaves a default behind, avoiding an extra allocation when flushing a buffer.
  3. 3A trailing flush after the loop handles the final field that has no delimiter to trigger it.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Parsing a CSV line with a state machine — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code