From 5ab41c0315c613f4e19f24f239ebe30642d400ff Mon Sep 17 00:00:00 2001 From: ado Date: Sun, 16 Jul 2023 18:25:15 +0200 Subject: [PATCH 1/2] Fix bug where header reading corrupted the current line buffer --- include/ss/parser.hpp | 18 ++++++++++-------- test/test_parser.cpp | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/include/ss/parser.hpp b/include/ss/parser.hpp index 099ea33..fcb1b5a 100644 --- a/include/ss/parser.hpp +++ b/include/ss/parser.hpp @@ -44,7 +44,8 @@ public: if constexpr (ignore_header) { ignore_next(); } else { - header_ = reader_.get_next_row(); + // TODO read header after use_fields is called + header_ = reader_.get_header(); } } else { set_error_file_not_open(); @@ -769,14 +770,15 @@ private: return true; } - std::vector get_next_row() { - std::vector next_row; - next_line_converter_.split(next_line_buffer_, delim_); - auto& next_row_raw = next_line_converter_.splitter_.split_data_; - for (const auto& [begin, end] : next_row_raw) { - next_row.emplace_back(begin, end); + std::vector get_header() { + std::vector header; + std::string header_buffer = next_line_buffer_; + converter_.split(header_buffer.data(), delim_); + auto& header_row_raw = converter_.splitter_.split_data_; + for (const auto& [begin, end] : header_row_raw) { + header.emplace_back(begin, end); } - return next_row; + return header; } //////////////// diff --git a/test/test_parser.cpp b/test/test_parser.cpp index e4069d3..0005dac 100644 --- a/test/test_parser.cpp +++ b/test/test_parser.cpp @@ -689,6 +689,7 @@ std::string no_quote(const std::string& s) { } TEST_CASE("parser test csv on multiple lines with quotes") { + // TODO test with "_""_""_",... unique_file_name f; std::vector data = {{1, 2, "\"x\r\nx\nx\""}, {3, 4, "\"y\ny\r\ny\""}, From 29c471e33a14c2b162ff1a5d681cba85a044524c Mon Sep 17 00:00:00 2001 From: ado Date: Sun, 16 Jul 2023 20:26:09 +0200 Subject: [PATCH 2/2] Update valid method for throw_on_error --- include/ss/converter.hpp | 2 ++ include/ss/parser.hpp | 2 ++ include/ss/splitter.hpp | 2 ++ 3 files changed, 6 insertions(+) diff --git a/include/ss/converter.hpp b/include/ss/converter.hpp index b9c7c45..aadc797 100644 --- a/include/ss/converter.hpp +++ b/include/ss/converter.hpp @@ -169,6 +169,8 @@ public: bool valid() const { if constexpr (string_error) { return error_.empty(); + } else if constexpr (throw_on_error) { + return true; } else { return !error_; } diff --git a/include/ss/parser.hpp b/include/ss/parser.hpp index fcb1b5a..d084d94 100644 --- a/include/ss/parser.hpp +++ b/include/ss/parser.hpp @@ -63,6 +63,8 @@ public: bool valid() const { if constexpr (string_error) { return error_.empty(); + } else if constexpr (throw_on_error) { + return true; } else { return !error_; } diff --git a/include/ss/splitter.hpp b/include/ss/splitter.hpp index 87e1ac0..091e3c8 100644 --- a/include/ss/splitter.hpp +++ b/include/ss/splitter.hpp @@ -34,6 +34,8 @@ public: bool valid() const { if constexpr (string_error) { return error_.empty(); + } else if constexpr (throw_on_error) { + return true; } else { return !error_; }