Skip to content

Commit

Permalink
fixes #161 - writing CSV with autoquote enabled should quote strings …
Browse files Browse the repository at this point in the history
…containing linebreak
  • Loading branch information
d99kris committed Mar 31, 2024
1 parent 7e87d8c commit f438d37
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 3 additions & 2 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.82
* Version: 8.83
*
* Copyright (C) 2017-2024 Kristofer Berggren
* All rights reserved.
Expand Down Expand Up @@ -1748,7 +1748,8 @@ namespace rapidcsv
{
if (mSeparatorParams.mAutoQuote &&
((itc->find(mSeparatorParams.mSeparator) != std::string::npos) ||
(itc->find(' ') != std::string::npos)))
(itc->find(' ') != std::string::npos) ||
(itc->find('\n') != std::string::npos)))
{
// escape quotes in string
std::string str = *itc;
Expand Down
7 changes: 5 additions & 2 deletions tests/test067.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ int main()
"\"a b\"\n"
"\"\"\"a b\"\"\"\n"
"\" \"\"a\"\" \"\n"
"\"a\nb\"\n"
;

std::string csvreadref =
Expand All @@ -22,6 +23,7 @@ int main()
"\"a b\"\n"
"\"\"\"a b\"\"\"\n"
"\" \"\"a\"\" \"\n"
"\"a\nb\"\n"
;

std::string path = unittest::TempPath();
Expand All @@ -33,20 +35,21 @@ int main()
{
rapidcsv::Document doc(path, rapidcsv::LabelParams(0 /* pColumnNameIdx */, -1 /* pRowNameIdx */),
rapidcsv::SeparatorParams(',', false /* pTrim */, rapidcsv::sPlatformHasCR /* pHasCR */,
false /* pQuotedLinebreaks */, true /* pAutoQuote */));
true /* pQuotedLinebreaks */, true /* pAutoQuote */));
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("col 1", 0), "");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("col 1", 1), " ");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("col 1", 2), "a b");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("col 1", 3), "\"a b\"");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("col 1", 4), " \"a\" ");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("col 1", 5), "a\nb");
}

// write
{
unittest::WriteFile(path, csvreadref);
rapidcsv::Document doc(path, rapidcsv::LabelParams(0 /* pColumnNameIdx */, -1 /* pRowNameIdx */),
rapidcsv::SeparatorParams(',', false /* pTrim */, rapidcsv::sPlatformHasCR /* pHasCR */,
false /* pQuotedLinebreaks */, true /* pAutoQuote */));
true /* pQuotedLinebreaks */, true /* pAutoQuote */));

doc.Save();
const std::string& csvread = unittest::ReadFile(path);
Expand Down

0 comments on commit f438d37

Please sign in to comment.