From a1e13433bc2d8708f7e0f36b2b42109a7379e233 Mon Sep 17 00:00:00 2001 From: Vincent La Date: Sun, 19 May 2019 17:57:28 -0700 Subject: [PATCH] Fix potential bug with RawRowBuffer (#39) --- include/internal/row_buffer.cpp | 6 ++++ single_include/csv.hpp | 6 ++++ tests/test_csv_buffer.cpp | 58 +++++++++++++++++++++------------ 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/include/internal/row_buffer.cpp b/include/internal/row_buffer.cpp index 4f1c9bba..cd104f4c 100644 --- a/include/internal/row_buffer.cpp +++ b/include/internal/row_buffer.cpp @@ -52,11 +52,17 @@ namespace csv { // Save current row in progress auto new_buff = BufferPtr(new RawRowBuffer()); + // Save text new_buff->buffer = this->buffer.substr( this->current_end, // Position (this->buffer.size() - this->current_end) // Count ); + // Save split buffer in progress + for (size_t i = this->current_split_idx; i < this->split_buffer.size(); i++) { + new_buff->split_buffer.push_back(this->split_buffer[i]); + } + new_buff->col_names = this->col_names; // No need to remove unnecessary bits from this buffer diff --git a/single_include/csv.hpp b/single_include/csv.hpp index 4bc8c8d4..d68179b0 100644 --- a/single_include/csv.hpp +++ b/single_include/csv.hpp @@ -3817,11 +3817,17 @@ namespace csv { // Save current row in progress auto new_buff = BufferPtr(new RawRowBuffer()); + // Save text new_buff->buffer = this->buffer.substr( this->current_end, // Position (this->buffer.size() - this->current_end) // Count ); + // Save split buffer in progress + for (size_t i = this->current_split_idx; i < this->split_buffer.size(); i++) { + new_buff->split_buffer.push_back(this->split_buffer[i]); + } + new_buff->col_names = this->col_names; // No need to remove unnecessary bits from this buffer diff --git a/tests/test_csv_buffer.cpp b/tests/test_csv_buffer.cpp index 5055995d..cf55ada1 100644 --- a/tests/test_csv_buffer.cpp +++ b/tests/test_csv_buffer.cpp @@ -5,17 +5,17 @@ using namespace csv::internals; TEST_CASE("GiantStringBufferTest", "[test_giant_string_buffer]") { - RawRowBuffer buffer; + BufferPtr buffer = BufferPtr(new RawRowBuffer()); - buffer.buffer.append("1234"); - std::string first_row = std::string(buffer.get_row()); + buffer->buffer.append("1234"); + std::string first_row = std::string(buffer->get_row()); - buffer.buffer.append("5678"); - std::string second_row = std::string(buffer.get_row()); + buffer->buffer.append("5678"); + std::string second_row = std::string(buffer->get_row()); - buffer.reset(); - buffer.buffer.append("abcd"); - std::string third_row = std::string(buffer.get_row()); + buffer = buffer->reset(); + buffer->buffer.append("abcd"); + std::string third_row = std::string(buffer->get_row()); REQUIRE(first_row == "1234"); REQUIRE(second_row == "5678"); @@ -23,24 +23,40 @@ TEST_CASE("GiantStringBufferTest", "[test_giant_string_buffer]") { } TEST_CASE("GiantSplitBufferTest", "[test_giant_split_buffer]") { - RawRowBuffer buffer; - auto & splits = buffer.split_buffer; + BufferPtr buffer = BufferPtr(new RawRowBuffer()); + auto * splits = &(buffer->split_buffer); - splits.push_back(1); - splits.push_back(2); - splits.push_back(3); + splits->push_back(1); + splits->push_back(2); + splits->push_back(3); - auto pos = buffer.get_splits(); + auto pos = buffer->get_splits(); REQUIRE(pos.split_at(0) == 1); REQUIRE(pos.split_at(1) == 2); REQUIRE(pos.split_at(2) == 3); REQUIRE(pos.n_cols == 4); - splits.push_back(4); - splits.push_back(5); - - pos = buffer.get_splits(); - REQUIRE(pos.split_at(0) == 4); - REQUIRE(pos.split_at(1) == 5); - REQUIRE(pos.n_cols == 3); + SECTION("Two Splits Test") { + splits->push_back(4); + splits->push_back(5); + + pos = buffer->get_splits(); + REQUIRE(pos.split_at(0) == 4); + REQUIRE(pos.split_at(1) == 5); + REQUIRE(pos.n_cols == 3); + } + + SECTION("Reset In Middle Test") { + splits->push_back(1); + buffer = buffer->reset(); + splits = &(buffer->split_buffer); + splits->push_back(2); + splits->push_back(3); + + auto pos = buffer->get_splits(); + REQUIRE(pos.split_at(0) == 1); + REQUIRE(pos.split_at(1) == 2); + REQUIRE(pos.split_at(2) == 3); + REQUIRE(pos.n_cols == 4); + } } \ No newline at end of file