mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-06-26 10:54:28 +02:00
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:
parent
95380bbe96
commit
35e5098b51
7 changed files with 125 additions and 29 deletions
|
@ -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;
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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&);
|
||||
|
|
63
src/util.cpp
63
src/util.cpp
|
@ -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;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main (int, char**)
|
||||
{
|
||||
UnitTest t (7);
|
||||
UnitTest t (7 + 6 + 6 + 6);
|
||||
|
||||
// std::string escape (const std::string& input, int c)
|
||||
t.is (escape ("", 'x'), "", "escape '','x' --> ''");
|
||||
|
@ -44,6 +44,59 @@ int main (int, char**)
|
|||
t.is (quoteIfNeeded ("foo"), "foo", "quoteIfNeeded 'foo' --> 'foo'");
|
||||
t.is (quoteIfNeeded ("f o o"), "\"f o o\"", "quoteIfNeeded 'f o o' --> '\"f o o\"'");
|
||||
|
||||
{
|
||||
std::set<std::string> unjoined;
|
||||
std::string joined;
|
||||
|
||||
joined = join("", unjoined);
|
||||
t.is(joined.length(), (size_t) 0, "join -> length 0");
|
||||
t.is(joined, "", "join -> ''");
|
||||
|
||||
unjoined = {"", "a", "bc", "def"};
|
||||
joined = join("", unjoined);
|
||||
t.is(joined.length(), (size_t) 6, "join '' 'a' 'bc' 'def' -> length 6");
|
||||
t.is(joined, "abcdef", "join '' 'a' 'bc' 'def' -> 'abcdef'");
|
||||
|
||||
joined = join("-", unjoined);
|
||||
t.is(joined.length(), (size_t) 9, "join '' - 'a' - 'bc' - 'def' -> length 9");
|
||||
t.is(joined, "-a-bc-def", "join '' - 'a' - 'bc' - 'def' -> '-a-bc-def'");
|
||||
}
|
||||
|
||||
{
|
||||
std::set <std::string> unjoined;
|
||||
std::string joined;
|
||||
|
||||
joined = joinQuotedIfNeeded ("", unjoined);
|
||||
t.is (joined.length (), (size_t) 0, "join -> length 0");
|
||||
t.is (joined, "", "join -> ''");
|
||||
|
||||
unjoined = {"", "a", "bc", "d e f"};
|
||||
joined = joinQuotedIfNeeded ("", unjoined);
|
||||
t.is (joined.length (), (size_t) 10, "join '' 'a' 'bc' 'd e f' -> length 6");
|
||||
t.is (joined, "abc\"d e f\"", "join '' 'a' 'bc' 'def' -> 'abc\"d e f\"'");
|
||||
|
||||
joined = joinQuotedIfNeeded ("-", unjoined);
|
||||
t.is (joined.length (), (size_t) 13, "join '' - 'a' - 'bc' - 'def' -> length 9");
|
||||
t.is (joined, "-a-bc-\"d e f\"", "join '' - 'a' - 'bc' - 'def' -> '-a-bc-\"d e f\"'");
|
||||
}
|
||||
|
||||
{
|
||||
std::vector <std::string> unjoined;
|
||||
std::string joined;
|
||||
|
||||
joined = joinQuotedIfNeeded ("", unjoined);
|
||||
t.is (joined.length (), (size_t) 0, "join -> length 0");
|
||||
t.is (joined, "", "join -> ''");
|
||||
|
||||
unjoined = {"", "a", "bc", "d e f"};
|
||||
joined = joinQuotedIfNeeded ("", unjoined);
|
||||
t.is (joined.length (), (size_t) 10, "join '' 'a' 'bc' 'def' -> length 6");
|
||||
t.is (joined, "abc\"d e f\"", "join '' 'a' 'bc' 'def' -> 'abc\"d e f\"'");
|
||||
|
||||
joined = joinQuotedIfNeeded ("-", unjoined);
|
||||
t.is (joined.length (), (size_t) 13, "join '' - 'a' - 'bc' - 'def' -> length 9");
|
||||
t.is (joined, "-a-bc-\"d e f\"", "join '' - 'a' - 'bc' - 'def' -> '-a-bc-\"d e f\"'");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue