diff --git a/base/base_tests/string_utils_test.cpp b/base/base_tests/string_utils_test.cpp index bac640f4d3..84b48c1d85 100644 --- a/base/base_tests/string_utils_test.cpp +++ b/base/base_tests/string_utils_test.cpp @@ -730,19 +730,16 @@ UNIT_TEST(NormalizeDigits_UniString) TEST_EQUAL(nd("3456789"), "3456789", ()); } -UNIT_TEST(Split) +UNIT_TEST(CSV) { vector target; TEST(strings::ParseCSVRow(",Test\\,проверка,0,", target), ()); vector expected({"", "Test\\", "проверка", "0", ""}); TEST_EQUAL(target, expected, ()); - TEST(strings::ParseCSVRow("and there \"was none\"", target, ' '), ()); - vector expected2({"and", "there", "", "was none"}); + TEST(strings::ParseCSVRow("and there was none", target, ' ', 5), ()); + vector expected2({"and", "there", "", "was", "none"}); TEST_EQUAL(target, expected2, ()); TEST(!strings::ParseCSVRow("", target), ()); vector expected3; TEST_EQUAL(target, expected3, ()); - TEST(!strings::ParseCSVRow("\"this, a line.\"", target, ',', 2), (target)); - vector expected4({"this, a line."}); - TEST_EQUAL(target, expected4, ()); } diff --git a/base/string_utils.cpp b/base/string_utils.cpp index ab53723635..38c9c5ed33 100644 --- a/base/string_utils.cpp +++ b/base/string_utils.cpp @@ -332,47 +332,11 @@ bool ParseCSVRow(string const & s, vector & target, char const delimiter { target.clear(); using It = TokenizeIterator; - bool insideQuotes = false; - ostringstream quoted; for (It it(s, SimpleDelimiter(delimiter)); it; ++it) { string column = *it; - if (insideQuotes) - { - if (!column.empty() && column.back() == '"') - { - // Found the tail quote: remove it and add |quoted| to the vector. - insideQuotes = false; - column.pop_back(); - quoted << delimiter << column; - target.push_back(quoted.str()); - quoted.clear(); - } - else - quoted << delimiter << column; - } - else if (!column.empty() && column.front() == '"') - { - // Found the front quote: if there is the last one also, remove both and append column, - // otherwise push the column into a |quoted| buffer. - column.erase(0, 1); - if (column.back() == '"') - { - column.pop_back(); - strings::Trim(column); - target.push_back(column); - } - else - { - quoted << column; - insideQuotes = true; - } - } - else - { - strings::Trim(column); - target.push_back(column); - } + strings::Trim(column); + target.push_back(move(column)); } // Special case: if the string is empty, return an empty array instead of {""}. diff --git a/base/string_utils.hpp b/base/string_utils.hpp index f9883a544c..6b9e1f89c5 100644 --- a/base/string_utils.hpp +++ b/base/string_utils.hpp @@ -307,7 +307,7 @@ void Tokenize(string const & str, char const * delims, TFunctor && f) } /// Splits a string by the delimiter, keeps empty parts, on an empty string returns an empty vector. -/// Supports quoted columns, does not support newlines in columns and escaped quotes. +/// Does not support quoted columns, newlines in columns and escaped quotes. /// @return false if the line is empty or number of columns differs from |columns|. bool ParseCSVRow(string const & s, vector & target, char const delimiter = ',', size_t const columns = 0);