1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 03:17: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:
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

@ -96,17 +96,17 @@ void SearchPanel::search(StringView query)
// FIXME: Handle JSON parsing errors
auto const& json_place = json_places.at(i).as_object();
MapWidget::LatLng latlng = { json_place.get_byte_string("lat"sv).release_value().to_double().release_value(),
json_place.get_byte_string("lon"sv).release_value().to_double().release_value() };
MapWidget::LatLng latlng = { json_place.get_byte_string("lat"sv).release_value().to_number<double>().release_value(),
json_place.get_byte_string("lon"sv).release_value().to_number<double>().release_value() };
String name = MUST(String::formatted("{}\n{:.5}, {:.5}", json_place.get_byte_string("display_name"sv).release_value(), latlng.latitude, latlng.longitude));
// Calculate the right zoom level for bounding box
auto const& json_boundingbox = json_place.get_array("boundingbox"sv);
MapWidget::LatLngBounds bounds = {
{ json_boundingbox->at(0).as_string().to_double().release_value(),
json_boundingbox->at(2).as_string().to_double().release_value() },
{ json_boundingbox->at(1).as_string().to_double().release_value(),
json_boundingbox->at(3).as_string().to_double().release_value() }
{ json_boundingbox->at(0).as_string().to_number<double>().release_value(),
json_boundingbox->at(2).as_string().to_number<double>().release_value() },
{ json_boundingbox->at(1).as_string().to_number<double>().release_value(),
json_boundingbox->at(3).as_string().to_number<double>().release_value() }
};
m_places.append({ name, latlng, bounds.get_zoom() });

View file

@ -60,8 +60,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
// Map widget
Maps::UsersMapWidget::Options options {};
options.center.latitude = Config::read_string("Maps"sv, "MapView"sv, "CenterLatitude"sv, "30"sv).to_double().value_or(30.0);
options.center.longitude = Config::read_string("Maps"sv, "MapView"sv, "CenterLongitude"sv, "0"sv).to_double().value_or(0.0);
options.center.latitude = Config::read_string("Maps"sv, "MapView"sv, "CenterLatitude"sv, "30"sv).to_number<double>().value_or(30.0);
options.center.longitude = Config::read_string("Maps"sv, "MapView"sv, "CenterLongitude"sv, "0"sv).to_number<double>().value_or(0.0);
options.zoom = Config::read_i32("Maps"sv, "MapView"sv, "Zoom"sv, MAP_ZOOM_DEFAULT);
auto& map_widget = main_widget.add<Maps::UsersMapWidget>(options);
map_widget.set_frame_style(Gfx::FrameStyle::SunkenContainer);