Skip to content

Commit

Permalink
Merge pull request #155 from d99kris/integrate-trimquote-fix
Browse files Browse the repository at this point in the history
Integrate trimquote fix
  • Loading branch information
d99kris authored Jan 27, 2024
2 parents f032c52 + 9e1d3ec commit 2b95e6d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ if(RAPIDCSV_BUILD_TESTS)
add_unit_test(test093)
add_unit_test(test094)
add_unit_test(test095)
add_unit_test(test096)

# perf tests
add_perf_test(ptest001)
Expand Down
13 changes: 11 additions & 2 deletions src/rapidcsv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* rapidcsv.h
*
* URL: https://github.com/d99kris/rapidcsv
* Version: 8.80
* Version: 8.81
*
* Copyright (C) 2017-2023 Kristofer Berggren
* Copyright (C) 2017-2024 Kristofer Berggren
* All rights reserved.
*
* rapidcsv is distributed under the BSD 3-Clause license, see LICENSE for details.
Expand Down Expand Up @@ -1588,6 +1588,15 @@ namespace rapidcsv
{
quoted = !quoted;
}
else if (mSeparatorParams.mTrim)
{
// allow whitespace before first mQuoteChar
const auto firstQuote = std::find(cell.begin(), cell.end(), mSeparatorParams.mQuoteChar);
if (std::all_of(cell.begin(), firstQuote, [](int ch) { return isspace(ch); }))
{
quoted = !quoted;
}
}
cell += buffer[i];
}
else if (buffer[i] == mSeparatorParams.mSeparator)
Expand Down
39 changes: 39 additions & 0 deletions tests/test096.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// test096.cpp - read trimmed quoted cell values

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

int main()
{
int rv = 0;

std::string csv =
"-, A,B , C \n"
" \"1\",\"3,\" , \"9,\" , \"81,\"\n"
"\"2\" ,\"4\" , \"16\", \"256\" \n"
;

std::string path = unittest::TempPath();
unittest::WriteFile(path, csv);

try
{
rapidcsv::Document doc(path, rapidcsv::LabelParams(0, 0), rapidcsv::SeparatorParams(',', true));
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("A", "1"), "3,");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("B", "1"), "9,");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("C", "1"), "81,");

unittest::ExpectEqual(std::string, doc.GetCell<std::string>("A", "2"), "4");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("B", "2"), "16");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("C", "2"), "256");
}
catch (const std::exception& ex)
{
std::cout << ex.what() << std::endl;
rv = 1;
}

unittest::DeleteFile(path);

return rv;
}

0 comments on commit 2b95e6d

Please sign in to comment.