diff --git a/parser.hpp b/parser.hpp index be95484..8ae0350 100644 --- a/parser.hpp +++ b/parser.hpp @@ -133,9 +133,11 @@ class CsvParser { // If we're out of tokens to read return whatever's left in the // field and row buffers. If there's nothing left, return null. if (maybe_token == nullptr) { - m_state = State::EMPTY; - return !m_fieldbuf.empty() ? Field(std::move(m_fieldbuf)) - : Field(FieldType::CSV_END); + if(m_state == State::END_OF_ROW || m_scanposition == 0) { + return Field(FieldType::CSV_END); + } + m_state = State::END_OF_ROW; + return Field(std::move(m_fieldbuf)); } // Parsing the CSV is done using a finite state machine diff --git a/test/data/empty.csv b/test/data/empty.csv index 7fc5cfa..1b43064 100644 --- a/test/data/empty.csv +++ b/test/data/empty.csv @@ -1,3 +1,3 @@ a,b,c 1,"","" -2,3,4 \ No newline at end of file +2,3,"" \ No newline at end of file diff --git a/test/data/emptyUnquoted.csv b/test/data/emptyUnquoted.csv index e32b207..efbb534 100644 --- a/test/data/emptyUnquoted.csv +++ b/test/data/emptyUnquoted.csv @@ -1,3 +1,3 @@ a,b,c 1,, -2,3,4 \ No newline at end of file +2,3, \ No newline at end of file diff --git a/test/parser_test.cpp b/test/parser_test.cpp index 8292b8b..20eacc4 100644 --- a/test/parser_test.cpp +++ b/test/parser_test.cpp @@ -23,14 +23,14 @@ TEST(CsvParserTest, CommaInQuotes) { TEST(CsvParserTest, Empty) { std::ifstream f(TEST_DATA_DIR "/empty.csv"); CsvParser parser(f); - CSV expected = {{"a", "b", "c"}, {"1", "", ""}, {"2", "3", "4"}}; + CSV expected = {{"a", "b", "c"}, {"1", "", ""}, {"2", "3", ""}}; EXPECT_EQ(read_all(parser), expected); } TEST(CsvParserTest, EmptyUnquoted) { std::ifstream f(TEST_DATA_DIR "/emptyUnquoted.csv"); CsvParser parser(f); - CSV expected = {{"a", "b", "c"}, {"1", "", ""}, {"2", "3", "4"}}; + CSV expected = {{"a", "b", "c"}, {"1", "", ""}, {"2", "3", ""}}; EXPECT_EQ(read_all(parser), expected); }