From 1ab6cb1ee91b161c130bbfe266edf1f59e1242bd Mon Sep 17 00:00:00 2001 From: davidot Date: Wed, 12 Oct 2022 23:50:54 +0200 Subject: [PATCH] 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. --- .../Libraries/LibWeb/SVG/AttributeParser.cpp | 60 ++++--------------- .../Libraries/LibWeb/SVG/AttributeParser.h | 1 - 2 files changed, 10 insertions(+), 51 deletions(-) diff --git a/Userland/Libraries/LibWeb/SVG/AttributeParser.cpp b/Userland/Libraries/LibWeb/SVG/AttributeParser.cpp index 7dc030cb18..236f707b7a 100644 --- a/Userland/Libraries/LibWeb/SVG/AttributeParser.cpp +++ b/Userland/Libraries/LibWeb/SVG/AttributeParser.cpp @@ -6,6 +6,7 @@ */ #include "AttributeParser.h" +#include #include #include @@ -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(); + // 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('-')); - if (!match('e') && !match('E')) - return number; - consume(); + auto remaining_source_text = m_source.substring_view(m_cursor); + char const* start = remaining_source_text.characters_without_null_termination(); - auto exponent_sign = parse_sign(); + auto maybe_float = parse_first_floating_point(start, start + remaining_source_text.length()); + VERIFY(maybe_float.parsed_value()); + m_cursor += maybe_float.end_ptr - start; - StringBuilder exponent_builder; - while (!done() && isdigit(ch())) - exponent_builder.append(consume()); - VERIFY(exponent_builder.length() > 0); - - 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; - } - } - - return number; + return maybe_float.value; } float AttributeParser::parse_flag() diff --git a/Userland/Libraries/LibWeb/SVG/AttributeParser.h b/Userland/Libraries/LibWeb/SVG/AttributeParser.h index b589a14f0e..3ba483d519 100644 --- a/Userland/Libraries/LibWeb/SVG/AttributeParser.h +++ b/Userland/Libraries/LibWeb/SVG/AttributeParser.h @@ -68,7 +68,6 @@ private: Vector 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();