From d78a5861262ead1fc7b25188879bb6cfe3b0b87a Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 19 Aug 2014 23:16:21 +0100 Subject: [PATCH] Do not allow using color names as face names --- src/color.cc | 21 +++++++++++++++++---- src/color.hh | 7 +++++-- src/face_registry.cc | 5 +++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/color.cc b/src/color.cc index 54e48002..2dc71af0 100644 --- a/src/color.cc +++ b/src/color.cc @@ -5,7 +5,20 @@ namespace Kakoune { -Color str_to_color(const String& color) +bool is_color_name(StringView color) +{ + return color == "default" or + color == "black" or + color == "red" or + color == "green" or + color == "yellow" or + color == "blue" or + color == "magenta" or + color == "cyan" or + color == "white"; +} + +Color str_to_color(StringView color) { if (color == "default") return Colors::Default; if (color == "black") return Colors::Black; @@ -18,10 +31,10 @@ Color str_to_color(const String& color) if (color == "white") return Colors::White; static const Regex rgb_regex{"rgb:[0-9a-fA-F]{6}"}; - if (boost::regex_match(color, rgb_regex)) + if (boost::regex_match(color.begin(), color.end(), rgb_regex)) { unsigned l; - sscanf(color.c_str() + 4, "%x", &l); + sscanf(color.zstr() + 4, "%x", &l); return { (unsigned char)((l >> 16) & 0xFF), (unsigned char)((l >> 8) & 0xFF), (unsigned char)(l & 0xFF) }; @@ -59,7 +72,7 @@ String option_to_string(Color color) return color_to_str(color); } -void option_from_string(const String& str, Color& color) +void option_from_string(StringView str, Color& color) { color = str_to_color(str); } diff --git a/src/color.hh b/src/color.hh index 838041f4..f6b3e842 100644 --- a/src/color.hh +++ b/src/color.hh @@ -7,6 +7,7 @@ namespace Kakoune { class String; +class StringView; enum class Colors : char { @@ -40,11 +41,13 @@ struct Color { return color != c.color or r != c.r or g != c.g or b != c.b; } }; -Color str_to_color(const String& color); +Color str_to_color(StringView color); String color_to_str(Color color); String option_to_string(Color color); -void option_from_string(const String& str, Color& color); +void option_from_string(StringView str, Color& color); + +bool is_color_name(StringView color); } diff --git a/src/face_registry.cc b/src/face_registry.cc index c28cc2a3..93e9c9f0 100644 --- a/src/face_registry.cc +++ b/src/face_registry.cc @@ -51,8 +51,9 @@ void FaceRegistry::register_alias(const String& name, const String& facedesc, if (not override and m_aliases.find(name) != m_aliases.end()) throw runtime_error("alias '" + name + "' already defined"); - if (name.empty() or - find_if(name, [](char c){ return not isalnum(c); }) != name.end()) + if (name.empty() or is_color_name(name) or + std::any_of(name.begin(), name.end(), + [](char c){ return not isalnum(c); })) throw runtime_error("invalid alias name"); FaceOrAlias& alias = m_aliases[name];