Skip to content

Commit 80a458a

Browse files
committed
switch on first character
1 parent 0648ce0 commit 80a458a

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

csscolorparser.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,6 @@ void skip_whitespace(const std::string& text, size_t& pos, size_t end){
269269
}
270270
}
271271

272-
uint32_t CSSColorParser::Color::getInt() {
273-
uint32_t color = ((uint32_t)(255.0 * a) << 24) + ((uint32_t)b << 16) + ((uint32_t)g << 8) + ((uint32_t)r);
274-
return color;
275-
}
276-
277-
Color CSSColorParser::parse(const std::string& css_str) {
278-
bool valid;
279-
return parse(css_str, valid);
280-
}
281-
282272
void parseHexRGB(const std::string& css_str, size_t pos, size_t end, bool& matched, bool& valid, Color& color) {
283273
if (!match('#', css_str, pos, end)) {
284274
return;
@@ -471,6 +461,16 @@ void parseNamedColor(const std::string& css_str, size_t pos, size_t end, bool& v
471461
}
472462

473463

464+
uint32_t CSSColorParser::Color::getInt() {
465+
uint32_t color = ((uint32_t)(255.0 * a) << 24) + ((uint32_t)b << 16) + ((uint32_t)g << 8) + ((uint32_t)r);
466+
return color;
467+
}
468+
469+
Color CSSColorParser::parse(const std::string& css_str) {
470+
bool valid;
471+
return parse(css_str, valid);
472+
}
473+
474474
Color CSSColorParser::parse(const std::string& css_str, bool& valid) {
475475
valid = false;
476476

@@ -484,22 +484,24 @@ Color CSSColorParser::parse(const std::string& css_str, bool& valid) {
484484
return {};
485485
}
486486

487-
parseHexRGB(css_str, pos, end, matched, valid, color);
488-
if (matched) {
489-
return color;
490-
}
487+
switch(*(css_str.c_str() + pos)) {
488+
case '#':
489+
parseHexRGB(css_str, pos, end, matched, valid, color);
490+
break;
491491

492-
parseRGB(css_str, pos, end, matched, valid, color);
493-
if (matched) {
494-
return color;
495-
}
492+
case 'r':
493+
case 'R':
494+
parseRGB(css_str, pos, end, matched, valid, color);
495+
break;
496496

497-
parseHSL(css_str, pos, end, matched, valid, color);
498-
if (matched) {
499-
return color;
497+
case 'h':
498+
case 'H':
499+
parseHSL(css_str, pos, end, matched, valid, color);
500+
break;
500501
}
501502

502-
parseNamedColor(css_str, pos, end, valid, color);
503+
if (!matched)
504+
parseNamedColor(css_str, pos, end, valid, color);
503505

504506
return color;
505507
}

0 commit comments

Comments
 (0)