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

@ -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

View file

@ -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

View file

@ -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;

View file

@ -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);
}

View file

@ -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));