Skip to content

Commit

Permalink
fixes #156 - handle trailing comment line without linebreak
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Feb 10, 2024
1 parent 2b95e6d commit 7e87d8c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Project
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
cmake_minimum_required(VERSION 3.14...3.22 FATAL_ERROR)
project(rapidcsv VERSION 1.0 LANGUAGES CXX)
set (CMAKE_CXX_STANDARD 11)
if(MSVC)
Expand Down Expand Up @@ -180,6 +180,7 @@ if(RAPIDCSV_BUILD_TESTS)
add_unit_test(test094)
add_unit_test(test095)
add_unit_test(test096)
add_unit_test(test097)

# perf tests
add_perf_test(ptest001)
Expand Down
22 changes: 18 additions & 4 deletions src/rapidcsv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* rapidcsv.h
*
* URL: https://github.com/d99kris/rapidcsv
* Version: 8.81
* Version: 8.82
*
* Copyright (C) 2017-2024 Kristofer Berggren
* All rights reserved.
Expand Down Expand Up @@ -1663,12 +1663,26 @@ namespace rapidcsv
p_FileLength -= readLength;
}

// Handle last line without linebreak
if (!cell.empty() || !row.empty())
// Handle last cell without linebreak
if (!cell.empty())
{
row.push_back(Unquote(Trim(cell)));
cell.clear();
mData.push_back(row);
}

// Handle last line without linebreak
if (!row.empty())
{
if (mLineReaderParams.mSkipCommentLines && !row.at(0).empty() &&
(row.at(0)[0] == mLineReaderParams.mCommentPrefix))
{
// skip comment line
}
else
{
mData.push_back(row);
}

row.clear();
}

Expand Down
41 changes: 41 additions & 0 deletions tests/test097.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// test097.cpp - trailing comment line without linebreak

#include <rapidcsv.h>
#include "unittest.h"

int main()
{
int rv = 0;

std::string csv =
"A,B,C\n"
"1,3,9\n"
"2,4,16\n"
"# comment line 1\n"
"# comment line 2"
;

std::string path = unittest::TempPath();
unittest::WriteFile(path, csv);
try
{
rapidcsv::Document doc(path, rapidcsv::LabelParams(),
rapidcsv::SeparatorParams(),
rapidcsv::ConverterParams(),
rapidcsv::LineReaderParams(true /* pSkipCommentLines */));

unittest::ExpectEqual(size_t, doc.GetRowCount(), 2);
unittest::ExpectEqual(size_t, doc.GetColumn<std::string>("A").size(), 2);
unittest::ExpectEqual(int, doc.GetCell<int>(0, 0), 1);
unittest::ExpectEqual(int, doc.GetCell<int>(0, 1), 2);
}
catch (const std::exception& ex)
{
std::cout << ex.what() << std::endl;
rv = 1;
}

unittest::DeleteFile(path);

return rv;
}

0 comments on commit 7e87d8c

Please sign in to comment.