mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:47:34 +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:
parent
15f4043a7a
commit
fdfda6dec2
55 changed files with 354 additions and 455 deletions
|
@ -124,14 +124,13 @@ next_entry:
|
|||
auto& e_passwd = parts[1];
|
||||
auto& e_gid_string = parts[2];
|
||||
auto& e_members_string = parts[3];
|
||||
bool ok;
|
||||
gid_t e_gid = e_gid_string.to_uint(ok);
|
||||
if (!ok) {
|
||||
auto e_gid = e_gid_string.to_uint();
|
||||
if (!e_gid.has_value()) {
|
||||
fprintf(stderr, "getgrent(): Malformed GID on line %u\n", __grdb_line_number);
|
||||
goto next_entry;
|
||||
}
|
||||
auto members = e_members_string.split(',');
|
||||
__grdb_entry->gr_gid = e_gid;
|
||||
__grdb_entry->gr_gid = e_gid.value();
|
||||
__grdb_entry->gr_name = __grdb_entry->name_buffer;
|
||||
__grdb_entry->gr_passwd = __grdb_entry->passwd_buffer;
|
||||
for (size_t i = 0; i < members.size(); ++i) {
|
||||
|
|
|
@ -383,12 +383,11 @@ static bool fill_getserv_buffers(char* line, ssize_t read)
|
|||
perror("malformed services file: port/protocol");
|
||||
return false;
|
||||
}
|
||||
bool conversion_checker;
|
||||
__getserv_port_buffer = port_protocol_split[0].to_int(conversion_checker);
|
||||
|
||||
if (!conversion_checker) {
|
||||
auto number = port_protocol_split[0].to_int();
|
||||
if (!number.has_value())
|
||||
return false;
|
||||
}
|
||||
|
||||
__getserv_port_buffer = number.value();
|
||||
|
||||
//Removing any annoying whitespace at the end of the protocol.
|
||||
port_protocol_split[1].replace(" ", "", true);
|
||||
|
@ -571,12 +570,11 @@ static bool fill_getproto_buffers(char* line, ssize_t read)
|
|||
return false;
|
||||
}
|
||||
|
||||
bool conversion_checker;
|
||||
__getproto_protocol_buffer = split_line[1].to_int(conversion_checker);
|
||||
|
||||
if (!conversion_checker) {
|
||||
auto number = split_line[1].to_int();
|
||||
if (!number.has_value())
|
||||
return false;
|
||||
}
|
||||
|
||||
__getproto_protocol_buffer = number.value();
|
||||
|
||||
__getproto_alias_list_buffer.clear();
|
||||
|
||||
|
|
|
@ -128,19 +128,18 @@ next_entry:
|
|||
auto& e_gecos = parts[4];
|
||||
auto& e_dir = parts[5];
|
||||
auto& e_shell = parts[6];
|
||||
bool ok;
|
||||
uid_t e_uid = e_uid_string.to_uint(ok);
|
||||
if (!ok) {
|
||||
auto e_uid = e_uid_string.to_uint();
|
||||
if (!e_uid.has_value()) {
|
||||
fprintf(stderr, "getpwent(): Malformed UID on line %u\n", __pwdb_line_number);
|
||||
goto next_entry;
|
||||
}
|
||||
gid_t e_gid = e_gid_string.to_uint(ok);
|
||||
if (!ok) {
|
||||
auto e_gid = e_gid_string.to_uint();
|
||||
if (!e_gid.has_value()) {
|
||||
fprintf(stderr, "getpwent(): Malformed GID on line %u\n", __pwdb_line_number);
|
||||
goto next_entry;
|
||||
}
|
||||
__pwdb_entry->pw_uid = e_uid;
|
||||
__pwdb_entry->pw_gid = e_gid;
|
||||
__pwdb_entry->pw_uid = e_uid.value();
|
||||
__pwdb_entry->pw_gid = e_gid.value();
|
||||
__pwdb_entry->pw_name = __pwdb_entry->name_buffer;
|
||||
__pwdb_entry->pw_passwd = __pwdb_entry->passwd_buffer;
|
||||
__pwdb_entry->pw_gecos = __pwdb_entry->gecos_buffer;
|
||||
|
|
|
@ -280,9 +280,9 @@ void ArgsParser::add_option(int& value, const char* help_string, const char* lon
|
|||
short_name,
|
||||
value_name,
|
||||
[&value](const char* s) {
|
||||
bool ok;
|
||||
value = StringView(s).to_int(ok);
|
||||
return ok;
|
||||
auto opt = StringView(s).to_int();
|
||||
value = opt.value_or(0);
|
||||
return opt.has_value();
|
||||
}
|
||||
};
|
||||
add_option(move(option));
|
||||
|
@ -316,9 +316,9 @@ void ArgsParser::add_positional_argument(int& value, const char* help_string, co
|
|||
required == Required::Yes ? 1 : 0,
|
||||
1,
|
||||
[&value](const char* s) {
|
||||
bool ok;
|
||||
value = StringView(s).to_int(ok);
|
||||
return ok;
|
||||
auto opt = StringView(s).to_int();
|
||||
value = opt.value_or(0);
|
||||
return opt.has_value();
|
||||
}
|
||||
};
|
||||
add_positional_argument(move(arg));
|
||||
|
|
|
@ -129,11 +129,7 @@ int ConfigFile::read_num_entry(const String& group, const String& key, int defau
|
|||
return default_value;
|
||||
}
|
||||
|
||||
bool ok;
|
||||
int value = read_entry(group, key).to_int(ok);
|
||||
if (!ok)
|
||||
return default_value;
|
||||
return value;
|
||||
return read_entry(group, key).to_int().value_or(default_value);
|
||||
}
|
||||
|
||||
bool ConfigFile::read_bool_entry(const String& group, const String& key, bool default_value) const
|
||||
|
|
|
@ -35,10 +35,9 @@ SpinBox::SpinBox()
|
|||
m_editor = add<TextBox>();
|
||||
m_editor->set_text("0");
|
||||
m_editor->on_change = [this] {
|
||||
bool ok;
|
||||
int value = m_editor->text().to_uint(ok);
|
||||
if (ok)
|
||||
set_value(value);
|
||||
auto value = m_editor->text().to_uint();
|
||||
if (value.has_value())
|
||||
set_value(value.value());
|
||||
else
|
||||
m_editor->set_text(String::number(m_value));
|
||||
};
|
||||
|
|
|
@ -91,10 +91,9 @@ void TextEditor::create_actions()
|
|||
auto input_box = InputBox::construct("Line:", "Go to line", window());
|
||||
auto result = input_box->exec();
|
||||
if (result == InputBox::ExecOK) {
|
||||
bool ok;
|
||||
auto line_number = input_box->text_value().to_uint(ok);
|
||||
if (ok)
|
||||
set_cursor(line_number - 1, 0);
|
||||
auto line_number = input_box->text_value().to_uint();
|
||||
if (line_number.has_value())
|
||||
set_cursor(line_number.value() - 1, 0);
|
||||
}
|
||||
},
|
||||
this);
|
||||
|
|
|
@ -158,13 +158,8 @@ public:
|
|||
ASSERT(as_uint() <= INT32_MAX);
|
||||
return (int)as_uint();
|
||||
}
|
||||
if (is_string()) {
|
||||
bool ok;
|
||||
int value = as_string().to_int(ok);
|
||||
if (!ok)
|
||||
return 0;
|
||||
return value;
|
||||
}
|
||||
if (is_string())
|
||||
return as_string().to_int().value_or(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -77,13 +77,13 @@ void Job::on_socket_connected()
|
|||
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
|
||||
bool ok;
|
||||
m_status = parts[0].to_uint(ok);
|
||||
if (!ok) {
|
||||
auto status = parts[0].to_uint();
|
||||
if (!status.has_value()) {
|
||||
fprintf(stderr, "Job: Expected numeric status code\n");
|
||||
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
|
||||
m_status = status.value();
|
||||
m_meta = parts[1];
|
||||
|
||||
if (m_status >= 10 && m_status < 20) {
|
||||
|
|
|
@ -139,22 +139,11 @@ static Optional<Color> parse_rgb_color(const StringView& string)
|
|||
if (parts.size() != 3)
|
||||
return {};
|
||||
|
||||
bool ok;
|
||||
auto r = parts[0].to_int(ok);
|
||||
if (!ok)
|
||||
return {};
|
||||
auto g = parts[1].to_int(ok);
|
||||
if (!ok)
|
||||
return {};
|
||||
auto b = parts[2].to_int(ok);
|
||||
if (!ok)
|
||||
return {};
|
||||
auto r = parts[0].to_uint().value_or(256);
|
||||
auto g = parts[1].to_uint().value_or(256);
|
||||
auto b = parts[2].to_uint().value_or(256);
|
||||
|
||||
if (r < 0 || r > 255)
|
||||
return {};
|
||||
if (g < 0 || g > 255)
|
||||
return {};
|
||||
if (b < 0 || b > 255)
|
||||
if (r > 255 || g > 255 || b > 255)
|
||||
return {};
|
||||
|
||||
return Color(r, g, b);
|
||||
|
@ -171,27 +160,14 @@ static Optional<Color> parse_rgba_color(const StringView& string)
|
|||
if (parts.size() != 4)
|
||||
return {};
|
||||
|
||||
bool ok;
|
||||
auto r = parts[0].to_int(ok);
|
||||
if (!ok)
|
||||
return {};
|
||||
auto g = parts[1].to_int(ok);
|
||||
if (!ok)
|
||||
return {};
|
||||
auto b = parts[2].to_int(ok);
|
||||
if (!ok)
|
||||
return {};
|
||||
auto r = parts[0].to_int().value_or(256);
|
||||
auto g = parts[1].to_int().value_or(256);
|
||||
auto b = parts[2].to_int().value_or(256);
|
||||
|
||||
double alpha = strtod(parts[3].to_string().characters(), nullptr);
|
||||
int a = alpha * 255;
|
||||
unsigned a = alpha * 255;
|
||||
|
||||
if (r < 0 || r > 255)
|
||||
return {};
|
||||
if (g < 0 || g > 255)
|
||||
return {};
|
||||
if (b < 0 || b > 255)
|
||||
return {};
|
||||
if (a < 0 || a > 255)
|
||||
if (r > 255 || g > 255 || b > 255 || a > 255)
|
||||
return {};
|
||||
|
||||
return Color(r, g, b, a);
|
||||
|
|
|
@ -108,12 +108,12 @@ void Job::on_socket_connected()
|
|||
fprintf(stderr, "Job: Expected 3-part HTTP status, got '%s'\n", line.data());
|
||||
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
bool ok;
|
||||
m_code = parts[1].to_uint(ok);
|
||||
if (!ok) {
|
||||
auto code = parts[1].to_uint();
|
||||
if (!code.has_value()) {
|
||||
fprintf(stderr, "Job: Expected numeric HTTP status\n");
|
||||
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
m_code = code.value();
|
||||
m_state = State::InHeaders;
|
||||
return;
|
||||
}
|
||||
|
@ -266,10 +266,9 @@ void Job::on_socket_connected()
|
|||
Optional<u32> content_length {};
|
||||
|
||||
if (content_length_header.has_value()) {
|
||||
bool ok;
|
||||
auto length = content_length_header.value().to_uint(ok);
|
||||
if (ok)
|
||||
content_length = length;
|
||||
auto length = content_length_header.value().to_uint();
|
||||
if (length.has_value())
|
||||
content_length = length.value();
|
||||
}
|
||||
|
||||
deferred_invoke([this, content_length](auto&) { did_progress(content_length, m_received_size); });
|
||||
|
|
|
@ -410,9 +410,8 @@ bool Object::define_property(PropertyName property_name, Value value, PropertyAt
|
|||
{
|
||||
if (property_name.is_number())
|
||||
return put_own_property_by_index(*this, property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
|
||||
bool ok;
|
||||
i32 property_index = property_name.as_string().to_int(ok);
|
||||
if (ok && property_index >= 0)
|
||||
i32 property_index = property_name.as_string().to_int().value_or(-1);
|
||||
if (property_index >= 0)
|
||||
return put_own_property_by_index(*this, property_index, value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
|
||||
return put_own_property(*this, property_name.as_string(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
|
||||
}
|
||||
|
@ -543,9 +542,8 @@ Value Object::delete_property(PropertyName property_name)
|
|||
ASSERT(property_name.is_valid());
|
||||
if (property_name.is_number())
|
||||
return Value(m_indexed_properties.remove(property_name.as_number()));
|
||||
bool ok;
|
||||
int property_index = property_name.as_string().to_int(ok);
|
||||
if (ok && property_index >= 0)
|
||||
int property_index = property_name.as_string().to_int().value_or(-1);
|
||||
if (property_index >= 0)
|
||||
return Value(m_indexed_properties.remove(property_name.as_number()));
|
||||
|
||||
auto metadata = shape().lookup(property_name.as_string());
|
||||
|
@ -602,9 +600,8 @@ Value Object::get(PropertyName property_name) const
|
|||
return get_by_index(property_name.as_number());
|
||||
|
||||
auto property_string = property_name.to_string();
|
||||
bool ok;
|
||||
i32 property_index = property_string.to_int(ok);
|
||||
if (ok && property_index >= 0)
|
||||
i32 property_index = property_string.to_int().value_or(-1);
|
||||
if (property_index >= 0)
|
||||
return get_by_index(property_index);
|
||||
|
||||
const Object* object = this;
|
||||
|
@ -656,9 +653,8 @@ bool Object::put(PropertyName property_name, Value value)
|
|||
ASSERT(!value.is_empty());
|
||||
|
||||
auto property_string = property_name.to_string();
|
||||
bool ok;
|
||||
i32 property_index = property_string.to_int(ok);
|
||||
if (ok && property_index >= 0)
|
||||
i32 property_index = property_string.to_int().value_or(-1);
|
||||
if (property_index >= 0)
|
||||
return put_by_index(property_index, value);
|
||||
|
||||
// If there's a setter in the prototype chain, we go to the setter.
|
||||
|
@ -737,9 +733,8 @@ bool Object::has_own_property(PropertyName property_name) const
|
|||
if (property_name.is_number())
|
||||
return has_indexed_property(property_name.as_number());
|
||||
|
||||
bool ok;
|
||||
i32 property_index = property_name.as_string().to_int(ok);
|
||||
if (ok && property_index >= 0)
|
||||
i32 property_index = property_name.as_string().to_int().value_or(-1);
|
||||
if (property_index >= 0)
|
||||
return has_indexed_property(property_index);
|
||||
|
||||
return shape().lookup(property_name.as_string()).has_value();
|
||||
|
|
|
@ -1431,14 +1431,17 @@ Vector<size_t, 2> Editor::vt_dsr()
|
|||
|
||||
if (buf[0] == '\033' && buf[1] == '[') {
|
||||
auto parts = StringView(buf + 2, length - 3).split_view(';');
|
||||
bool ok;
|
||||
row = parts[0].to_int(ok);
|
||||
if (!ok) {
|
||||
auto row_opt = parts[0].to_int();
|
||||
if (!row_opt.has_value()) {
|
||||
dbg() << "Terminal DSR issue; received garbage row";
|
||||
} else {
|
||||
row = row_opt.value();
|
||||
}
|
||||
col = parts[1].to_int(ok);
|
||||
if (!ok) {
|
||||
auto col_opt = parts[1].to_int();
|
||||
if (!col_opt.has_value()) {
|
||||
dbg() << "Terminal DSR issue; received garbage col";
|
||||
} else {
|
||||
col = col_opt.value();
|
||||
}
|
||||
}
|
||||
return { row, col };
|
||||
|
|
|
@ -544,11 +544,8 @@ void Terminal::execute_xterm_command()
|
|||
auto param_string = String::copy(m_xterm_parameters);
|
||||
auto params = param_string.split(';', true);
|
||||
m_xterm_parameters.clear_with_capacity();
|
||||
for (auto& parampart : params) {
|
||||
bool ok;
|
||||
unsigned value = parampart.to_uint(ok);
|
||||
numeric_params.append(ok ? value : 0);
|
||||
}
|
||||
for (auto& parampart : params)
|
||||
numeric_params.append(parampart.to_uint().value_or(0));
|
||||
|
||||
while (params.size() < 3) {
|
||||
params.append(String::empty());
|
||||
|
@ -594,15 +591,14 @@ void Terminal::execute_escape_sequence(u8 final)
|
|||
}
|
||||
auto paramparts = String::copy(m_parameters).split(';');
|
||||
for (auto& parampart : paramparts) {
|
||||
bool ok;
|
||||
unsigned value = parampart.to_uint(ok);
|
||||
if (!ok) {
|
||||
auto value = parampart.to_uint();
|
||||
if (!value.has_value()) {
|
||||
// FIXME: Should we do something else?
|
||||
m_parameters.clear_with_capacity();
|
||||
m_intermediates.clear_with_capacity();
|
||||
return;
|
||||
}
|
||||
params.append(value);
|
||||
params.append(value.value());
|
||||
}
|
||||
|
||||
#if defined(TERMINAL_DEBUG)
|
||||
|
|
|
@ -47,22 +47,12 @@ HTMLCanvasElement::~HTMLCanvasElement()
|
|||
|
||||
int HTMLCanvasElement::requested_width() const
|
||||
{
|
||||
bool ok = false;
|
||||
unsigned width = attribute(HTML::AttributeNames::width).to_int(ok);
|
||||
if (ok)
|
||||
return width;
|
||||
|
||||
return 300;
|
||||
return attribute(HTML::AttributeNames::width).to_int().value_or(300);
|
||||
}
|
||||
|
||||
int HTMLCanvasElement::requested_height() const
|
||||
{
|
||||
bool ok = false;
|
||||
unsigned height = attribute(HTML::AttributeNames::height).to_int(ok);
|
||||
if (ok)
|
||||
return height;
|
||||
|
||||
return 150;
|
||||
return attribute(HTML::AttributeNames::height).to_int().value_or(150);
|
||||
}
|
||||
|
||||
RefPtr<LayoutNode> HTMLCanvasElement::create_layout_node(const StyleProperties* parent_style) const
|
||||
|
|
|
@ -122,28 +122,12 @@ void HTMLImageElement::animate()
|
|||
|
||||
int HTMLImageElement::preferred_width() const
|
||||
{
|
||||
bool ok = false;
|
||||
int width = attribute(HTML::AttributeNames::width).to_int(ok);
|
||||
if (ok)
|
||||
return width;
|
||||
|
||||
if (m_image_decoder)
|
||||
return m_image_decoder->width();
|
||||
|
||||
return 0;
|
||||
return attribute(HTML::AttributeNames::width).to_int().value_or(m_image_decoder ? m_image_decoder->width() : 0);
|
||||
}
|
||||
|
||||
int HTMLImageElement::preferred_height() const
|
||||
{
|
||||
bool ok = false;
|
||||
int height = attribute(HTML::AttributeNames::height).to_int(ok);
|
||||
if (ok)
|
||||
return height;
|
||||
|
||||
if (m_image_decoder)
|
||||
return m_image_decoder->height();
|
||||
|
||||
return 0;
|
||||
return attribute(HTML::AttributeNames::height).to_int().value_or(m_image_decoder ? m_image_decoder->height() : 0);
|
||||
}
|
||||
|
||||
RefPtr<LayoutNode> HTMLImageElement::create_layout_node(const StyleProperties* parent_style) const
|
||||
|
|
|
@ -85,10 +85,9 @@ RefPtr<LayoutNode> HTMLInputElement::create_layout_node(const StyleProperties*)
|
|||
int text_width = Gfx::Font::default_font().width(value());
|
||||
auto size_value = attribute(HTML::AttributeNames::size);
|
||||
if (!size_value.is_null()) {
|
||||
bool ok;
|
||||
auto size = size_value.to_int(ok);
|
||||
if (ok && size >= 0)
|
||||
text_width = Gfx::Font::default_font().glyph_width('x') * size;
|
||||
auto size = size_value.to_uint();
|
||||
if (size.has_value())
|
||||
text_width = Gfx::Font::default_font().glyph_width('x') * size.value();
|
||||
}
|
||||
text_box.set_relative_rect(0, 0, text_width + 20, 20);
|
||||
widget = text_box;
|
||||
|
|
|
@ -53,9 +53,8 @@ void LayoutFrame::layout(LayoutMode layout_mode)
|
|||
set_has_intrinsic_width(true);
|
||||
set_has_intrinsic_height(true);
|
||||
// FIXME: Do proper error checking, etc.
|
||||
bool ok;
|
||||
set_intrinsic_width(node().attribute(HTML::AttributeNames::width).to_int(ok));
|
||||
set_intrinsic_height(node().attribute(HTML::AttributeNames::height).to_int(ok));
|
||||
set_intrinsic_width(node().attribute(HTML::AttributeNames::width).to_int().value_or(300));
|
||||
set_intrinsic_height(node().attribute(HTML::AttributeNames::height).to_int().value_or(150));
|
||||
|
||||
LayoutReplaced::layout(layout_mode);
|
||||
}
|
||||
|
|
|
@ -266,18 +266,17 @@ static bool parse_html_document(const StringView& html, Document& document, Pare
|
|||
}
|
||||
|
||||
if (j < 7) { // We found ; char
|
||||
bool ok;
|
||||
u32 codepoint;
|
||||
Optional<u32> codepoint;
|
||||
String str_code_point = html.substring_view(i + 2, j - 2);
|
||||
if (str_code_point.starts_with('x')) {
|
||||
String str = str_code_point.substring(1, str_code_point.length() - 1);
|
||||
codepoint = AK::StringUtils::convert_to_uint_from_hex(str, ok);
|
||||
codepoint = AK::StringUtils::convert_to_uint_from_hex(str);
|
||||
} else {
|
||||
codepoint = str_code_point.to_uint(ok);
|
||||
codepoint = str_code_point.to_uint();
|
||||
}
|
||||
|
||||
if (ok) {
|
||||
Vector<char> bytes = codepoint_to_bytes(codepoint);
|
||||
if (codepoint.has_value()) {
|
||||
Vector<char> bytes = codepoint_to_bytes(codepoint.value());
|
||||
if (bytes.size() > 0) {
|
||||
for (size_t i = 0; i < bytes.size(); i++) {
|
||||
text_buffer.append(bytes.at(i));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue