Do not allow using color names as face names

This commit is contained in:
Maxime Coste 2014-08-19 23:16:21 +01:00
parent d78e331304
commit d78a586126
3 changed files with 25 additions and 8 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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];