mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 10:38:13 +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
|
@ -122,7 +122,7 @@ template<>
|
|||
NullableTree CppASTConverter::convert_node(Cpp::NumericLiteral const& literal)
|
||||
{
|
||||
// TODO: Numerical literals are not limited to i64.
|
||||
return make_ref_counted<MathematicalConstant>(literal.value().to_int<i64>().value());
|
||||
return make_ref_counted<MathematicalConstant>(literal.value().to_number<i64>().value());
|
||||
}
|
||||
|
||||
template<>
|
||||
|
|
|
@ -254,7 +254,7 @@ ParseErrorOr<Tree> TextParser::parse_expression()
|
|||
if (token.type == TokenType::Identifier) {
|
||||
expression = make_ref_counted<UnresolvedReference>(token.data);
|
||||
} else if (token.type == TokenType::Number) {
|
||||
expression = make_ref_counted<MathematicalConstant>(token.data.to_int<i64>().value());
|
||||
expression = make_ref_counted<MathematicalConstant>(token.data.to_number<i64>().value());
|
||||
} else if (token.type == TokenType::String) {
|
||||
expression = make_ref_counted<StringLiteral>(token.data);
|
||||
} else {
|
||||
|
|
|
@ -26,19 +26,19 @@ static ErrorOr<ApprovalDate> parse_approval_date(StringView date)
|
|||
if (parts.size() != 3)
|
||||
return Error::from_string_literal("Failed to parse approval date parts (mm/dd/yyyy)");
|
||||
|
||||
auto month = parts[0].to_uint();
|
||||
auto month = parts[0].to_number<unsigned>();
|
||||
if (!month.has_value())
|
||||
return Error::from_string_literal("Failed to parse month from approval date");
|
||||
if (month.value() == 0 || month.value() > 12)
|
||||
return Error::from_string_literal("Invalid month in approval date");
|
||||
|
||||
auto day = parts[1].to_uint();
|
||||
auto day = parts[1].to_number<unsigned>();
|
||||
if (!day.has_value())
|
||||
return Error::from_string_literal("Failed to parse day from approval date");
|
||||
if (day.value() == 0 || day.value() > 31)
|
||||
return Error::from_string_literal("Invalid day in approval date");
|
||||
|
||||
auto year = parts[2].to_uint();
|
||||
auto year = parts[2].to_number<unsigned>();
|
||||
if (!year.has_value())
|
||||
return Error::from_string_literal("Failed to parse year from approval date");
|
||||
if (year.value() < 1900 || year.value() > 2999)
|
||||
|
|
|
@ -652,7 +652,7 @@ static ErrorOr<void> parse_week_data(ByteString core_path, CLDR& cldr)
|
|||
auto const& weekend_end_object = week_data_object.get_object("weekendEnd"sv).value();
|
||||
|
||||
minimum_days_object.for_each_member([&](auto const& region, auto const& value) {
|
||||
auto minimum_days = value.as_string().template to_uint<u8>();
|
||||
auto minimum_days = value.as_string().template to_number<u8>();
|
||||
cldr.minimum_days.set(region, *minimum_days);
|
||||
|
||||
if (!cldr.minimum_days_regions.contains_slow(region))
|
||||
|
@ -1279,7 +1279,7 @@ static void parse_calendar_symbols(Calendar& calendar, JsonObject const& calenda
|
|||
auto symbol_lists = create_symbol_lists(2);
|
||||
|
||||
auto append_symbol = [&](auto& symbols, auto const& key, auto symbol) {
|
||||
if (auto key_index = key.to_uint(); key_index.has_value())
|
||||
if (auto key_index = key.template to_number<unsigned>(); key_index.has_value())
|
||||
symbols[*key_index] = cldr.unique_strings.ensure(move(symbol));
|
||||
};
|
||||
|
||||
|
@ -1303,7 +1303,7 @@ static void parse_calendar_symbols(Calendar& calendar, JsonObject const& calenda
|
|||
auto symbol_lists = create_symbol_lists(12);
|
||||
|
||||
auto append_symbol = [&](auto& symbols, auto const& key, auto symbol) {
|
||||
auto key_index = key.to_uint().value() - 1;
|
||||
auto key_index = key.template to_number<unsigned>().value() - 1;
|
||||
symbols[key_index] = cldr.unique_strings.ensure(move(symbol));
|
||||
};
|
||||
|
||||
|
@ -1611,7 +1611,7 @@ static ErrorOr<void> parse_day_periods(ByteString core_path, CLDR& cldr)
|
|||
VERIFY(time.substring_view(hour_end_index) == ":00"sv);
|
||||
|
||||
auto hour = time.substring_view(0, hour_end_index);
|
||||
return hour.template to_uint<u8>().value();
|
||||
return hour.template to_number<u8>().value();
|
||||
};
|
||||
|
||||
auto parse_day_period = [&](auto const& symbol, auto const& ranges) -> Optional<DayPeriod> {
|
||||
|
|
|
@ -441,7 +441,7 @@ static ErrorOr<void> parse_number_systems(ByteString locale_numbers_path, CLDR&
|
|||
auto patterns = value.as_string().split(';');
|
||||
NumberFormat format {};
|
||||
|
||||
if (auto type = split_key[0].template to_uint<u64>(); type.has_value()) {
|
||||
if (auto type = split_key[0].template to_number<u64>(); type.has_value()) {
|
||||
VERIFY(*type % 10 == 0);
|
||||
format.magnitude = static_cast<u8>(log10(*type));
|
||||
|
||||
|
@ -580,7 +580,7 @@ static ErrorOr<void> parse_number_systems(ByteString locale_numbers_path, CLDR&
|
|||
locale.number_systems.append(system_index);
|
||||
}
|
||||
|
||||
locale.minimum_grouping_digits = minimum_grouping_digits.template to_uint<u8>().value();
|
||||
locale.minimum_grouping_digits = minimum_grouping_digits.template to_number<u8>().value();
|
||||
return {};
|
||||
}
|
||||
|
||||
|
|
|
@ -245,7 +245,7 @@ static Relation parse_relation(StringView relation)
|
|||
auto symbol = lhs.substring_view(0, *index);
|
||||
VERIFY(symbol.length() == 1);
|
||||
|
||||
auto modulus = lhs.substring_view(*index + modulus_operator.length()).to_uint();
|
||||
auto modulus = lhs.substring_view(*index + modulus_operator.length()).to_number<unsigned>();
|
||||
VERIFY(modulus.has_value());
|
||||
|
||||
parsed.symbol = symbol[0];
|
||||
|
@ -257,15 +257,15 @@ static Relation parse_relation(StringView relation)
|
|||
|
||||
rhs.for_each_split_view(set_operator, SplitBehavior::Nothing, [&](auto set) {
|
||||
if (auto index = set.find(range_operator); index.has_value()) {
|
||||
auto range_begin = set.substring_view(0, *index).to_uint();
|
||||
auto range_begin = set.substring_view(0, *index).template to_number<unsigned>();
|
||||
VERIFY(range_begin.has_value());
|
||||
|
||||
auto range_end = set.substring_view(*index + range_operator.length()).to_uint();
|
||||
auto range_end = set.substring_view(*index + range_operator.length()).template to_number<unsigned>();
|
||||
VERIFY(range_end.has_value());
|
||||
|
||||
parsed.comparators.empend(Array { *range_begin, *range_end });
|
||||
} else {
|
||||
auto value = set.to_uint();
|
||||
auto value = set.template to_number<unsigned>();
|
||||
VERIFY(value.has_value());
|
||||
|
||||
parsed.comparators.empend(*value);
|
||||
|
|
|
@ -172,7 +172,7 @@ static Optional<DateTime> parse_date_time(ReadonlySpan<StringView> segments)
|
|||
return {};
|
||||
|
||||
DateTime date_time {};
|
||||
date_time.year = segments[0].to_uint().value();
|
||||
date_time.year = segments[0].to_number<unsigned>().value();
|
||||
|
||||
if (segments.size() > 1)
|
||||
date_time.month = find_index(short_month_names.begin(), short_month_names.end(), segments[1]) + 1;
|
||||
|
@ -186,15 +186,15 @@ static Optional<DateTime> parse_date_time(ReadonlySpan<StringView> segments)
|
|||
date_time.after_weekday = find_index(short_day_names.begin(), short_day_names.end(), weekday);
|
||||
|
||||
auto day = segments[2].substring_view(*index + ">="sv.length());
|
||||
date_time.day = day.to_uint().value();
|
||||
date_time.day = day.to_number<unsigned>().value();
|
||||
} else if (auto index = segments[2].find("<="sv); index.has_value()) {
|
||||
auto weekday = segments[2].substring_view(0, *index);
|
||||
date_time.before_weekday = find_index(short_day_names.begin(), short_day_names.end(), weekday);
|
||||
|
||||
auto day = segments[2].substring_view(*index + "<="sv.length());
|
||||
date_time.day = day.to_uint().value();
|
||||
date_time.day = day.to_number<unsigned>().value();
|
||||
} else {
|
||||
date_time.day = segments[2].to_uint().value();
|
||||
date_time.day = segments[2].to_number<unsigned>().value();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -202,9 +202,9 @@ static Optional<DateTime> parse_date_time(ReadonlySpan<StringView> segments)
|
|||
// FIXME: Some times end with a letter, e.g. "2:00u" and "2:00s". Figure out what this means and handle it.
|
||||
auto time_segments = segments[3].split_view(':');
|
||||
|
||||
date_time.hour = time_segments[0].to_int().value();
|
||||
date_time.minute = time_segments.size() > 1 ? time_segments[1].substring_view(0, 2).to_uint().value() : 0;
|
||||
date_time.second = time_segments.size() > 2 ? time_segments[2].substring_view(0, 2).to_uint().value() : 0;
|
||||
date_time.hour = time_segments[0].to_number<int>().value();
|
||||
date_time.minute = time_segments.size() > 1 ? time_segments[1].substring_view(0, 2).to_number<unsigned>().value() : 0;
|
||||
date_time.second = time_segments.size() > 2 ? time_segments[2].substring_view(0, 2).to_number<unsigned>().value() : 0;
|
||||
}
|
||||
|
||||
return date_time;
|
||||
|
@ -214,9 +214,9 @@ static i64 parse_time_offset(StringView segment)
|
|||
{
|
||||
auto segments = segment.split_view(':');
|
||||
|
||||
i64 hours = segments[0].to_int().value();
|
||||
i64 minutes = segments.size() > 1 ? segments[1].to_uint().value() : 0;
|
||||
i64 seconds = segments.size() > 2 ? segments[2].to_uint().value() : 0;
|
||||
i64 hours = segments[0].to_number<int>().value();
|
||||
i64 minutes = segments.size() > 1 ? segments[1].to_number<unsigned>().value() : 0;
|
||||
i64 seconds = segments.size() > 2 ? segments[2].to_number<unsigned>().value() : 0;
|
||||
|
||||
i64 sign = ((hours < 0) || (segments[0] == "-0"sv)) ? -1 : 1;
|
||||
return (hours * 3600) + sign * ((minutes * 60) + seconds);
|
||||
|
@ -309,12 +309,12 @@ static void parse_rule(StringView rule_line, TimeZoneData& time_zone_data)
|
|||
|
||||
DaylightSavingsOffset dst_offset {};
|
||||
dst_offset.offset = parse_time_offset(segments[8]);
|
||||
dst_offset.year_from = segments[2].to_uint().value();
|
||||
dst_offset.year_from = segments[2].to_number<unsigned>().value();
|
||||
|
||||
if (segments[3] == "only")
|
||||
dst_offset.year_to = dst_offset.year_from;
|
||||
else if (segments[3] != "max"sv)
|
||||
dst_offset.year_to = segments[3].to_uint().value();
|
||||
dst_offset.year_to = segments[3].to_number<unsigned>().value();
|
||||
|
||||
auto in_effect = Array { "0"sv, segments[5], segments[6], segments[7] };
|
||||
dst_offset.in_effect = parse_date_time(in_effect).release_value();
|
||||
|
@ -369,22 +369,22 @@ static ErrorOr<void> parse_time_zone_coordinates(Core::InputBufferedFile& file,
|
|||
|
||||
if (coordinate.length() == 5) {
|
||||
// ±DDMM
|
||||
parsed.degrees = coordinate.substring_view(0, 3).to_int().value();
|
||||
parsed.minutes = coordinate.substring_view(3).to_int().value();
|
||||
parsed.degrees = coordinate.substring_view(0, 3).template to_number<int>().value();
|
||||
parsed.minutes = coordinate.substring_view(3).template to_number<int>().value();
|
||||
} else if (coordinate.length() == 6) {
|
||||
// ±DDDMM
|
||||
parsed.degrees = coordinate.substring_view(0, 4).to_int().value();
|
||||
parsed.minutes = coordinate.substring_view(4).to_int().value();
|
||||
parsed.degrees = coordinate.substring_view(0, 4).template to_number<int>().value();
|
||||
parsed.minutes = coordinate.substring_view(4).template to_number<int>().value();
|
||||
} else if (coordinate.length() == 7) {
|
||||
// ±DDMMSS
|
||||
parsed.degrees = coordinate.substring_view(0, 3).to_int().value();
|
||||
parsed.minutes = coordinate.substring_view(3, 2).to_int().value();
|
||||
parsed.seconds = coordinate.substring_view(5).to_int().value();
|
||||
parsed.degrees = coordinate.substring_view(0, 3).template to_number<int>().value();
|
||||
parsed.minutes = coordinate.substring_view(3, 2).template to_number<int>().value();
|
||||
parsed.seconds = coordinate.substring_view(5).template to_number<int>().value();
|
||||
} else if (coordinate.length() == 8) {
|
||||
// ±DDDDMMSS
|
||||
parsed.degrees = coordinate.substring_view(0, 4).to_int().value();
|
||||
parsed.minutes = coordinate.substring_view(4, 2).to_int().value();
|
||||
parsed.seconds = coordinate.substring_view(6).to_int().value();
|
||||
parsed.degrees = coordinate.substring_view(0, 4).template to_number<int>().value();
|
||||
parsed.minutes = coordinate.substring_view(4, 2).template to_number<int>().value();
|
||||
parsed.seconds = coordinate.substring_view(6).template to_number<int>().value();
|
||||
} else {
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
|
|
@ -675,7 +675,7 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter
|
|||
else
|
||||
@cpp_name@ = JS::js_null();
|
||||
)~~~");
|
||||
} else if (optional_default_value->to_int().has_value() || optional_default_value->to_uint().has_value()) {
|
||||
} else if (optional_default_value->to_number<int>().has_value() || optional_default_value->to_number<unsigned>().has_value()) {
|
||||
scoped_generator.append(R"~~~(
|
||||
else
|
||||
@cpp_name@ = JS::Value(@parameter.optional_default_value@);
|
||||
|
@ -1428,7 +1428,7 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter
|
|||
union_generator.append(R"~~~(
|
||||
@union_type@ @cpp_name@ = @js_name@@js_suffix@.is_undefined() ? TRY(@js_name@@js_suffix@_to_dictionary(@js_name@@js_suffix@)) : TRY(@js_name@@js_suffix@_to_variant(@js_name@@js_suffix@));
|
||||
)~~~");
|
||||
} else if (optional_default_value->to_int().has_value() || optional_default_value->to_uint().has_value()) {
|
||||
} else if (optional_default_value->to_number<int>().has_value() || optional_default_value->to_number<unsigned>().has_value()) {
|
||||
union_generator.append(R"~~~(
|
||||
@union_type@ @cpp_name@ = @js_name@@js_suffix@.is_undefined() ? @parameter.optional_default_value@ : TRY(@js_name@@js_suffix@_to_variant(@js_name@@js_suffix@));
|
||||
)~~~");
|
||||
|
|
|
@ -92,7 +92,7 @@ parse_state_machine(StringView input)
|
|||
if (lexer.next_is('\\')) {
|
||||
num = (int)lexer.consume_escaped_character('\\');
|
||||
} else {
|
||||
num = lexer.consume_until('\'').to_int().value();
|
||||
num = lexer.consume_until('\'').to_number<int>().value();
|
||||
lexer.ignore();
|
||||
}
|
||||
lexer.consume_specific('\'');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue