Extract join function

- use join function for std::vector<std::string> from libshared
- add join function for std::set<std::string>
- add joinQuotedIfNeeded for std::set<std::string> and std::vector<std::string>
- add unit tests
- These functions could be replaced with templates and moved to libshared.
This commit is contained in:
Thomas Lauf 2018-01-05 14:25:57 +01:00
parent 95380bbe96
commit 35e5098b51
7 changed files with 125 additions and 29 deletions

View file

@ -84,14 +84,7 @@ int CmdReport (
rules.set ("temp.report.start", filter.range.start.toEpoch () > 0 ? filter.range.start.toISO () : "");
rules.set ("temp.report.end", filter.range.end.toEpoch () > 0 ? filter.range.end.toISO () : "");
std::string combinedTags;
for (auto& tag : filter.tags ())
{
if (combinedTags != "")
combinedTags += ",";
combinedTags += quoteIfNeeded (tag);
}
rules.set ("temp.report.tags", combinedTags);
rules.set ("temp.report.tags", joinQuotedIfNeeded (",", filter.tags ()));
rules.set ("temp.version", VERSION);
std::stringstream header;

View file

@ -105,13 +105,7 @@ int CmdSummary (
if (track.range.is_open () && day <= Datetime ())
today.end = Datetime ();
std::string tags = "";
for (auto& tag : track.tags ())
{
if (tags != "")
tags += ", ";
tags += tag;
}
std::string tags = join(", ", track.tags());
if (ids)
table.set (row, 3, format ("@{1}", track.id), colorID);
@ -156,11 +150,7 @@ int CmdSummary (
if (filter.tags ().size ())
{
std::vector <std::string> tags;
for (auto& tag : filter.tags ())
tags.push_back (quoteIfNeeded (tag));
std::cout << " tagged with " << join (", ", tags);
std::cout << " tagged with " << joinQuotedIfNeeded (", ", filter.tags ());
}
std::cout << ".\n";

View file

@ -95,10 +95,7 @@ int CmdTag (
// Feedback.
if (rules.getBoolean ("verbose"))
{
std::cout << "Added";
for (auto& tag : tags)
std::cout << ' ' << quoteIfNeeded (tag);
std::cout << " to @" << id << '\n';
std::cout << "Added " << joinQuotedIfNeeded (" ", tags) << " to @" << id << '\n';
}
}

View file

@ -94,10 +94,7 @@ int CmdUntag (
if (rules.getBoolean ("verbose"))
{
std::cout << "Removed";
for (auto& tag : tags)
std::cout << ' ' << quoteIfNeeded (tag);
std::cout << " from @" << id << '\n';
std::cout << "Removed " << joinQuotedIfNeeded (" ", tags) << " from @" << id << '\n';
}
}

View file

@ -90,6 +90,9 @@ void debug (const std::string&);
// utiŀ.cpp
std::string escape (const std::string&, int);
std::string quoteIfNeeded (const std::string&);
std::string join(const std::string& glue, const std::set <std::string>& array);
std::string joinQuotedIfNeeded(const std::string& glue, const std::set <std::string>& array);
std::string joinQuotedIfNeeded(const std::string& glue, const std::vector <std::string>& array);
// dom.cpp
bool domGet (Database&, const Rules&, const std::string&, std::string&);

View file

@ -76,3 +76,66 @@ std::string quoteIfNeeded (const std::string& input)
}
////////////////////////////////////////////////////////////////////////////////
std::string join(const std::string& glue, const std::set <std::string>& array)
{
if (array.empty ())
{
return "";
}
auto iterator = array.begin ();
std::string value = *iterator++;
while (iterator != array.end ())
{
value += glue + *iterator++;
}
return value;
}
////////////////////////////////////////////////////////////////////////////////
std::string joinQuotedIfNeeded(const std::string& glue, const std::set <std::string>& array)
{
if (array.empty ())
{
return "";
}
auto iterator = array.begin ();
std::string value = *iterator++;
while (iterator != array.end ())
{
value += glue + quoteIfNeeded(*iterator++);
}
return value;
}
////////////////////////////////////////////////////////////////////////////////
std::string joinQuotedIfNeeded(const std::string& glue, const std::vector <std::string>& array)
{
if (array.empty ())
{
return "";
}
auto iterator = array.begin ();
std::string value = *iterator++;
while (iterator != array.end ())
{
value += glue + quoteIfNeeded(*iterator++);
}
return value;
}
////////////////////////////////////////////////////////////////////////////////