diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b6c73e..af3bb2b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/rapidcsv.h b/src/rapidcsv.h index cc603af..ee21404 100644 --- a/src/rapidcsv.h +++ b/src/rapidcsv.h @@ -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. @@ -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) diff --git a/tests/test096.cpp b/tests/test096.cpp new file mode 100644 index 0000000..c0ad399 --- /dev/null +++ b/tests/test096.cpp @@ -0,0 +1,39 @@ +// test096.cpp - read trimmed quoted cell values + +#include +#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("A", "1"), "3,"); + unittest::ExpectEqual(std::string, doc.GetCell("B", "1"), "9,"); + unittest::ExpectEqual(std::string, doc.GetCell("C", "1"), "81,"); + + unittest::ExpectEqual(std::string, doc.GetCell("A", "2"), "4"); + unittest::ExpectEqual(std::string, doc.GetCell("B", "2"), "16"); + unittest::ExpectEqual(std::string, doc.GetCell("C", "2"), "256"); + } + catch (const std::exception& ex) + { + std::cout << ex.what() << std::endl; + rv = 1; + } + + unittest::DeleteFile(path); + + return rv; +}