From 4d481e03f3534cb703a276cd430056d3ec27fcb0 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Wed, 7 Oct 2009 13:00:57 +0100 Subject: [PATCH] color: Measure the string just once when parsing Instead of measuring the color hexadecimal string for each case, just measure it once and then use a switch() to go to the right case. --- clutter/clutter-color.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/clutter/clutter-color.c b/clutter/clutter-color.c index e81e911d2..e0b4614ea 100644 --- a/clutter/clutter-color.c +++ b/clutter/clutter-color.c @@ -406,9 +406,11 @@ clutter_color_from_string (ClutterColor *color, if (sscanf (str + 1, "%x", &result)) { - if (strlen (str) == 9) + gsize length = strlen (str); + + switch (length) { - /* #rrggbbaa */ + case 9: /* rrggbbaa */ color->red = (result >> 24) & 0xff; color->green = (result >> 16) & 0xff; color->blue = (result >> 8) & 0xff; @@ -416,10 +418,17 @@ clutter_color_from_string (ClutterColor *color, color->alpha = result & 0xff; return TRUE; - } - else if (strlen (str) == 5) - { - /* #rgba */ + + case 7: /* #rrggbb */ + color->red = (result >> 16) & 0xff; + color->green = (result >> 8) & 0xff; + color->blue = result & 0xff; + + color->alpha = 0xff; + + return TRUE; + + case 5: /* #rgba */ color->red = ((result >> 12) & 0xf); color->green = ((result >> 8) & 0xf); color->blue = ((result >> 4) & 0xf); @@ -431,19 +440,8 @@ clutter_color_from_string (ClutterColor *color, color->alpha = (color->alpha << 4) | color->alpha; return TRUE; - } - else if (strlen (str) == 7) - { - /* #rrggbb */ - color->red = (result >> 16) & 0xff; - color->green = (result >> 8) & 0xff; - color->blue = result & 0xff; - color->alpha = 0xff; - } - else if (strlen (str) == 4) - { - /* #rgb */ + case 4: /* #rgb */ color->red = ((result >> 8) & 0xf); color->green = ((result >> 4) & 0xf); color->blue = result & 0xf; @@ -453,6 +451,12 @@ clutter_color_from_string (ClutterColor *color, color->blue = (color->blue << 4) | color->blue; color->alpha = 0xff; + + return TRUE; + + default: + /* pass through to Pango */ + break; } } }