diff options
author | Ilya Zverev <zverik@textual.ru> | 2016-06-27 18:40:52 +0300 |
---|---|---|
committer | Ilya Zverev <zverik@textual.ru> | 2016-06-28 15:19:13 +0300 |
commit | 826807555c93ba11ca7f193c0df34f80fb83a725 (patch) | |
tree | 7a207cee62d65246c1d551196e382736874e443a /base | |
parent | 291427d5de614af7a64f5d12af5b2e10a5a134fe (diff) |
[booking] Remove CSV in favour of a simple split
Diffstat (limited to 'base')
-rw-r--r-- | base/base_tests/string_utils_test.cpp | 9 | ||||
-rw-r--r-- | base/string_utils.cpp | 40 | ||||
-rw-r--r-- | base/string_utils.hpp | 2 |
3 files changed, 6 insertions, 45 deletions
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<string> target; TEST(strings::ParseCSVRow(",Test\\,проверка,0,", target), ()); vector<string> expected({"", "Test\\", "проверка", "0", ""}); TEST_EQUAL(target, expected, ()); - TEST(strings::ParseCSVRow("and there \"was none\"", target, ' '), ()); - vector<string> expected2({"and", "there", "", "was none"}); + TEST(strings::ParseCSVRow("and there was none", target, ' ', 5), ()); + vector<string> expected2({"and", "there", "", "was", "none"}); TEST_EQUAL(target, expected2, ()); TEST(!strings::ParseCSVRow("", target), ()); vector<string> expected3; TEST_EQUAL(target, expected3, ()); - TEST(!strings::ParseCSVRow("\"this, a line.\"", target, ',', 2), (target)); - vector<string> 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<string> & target, char const delimiter { target.clear(); using It = TokenizeIterator<SimpleDelimiter, string::const_iterator, true>; - 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<string> & target, char const delimiter = ',', size_t const columns = 0); |