mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:47:34 +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:
parent
a4ecc65398
commit
e2e7c4d574
155 changed files with 397 additions and 412 deletions
|
@ -99,14 +99,14 @@ Optional<i32> AriaData::parse_integer(Optional<String> const& value)
|
|||
{
|
||||
if (!value.has_value())
|
||||
return {};
|
||||
return value->bytes_as_string_view().to_int();
|
||||
return value->bytes_as_string_view().to_number<i32>();
|
||||
}
|
||||
|
||||
Optional<f64> AriaData::parse_number(Optional<String> const& value)
|
||||
{
|
||||
if (!value.has_value())
|
||||
return {};
|
||||
return value->bytes_as_string_view().to_double(TrimWhitespace::Yes);
|
||||
return value->to_number<double>(TrimWhitespace::Yes);
|
||||
}
|
||||
|
||||
Optional<String> AriaData::aria_active_descendant_or_default() const
|
||||
|
|
|
@ -835,7 +835,7 @@ Optional<Selector::SimpleSelector::ANPlusBPattern> Parser::parse_a_n_plus_b_patt
|
|||
if (is_ndashdigit_dimension(first_value)) {
|
||||
auto const& dimension = first_value.token();
|
||||
int a = dimension.dimension_value_int();
|
||||
auto maybe_b = dimension.dimension_unit().bytes_as_string_view().substring_view(1).to_int();
|
||||
auto maybe_b = dimension.dimension_unit().bytes_as_string_view().substring_view(1).to_number<int>();
|
||||
if (maybe_b.has_value()) {
|
||||
transaction.commit();
|
||||
return Selector::SimpleSelector::ANPlusBPattern { a, maybe_b.value() };
|
||||
|
@ -845,7 +845,7 @@ Optional<Selector::SimpleSelector::ANPlusBPattern> Parser::parse_a_n_plus_b_patt
|
|||
}
|
||||
// <dashndashdigit-ident>
|
||||
if (is_dashndashdigit_ident(first_value)) {
|
||||
auto maybe_b = first_value.token().ident().bytes_as_string_view().substring_view(2).to_int();
|
||||
auto maybe_b = first_value.token().ident().bytes_as_string_view().substring_view(2).to_number<int>();
|
||||
if (maybe_b.has_value()) {
|
||||
transaction.commit();
|
||||
return Selector::SimpleSelector::ANPlusBPattern { -1, maybe_b.value() };
|
||||
|
@ -958,7 +958,7 @@ Optional<Selector::SimpleSelector::ANPlusBPattern> Parser::parse_a_n_plus_b_patt
|
|||
|
||||
// '+'?† <ndashdigit-ident>
|
||||
if (is_ndashdigit_ident(first_after_plus)) {
|
||||
auto maybe_b = first_after_plus.token().ident().bytes_as_string_view().substring_view(1).to_int();
|
||||
auto maybe_b = first_after_plus.token().ident().bytes_as_string_view().substring_view(1).to_number<int>();
|
||||
if (maybe_b.has_value()) {
|
||||
transaction.commit();
|
||||
return Selector::SimpleSelector::ANPlusBPattern { 1, maybe_b.value() };
|
||||
|
|
|
@ -578,7 +578,7 @@ double Tokenizer::convert_a_string_to_a_number(StringView string)
|
|||
{
|
||||
// FIXME: We already found the whole part, fraction part and exponent during
|
||||
// validation, we could probably skip
|
||||
return string.to_double(AK::TrimWhitespace::No).release_value();
|
||||
return string.to_number<double>(AK::TrimWhitespace::No).release_value();
|
||||
}
|
||||
|
||||
// https://www.w3.org/TR/css-syntax-3/#consume-name
|
||||
|
|
|
@ -167,7 +167,7 @@ void on_max_age_attribute(ParsedCookie& parsed_cookie, StringView attribute_valu
|
|||
return;
|
||||
|
||||
// Let delta-seconds be the attribute-value converted to an integer.
|
||||
if (auto delta_seconds = attribute_value.to_int(); delta_seconds.has_value()) {
|
||||
if (auto delta_seconds = attribute_value.to_number<int>(); delta_seconds.has_value()) {
|
||||
if (*delta_seconds <= 0) {
|
||||
// If delta-seconds is less than or equal to zero (0), let expiry-time be the earliest representable date and time.
|
||||
parsed_cookie.expiry_time_from_max_age_attribute = UnixDateTime::earliest();
|
||||
|
@ -251,7 +251,7 @@ Optional<UnixDateTime> parse_date_time(StringView date_string)
|
|||
if (!all_of(token, isdigit))
|
||||
return false;
|
||||
|
||||
if (auto converted = token.to_uint(); converted.has_value()) {
|
||||
if (auto converted = token.to_number<unsigned>(); converted.has_value()) {
|
||||
result = *converted;
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -342,11 +342,11 @@ ErrorOr<HeaderList::ExtractLengthResult> HeaderList::extract_length() const
|
|||
}
|
||||
|
||||
// 5. If candidateValue is the empty string or has a code point that is not an ASCII digit, then return null.
|
||||
// NOTE: to_uint does this for us.
|
||||
// NOTE: to_number does this for us.
|
||||
// 6. Return candidateValue, interpreted as decimal number.
|
||||
// NOTE: The spec doesn't say anything about trimming here, so we don't trim. If it contains a space, step 5 will cause us to return null.
|
||||
// FIXME: This will return an empty Optional if it cannot fit into a u64, is this correct?
|
||||
auto conversion_result = AK::StringUtils::convert_to_uint<u64>(candidate_value.value(), TrimWhitespace::No);
|
||||
auto conversion_result = candidate_value.value().to_number<u64>(TrimWhitespace::No);
|
||||
if (!conversion_result.has_value())
|
||||
return Empty {};
|
||||
return ExtractLengthResult { conversion_result.release_value() };
|
||||
|
@ -851,7 +851,7 @@ Optional<RangeHeaderValue> parse_single_range_header_value(ReadonlyBytes value)
|
|||
auto range_start = lexer.consume_while(is_ascii_digit);
|
||||
|
||||
// 5. Let rangeStartValue be rangeStart, interpreted as decimal number, if rangeStart is not the empty string; otherwise null.
|
||||
auto range_start_value = range_start.to_uint<u64>();
|
||||
auto range_start_value = range_start.to_number<u64>();
|
||||
|
||||
// 6. If the code point at position within data is not U+002D (-), then return failure.
|
||||
// 7. Advance position by 1.
|
||||
|
@ -862,7 +862,7 @@ Optional<RangeHeaderValue> parse_single_range_header_value(ReadonlyBytes value)
|
|||
auto range_end = lexer.consume_while(is_ascii_digit);
|
||||
|
||||
// 9. Let rangeEndValue be rangeEnd, interpreted as decimal number, if rangeEnd is not the empty string; otherwise null.
|
||||
auto range_end_value = range_end.to_uint<u64>();
|
||||
auto range_end_value = range_end.to_number<u64>();
|
||||
|
||||
// 10. If position is not past the end of data, then return failure.
|
||||
if (!lexer.is_eof())
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()))
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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()) {
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -24,8 +24,8 @@ void FrameBox::prepare_for_replaced_layout()
|
|||
VERIFY(dom_node().nested_browsing_context());
|
||||
|
||||
// FIXME: Do proper error checking, etc.
|
||||
set_natural_width(dom_node().deprecated_attribute(HTML::AttributeNames::width).to_int().value_or(300));
|
||||
set_natural_height(dom_node().deprecated_attribute(HTML::AttributeNames::height).to_int().value_or(150));
|
||||
set_natural_width(dom_node().deprecated_attribute(HTML::AttributeNames::width).to_number<int>().value_or(300));
|
||||
set_natural_height(dom_node().deprecated_attribute(HTML::AttributeNames::height).to_number<int>().value_or(150));
|
||||
}
|
||||
|
||||
void FrameBox::did_set_content_size()
|
||||
|
|
|
@ -72,7 +72,7 @@ void TableFormattingContext::compute_constrainedness()
|
|||
m_columns[column_index].is_constrained = true;
|
||||
}
|
||||
auto const& col_node = static_cast<HTML::HTMLTableColElement const&>(*column_box.dom_node());
|
||||
unsigned span = col_node.deprecated_attribute(HTML::AttributeNames::span).to_uint().value_or(1);
|
||||
unsigned span = col_node.deprecated_attribute(HTML::AttributeNames::span).to_number<unsigned>().value_or(1);
|
||||
column_index += span;
|
||||
});
|
||||
});
|
||||
|
@ -189,7 +189,7 @@ void TableFormattingContext::compute_outer_content_sizes()
|
|||
// The outer max-content width of a table-column or table-column-group is max(min-width, min(max-width, width)).
|
||||
m_columns[column_index].max_size = max(min_width, min(max_width, width));
|
||||
auto const& col_node = static_cast<HTML::HTMLTableColElement const&>(*column_box.dom_node());
|
||||
unsigned span = col_node.deprecated_attribute(HTML::AttributeNames::span).to_uint().value_or(1);
|
||||
unsigned span = col_node.deprecated_attribute(HTML::AttributeNames::span).to_number<unsigned>().value_or(1);
|
||||
column_index += span;
|
||||
});
|
||||
});
|
||||
|
@ -1380,7 +1380,7 @@ void TableFormattingContext::BorderConflictFinder::collect_conflicting_col_eleme
|
|||
for (auto* child_of_column_group = child->first_child(); child_of_column_group; child_of_column_group = child_of_column_group->next_sibling()) {
|
||||
VERIFY(child_of_column_group->display().is_table_column());
|
||||
auto const& col_node = static_cast<HTML::HTMLTableColElement const&>(*child_of_column_group->dom_node());
|
||||
unsigned span = col_node.deprecated_attribute(HTML::AttributeNames::span).to_uint().value_or(1);
|
||||
unsigned span = col_node.deprecated_attribute(HTML::AttributeNames::span).to_number<unsigned>().value_or(1);
|
||||
for (size_t i = column_index; i < column_index + span; ++i) {
|
||||
m_col_elements_by_index[i] = child_of_column_group;
|
||||
}
|
||||
|
@ -1744,7 +1744,7 @@ void TableFormattingContext::initialize_intrinsic_percentages_from_rows_or_colum
|
|||
m_columns[column_index].has_intrinsic_percentage = computed_values.max_width().is_percentage() || computed_values.width().is_percentage();
|
||||
m_columns[column_index].intrinsic_percentage = min(width_percentage, max_width_percentage);
|
||||
auto const& col_node = static_cast<HTML::HTMLTableColElement const&>(*column_box.dom_node());
|
||||
unsigned span = col_node.deprecated_attribute(HTML::AttributeNames::span).to_uint().value_or(1);
|
||||
unsigned span = col_node.deprecated_attribute(HTML::AttributeNames::span).to_number<unsigned>().value_or(1);
|
||||
column_index += span;
|
||||
});
|
||||
});
|
||||
|
|
|
@ -30,7 +30,7 @@ Optional<ViewBox> try_parse_view_box(StringView string)
|
|||
while (!lexer.is_eof()) {
|
||||
lexer.consume_while([](auto ch) { return is_ascii_space(ch); });
|
||||
auto token = lexer.consume_until([](auto ch) { return is_ascii_space(ch) && ch != ','; });
|
||||
auto maybe_number = token.to_float();
|
||||
auto maybe_number = token.to_number<float>();
|
||||
if (!maybe_number.has_value())
|
||||
return {};
|
||||
switch (state) {
|
||||
|
|
|
@ -253,7 +253,7 @@ ErrorOr<JsonValue, Client::WrappedError> Client::read_body_as_json()
|
|||
|
||||
for (auto const& header : m_request->headers()) {
|
||||
if (header.name.equals_ignoring_ascii_case("Content-Length"sv)) {
|
||||
content_length = header.value.to_uint<size_t>(TrimWhitespace::Yes).value_or(0);
|
||||
content_length = header.value.to_number<size_t>(TrimWhitespace::Yes).value_or(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue