Explorar el Código

LibWeb: Make SVG::AttributeParser use the new double parser

Because the result will be a float anyway get rid of the int parsing.
Also the grammar of SVG numbers matches the double parser grammar except
it can't have a sign but that should have been checked by the caller.
davidot hace 2 años
padre
commit
1ab6cb1ee9

+ 10 - 50
Userland/Libraries/LibWeb/SVG/AttributeParser.cpp

@@ -6,6 +6,7 @@
  */
 
 #include "AttributeParser.h"
+#include <AK/FloatingPointStringConversions.h>
 #include <AK/StringBuilder.h>
 #include <ctype.h>
 
@@ -346,29 +347,6 @@ void AttributeParser::parse_comma_whitespace()
     }
 }
 
-float AttributeParser::parse_fractional_constant()
-{
-    StringBuilder builder;
-    bool floating_point = false;
-
-    while (!done() && isdigit(ch()))
-        builder.append(consume());
-
-    if (match('.')) {
-        floating_point = true;
-        builder.append('.');
-        consume();
-        while (!done() && isdigit(ch()))
-            builder.append(consume());
-    } else {
-        VERIFY(builder.length() > 0);
-    }
-
-    if (floating_point)
-        return strtof(builder.to_string().characters(), nullptr);
-    return builder.to_string().to_int().value();
-}
-
 // https://www.w3.org/TR/SVG11/types.html#DataTypeNumber
 float AttributeParser::parse_number()
 {
@@ -379,36 +357,18 @@ float AttributeParser::parse_number()
 // https://www.w3.org/TR/SVG11/paths.html#PathDataBNF
 float AttributeParser::parse_nonnegative_number()
 {
-    auto number = parse_fractional_constant();
-
-    if (!match('e') && !match('E'))
-        return number;
-    consume();
-
-    auto exponent_sign = parse_sign();
+    // NOTE: The grammar is almost a floating point except we cannot have a sign
+    //       at the start. That condition should have been checked by the caller.
+    VERIFY(!match('+') && !match('-'));
 
-    StringBuilder exponent_builder;
-    while (!done() && isdigit(ch()))
-        exponent_builder.append(consume());
-    VERIFY(exponent_builder.length() > 0);
+    auto remaining_source_text = m_source.substring_view(m_cursor);
+    char const* start = remaining_source_text.characters_without_null_termination();
 
-    auto exponent = exponent_builder.to_string().to_int().value();
-
-    // Fast path: If the number is 0, there's no point in computing the exponentiation.
-    if (number == 0)
-        return number;
-
-    if (exponent_sign < 0) {
-        for (int i = 0; i < exponent; ++i) {
-            number /= 10;
-        }
-    } else if (exponent_sign > 0) {
-        for (int i = 0; i < exponent; ++i) {
-            number *= 10;
-        }
-    }
+    auto maybe_float = parse_first_floating_point<float>(start, start + remaining_source_text.length());
+    VERIFY(maybe_float.parsed_value());
+    m_cursor += maybe_float.end_ptr - start;
 
-    return number;
+    return maybe_float.value;
 }
 
 float AttributeParser::parse_flag()

+ 0 - 1
Userland/Libraries/LibWeb/SVG/AttributeParser.h

@@ -68,7 +68,6 @@ private:
     Vector<float> parse_elliptical_arg_argument();
     void parse_whitespace(bool must_match_once = false);
     void parse_comma_whitespace();
-    float parse_fractional_constant();
     float parse_number();
     float parse_nonnegative_number();
     float parse_flag();