From bd3d58484a1c566ccbb4c8932f35c21f863cc14e Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 11 May 2015 17:33:20 -0400 Subject: [PATCH] C++11: Cleaned up column code wiht range-based for --- src/columns/ColDepends.cpp | 19 ++++++-------- src/columns/ColDescription.cpp | 45 ++++++++++++++-------------------- src/columns/ColProject.cpp | 5 ++-- src/columns/ColString.cpp | 10 +++----- src/columns/ColTags.cpp | 10 +++----- src/columns/ColUDA.cpp | 10 +++----- src/columns/Column.cpp | 18 ++++++-------- 7 files changed, 48 insertions(+), 69 deletions(-) diff --git a/src/columns/ColDepends.cpp b/src/columns/ColDepends.cpp index 345562761..73b390851 100644 --- a/src/columns/ColDepends.cpp +++ b/src/columns/ColDepends.cpp @@ -99,18 +99,17 @@ void ColumnDepends::measure (Task& task, unsigned int& minimum, unsigned int& ma if (task.has ("depends")) { std::vector blocking_ids; - std::vector ::iterator i; - for (i = blocking.begin (); i != blocking.end (); ++i) - blocking_ids.push_back (i->id); + for (auto& i : blocking) + blocking_ids.push_back (i.id); std::string all; join (all, " ", blocking_ids); maximum = all.length (); unsigned int length; - for (i = blocking.begin (); i != blocking.end (); ++i) + for (auto& i : blocking) { - length = format (i->id).length (); + length = format (i.id).length (); if (length > minimum) minimum = length; } @@ -150,9 +149,8 @@ void ColumnDepends::render ( _style == "list") { std::vector blocking_ids; - std::vector ::iterator t; - for (t = blocking.begin (); t != blocking.end (); ++t) - blocking_ids.push_back (t->id); + for (auto& t : blocking) + blocking_ids.push_back (t.id); std::string combined; join (combined, " ", blocking_ids); @@ -160,9 +158,8 @@ void ColumnDepends::render ( std::vector all; wrapText (all, combined, width, _hyphenate); - std::vector ::iterator i; - for (i = all.begin (); i != all.end (); ++i) - lines.push_back (color.colorize (leftJustify (*i, width))); + for (auto& i : all) + lines.push_back (color.colorize (leftJustify (i, width))); } } } diff --git a/src/columns/ColDescription.cpp b/src/columns/ColDescription.cpp index 905f2cc86..aa0801c29 100644 --- a/src/columns/ColDescription.cpp +++ b/src/columns/ColDescription.cpp @@ -116,10 +116,9 @@ void ColumnDescription::measure (Task& task, unsigned int& minimum, unsigned int std::map annos; task.getAnnotations (annos); - std::map ::iterator i; - for (i = annos.begin (); i != annos.end (); i++) + for (auto& i : annos) { - unsigned int len = min_anno + 1 + utf8_width (i->second); + unsigned int len = min_anno + 1 + utf8_width (i.second); if (len > maximum) maximum = len; } @@ -144,9 +143,8 @@ void ColumnDescription::measure (Task& task, unsigned int& minimum, unsigned int unsigned int min_anno = Date::length (_dateformat); std::map annos; task.getAnnotations (annos); - std::map ::iterator i; - for (i = annos.begin (); i != annos.end (); i++) - maximum += min_anno + 1 + utf8_width (i->second); + for (auto& i : annos) + maximum += min_anno + 1 + utf8_width (i.second); } } @@ -195,20 +193,18 @@ void ColumnDescription::render ( task.getAnnotations (annos); if (annos.size ()) { - std::map ::iterator i; - for (i = annos.begin (); i != annos.end (); i++) + for (auto& i : annos) { - Date dt (strtol (i->first.substr (11).c_str (), NULL, 10)); - description += "\n" + std::string (_indent, ' ') + dt.toString (_dateformat) + " " + i->second; + Date dt (strtol (i.first.substr (11).c_str (), NULL, 10)); + description += "\n" + std::string (_indent, ' ') + dt.toString (_dateformat) + " " + i.second; } } std::vector raw; wrapText (raw, description, width, _hyphenate); - std::vector ::iterator i; - for (i = raw.begin (); i != raw.end (); ++i) - lines.push_back (color.colorize (leftJustify (*i, width))); + for (auto& i : raw) + lines.push_back (color.colorize (leftJustify (i, width))); } // This is a description @@ -217,9 +213,8 @@ void ColumnDescription::render ( std::vector raw; wrapText (raw, description, width, _hyphenate); - std::vector ::iterator i; - for (i = raw.begin (); i != raw.end (); ++i) - lines.push_back (color.colorize (leftJustify (*i, width))); + for (auto& i : raw) + lines.push_back (color.colorize (leftJustify (i, width))); } // This is a description ... @@ -229,20 +224,18 @@ void ColumnDescription::render ( task.getAnnotations (annos); if (annos.size ()) { - std::map ::iterator i; - for (i = annos.begin (); i != annos.end (); i++) + for (auto& i : annos) { - Date dt (atoi (i->first.substr (11).c_str ())); - description += " " + dt.toString (_dateformat) + " " + i->second; + Date dt (atoi (i.first.substr (11).c_str ())); + description += " " + dt.toString (_dateformat) + " " + i.second; } } std::vector raw; wrapText (raw, description, width, _hyphenate); - std::vector ::iterator i; - for (i = raw.begin (); i != raw.end (); ++i) - lines.push_back (color.colorize (leftJustify (*i, width))); + for (auto& i : raw) + lines.push_back (color.colorize (leftJustify (i, width))); } // This is a des... @@ -267,9 +260,8 @@ void ColumnDescription::render ( std::vector raw; wrapText (raw, description, width, _hyphenate); - std::vector ::iterator i; - for (i = raw.begin (); i != raw.end (); ++i) - lines.push_back (color.colorize (leftJustify (*i, width))); + for (auto& i : raw) + lines.push_back (color.colorize (leftJustify (i, width))); } // This is a des... [2] @@ -292,7 +284,6 @@ void ColumnDescription::render ( lines.push_back (color.colorize (description.substr (0, width - len_annos - 3) + "..." + annos_count)); else lines.push_back (color.colorize (leftJustify (description + annos_count, width))); - } } diff --git a/src/columns/ColProject.cpp b/src/columns/ColProject.cpp index 829c15716..25371cb5c 100644 --- a/src/columns/ColProject.cpp +++ b/src/columns/ColProject.cpp @@ -118,9 +118,8 @@ void ColumnProject::render ( std::vector raw; wrapText (raw, project, width, _hyphenate); - std::vector ::iterator i; - for (i = raw.begin (); i != raw.end (); ++i) - lines.push_back (color.colorize (leftJustify (*i, width))); + for (auto& i : raw) + lines.push_back (color.colorize (leftJustify (i, width))); } } diff --git a/src/columns/ColString.cpp b/src/columns/ColString.cpp index fb423d5d4..df825633e 100644 --- a/src/columns/ColString.cpp +++ b/src/columns/ColString.cpp @@ -100,18 +100,16 @@ void ColumnString::render ( std::vector raw; wrapText (raw, value, width, _hyphenate); - std::vector ::iterator i; - for (i = raw.begin (); i != raw.end (); ++i) - lines.push_back (color.colorize (leftJustify (*i, width))); + for (auto& i : raw) + lines.push_back (color.colorize (leftJustify (i, width))); } else if (_style == "right") { std::vector raw; wrapText (raw, value, width, _hyphenate); - std::vector ::iterator i; - for (i = raw.begin (); i != raw.end (); ++i) - lines.push_back (color.colorize (rightJustify (*i, width))); + for (auto& i : raw) + lines.push_back (color.colorize (rightJustify (i, width))); } else if (_style == "left_fixed") { diff --git a/src/columns/ColTags.cpp b/src/columns/ColTags.cpp index 5c3324e88..9e64d7f18 100644 --- a/src/columns/ColTags.cpp +++ b/src/columns/ColTags.cpp @@ -107,10 +107,9 @@ void ColumnTags::measure (Task& task, unsigned int& minimum, unsigned int& maxim { std::vector all; split (all, tags, ','); - std::vector ::iterator i; - for (i = all.begin (); i != all.end (); ++i) + for (auto& i : all) { - unsigned int length = utf8_width (*i); + unsigned int length = utf8_width (i); if (length > minimum) minimum = length; } @@ -145,9 +144,8 @@ void ColumnTags::render ( std::vector all; wrapText (all, tags, width, _hyphenate); - std::vector ::iterator i; - for (i = all.begin (); i != all.end (); ++i) - lines.push_back (color.colorize (leftJustify (*i, width))); + for (auto& i : all) + lines.push_back (color.colorize (leftJustify (i, width))); } else if (_style == "indicator") { diff --git a/src/columns/ColUDA.cpp b/src/columns/ColUDA.cpp index 5fc023866..8a72ccb29 100644 --- a/src/columns/ColUDA.cpp +++ b/src/columns/ColUDA.cpp @@ -64,9 +64,8 @@ bool ColumnUDA::validate (std::string& value) return true; // Look for exact match value. - std::vector ::iterator i; - for (i = _values.begin (); i != _values.end (); ++i) - if (*i == value) + for (auto& i : _values) + if (i == value) return true; // Fail if not found. @@ -171,9 +170,8 @@ void ColumnUDA::render ( std::vector raw; wrapText (raw, value, width, _hyphenate); - std::vector ::iterator i; - for (i = raw.begin (); i != raw.end (); ++i) - lines.push_back (color.colorize (leftJustify (*i, width))); + for (auto& i : raw) + lines.push_back (color.colorize (leftJustify (i, width))); } else if (_type == "numeric") { diff --git a/src/columns/Column.cpp b/src/columns/Column.cpp index 2f9dc3144..a6f57dcbf 100644 --- a/src/columns/Column.cpp +++ b/src/columns/Column.cpp @@ -149,24 +149,22 @@ void Column::uda (std::map & all) // For each UDA, instantiate and initialize ColumnUDA(). std::map udas; - Config::const_iterator i; - for (i = context.config.begin (); i != context.config.end (); ++i) + for (auto& i : context.config) { - if (i->first.substr (0, 4) == "uda.") + if (i.first.substr (0, 4) == "uda.") { std::string::size_type period = 4; - if ((period = i->first.find ('.', period)) != std::string::npos) - udas[i->first.substr (4, period - 4)] = 0; + if ((period = i.first.find ('.', period)) != std::string::npos) + udas[i.first.substr (4, period - 4)] = 0; } } - std::map ::iterator uda; - for (uda = udas.begin (); uda != udas.end (); ++uda) + for (auto& uda : udas) { - if (all.find (uda->first) != all.end ()) - throw format (STRING_UDA_COLLISION, uda->first); + if (all.find (uda.first) != all.end ()) + throw format (STRING_UDA_COLLISION, uda.first); - Column* c = Column::uda (uda->first); + Column* c = Column::uda (uda.first); all[c->_name] = c; } }