From ade1e985bdf95ed209e8592a4c61689231e0f4ed Mon Sep 17 00:00:00 2001 From: Vincent La Date: Sun, 26 May 2024 14:18:04 -0700 Subject: [PATCH] CSV Parser 2.2.3 Fix off-by-one issue in tests and update README --- README.md | 1 + include/csv.hpp | 2 +- single_include/csv.hpp | 12 ++++++------ single_include_test/csv.hpp | 12 ++++++------ tests/test_round_trip.cpp | 4 ++-- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index e7500e4..63bc8a9 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ However, in reality we know that RFC 4180 is just a suggestion, and there's many * Automatic delimiter guessing * Ability to ignore comments in leading rows and elsewhere * Ability to handle rows of different lengths + * Ability to handle arbitrary line endings (as long as they are some combination of carriage return and newline) By default, rows of variable length are silently ignored, although you may elect to keep them or throw an error. diff --git a/include/csv.hpp b/include/csv.hpp index 5bcee1b..e39b3e3 100644 --- a/include/csv.hpp +++ b/include/csv.hpp @@ -1,5 +1,5 @@ /* -CSV for C++, version 2.2.2 +CSV for C++, version 2.2.3 https://github.com/vincentlaucsb/csv-parser MIT License diff --git a/single_include/csv.hpp b/single_include/csv.hpp index 42ed9ef..96fe099 100644 --- a/single_include/csv.hpp +++ b/single_include/csv.hpp @@ -1,6 +1,6 @@ #pragma once /* -CSV for C++, version 2.2.2 +CSV for C++, version 2.2.3 https://github.com/vincentlaucsb/csv-parser MIT License @@ -6627,7 +6627,7 @@ namespace csv { return std::to_string(value); #else // TODO: Figure out why the below code doesn't work on clang - std::string result; + std::string result = ""; T integral_part; T fractional_part = std::abs(std::modf(value, &integral_part)); @@ -6637,8 +6637,7 @@ namespace csv { if (value < 0) result = "-"; if (integral_part == 0) { - - result = "0"; + result += "0"; } else { for (int n_digits = num_digits(integral_part); n_digits > 0; n_digits --) { @@ -7065,8 +7064,8 @@ namespace csv { case ParseFlags::NEWLINE: this->data_pos++; - // Catches CRLF (or LFLF) - if (this->data_pos < in.size() && parse_flag(in[this->data_pos]) == ParseFlags::NEWLINE) + // Catches CRLF (or LFLF, CRCRLF, or any other non-sensical combination of newlines) + while (this->data_pos < in.size() && parse_flag(in[this->data_pos]) == ParseFlags::NEWLINE) this->data_pos++; // End of record -> Write record @@ -7636,6 +7635,7 @@ namespace csv { if (this->records->empty()) return this->end(); } + this->_n_rows++; CSVReader::iterator ret(this, this->records->pop_front()); return ret; } diff --git a/single_include_test/csv.hpp b/single_include_test/csv.hpp index 42ed9ef..96fe099 100644 --- a/single_include_test/csv.hpp +++ b/single_include_test/csv.hpp @@ -1,6 +1,6 @@ #pragma once /* -CSV for C++, version 2.2.2 +CSV for C++, version 2.2.3 https://github.com/vincentlaucsb/csv-parser MIT License @@ -6627,7 +6627,7 @@ namespace csv { return std::to_string(value); #else // TODO: Figure out why the below code doesn't work on clang - std::string result; + std::string result = ""; T integral_part; T fractional_part = std::abs(std::modf(value, &integral_part)); @@ -6637,8 +6637,7 @@ namespace csv { if (value < 0) result = "-"; if (integral_part == 0) { - - result = "0"; + result += "0"; } else { for (int n_digits = num_digits(integral_part); n_digits > 0; n_digits --) { @@ -7065,8 +7064,8 @@ namespace csv { case ParseFlags::NEWLINE: this->data_pos++; - // Catches CRLF (or LFLF) - if (this->data_pos < in.size() && parse_flag(in[this->data_pos]) == ParseFlags::NEWLINE) + // Catches CRLF (or LFLF, CRCRLF, or any other non-sensical combination of newlines) + while (this->data_pos < in.size() && parse_flag(in[this->data_pos]) == ParseFlags::NEWLINE) this->data_pos++; // End of record -> Write record @@ -7636,6 +7635,7 @@ namespace csv { if (this->records->empty()) return this->end(); } + this->_n_rows++; CSVReader::iterator ret(this, this->records->pop_front()); return ret; } diff --git a/tests/test_round_trip.cpp b/tests/test_round_trip.cpp index 057949f..1f9714b 100644 --- a/tests/test_round_trip.cpp +++ b/tests/test_round_trip.cpp @@ -18,7 +18,7 @@ TEST_CASE("Simple Buffered Integer Round Trip Test", "[test_roundtrip_int]") { const size_t n_rows = 1000000; - for (size_t i = 0; i <= n_rows; i++) { + for (size_t i = 0; i < n_rows; i++) { auto str = internals::to_string(i); writer << std::array({str, str, str, str, str}); } @@ -49,7 +49,7 @@ TEST_CASE("Simple Integer Round Trip Test", "[test_roundtrip_int]") { const size_t n_rows = 1000000; - for (size_t i = 0; i <= n_rows; i++) { + for (size_t i = 0; i < n_rows; i++) { auto str = internals::to_string(i); writer << std::array({ str, str, str, str, str }); }