1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:17:44 +00:00

AK: Make string-to-number conversion helpers return Optional

Get rid of the weird old signature:

- int StringType::to_int(bool& ok) const

And replace it with sensible new signature:

- Optional<int> StringType::to_int() const
This commit is contained in:
Andreas Kling 2020-06-12 21:07:52 +02:00
parent 15f4043a7a
commit fdfda6dec2
55 changed files with 354 additions and 455 deletions

View file

@ -76,10 +76,10 @@ bool handle_disassemble_command(const String& command, void* first_instruction)
auto parts = command.split(' ');
size_t number_of_instructions_to_disassemble = 5;
if (parts.size() == 2) {
bool ok;
number_of_instructions_to_disassemble = parts[1].to_uint(ok);
if (!ok)
auto number = parts[1].to_uint();
if (!number.has_value())
return false;
number_of_instructions_to_disassemble = number.value();
}
// FIXME: Instead of using a fixed "dump_size",
@ -126,14 +126,13 @@ bool handle_breakpoint_command(const String& command)
auto source_arguments = argument.split(':');
if (source_arguments.size() != 2)
return false;
bool ok = false;
size_t line = source_arguments[1].to_uint(ok);
if (!ok)
auto line = source_arguments[1].to_uint();
if (!line.has_value())
return false;
auto file = source_arguments[0];
if (!file.contains("/"))
file = String::format("./%s", file.characters());
auto result = g_debug_session->debug_info().get_instruction_from_source(file, line);
auto result = g_debug_session->debug_info().get_instruction_from_source(file, line.value());
if (!result.has_value()) {
printf("No matching instruction found\n");
return false;

View file

@ -303,19 +303,9 @@ void DisplaySettingsWidget::load_current_settings()
/// Resolution ////////////////////////////////////////////////////////////////////////////////
Gfx::IntSize find_size;
bool okay = false;
// Let's attempt to find the current resolution and select it!
find_size.set_width(ws_config->read_entry("Screen", "Width", "1024").to_int(okay));
if (!okay) {
fprintf(stderr, "DisplaySettings: failed to convert width to int!");
ASSERT_NOT_REACHED();
}
find_size.set_height(ws_config->read_entry("Screen", "Height", "768").to_int(okay));
if (!okay) {
fprintf(stderr, "DisplaySettings: failed to convert height to int!");
ASSERT_NOT_REACHED();
}
find_size.set_width(ws_config->read_num_entry("Screen", "Width", 1024));
find_size.set_height(ws_config->read_num_entry("Screen", "Height", 768));
size_t index = m_resolutions.find_first_index(find_size).value_or(0);
Gfx::IntSize m_current_resolution = m_resolutions.at(index);

View file

@ -82,11 +82,10 @@ HexEditorWidget::HexEditorWidget()
auto input_box = GUI::InputBox::construct("Enter new file size:", "New file size", window());
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
auto valid = false;
auto file_size = input_box->text_value().to_int(valid);
if (valid && file_size > 0) {
auto file_size = input_box->text_value().to_int();
if (file_size.has_value() && file_size.value() > 0) {
m_document_dirty = false;
m_editor->set_buffer(ByteBuffer::create_zeroed(file_size));
m_editor->set_buffer(ByteBuffer::create_zeroed(file_size.value()));
set_path(LexicalPath());
update_title();
} else {
@ -149,11 +148,9 @@ HexEditorWidget::HexEditorWidget()
m_goto_decimal_offset_action = GUI::Action::create("Go To Offset (Decimal)...", { Mod_Ctrl | Mod_Shift, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](const GUI::Action&) {
auto input_box = GUI::InputBox::construct("Enter Decimal offset:", "Go To", window());
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
auto valid = false;
auto new_offset = input_box->text_value().to_int(valid);
if (valid) {
m_editor->set_position(new_offset);
}
auto new_offset = input_box->text_value().to_int();
if (new_offset.has_value())
m_editor->set_position(new_offset.value());
}
});

View file

@ -267,11 +267,10 @@ void IRCClient::handle(const Message& msg)
}
#endif
bool is_numeric;
int numeric = msg.command.to_uint(is_numeric);
auto numeric = msg.command.to_uint();
if (is_numeric) {
switch (numeric) {
if (numeric.has_value()) {
switch (numeric.value()) {
case RPL_WELCOME:
return handle_rpl_welcome(msg);
case RPL_WHOISCHANNELS:
@ -798,10 +797,9 @@ void IRCClient::handle_rpl_topicwhotime(const Message& msg)
auto& channel_name = msg.arguments[1];
auto& nick = msg.arguments[2];
auto setat = msg.arguments[3];
bool ok;
time_t setat_time = setat.to_uint(ok);
if (ok)
setat = Core::DateTime::from_timestamp(setat_time).to_string();
auto setat_time = setat.to_uint();
if (setat_time.has_value())
setat = Core::DateTime::from_timestamp(setat_time.value()).to_string();
ensure_channel(channel_name).add_message(String::format("*** (set by %s at %s)", nick.characters(), setat.characters()), Color::Blue);
}