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

Everywhere: Use to_number<T> instead of to_{int,uint,float,double}

In a bunch of cases, this actually ends up simplifying the code as
to_number will handle something such as:

```
Optional<I> opt;
if constexpr (IsSigned<I>)
    opt = view.to_int<I>();
else
    opt = view.to_uint<I>();
```

For us.

The main goal here however is to have a single generic number conversion
API between all of the String classes.
This commit is contained in:
Shannon Booth 2023-12-23 15:59:14 +13:00 committed by Andreas Kling
parent a4ecc65398
commit e2e7c4d574
155 changed files with 397 additions and 412 deletions

View file

@ -176,7 +176,7 @@ unsigned HTMLImageElement::width() const
// NOTE: This step seems to not be in the spec, but all browsers do it.
auto width_attr = deprecated_get_attribute(HTML::AttributeNames::width);
if (auto converted = width_attr.to_uint(); converted.has_value())
if (auto converted = width_attr.to_number<unsigned>(); converted.has_value())
return *converted;
// ...or else the density-corrected intrinsic width and height of the image, in CSS pixels,
@ -204,7 +204,7 @@ unsigned HTMLImageElement::height() const
// NOTE: This step seems to not be in the spec, but all browsers do it.
auto height_attr = deprecated_get_attribute(HTML::AttributeNames::height);
if (auto converted = height_attr.to_uint(); converted.has_value())
if (auto converted = height_attr.to_number<unsigned>(); converted.has_value())
return *converted;
// ...or else the density-corrected intrinsic height and height of the image, in CSS pixels,

View file

@ -193,7 +193,7 @@ Optional<ARIA::Role> HTMLSelectElement::default_role() const
if (has_attribute(AttributeNames::multiple))
return ARIA::Role::listbox;
if (has_attribute(AttributeNames::size)) {
auto size_attribute = deprecated_attribute(AttributeNames::size).to_int();
auto size_attribute = deprecated_attribute(AttributeNames::size).to_number<int>();
if (size_attribute.has_value() && size_attribute.value() > 1)
return ARIA::Role::listbox;
}

View file

@ -46,7 +46,7 @@ void HTMLTableElement::visit_edges(Cell::Visitor& visitor)
static unsigned parse_border(ByteString const& value)
{
return value.to_uint().value_or(0);
return value.to_number<unsigned>().value_or(0);
}
void HTMLTableElement::apply_presentational_hints(CSS::StyleProperties& style) const

View file

@ -84,7 +84,7 @@ Optional<u32> parse_non_negative_integer(StringView string)
Optional<double> parse_floating_point_number(StringView string)
{
// FIXME: Implement spec compliant floating point number parsing
auto maybe_double = MUST(String::from_utf8(string)).to_number<double>(TrimWhitespace::Yes);
auto maybe_double = string.to_number<double>(TrimWhitespace::Yes);
if (!maybe_double.has_value())
return {};
if (!isfinite(maybe_double.value()))

View file

@ -4475,7 +4475,7 @@ RefPtr<CSS::StyleValue> parse_dimension_value(StringView string)
number_string.append(*position);
++position;
}
auto integer_value = number_string.string_view().to_int();
auto integer_value = number_string.string_view().to_number<int>();
// 6. If position is past the end of input, then return value as a length.
if (position == input.end())

View file

@ -269,8 +269,8 @@ descriptor_parser:
auto last_character = descriptor.bytes_as_string_view().bytes().last();
auto descriptor_without_last_character = descriptor.bytes_as_string_view().substring_view(0, descriptor.bytes_as_string_view().length() - 1);
auto as_int = descriptor_without_last_character.to_int<i32>();
auto as_float = descriptor_without_last_character.to_float();
auto as_int = descriptor_without_last_character.to_number<i32>();
auto as_float = descriptor_without_last_character.to_number<float>();
// - If the descriptor consists of a valid non-negative integer followed by a U+0077 LATIN SMALL LETTER W character
if (last_character == 'w' && as_int.has_value()) {

View file

@ -235,7 +235,7 @@ static T parse_boolean_feature(StringView value)
return T::Yes;
// 4. Let parsed be the result of parsing value as an integer.
auto parsed = value.to_int<i64>();
auto parsed = value.to_number<i64>();
// 5. If parsed is an error, then set it to 0.
if (!parsed.has_value())