From 1579705fec412a726c940ed0d8bfd5a8fb8b2965 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 3 Feb 2016 19:39:38 -0500 Subject: [PATCH] Color: C++11 --- src/Color.cpp | 33 +++++++++++++++------------------ src/Color.h | 7 ++----- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/Color.cpp b/src/Color.cpp index 07dfc9d27..05be8acd0 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -26,8 +26,6 @@ #include #include -#include -#include #include #include #include @@ -143,10 +141,9 @@ Color::Color (const std::string& spec) bool bg = false; int index; - std::string word; - for (auto& it : words) + for (auto& word : words) { - word = Lexer::lowerCase (Lexer::trim (it)); + word = Lexer::lowerCase (Lexer::trim (word)); if (word == "bold") fg_value |= _COLOR_BOLD; else if (word == "bright") bg_value |= _COLOR_BRIGHT; @@ -176,9 +173,9 @@ Color::Color (const std::string& spec) else if (! word.compare (0, 4, "grey", 4) || ! word.compare (0, 4, "gray", 4)) { - index = atoi (word.substr (4).c_str ()); + index = strtol (word.substr (4).c_str (), nullptr, 10); if (index < 0 || index > 23) - throw format (STRING_COLOR_UNRECOGNIZED, it); + throw format (STRING_COLOR_UNRECOGNIZED, word); if (bg) { @@ -197,18 +194,18 @@ Color::Color (const std::string& spec) // rgbRGB, where 0 <= R,G,B <= 5. else if (! word.compare (0, 3, "rgb", 3)) { - index = atoi (word.substr (3).c_str ()); + index = strtol (word.substr (3).c_str (), nullptr, 10); if (word.length () != 6 || index < 0 || index > 555) - throw format (STRING_COLOR_UNRECOGNIZED, it); + throw format (STRING_COLOR_UNRECOGNIZED, word); - int r = atoi (word.substr (3, 1).c_str ()); - int g = atoi (word.substr (4, 1).c_str ()); - int b = atoi (word.substr (5, 1).c_str ()); + int r = strtol (word.substr (3, 1).c_str (), nullptr, 10); + int g = strtol (word.substr (4, 1).c_str (), nullptr, 10); + int b = strtol (word.substr (5, 1).c_str (), nullptr, 10); if (r < 0 || r > 5 || g < 0 || g > 5 || b < 0 || b > 5) - throw format (STRING_COLOR_UNRECOGNIZED, it); + throw format (STRING_COLOR_UNRECOGNIZED, word); index = 16 + r*36 + g*6 + b; @@ -229,9 +226,9 @@ Color::Color (const std::string& spec) // colorN, where 0 <= N <= 255. else if (! word.compare (0, 5, "color", 5)) { - index = atoi (word.substr (5).c_str ()); + index = strtol (word.substr (5).c_str (), nullptr, 10); if (index < 0 || index > 255) - throw format (STRING_COLOR_UNRECOGNIZED, it); + throw format (STRING_COLOR_UNRECOGNIZED, word); upgrade (); @@ -249,7 +246,7 @@ Color::Color (const std::string& spec) } } else if (word != "") - throw format (STRING_COLOR_UNRECOGNIZED, it); + throw format (STRING_COLOR_UNRECOGNIZED, word); } // Now combine the fg and bg into a single color. @@ -409,7 +406,7 @@ void Color::upgrade () } //////////////////////////////////////////////////////////////////////////////// -std::string Color::colorize (const std::string& input) +std::string Color::colorize (const std::string& input) const { std::string result; _colorize (result, input); @@ -428,7 +425,7 @@ std::string Color::colorize (const std::string& input) // // 256 fg \033[38;5;Nm // 256 bg \033[48;5;Nm -void Color::_colorize (std::string &result, const std::string& input) +void Color::_colorize (std::string &result, const std::string& input) const { if (!nontrivial ()) { diff --git a/src/Color.h b/src/Color.h index ff3a09a26..f998c40f6 100644 --- a/src/Color.h +++ b/src/Color.h @@ -29,7 +29,6 @@ #include -//////////////////////////////////////////////////////////////////////////////// #define _COLOR_INVERSE 0x00400000 // Inverse attribute. #define _COLOR_256 0x00200000 // 256-color mode. #define _COLOR_HASBG 0x00100000 // Has background color (all values taken). @@ -57,9 +56,9 @@ public: void upgrade (); void blend (const Color&); - std::string colorize (const std::string&); + std::string colorize (const std::string&) const; static std::string colorize (const std::string&, const std::string&); - void _colorize (std::string&, const std::string&); + void _colorize (std::string&, const std::string&) const; static std::string strip (const std::string&); bool nontrivial () const; @@ -74,5 +73,3 @@ private: }; #endif - -////////////////////////////////////////////////////////////////////////////////