[booking] Remove CSV in favour of a simple split

This commit is contained in:
Ilya Zverev 2016-06-27 18:40:52 +03:00
parent 291427d5de
commit 826807555c
3 changed files with 6 additions and 45 deletions

View file

@ -730,19 +730,16 @@ UNIT_TEST(NormalizeDigits_UniString)
TEST_EQUAL(nd(""), "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, ());
}

View file

@ -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 {""}.

View file

@ -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);