Skip to content

Commit

Permalink
fixes #168 - handle trailing empty cell without linebreak
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Aug 31, 2024
1 parent f438d37 commit a98b85e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 11 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ if(RAPIDCSV_BUILD_TESTS)
add_unit_test(test095)
add_unit_test(test096)
add_unit_test(test097)
add_unit_test(test098)

# perf tests
add_perf_test(ptest001)
Expand Down
8 changes: 6 additions & 2 deletions make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# make.sh
#
# Copyright (C) 2020-2021 Kristofer Berggren
# Copyright (C) 2020-2024 Kristofer Berggren
# All rights reserved.
#
# See LICENSE for redistribution information.
Expand Down Expand Up @@ -168,7 +168,11 @@ if [[ "${INSTALL}" == "1" ]]; then
if [ "${OS}" == "Linux" ]; then
cd build-release && sudo make install && cd .. || exiterr "install failed (linux), exiting."
elif [ "${OS}" == "Darwin" ]; then
cd build-release && make install && cd .. || exiterr "install failed (mac), exiting."
GHSUDO=""
if [[ "${GITHUB_ACTIONS}" == "true" ]]; then
GHSUDO="sudo"
fi
cd build-release && ${GHSUDO} make install && cd .. || exiterr "install failed (mac), exiting."
else
exiterr "install failed (unsupported os ${OS}), exiting."
fi
Expand Down
19 changes: 10 additions & 9 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.83
* Version: 8.84
*
* Copyright (C) 2017-2024 Kristofer Berggren
* All rights reserved.
Expand Down Expand Up @@ -1351,7 +1351,7 @@ namespace rapidcsv

SetCell<T>(static_cast<size_t>(columnIdx), pRowIdx, pCell);
}

/**
* @brief Get column name
* @param pColumnIdx zero-based column index.
Expand Down Expand Up @@ -1663,16 +1663,15 @@ namespace rapidcsv
p_FileLength -= readLength;
}

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

// Handle last line without linebreak
if (!row.empty())
else
{
row.push_back(Unquote(Trim(cell)));

if (mLineReaderParams.mSkipCommentLines && !row.at(0).empty() &&
(row.at(0)[0] == mLineReaderParams.mCommentPrefix))
{
Expand All @@ -1683,7 +1682,9 @@ namespace rapidcsv
mData.push_back(row);
}

cell.clear();
row.clear();
quoted = false;
}

// Assume CR/LF if at least half the linebreaks have CR
Expand Down
57 changes: 57 additions & 0 deletions tests/test098.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// test098.cpp - empty cells

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

int main()
{
int rv = 0;

try
{
// empty cells without trailing linebreak on last line
{
std::string csv =
",\n"
",\n"
","
;

std::istringstream sstream(csv);
rapidcsv::Document doc(sstream, rapidcsv::LabelParams(-1, -1));
unittest::ExpectEqual(size_t, doc.GetRowCount(), 3);
unittest::ExpectEqual(size_t, doc.GetRow<std::string>(0).size(), 2);
unittest::ExpectEqual(size_t, doc.GetRow<std::string>(1).size(), 2);
unittest::ExpectEqual(size_t, doc.GetRow<std::string>(2).size(), 2);
unittest::ExpectEqual(std::string, doc.GetCell<std::string>(1 /* col */, 0 /* row */), "");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>(1 /* col */, 1 /* row */), "");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>(1 /* col */, 2 /* row */), "");
}

// empty cells with trailing linebreak on last line
{
std::string csv =
",\n"
",\n"
",\n"
;

std::istringstream sstream(csv);
rapidcsv::Document doc(sstream, rapidcsv::LabelParams(-1, -1));
unittest::ExpectEqual(size_t, doc.GetRowCount(), 3);
unittest::ExpectEqual(size_t, doc.GetRow<std::string>(0).size(), 2);
unittest::ExpectEqual(size_t, doc.GetRow<std::string>(1).size(), 2);
unittest::ExpectEqual(size_t, doc.GetRow<std::string>(2).size(), 2);
unittest::ExpectEqual(std::string, doc.GetCell<std::string>(0 /* col */, 0 /* row */), "");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>(0 /* col */, 1 /* row */), "");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>(0 /* col */, 2 /* row */), "");
}
}
catch (const std::exception& ex)
{
std::cout << ex.what() << std::endl;
rv = 1;
}

return rv;
}

0 comments on commit a98b85e

Please sign in to comment.