title: Parsing CSV data using a finite state machine
body: <p>I want to read a file containing comma-separated values, so have written a finite state machine:</p>
  
  <pre><code>private IList&lt;string&gt; Split(string line)
  {
      List&lt;string&gt; values = new List&lt;string&gt;();
      string value = string.Empty;
      ParseState state = ParseState.Initial;
      foreach (char c in line)
      {
          switch (state)
          {
              case ParseState.Initial:
                  switch (c)
                  {
                      case COMMA:
                          values.Add(string.Empty);
                          break;
                      case QUOTE:
                          state = ParseState.Quote;
                          break;
                      default:
                          value += c;
                          state = ParseState.Data;
                          break;
                  }
                  break;
              case ParseState.Data:
                  switch (c)
                  {
                      case COMMA:
                          values.Add(value);
                          value = string.Empty;
                          state = ParseState.Initial;
                          break;
                      case QUOTE:
                          throw new InvalidDataException("Improper quotes");
                      default:
                          value += c;
                          break;
                  }
                  break;
              case ParseState.Quote:
                  switch (c)
                  {
                      case QUOTE:
                          state = ParseState.QuoteInQuote;
                          break;
                      default:
                          value += c;
                          break;
                  }
                  break;
              case ParseState.QuoteInQuote:
                  switch (c)
                  {
                      case COMMA:
                          values.Add(value);
                          value = string.Empty;
                          state = ParseState.Initial;
                          break;
                      case QUOTE:
                          value += c;
                          state = ParseState.Quote;
                          break;
                      default:
                          throw new InvalidDataException("Unpaired quotes");
                  }
                  break;
          }
      }
  
      switch (state)
      {
          case ParseState.Initial:
          case ParseState.Data:
          case ParseState.QuoteInQuote:
              values.Add(value);
              break;
          case ParseState.Quote:
              throw new InvalidDataException("Unclosed quotes");
      }
  
      return values;
  }
  </code></pre>
  
  <p>Yes, I know the advice about CSV parsers is "don't write your own", but </p>
  
  <ol>
  <li>I needed it quickly and</li>
  <li>our download policy at work would take several days to allow me to
  get open source off the 'net.</li>
  </ol>
  
  <p>Hey, at least I didn't start with <code>string.Split()</code> or, worse, try using a <code>Regex</code>!</p>
  
  <p>And yes, I know it could be improved by using a <code>StringBuilder</code>, and it's restrictive on quotes in the data, but </p>
  
  <ol>
  <li>performance is not an issue and</li>
  <li>this is only to generate well-defined test data in-house,</li>
  </ol>
  
  <p>so I don't care about those.</p>
  
  <p>What I <em>do</em> care about is the apparent trailing block at the end for mopping up all the data after the final comma, and the way that it's starting to look like some sort of an anti-pattern down there, which was exactly the sort of thing that "good" patterns such as a FSM were supposed to avoid.</p>
  
  <p>So my question is this: <strong>is this block at the end some sort of anti-pattern, and is it something that's going to come back to bite me in the future?</strong></p>