Paste number 33499: SVGColor improvementIndex: WebCore/ksvg2/svg/SVGColor.cpp

Paste number 33499: SVGColor improvementIndex: WebCore/ksvg2/svg/SVGColor.cpp
Pasted by: rwlbuis
When:14 years, 9 months ago
Share:Tweet this! | http://paste.lisp.org/+PUJ
Channel:#webkit
Paste contents:
Raw Source | XML | Display As
Index: WebCore/ksvg2/svg/SVGColor.cpp
===================================================================
--- WebCore/ksvg2/svg/SVGColor.cpp      (revision 18430)
+++ WebCore/ksvg2/svg/SVGColor.cpp      (working copy)
@@ -23,8 +23,10 @@
 #include "config.h"
 #ifdef SVG_SUPPORT
 #include "SVGColor.h"
+#include "SVGParserUtils.h"

 #include "SVGException.h"
+#include "DeprecatedString.h"

 namespace WebCore {

@@ -224,39 +226,58 @@ void SVGColor::setRGBColor(const String&
         return;
     }

+    Color color;
     String parse = m_rgbColor.stripWhiteSpace();
     if (parse.startsWith("rgb(")) {
-        Vector<String> colors = parse.split(',');
-        String r = colors[0].right((colors[0].length() - 4));
-        String g = colors[1];
-        String b = colors[2].left((colors[2].length() - 1));
-    
-        if (r.contains("%")) {
-            r = r.left(r.length() - 1);
-            r = String::number(int(((255.0 * r.toDouble()) / 100.0)));
+        bool percentages = false;
+        double r = -1, g = -1, b = -1;
+        const UChar* ptr = parse.characters() + 4;
+        const UChar* end = parse.characters() + parse.length();
+        skipOptionalSpaces(ptr, end);
+        if (!parseNumber(ptr, end, r))
+            return;
+        if (*ptr == '%') {
+            percentages = true;
+            ptr++;
+            if (!skipOptionalSpacesOrDelimiter(ptr, end))
+                return;
+            if (!parseNumber(ptr, end, g, false))
+                return;
+            if (*ptr != '%')
+                return;
+            ptr++;
+            if (!skipOptionalSpacesOrDelimiter(ptr, end))
+                return;
+            if (!parseNumber(ptr, end, b, false))
+                return;
+            if (*ptr != '%')
+                return;
+            ptr++;
+        } else {
+            if (!parseNumber(ptr, end, g))
+                return;
+            if (!parseNumber(ptr, end, b, false))
+                return;
         }

-        if (g.contains("%")) {
-            g = g.left(g.length() - 1);
-            g = String::number(int(((255.0 * g.toDouble()) / 100.0)));
-        }
-    
-        if (b.contains("%")) {
-            b = b.left(b.length() - 1);
-            b = String::number(int(((255.0 * b.toDouble()) / 100.0)));
-        }
-
-        m_color = Color(r.toInt(), g.toInt(), b.toInt());
+        skipOptionalSpaces(ptr, end);
+        if (*ptr++ != ')')
+            return;
+
+        if (percentages)
+            color = Color(int((255.0 * r) / 100.0),
+                          int((255.0 * g) / 100.0),
+                          int((255.0 * b) / 100.0));
+        else
+            color = Color(int(r), int(g), int(b));
     } else {
         String colorName = m_rgbColor.lower();
-//        int col = WebCore::getValueID(name.ascii(), name.length());
-//        if (col == 0)
-            m_color = Color(colorName);
-//        else
-//            m_color = cmap[col - SVGCSS_VAL_ALICEBLUE];
+        color = Color(colorName);
     }
-    if (!m_color.isValid())
+    if (!color.isValid())
         ec = SVG_INVALID_VALUE_ERR;
+    else
+        m_color = color;
 }

 void SVGColor::setRGBColorICCColor(const String& /* rgbColor */, const String& /* iccColor */, ExceptionCode& ec)

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.