Skip to content

Commit 7bdb3eb

Browse files
committed
use plain array instead of std::map
1 parent 6035ad8 commit 7bdb3eb

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

csscolorparser.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
#include <sstream>
3030
#include <cmath>
3131
#include <algorithm>
32-
#include <map>
3332

3433
using namespace CSSColorParser;
3534

3635
// http://www.w3.org/TR/css3-color/
37-
const std::map<std::string, Color> kCSSColorTable = {
36+
struct NamedColor { const char *const name; const Color color; };
37+
const NamedColor namedColors[] = {
3838
{ "transparent", { 0, 0, 0, 0 } }, { "aliceblue", { 240, 248, 255, 1 } },
3939
{ "antiquewhite", { 250, 235, 215, 1 } }, { "aqua", { 0, 255, 255, 1 } },
4040
{ "aquamarine", { 127, 255, 212, 1 } }, { "azure", { 240, 255, 255, 1 } },
@@ -111,6 +111,8 @@ const std::map<std::string, Color> kCSSColorTable = {
111111
{ "yellow", { 255, 255, 0, 1 } }, { "yellowgreen", { 154, 205, 50, 1 } }
112112
};
113113

114+
const size_t namedColorCount = sizeof (namedColors) / sizeof (NamedColor);
115+
114116

115117
template <typename T>
116118
uint8_t clamp_css_byte(T i) { // Clamp to integer 0 .. 255.
@@ -187,10 +189,11 @@ Color CSSColorParser::parse(const std::string& css_str) {
187189
// Convert to lowercase.
188190
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
189191

190-
// Color keywords (and transparent) lookup.
191-
auto it = kCSSColorTable.find(str);
192-
if (it != kCSSColorTable.end()) {
193-
return it->second;
192+
193+
for (size_t i = 0; i < namedColorCount; i++) {
194+
if (str == namedColors[i].name) {
195+
return namedColors[i].color;
196+
}
194197
}
195198

196199
// #abc and #abc123 syntax.

0 commit comments

Comments
 (0)