1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:47:45 +00:00

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.
This commit is contained in:
davidot 2022-10-12 23:50:54 +02:00 committed by Linus Groh
parent 051134a21e
commit 1ab6cb1ee9
2 changed files with 10 additions and 51 deletions

View file

@ -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();
// 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<float>(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()