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

@ -417,7 +417,7 @@ void AbstractTableView::set_visible_columns(StringView column_names)
column_header().set_section_visible(column, false);
column_names.for_each_split_view(',', SplitBehavior::Nothing, [&, this](StringView column_id_string) {
if (auto column = column_id_string.to_int(); column.has_value()) {
if (auto column = column_id_string.to_number<int>(); column.has_value()) {
column_header().set_section_visible(column.value(), true);
}
});

View file

@ -79,23 +79,23 @@ RefPtr<Gfx::Bitmap> Clipboard::DataAndType::as_bitmap() const
if (mime_type != "image/x-serenityos")
return nullptr;
auto width = metadata.get("width").value_or("0").to_uint();
auto width = metadata.get("width").value_or("0").to_number<unsigned>();
if (!width.has_value() || width.value() == 0)
return nullptr;
auto height = metadata.get("height").value_or("0").to_uint();
auto height = metadata.get("height").value_or("0").to_number<unsigned>();
if (!height.has_value() || height.value() == 0)
return nullptr;
auto scale = metadata.get("scale").value_or("0").to_uint();
auto scale = metadata.get("scale").value_or("0").to_number<unsigned>();
if (!scale.has_value() || scale.value() == 0)
return nullptr;
auto pitch = metadata.get("pitch").value_or("0").to_uint();
auto pitch = metadata.get("pitch").value_or("0").to_number<unsigned>();
if (!pitch.has_value() || pitch.value() == 0)
return nullptr;
auto format = metadata.get("format").value_or("0").to_uint();
auto format = metadata.get("format").value_or("0").to_number<unsigned>();
if (!format.has_value() || format.value() == 0)
return nullptr;

View file

@ -24,7 +24,7 @@ SpinBox::SpinBox()
if (!weak_this)
return;
auto value = m_editor->text().to_uint();
auto value = m_editor->text().to_number<unsigned>();
if (!value.has_value() && m_editor->text().length() > 0)
m_editor->do_delete();
};
@ -81,7 +81,7 @@ void SpinBox::set_value_from_current_text()
if (m_editor->text().is_empty())
return;
auto value = m_editor->text().to_int();
auto value = m_editor->text().to_number<int>();
if (value.has_value())
set_value(value.value());
else

View file

@ -35,7 +35,7 @@ ValueSlider::ValueSlider(Gfx::Orientation orientation, String suffix)
ByteString value = m_textbox->text();
if (value.ends_with(m_suffix, AK::CaseSensitivity::CaseInsensitive))
value = value.substring_view(0, value.length() - m_suffix.bytes_as_string_view().length());
auto integer_value = value.to_int();
auto integer_value = value.to_number<int>();
if (integer_value.has_value())
AbstractSlider::set_value(integer_value.value());
};

View file

@ -120,10 +120,7 @@ public:
[](FloatingPoint auto v) { return (T)v; },
[](Detail::Boolean v) -> T { return v.value ? 1 : 0; },
[](ByteString const& v) {
if constexpr (IsUnsigned<T>)
return v.to_uint<T>().value_or(0u);
else
return v.to_int<T>().value_or(0);
return v.to_number<T>().value_or(0);
},
[](Enum auto const&) -> T { return 0; },
[](OneOf<Gfx::IntPoint, Gfx::IntRect, Gfx::IntSize, Color, NonnullRefPtr<Gfx::Font const>, NonnullRefPtr<Gfx::Bitmap const>, GUI::Icon> auto const&) -> T { return 0; });

View file

@ -142,10 +142,10 @@ void Window::show()
auto parts = StringView { launch_origin_rect_string, strlen(launch_origin_rect_string) }.split_view(',');
if (parts.size() == 4) {
launch_origin_rect = Gfx::IntRect {
parts[0].to_int().value_or(0),
parts[1].to_int().value_or(0),
parts[2].to_int().value_or(0),
parts[3].to_int().value_or(0),
parts[0].to_number<int>().value_or(0),
parts[1].to_number<int>().value_or(0),
parts[2].to_number<int>().value_or(0),
parts[3].to_number<int>().value_or(0),
};
}
unsetenv("__libgui_launch_origin_rect");