|
@@ -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()
|