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

@ -80,7 +80,7 @@ Optional<float> EditGuideDialog::offset_as_pixel(ImageEditor const& editor)
{
float offset = 0;
if (m_offset.ends_with('%')) {
auto percentage = m_offset.substring_view(0, m_offset.length() - 1).to_int();
auto percentage = m_offset.substring_view(0, m_offset.length() - 1).to_number<int>();
if (!percentage.has_value())
return {};
@ -89,7 +89,7 @@ Optional<float> EditGuideDialog::offset_as_pixel(ImageEditor const& editor)
else if (orientation() == PixelPaint::Guide::Orientation::Vertical)
offset = editor.image().size().width() * ((double)percentage.value() / 100.0);
} else {
auto parsed_int = m_offset.to_int();
auto parsed_int = m_offset.to_number<int>();
if (!parsed_int.has_value())
return {};
offset = parsed_int.value();

View file

@ -927,7 +927,7 @@ ByteString ImageEditor::generate_unique_layer_name(ByteString const& original_la
auto after_copy_suffix_view = original_layer_name.substring_view(copy_suffix_index.value() + copy_string_view.length());
if (!after_copy_suffix_view.is_empty()) {
auto after_copy_suffix_number = after_copy_suffix_view.trim_whitespace().to_int();
auto after_copy_suffix_number = after_copy_suffix_view.trim_whitespace().to_number<int>();
if (!after_copy_suffix_number.has_value())
return ByteString::formatted("{}{}", original_layer_name, copy_string_view);
}

View file

@ -370,8 +370,8 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
auto layer_x_position = data_and_type.metadata.get("pixelpaint-layer-x");
auto layer_y_position = data_and_type.metadata.get("pixelpaint-layer-y");
if (layer_x_position.has_value() && layer_y_position.has_value()) {
auto x = layer_x_position.value().to_int();
auto y = layer_y_position.value().to_int();
auto x = layer_x_position.value().to_number<int>();
auto y = layer_y_position.value().to_number<int>();
if (x.has_value() && x.value()) {
auto pasted_layer_location = Gfx::IntPoint { x.value(), y.value() };
@ -1211,7 +1211,7 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
}
}
auto zoom_level_optional = value.view().trim("%"sv, TrimMode::Right).to_int();
auto zoom_level_optional = value.view().trim("%"sv, TrimMode::Right).to_number<int>();
if (!zoom_level_optional.has_value()) {
// Indicate that a parse-error occurred by resetting the text to the current state.
editor->on_scale_change(editor->scale());

View file

@ -1,4 +1,5 @@
/*
* Copyright (c) 2019-2023, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
@ -188,8 +189,8 @@ NonnullRefPtr<GUI::Widget> EllipseTool::get_properties_widget()
m_aspect_w_textbox->set_fixed_height(20);
m_aspect_w_textbox->set_fixed_width(25);
m_aspect_w_textbox->on_change = [this] {
auto x = m_aspect_w_textbox->text().to_int().value_or(0);
auto y = m_aspect_h_textbox->text().to_int().value_or(0);
auto x = m_aspect_w_textbox->text().to_number<int>().value_or(0);
auto y = m_aspect_h_textbox->text().to_number<int>().value_or(0);
if (x > 0 && y > 0) {
m_aspect_ratio = (float)x / (float)y;
} else {

View file

@ -237,8 +237,8 @@ NonnullRefPtr<GUI::Widget> RectangleTool::get_properties_widget()
m_aspect_w_textbox->set_fixed_height(20);
m_aspect_w_textbox->set_fixed_width(25);
m_aspect_w_textbox->on_change = [this] {
auto x = m_aspect_w_textbox->text().to_int().value_or(0);
auto y = m_aspect_h_textbox->text().to_int().value_or(0);
auto x = m_aspect_w_textbox->text().to_number<int>().value_or(0);
auto y = m_aspect_h_textbox->text().to_number<int>().value_or(0);
if (x > 0 && y > 0) {
m_aspect_ratio = (float)x / (float)y;
} else {