mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 05:07:35 +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
|
@ -13,12 +13,12 @@
|
|||
|
||||
static inline GLuint get_index_value(StringView& representation)
|
||||
{
|
||||
return representation.to_uint().value_or(1) - 1;
|
||||
return representation.to_number<GLuint>().value_or(1) - 1;
|
||||
}
|
||||
|
||||
static ErrorOr<GLfloat> parse_float(StringView string)
|
||||
{
|
||||
auto maybe_float = string.to_float(TrimWhitespace::No);
|
||||
auto maybe_float = string.to_number<GLfloat>(TrimWhitespace::No);
|
||||
if (!maybe_float.has_value())
|
||||
return Error::from_string_literal("Wavefront: Expected floating point value when parsing TexCoord line");
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ static bool handle_disassemble_command(ByteString const& command, FlatPtr first_
|
|||
auto parts = command.split(' ');
|
||||
size_t number_of_instructions_to_disassemble = 5;
|
||||
if (parts.size() == 2) {
|
||||
auto number = parts[1].to_uint();
|
||||
auto number = parts[1].to_number<unsigned>();
|
||||
if (!number.has_value())
|
||||
return false;
|
||||
number_of_instructions_to_disassemble = number.value();
|
||||
|
@ -142,7 +142,7 @@ static bool handle_breakpoint_command(ByteString const& command)
|
|||
auto source_arguments = argument.split(':');
|
||||
if (source_arguments.size() != 2)
|
||||
return false;
|
||||
auto line = source_arguments[1].to_uint();
|
||||
auto line = source_arguments[1].to_number<unsigned>();
|
||||
if (!line.has_value())
|
||||
return false;
|
||||
auto file = source_arguments[0];
|
||||
|
|
|
@ -106,12 +106,12 @@ FileOperationProgressWidget::FileOperationProgressWidget(FileOperation operation
|
|||
if (parts[0] == "PROGRESS"sv) {
|
||||
VERIFY(parts.size() >= 8);
|
||||
did_progress(
|
||||
parts[3].to_uint().value_or(0),
|
||||
parts[4].to_uint().value_or(0),
|
||||
parts[1].to_uint().value_or(0),
|
||||
parts[2].to_uint().value_or(0),
|
||||
parts[5].to_uint().value_or(0),
|
||||
parts[6].to_uint().value_or(0),
|
||||
parts[3].to_number<unsigned>().value_or(0),
|
||||
parts[4].to_number<unsigned>().value_or(0),
|
||||
parts[1].to_number<unsigned>().value_or(0),
|
||||
parts[2].to_number<unsigned>().value_or(0),
|
||||
parts[5].to_number<unsigned>().value_or(0),
|
||||
parts[6].to_number<unsigned>().value_or(0),
|
||||
parts[7]);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1057,11 +1057,11 @@ void MainWidget::paste_glyphs()
|
|||
if (!mime_type.starts_with("glyph/x-fonteditor"sv))
|
||||
return;
|
||||
|
||||
auto glyph_count = metadata.get("count").value().to_uint().value_or(0);
|
||||
auto glyph_count = metadata.get("count").value().to_number<unsigned>().value_or(0);
|
||||
if (!glyph_count)
|
||||
return;
|
||||
|
||||
auto height = metadata.get("height").value().to_uint().value_or(0);
|
||||
auto height = metadata.get("height").value().to_number<unsigned>().value_or(0);
|
||||
if (!height)
|
||||
return;
|
||||
|
||||
|
|
|
@ -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() });
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -12,7 +12,7 @@ NumericInput::NumericInput()
|
|||
set_text("0"sv);
|
||||
|
||||
on_change = [&] {
|
||||
auto number_opt = text().to_int();
|
||||
auto number_opt = text().to_number<int>();
|
||||
if (number_opt.has_value()) {
|
||||
set_current_number(number_opt.value(), GUI::AllowCallback::No);
|
||||
return;
|
||||
|
@ -26,7 +26,7 @@ NumericInput::NumericInput()
|
|||
first = false;
|
||||
}
|
||||
|
||||
auto new_number_opt = builder.to_byte_string().to_int();
|
||||
auto new_number_opt = builder.to_byte_string().to_number<int>();
|
||||
if (!new_number_opt.has_value()) {
|
||||
m_needs_text_reset = true;
|
||||
return;
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -148,12 +148,12 @@ ErrorOr<Gfx::IntSize> Presentation::parse_presentation_size(JsonObject const& me
|
|||
return Error::from_string_view("Width or aspect in incorrect format"sv);
|
||||
|
||||
// We intentionally discard floating-point data here. If you need more resolution, just use a larger width.
|
||||
auto const width = maybe_width->to_int();
|
||||
auto const width = maybe_width->to_number<int>();
|
||||
auto const aspect_parts = maybe_aspect->split_view(':');
|
||||
if (aspect_parts.size() != 2)
|
||||
return Error::from_string_view("Aspect specification must have the exact format `width:height`"sv);
|
||||
auto aspect_width = aspect_parts[0].to_int<int>();
|
||||
auto aspect_height = aspect_parts[1].to_int<int>();
|
||||
auto aspect_width = aspect_parts[0].to_number<int>();
|
||||
auto aspect_height = aspect_parts[1].to_number<int>();
|
||||
if (!aspect_width.has_value() || !aspect_height.has_value() || aspect_width.value() == 0 || aspect_height.value() == 0)
|
||||
return Error::from_string_view("Aspect width and height must be non-zero integers"sv);
|
||||
|
||||
|
|
|
@ -70,7 +70,7 @@ NonnullOwnPtr<Vector<M3UEntry>> M3UParser::parse(bool include_extended_info)
|
|||
VERIFY(separator.has_value());
|
||||
auto seconds = ext_inf.value().substring_view(0, separator.value());
|
||||
VERIFY(!seconds.is_whitespace() && !seconds.is_null() && !seconds.is_empty());
|
||||
metadata_for_next_file.track_length_in_seconds = seconds.to_uint();
|
||||
metadata_for_next_file.track_length_in_seconds = seconds.to_number<unsigned>();
|
||||
auto display_name = ext_inf.value().substring_view(seconds.length() + 1);
|
||||
VERIFY(!display_name.is_empty() && !display_name.is_null() && !display_name.is_empty());
|
||||
metadata_for_next_file.track_display_title = display_name;
|
||||
|
|
|
@ -79,7 +79,7 @@ CellType const& Cell::type() const
|
|||
return *m_type;
|
||||
|
||||
if (m_kind == LiteralString) {
|
||||
if (m_data.to_int().has_value())
|
||||
if (m_data.to_number<int>().has_value())
|
||||
return *CellType::get_by_name("Numeric"sv);
|
||||
}
|
||||
|
||||
|
|
|
@ -204,7 +204,7 @@ Optional<Position> Sheet::parse_cell_name(StringView name) const
|
|||
if (it == m_columns.end())
|
||||
return {};
|
||||
|
||||
return Position { it.index(), row.to_uint().value() };
|
||||
return Position { it.index(), row.to_number<unsigned>().value() };
|
||||
}
|
||||
|
||||
Optional<size_t> Sheet::column_index(StringView column_name) const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue