Use the empty method to check for emptiness instead of comparing to an empty object

This commit is contained in:
Thomas Lauf 2018-05-10 20:55:03 +02:00
parent b5acfed98f
commit 2c5812f7a3
17 changed files with 51 additions and 50 deletions

View file

@ -327,7 +327,7 @@ bool CLI::canonicalize (
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string CLI::getBinary () const std::string CLI::getBinary () const
{ {
if (_args.size ()) if (! _args.empty ())
return _args[0].attribute ("raw"); return _args[0].attribute ("raw");
return ""; return "";
@ -366,7 +366,7 @@ std::string CLI::dump (const std::string& title) const
out << '\n'; out << '\n';
if (_args.size ()) if (! _args.empty ())
{ {
out << " _args\n"; out << " _args\n";
for (auto& a : _args) for (auto& a : _args)

View file

@ -61,14 +61,14 @@ std::vector <std::string> Database::files () const
// Walk backwards through the files until an interval is found. // Walk backwards through the files until an interval is found.
std::string Database::lastLine () std::string Database::lastLine ()
{ {
if (! _files.size ()) if (_files.empty ())
initializeDatafiles (); initializeDatafiles ();
std::vector <Datafile>::reverse_iterator ri; std::vector <Datafile>::reverse_iterator ri;
for (ri = _files.rbegin (); ri != _files.rend (); ri++) for (ri = _files.rbegin (); ri != _files.rend (); ri++)
{ {
auto line = ri->lastLine (); auto line = ri->lastLine ();
if (line != "") if (! line.empty ())
return line; return line;
} }
@ -78,7 +78,7 @@ std::string Database::lastLine ()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::vector <std::string> Database::allLines () std::vector <std::string> Database::allLines ()
{ {
if (! _files.size ()) if (_files.empty ())
initializeDatafiles (); initializeDatafiles ();
std::vector <std::string> all; std::vector <std::string> all;

View file

@ -44,7 +44,7 @@ void Interval::initialize (const std::string& line)
tokens.push_back (Lexer::dequote (token)); tokens.push_back (Lexer::dequote (token));
// Minimal requirement 'inc'. // Minimal requirement 'inc'.
if (tokens.size () && if (!tokens.empty () &&
tokens[0] == "inc") tokens[0] == "inc")
{ {
unsigned int offset = 0; unsigned int offset = 0;
@ -86,7 +86,7 @@ bool Interval::empty () const
{ {
return range.start.toEpoch () == 0 && return range.start.toEpoch () == 0 &&
range.end.toEpoch () == 0 && range.end.toEpoch () == 0 &&
_tags.size () == 0; _tags.empty ();
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -126,7 +126,7 @@ std::string Interval::serialize () const
if (range.end.toEpoch ()) if (range.end.toEpoch ())
out << " - " << range.end.toISO (); out << " - " << range.end.toISO ();
if (_tags.size ()) if (! _tags.empty ())
{ {
out << " #"; out << " #";
for (auto& tag : _tags) for (auto& tag : _tags)
@ -152,7 +152,7 @@ std::string Interval::json () const
out << "\"end\":\"" << range.end.toISO () << "\""; out << "\"end\":\"" << range.end.toISO () << "\"";
} }
if (_tags.size ()) if (! _tags.empty ())
{ {
std::string tags; std::string tags;
for (auto& tag : _tags) for (auto& tag : _tags)
@ -192,7 +192,7 @@ std::string Interval::dump () const
if (range.end.toEpoch ()) if (range.end.toEpoch ())
out << " - " << range.end.toISOLocalExtended (); out << " - " << range.end.toISOLocalExtended ();
if (_tags.size ()) if (! _tags.empty ())
{ {
out << " #"; out << " #";
for (auto& tag : _tags) for (auto& tag : _tags)

View file

@ -223,7 +223,7 @@ std::vector <std::string> Rules::all (const std::string& stem) const
{ {
std::vector <std::string> items; std::vector <std::string> items;
for (const auto& it : _settings) for (const auto& it : _settings)
if (stem == "" || it.first.find (stem) == 0) if (stem.empty () || it.first.find (stem) == 0)
items.push_back (it.first); items.push_back (it.first);
return items; return items;
@ -274,7 +274,7 @@ void Rules::parse (const std::string& input, int nest /* = 1 */)
auto indent = line.find_first_not_of (' '); auto indent = line.find_first_not_of (' ');
auto tokens = Lexer::tokenize (line); auto tokens = Lexer::tokenize (line);
if (tokens.size ()) if (! tokens.empty ())
{ {
if (inRule) if (inRule)
{ {
@ -302,7 +302,7 @@ void Rules::parse (const std::string& input, int nest /* = 1 */)
// //
if (firstWord == "define") if (firstWord == "define")
{ {
if (ruleDef != "") if (! ruleDef.empty ())
{ {
parseRule (ruleDef); parseRule (ruleDef);
ruleDef = ""; ruleDef = "";
@ -357,7 +357,7 @@ void Rules::parse (const std::string& input, int nest /* = 1 */)
} }
} }
if (ruleDef != "") if (! ruleDef.empty ())
parseRule (ruleDef); parseRule (ruleDef);
} }
@ -411,7 +411,7 @@ void Rules::parseRuleSettings (
// If indent decreased. // If indent decreased.
else if (indent < indents.back ()) else if (indent < indents.back ())
{ {
while (indents.size () && indent != indents.back ()) while (!indents.empty () && indent != indents.back ())
{ {
indents.pop_back (); indents.pop_back ();
hierarchy.pop_back (); hierarchy.pop_back ();
@ -423,7 +423,7 @@ void Rules::parseRuleSettings (
} }
// Descend. // Descend.
if (group != "") if (! group.empty ())
hierarchy.push_back (group); hierarchy.push_back (group);
// Settings. // Settings.

View file

@ -241,7 +241,7 @@ static void determineHourRange (
last_hour = 23; last_hour = 23;
// If there is no data, show the whole day. // If there is no data, show the whole day.
if (tracked.size ()) if (! tracked.empty ())
{ {
// Get the extreme time range for the filtered data. // Get the extreme time range for the filtered data.
first_hour = 23; first_hour = 23;
@ -562,7 +562,7 @@ static void renderInterval (
for (auto& tag : track.tags ()) for (auto& tag : track.tags ())
{ {
if (label != "") if (! label.empty ())
label += ' '; label += ' ';
label += tag; label += tag;

View file

@ -126,8 +126,7 @@ static bool setConfigVariable (Database& database, const Rules& rules, std::stri
value))) value)))
{ {
// Add blank line required by rules. // Add blank line required by rules.
if (lines.size () && if (lines.empty () || lines.back ().empty ())
lines.back () != "")
lines.push_back (""); lines.push_back ("");
// Add new line. // Add new line.
@ -150,8 +149,7 @@ static bool setConfigVariable (Database& database, const Rules& rules, std::stri
// new leaf/value properly. But that's non-trivial. // new leaf/value properly. But that's non-trivial.
// Add blank line required by rules. // Add blank line required by rules.
if (lines.size () && if (lines.empty () || lines.back ().empty ())
lines.back () != "")
lines.push_back (""); lines.push_back ("");
// Add new line. // Add new line.
@ -272,7 +270,7 @@ int CmdConfig (
// task config name value # set name to value // task config name value # set name to value
// task config name "" # set name to blank // task config name "" # set name to blank
// task config name # remove name // task config name # remove name
if (words.size ()) if (! words.empty ())
{ {
bool confirmation = rules.getBoolean ("confirmation"); bool confirmation = rules.getBoolean ("confirmation");
std::string name = words[0]; std::string name = words[0];
@ -290,7 +288,7 @@ int CmdConfig (
} }
} }
if (name != "") if (! name.empty ())
{ {
bool change = false; bool change = false;

View file

@ -197,7 +197,7 @@ int CmdDiagnostics (
<< " Location: " << describeFile (extDir) << '\n'; << " Location: " << describeFile (extDir) << '\n';
auto exts = extensions.all (); auto exts = extensions.all ();
if (exts.size ()) if (! exts.empty ())
{ {
unsigned int longest = 0; unsigned int longest = 0;
for (auto& e : exts) for (auto& e : exts)

View file

@ -67,7 +67,7 @@ int CmdHelpUsage (const Extensions& extensions)
<< " timew week [<interval>] [<tag> ...]\n" << " timew week [<interval>] [<tag> ...]\n"
<< '\n'; << '\n';
if (extensions.all ().size ()) if (!extensions.all ().empty ())
{ {
std::cout << "Extensions (extensions do not provide help):\n"; std::cout << "Extensions (extensions do not provide help):\n";
@ -120,7 +120,8 @@ int CmdHelp (
const Extensions& extensions) const Extensions& extensions)
{ {
auto words = cli.getWords (); auto words = cli.getWords ();
if (words.size ())
if (! words.empty ())
{ {
// Ruler 1 2 3 4 5 6 7 8 // Ruler 1 2 3 4 5 6 7 8
// 12345678901234567890123456789012345678901234567890123456789012345678901234567890 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890

View file

@ -47,7 +47,7 @@ static std::string findExtension (
std::vector <std::string> matches; std::vector <std::string> matches;
autoComplete (partial, options, matches); autoComplete (partial, options, matches);
if (matches.size () == 0) if (matches.empty ())
throw format ("The report '{1}' is not recognized.", partial); throw format ("The report '{1}' is not recognized.", partial);
if (matches.size () > 1) if (matches.size () > 1)
@ -75,7 +75,7 @@ int CmdReport (
if (arg.hasTag ("EXT")) if (arg.hasTag ("EXT"))
script = findExtension (extensions, arg.attribute ("canonical")); script = findExtension (extensions, arg.attribute ("canonical"));
if (script == "") if (script.empty ())
throw std::string ("Specify which report to run."); throw std::string ("Specify which report to run.");
// Compose Header info. // Compose Header info.

View file

@ -84,7 +84,7 @@ int CmdStop (
// If tags are specified, but are not a full set of tags, remove them // If tags are specified, but are not a full set of tags, remove them
// before closing the interval. // before closing the interval.
if (filter.tags ().size () && if (! filter.tags ().empty () &&
setIntersect (filter.tags (), latest.tags ()).size () != latest.tags ().size ()) setIntersect (filter.tags (), latest.tags ()).size () != latest.tags ().size ())
{ {
for (auto& tag : filter.tags ()) for (auto& tag : filter.tags ())
@ -95,7 +95,7 @@ int CmdStop (
} }
// Open a new interval with remaining tags, if any. // Open a new interval with remaining tags, if any.
if (filter.tags ().size () && if (! filter.tags ().empty () &&
modified.tags ().size () != latest.tags ().size ()) modified.tags ().size () != latest.tags ().size ())
{ {
modified.range.start = modified.range.end; modified.range.start = modified.range.end;

View file

@ -65,7 +65,7 @@ int CmdSummary (
std::cout << " - " << filter.range.end.toISOLocalExtended (); std::cout << " - " << filter.range.end.toISOLocalExtended ();
} }
if (filter.tags ().size ()) if (! filter.tags ().empty ())
{ {
std::cout << " tagged with " << joinQuotedIfNeeded (", ", filter.tags ()); std::cout << " tagged with " << joinQuotedIfNeeded (", ", filter.tags ());
} }

View file

@ -48,7 +48,7 @@ int CmdTags (
tags.insert (tag); tags.insert (tag);
// Shows all tags. // Shows all tags.
if (tags.size ()) if (! tags.empty ())
{ {
Table t; Table t;
t.width (1024); t.width (1024);

View file

@ -81,16 +81,16 @@ Interval getFilter (const CLI& cli)
} }
else if (arg._lextype == Lexer::Type::date) else if (arg._lextype == Lexer::Type::date)
{ {
if (start == "") if (start.empty ())
start = raw; start = raw;
else if (end == "") else if (end.empty ())
end = raw; end = raw;
args.push_back ("<date>"); args.push_back ("<date>");
} }
else if (arg._lextype == Lexer::Type::duration) else if (arg._lextype == Lexer::Type::duration)
{ {
if (duration == "") if (duration.empty ())
duration = raw; duration = raw;
args.push_back ("<duration>"); args.push_back ("<duration>");
@ -208,7 +208,7 @@ Interval getFilter (const CLI& cli)
} }
// Unrecognized date range construct. // Unrecognized date range construct.
else if (args.size ()) else if (! args.empty ())
{ {
throw std::string ("Unrecognized date range: '") + join (" ", args) + "'."; throw std::string ("Unrecognized date range: '") + join (" ", args) + "'.";
} }
@ -291,9 +291,9 @@ std::vector <Range> getAllExclusions (
// daysOff are combined with existing holidays. // daysOff are combined with existing holidays.
results = addRanges (range, results, daysOff); results = addRanges (range, results, daysOff);
if (daysOn.size ()) if (! daysOn.empty ())
debug (format ("Found {1} additional working days", daysOn.size ())); debug (format ("Found {1} additional working days", daysOn.size ()));
if (daysOff.size ()) if (! daysOff.empty ())
debug (format ("Found {1} additional non-working days", daysOff.size ())); debug (format ("Found {1} additional non-working days", daysOff.size ()));
// daysOn are subtracted from the existing holidays. // daysOn are subtracted from the existing holidays.
@ -606,7 +606,7 @@ std::vector <Interval> getTracked (
if (outer.total ()) if (outer.total ())
filter.range = outer; filter.range = outer;
if (inclusions.size() > 0) { if (! inclusions.empty ()) {
auto latest = inclusions.back(); auto latest = inclusions.back();
if (latest.range.is_open()) {; if (latest.range.is_open()) {;
filter.range.end = 0; filter.range.end = 0;
@ -616,7 +616,7 @@ std::vector <Interval> getTracked (
std::vector <Interval> intervals = inclusions; std::vector <Interval> intervals = inclusions;
if (intervals.size () > 0) if (! intervals.empty ())
{ {
auto latest = inclusions.back (); auto latest = inclusions.back ();
@ -682,7 +682,7 @@ Interval getLatestInterval (Database& database)
} }
auto lastLine = database.lastLine (); auto lastLine = database.lastLine ();
if (lastLine != "") if (! lastLine.empty ())
i.initialize (lastLine); i.initialize (lastLine);
return i; return i;

View file

@ -47,7 +47,7 @@ Color intervalColor (
std::string first_tag; std::string first_tag;
for (auto& tag : interval.tags ()) for (auto& tag : interval.tags ())
{ {
if (first_tag == "") if (first_tag.empty ())
first_tag = tag; first_tag = tag;
std::string name = std::string ("tags.") + tag + ".color"; std::string name = std::string ("tags.") + tag + ".color";
@ -58,7 +58,7 @@ Color intervalColor (
if (c.nontrivial ()) if (c.nontrivial ())
return c; return c;
if (interval.tags ().size ()) if (! interval.tags ().empty ())
return tag_colors[first_tag]; return tag_colors[first_tag];
return c; return c;
@ -106,7 +106,7 @@ std::string intervalSummarize (
std::string tags; std::string tags;
for (auto& tag : interval.tags ()) for (auto& tag : interval.tags ())
{ {
if (tags != "") if (! tags.empty ())
tags += " "; tags += " ";
tags += tagColor (rules, tag).colorize (quoteIfNeeded (tag)); tags += tagColor (rules, tag).colorize (quoteIfNeeded (tag));
@ -345,7 +345,8 @@ Palette createPalette (const Rules& rules)
{ {
Palette p; Palette p;
auto colors = rules.all ("theme.palette.color"); auto colors = rules.all ("theme.palette.color");
if (colors.size ())
if (! colors.empty ())
{ {
p.clear (); p.clear ();
for (auto& c : colors) for (auto& c : colors)

View file

@ -249,7 +249,8 @@ int dispatchCommand (
// Dispatch to the right command function. // Dispatch to the right command function.
std::string command = cli.getCommand (); std::string command = cli.getCommand ();
if (command != "")
if (! command.empty ())
{ {
// These signatures are expected to be all different, therefore no // These signatures are expected to be all different, therefore no
// command to fn mapping. // command to fn mapping.
@ -291,7 +292,7 @@ int dispatchCommand (
{ {
auto words = cli.getWords (); auto words = cli.getWords ();
if (words.size () > 0) if (! words.empty ())
{ {
throw format ("'{1}' is not a timew command. See 'timew help'.", words[0]); throw format ("'{1}' is not a timew command. See 'timew help'.", words[0]);
} }

View file

@ -296,7 +296,7 @@ int main (int, char**)
// no results. // no results.
exclusions = {{Datetime ("20151201T000000"), Datetime ("20160201T000000")}}; exclusions = {{Datetime ("20151201T000000"), Datetime ("20160201T000000")}};
subtracted = subtractRanges ({limit}, exclusions); subtracted = subtractRanges ({limit}, exclusions);
t.ok (subtracted.size () == 0, "subtractRanges: all_day - 2 overlapping months = 0 ranges"); t.ok (subtracted.empty (), "subtractRanges: all_day - 2 overlapping months = 0 ranges");
return 0; return 0;
} }

View file

@ -54,7 +54,7 @@ int main (int, char**)
// std::set <std::string> tags () const; // std::set <std::string> tags () const;
// void tag (const std::string&); // void tag (const std::string&);
Interval i2; Interval i2;
t.ok (i2.tags () == std::set <std::string> {}, "Interval(tag=) -> {}"); t.ok (i2.tags ().empty (), "Interval(tag=) -> {}");
i2.tag ("foo"); i2.tag ("foo");
t.ok (i2.tags () == std::set <std::string> {"foo"}, "Interval(tag=foo) -> {foo}"); t.ok (i2.tags () == std::set <std::string> {"foo"}, "Interval(tag=foo) -> {foo}");
i2.tag ("foo"); // Duplicate i2.tag ("foo"); // Duplicate