summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--AK/FlyString.cpp5
-rw-r--r--AK/FlyString.h2
-rw-r--r--AK/IPv4Address.h17
-rw-r--r--AK/JsonParser.cpp33
-rw-r--r--AK/String.cpp8
-rw-r--r--AK/String.h4
-rw-r--r--AK/StringUtils.cpp53
-rw-r--r--AK/StringUtils.h6
-rw-r--r--AK/StringView.cpp8
-rw-r--r--AK/StringView.h4
-rw-r--r--AK/Tests/TestQueue.cpp3
-rw-r--r--AK/Tests/TestString.cpp5
-rw-r--r--AK/Tests/TestStringUtils.cpp105
-rw-r--r--AK/URL.cpp6
-rw-r--r--AK/URLParser.cpp6
-rw-r--r--Applications/Debugger/main.cpp13
-rw-r--r--Applications/DisplaySettings/DisplaySettings.cpp14
-rw-r--r--Applications/HexEditor/HexEditorWidget.cpp15
-rw-r--r--Applications/IRCClient/IRCClient.cpp14
-rw-r--r--DevTools/HackStudio/Debugger/VariablesModel.cpp7
-rw-r--r--DevTools/IPCCompiler/main.cpp4
-rw-r--r--DevTools/Inspector/main.cpp7
-rw-r--r--Kernel/FileSystem/DevPtsFS.cpp7
-rw-r--r--Kernel/FileSystem/ProcFS.cpp40
-rw-r--r--Kernel/init.cpp21
-rw-r--r--Libraries/LibC/grp.cpp7
-rw-r--r--Libraries/LibC/netdb.cpp18
-rw-r--r--Libraries/LibC/pwd.cpp13
-rw-r--r--Libraries/LibCore/ArgsParser.cpp12
-rw-r--r--Libraries/LibCore/ConfigFile.cpp6
-rw-r--r--Libraries/LibGUI/SpinBox.cpp7
-rw-r--r--Libraries/LibGUI/TextEditor.cpp7
-rw-r--r--Libraries/LibGUI/Variant.h9
-rw-r--r--Libraries/LibGemini/Job.cpp6
-rw-r--r--Libraries/LibGfx/Color.cpp42
-rw-r--r--Libraries/LibHTTP/Job.cpp13
-rw-r--r--Libraries/LibJS/Runtime/Object.cpp25
-rw-r--r--Libraries/LibLine/Editor.cpp13
-rw-r--r--Libraries/LibVT/Terminal.cpp14
-rw-r--r--Libraries/LibWeb/DOM/HTMLCanvasElement.cpp14
-rw-r--r--Libraries/LibWeb/DOM/HTMLImageElement.cpp20
-rw-r--r--Libraries/LibWeb/DOM/HTMLInputElement.cpp7
-rw-r--r--Libraries/LibWeb/Layout/LayoutFrame.cpp5
-rw-r--r--Libraries/LibWeb/Parser/HTMLParser.cpp11
-rw-r--r--Shell/Shell.cpp7
-rw-r--r--Userland/allocate.cpp11
-rw-r--r--Userland/chgrp.cpp8
-rw-r--r--Userland/chown.cpp13
-rw-r--r--Userland/cut.cpp42
-rw-r--r--Userland/date.cpp7
-rw-r--r--Userland/find.cpp24
-rw-r--r--Userland/kill.cpp12
-rw-r--r--Userland/killall.cpp6
-rw-r--r--Userland/pidof.cpp17
-rw-r--r--Userland/truncate.cpp6
55 files changed, 354 insertions, 455 deletions
diff --git a/AK/FlyString.cpp b/AK/FlyString.cpp
index 54f25cc2d9..ce9f4c7f1f 100644
--- a/AK/FlyString.cpp
+++ b/AK/FlyString.cpp
@@ -26,6 +26,7 @@
#include <AK/FlyString.h>
#include <AK/HashTable.h>
+#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringUtils.h>
#include <AK/StringView.h>
@@ -88,9 +89,9 @@ FlyString::FlyString(const char* string)
{
}
-int FlyString::to_int(bool& ok) const
+Optional<int> FlyString::to_int() const
{
- return StringUtils::convert_to_int(view(), ok);
+ return StringUtils::convert_to_int(view());
}
bool FlyString::equals_ignoring_case(const StringView& other) const
diff --git a/AK/FlyString.h b/AK/FlyString.h
index 19ddd37f72..239dafe5cf 100644
--- a/AK/FlyString.h
+++ b/AK/FlyString.h
@@ -82,7 +82,7 @@ public:
FlyString to_lowercase() const;
- int to_int(bool& ok) const;
+ Optional<int> to_int() const;
bool equals_ignoring_case(const StringView&) const;
bool ends_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
diff --git a/AK/IPv4Address.h b/AK/IPv4Address.h
index aee493035f..810c9013a4 100644
--- a/AK/IPv4Address.h
+++ b/AK/IPv4Address.h
@@ -78,18 +78,11 @@ public:
auto parts = string.split_view('.');
if (parts.size() != 4)
return {};
- bool ok;
- auto a = parts[0].to_uint(ok);
- if (!ok || a > 255)
- return {};
- auto b = parts[1].to_uint(ok);
- if (!ok || b > 255)
- return {};
- auto c = parts[2].to_uint(ok);
- if (!ok || c > 255)
- return {};
- auto d = parts[3].to_uint(ok);
- if (!ok || d > 255)
+ auto a = parts[0].to_uint().value_or(256);
+ auto b = parts[1].to_uint().value_or(256);
+ auto c = parts[2].to_uint().value_or(256);
+ auto d = parts[3].to_uint().value_or(256);
+ if (a > 255 || b > 255 || c > 255 || d > 255)
return {};
return IPv4Address((u8)a, (u8)b, (u8)c, (u8)d);
}
diff --git a/AK/JsonParser.cpp b/AK/JsonParser.cpp
index dcf23ece38..7a883a8b3a 100644
--- a/AK/JsonParser.cpp
+++ b/AK/JsonParser.cpp
@@ -123,10 +123,9 @@ String JsonParser::consume_quoted_string()
sb.append(consume());
sb.append(consume());
- bool ok;
- u32 codepoint = AK::StringUtils::convert_to_uint_from_hex(sb.to_string(), ok);
- if (ok && codepoint < 128) {
- buffer.append((char)codepoint);
+ auto codepoint = AK::StringUtils::convert_to_uint_from_hex(sb.to_string());
+ if (codepoint.has_value() && codepoint.value() < 0x80) {
+ buffer.append((char)codepoint.value());
} else {
// FIXME: This is obviously not correct, but we don't have non-ASCII support so meh.
buffer.append('?');
@@ -202,7 +201,6 @@ JsonValue JsonParser::parse_string()
JsonValue JsonParser::parse_number()
{
- bool ok;
JsonValue value;
Vector<char, 128> number_buffer;
Vector<char, 128> fraction_buffer;
@@ -231,14 +229,17 @@ JsonValue JsonParser::parse_number()
#ifndef KERNEL
if (is_double) {
- int whole = number_string.to_uint(ok);
- if (!ok)
- whole = number_string.to_int(ok);
- ASSERT(ok);
+ // FIXME: This logic looks shaky.
+ int whole = 0;
+ auto to_signed_result = number_string.to_uint();
+ if (to_signed_result.has_value()) {
+ whole = to_signed_result.value();
+ } else {
+ whole = number_string.to_int().value();
+ }
- int fraction = fraction_string.to_uint(ok);
+ int fraction = fraction_string.to_uint().value();
fraction *= (whole < 0) ? -1 : 1;
- ASSERT(ok);
auto divider = 1;
for (size_t i = 0; i < fraction_buffer.size(); ++i) {
@@ -247,10 +248,12 @@ JsonValue JsonParser::parse_number()
value = JsonValue((double)whole + ((double)fraction / divider));
} else {
#endif
- value = JsonValue(number_string.to_uint(ok));
- if (!ok)
- value = JsonValue(number_string.to_int(ok));
- ASSERT(ok);
+ auto to_unsigned_result = number_string.to_uint();
+ if (to_unsigned_result.has_value()) {
+ value = JsonValue(to_unsigned_result.value());
+ } else {
+ value = JsonValue(number_string.to_int().value());
+ }
#ifndef KERNEL
}
#endif
diff --git a/AK/String.cpp b/AK/String.cpp
index e8dc9f9c05..0a2a05d13b 100644
--- a/AK/String.cpp
+++ b/AK/String.cpp
@@ -196,14 +196,14 @@ ByteBuffer String::to_byte_buffer() const
return ByteBuffer::copy(reinterpret_cast<const u8*>(characters()), length());
}
-int String::to_int(bool& ok) const
+Optional<int> String::to_int() const
{
- return StringUtils::convert_to_int(this->view(), ok);
+ return StringUtils::convert_to_int(view());
}
-unsigned String::to_uint(bool& ok) const
+Optional<unsigned> String::to_uint() const
{
- return StringUtils::convert_to_uint(this->view(), ok);
+ return StringUtils::convert_to_uint(view());
}
String String::number(unsigned long long value)
diff --git a/AK/String.h b/AK/String.h
index 6f99be9b5b..9432c18de6 100644
--- a/AK/String.h
+++ b/AK/String.h
@@ -108,8 +108,8 @@ public:
static String repeated(char, size_t count);
bool matches(const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
- int to_int(bool& ok) const;
- unsigned to_uint(bool& ok) const;
+ Optional<int> to_int() const;
+ Optional<unsigned> to_uint() const;
String to_lowercase() const;
String to_uppercase() const;
diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp
index ee6b2a2962..ae5f9676d5 100644
--- a/AK/StringUtils.cpp
+++ b/AK/StringUtils.cpp
@@ -26,6 +26,7 @@
*/
#include <AK/Memory.h>
+#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringUtils.h>
#include <AK/StringView.h>
@@ -87,69 +88,54 @@ bool matches(const StringView& str, const StringView& mask, CaseSensitivity case
return (mask_ptr == mask_end) && string_ptr == string_end;
}
-int convert_to_int(const StringView& str, bool& ok)
+Optional<int> convert_to_int(const StringView& str)
{
- if (str.is_empty()) {
- ok = false;
- return 0;
- }
+ if (str.is_empty())
+ return {};
bool negative = false;
size_t i = 0;
const auto characters = str.characters_without_null_termination();
if (characters[0] == '-' || characters[0] == '+') {
- if (str.length() == 1) {
- ok = false;
- return 0;
- }
+ if (str.length() == 1)
+ return {};
i++;
negative = (characters[0] == '-');
}
int value = 0;
for (; i < str.length(); i++) {
- if (characters[i] < '0' || characters[i] > '9') {
- ok = false;
- return 0;
- }
+ if (characters[i] < '0' || characters[i] > '9')
+ return {};
value = value * 10;
value += characters[i] - '0';
}
- ok = true;
-
return negative ? -value : value;
}
-unsigned convert_to_uint(const StringView& str, bool& ok)
+Optional<unsigned> convert_to_uint(const StringView& str)
{
- if (str.is_empty()) {
- ok = false;
- return 0;
- }
+ if (str.is_empty())
+ return {};
unsigned value = 0;
const auto characters = str.characters_without_null_termination();
for (size_t i = 0; i < str.length(); i++) {
- if (characters[i] < '0' || characters[i] > '9') {
- ok = false;
- return 0;
- }
+ if (characters[i] < '0' || characters[i] > '9')
+ return {};
+
value = value * 10;
value += characters[i] - '0';
}
- ok = true;
-
return value;
}
-unsigned convert_to_uint_from_hex(const StringView& str, bool& ok)
+Optional<unsigned> convert_to_uint_from_hex(const StringView& str)
{
- if (str.is_empty()) {
- ok = false;
- return 0;
- }
+ if (str.is_empty())
+ return {};
unsigned value = 0;
const auto count = str.length();
@@ -165,14 +151,11 @@ unsigned convert_to_uint_from_hex(const StringView& str, bool& ok)
} else if (digit >= 'A' && digit <= 'F') {
digit_val = 10 + (digit - 'A');
} else {
- ok = false;
- return 0;
+ return {};
}
value = (value << 4) + digit_val;
}
-
- ok = true;
return value;
}
diff --git a/AK/StringUtils.h b/AK/StringUtils.h
index e1326b10f6..fb5464c59e 100644
--- a/AK/StringUtils.h
+++ b/AK/StringUtils.h
@@ -39,9 +39,9 @@ enum class CaseSensitivity {
namespace StringUtils {
bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive);
-int convert_to_int(const StringView&, bool& ok);
-unsigned convert_to_uint(const StringView&, bool& ok);
-unsigned convert_to_uint_from_hex(const StringView&, bool& ok);
+Optional<int> convert_to_int(const StringView&);
+Optional<unsigned> convert_to_uint(const StringView&);
+Optional<unsigned> convert_to_uint_from_hex(const StringView&);
bool equals_ignoring_case(const StringView&, const StringView&);
bool ends_with(const StringView& a, const StringView& b, CaseSensitivity);
}
diff --git a/AK/StringView.cpp b/AK/StringView.cpp
index 48df207656..121b7f235d 100644
--- a/AK/StringView.cpp
+++ b/AK/StringView.cpp
@@ -215,14 +215,14 @@ StringView StringView::substring_view_starting_after_substring(const StringView&
return { remaining_characters, remaining_length };
}
-int StringView::to_int(bool& ok) const
+Optional<int> StringView::to_int() const
{
- return StringUtils::convert_to_int(*this, ok);
+ return StringUtils::convert_to_int(*this);
}
-unsigned StringView::to_uint(bool& ok) const
+Optional<unsigned> StringView::to_uint() const
{
- return StringUtils::convert_to_uint(*this, ok);
+ return StringUtils::convert_to_uint(*this);
}
unsigned StringView::hash() const
diff --git a/AK/StringView.h b/AK/StringView.h
index 1606dc66e1..2d05bea0a7 100644
--- a/AK/StringView.h
+++ b/AK/StringView.h
@@ -96,8 +96,8 @@ public:
// following newline.".
Vector<StringView> lines(bool consider_cr = true) const;
- int to_int(bool& ok) const;
- unsigned to_uint(bool& ok) const;
+ Optional<int> to_int() const;
+ Optional<unsigned> to_uint() const;
// Create a new substring view of this string view, starting either at the beginning of
// the given substring view, or after its end, and continuing until the end of this string
diff --git a/AK/Tests/TestQueue.cpp b/AK/Tests/TestQueue.cpp
index 7d198ce183..4ddaba9511 100644
--- a/AK/Tests/TestQueue.cpp
+++ b/AK/Tests/TestQueue.cpp
@@ -72,8 +72,7 @@ TEST_CASE(order)
}
for (int i = 0; i < 10000; ++i) {
- bool ok;
- EXPECT_EQ(strings.dequeue().to_int(ok), i);
+ EXPECT_EQ(strings.dequeue().to_int().value(), i);
}
EXPECT(strings.is_empty());
diff --git a/AK/Tests/TestString.cpp b/AK/Tests/TestString.cpp
index 9c8dd51d45..b3c70e6826 100644
--- a/AK/Tests/TestString.cpp
+++ b/AK/Tests/TestString.cpp
@@ -127,9 +127,8 @@ TEST_CASE(repeated)
TEST_CASE(to_int)
{
- bool ok;
- EXPECT(String("123").to_int(ok) == 123 && ok);
- EXPECT(String("-123").to_int(ok) == -123 && ok);
+ EXPECT_EQ(String("123").to_int().value(), 123);
+ EXPECT_EQ(String("-123").to_int().value(), -123);
}
TEST_CASE(to_lowercase)
diff --git a/AK/Tests/TestStringUtils.cpp b/AK/Tests/TestStringUtils.cpp
index 0fc2bd038f..ce17af6e6e 100644
--- a/AK/Tests/TestStringUtils.cpp
+++ b/AK/Tests/TestStringUtils.cpp
@@ -69,79 +69,88 @@ TEST_CASE(matches_case_insensitive)
TEST_CASE(convert_to_int)
{
- bool ok = false;
- AK::StringUtils::convert_to_int(StringView(), ok);
- EXPECT(!ok);
+ auto value = AK::StringUtils::convert_to_int(StringView());
+ EXPECT(!value.has_value());
- AK::StringUtils::convert_to_int("", ok);
- EXPECT(!ok);
+ AK::StringUtils::convert_to_int("");
+ EXPECT(!value.has_value());
- AK::StringUtils::convert_to_int("a", ok);
- EXPECT(!ok);
+ AK::StringUtils::convert_to_int("a");
+ EXPECT(!value.has_value());
- AK::StringUtils::convert_to_int("+", ok);
- EXPECT(!ok);
+ AK::StringUtils::convert_to_int("+");
+ EXPECT(!value.has_value());
- AK::StringUtils::convert_to_int("-", ok);
- EXPECT(!ok);
+ AK::StringUtils::convert_to_int("-");
+ EXPECT(!value.has_value());
- int actual = AK::StringUtils::convert_to_int("0", ok);
- EXPECT(ok && actual == 0);
+ auto actual = AK::StringUtils::convert_to_int("0");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), 0);
- actual = AK::StringUtils::convert_to_int("1", ok);
- EXPECT(ok && actual == 1);
+ actual = AK::StringUtils::convert_to_int("1");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), 1);
- actual = AK::StringUtils::convert_to_int("+1", ok);
- EXPECT(ok && actual == 1);
+ actual = AK::StringUtils::convert_to_int("+1");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), 1);
- actual = AK::StringUtils::convert_to_int("-1", ok);
- EXPECT(ok && actual == -1);
+ actual = AK::StringUtils::convert_to_int("-1");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), -1);
- actual = AK::StringUtils::convert_to_int("01", ok);
- EXPECT(ok && actual == 1);
+ actual = AK::StringUtils::convert_to_int("01");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), 1);
- actual = AK::StringUtils::convert_to_int("12345", ok);
- EXPECT(ok && actual == 12345);
+ actual = AK::StringUtils::convert_to_int("12345");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), 12345);
- actual = AK::StringUtils::convert_to_int("-12345", ok);
- EXPECT(ok && actual == -12345);
+ actual = AK::StringUtils::convert_to_int("-12345");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), -12345);
}
TEST_CASE(convert_to_uint)
{
- bool ok = false;
- AK::StringUtils::convert_to_uint(StringView(), ok);
- EXPECT(!ok);
+ auto value = AK::StringUtils::convert_to_uint(StringView());
+ EXPECT(!value.has_value());
- AK::StringUtils::convert_to_uint("", ok);
- EXPECT(!ok);
+ value = AK::StringUtils::convert_to_uint("");
+ EXPECT(!value.has_value());
- AK::StringUtils::convert_to_uint("a", ok);
- EXPECT(!ok);
+ value = AK::StringUtils::convert_to_uint("a");
+ EXPECT(!value.has_value());
- AK::StringUtils::convert_to_uint("+", ok);
- EXPECT(!ok);
+ value = AK::StringUtils::convert_to_uint("+");
+ EXPECT(!value.has_value());
- AK::StringUtils::convert_to_uint("-", ok);
- EXPECT(!ok);
+ value = AK::StringUtils::convert_to_uint("-");
+ EXPECT(!value.has_value());
- AK::StringUtils::convert_to_uint("+1", ok);
- EXPECT(!ok);
+ value = AK::StringUtils::convert_to_uint("+1");
+ EXPECT(!value.has_value());
- AK::StringUtils::convert_to_uint("-1", ok);
- EXPECT(!ok);
+ AK::StringUtils::convert_to_uint("-1");
+ EXPECT(!value.has_value());
- unsigned actual = AK::StringUtils::convert_to_uint("0", ok);
- EXPECT(ok && actual == 0u);
+ auto actual = AK::StringUtils::convert_to_uint("0");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), 0u);
- actual = AK::StringUtils::convert_to_uint("1", ok);
- EXPECT(ok && actual == 1u);
+ actual = AK::StringUtils::convert_to_uint("1");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), 1u);
- actual = AK::StringUtils::convert_to_uint("01", ok);
- EXPECT(ok && actual == 1u);
+ actual = AK::StringUtils::convert_to_uint("01");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), 1u);
- actual = AK::StringUtils::convert_to_uint("12345", ok);
- EXPECT(ok && actual == 12345u);
+ actual = AK::StringUtils::convert_to_uint("12345");
+ EXPECT_EQ(actual.has_value(), true);
+ EXPECT_EQ(actual.value(), 12345u);
}
TEST_CASE(ends_with)
diff --git a/AK/URL.cpp b/AK/URL.cpp
index 4278ea9ceb..424318c0f3 100644
--- a/AK/URL.cpp
+++ b/AK/URL.cpp
@@ -152,11 +152,11 @@ bool URL::parse(const StringView& string)
if (buffer.is_empty())
return false;
{
- bool ok;
- m_port = String::copy(buffer).to_uint(ok);
+ auto port_opt = String::copy(buffer).to_uint();
buffer.clear();
- if (!ok)
+ if (!port_opt.has_value())
return false;
+ m_port = port_opt.value();
}
if (peek() == '/') {
state = State::InPath;
diff --git a/AK/URLParser.cpp b/AK/URLParser.cpp
index 9d633d5310..1329272106 100644
--- a/AK/URLParser.cpp
+++ b/AK/URLParser.cpp
@@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/StringUtils.h>
@@ -60,9 +61,8 @@ String urldecode(const StringView& input)
builder.append(consume());
continue;
}
- bool ok;
- u8 byte_point = StringUtils::convert_to_uint_from_hex(input.substring_view(cursor + 1, 2), ok);
- builder.append(byte_point);
+ auto byte_point = StringUtils::convert_to_uint_from_hex(input.substring_view(cursor + 1, 2));
+ builder.append(byte_point.value());
consume();
consume();
consume();
diff --git a/Applications/Debugger/main.cpp b/Applications/Debugger/main.cpp
index 31f47046a2..367fe89001 100644
--- a/Applications/Debugger/main.cpp
+++ b/Applications/Debugger/main.cpp
@@ -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;
diff --git a/Applications/DisplaySettings/DisplaySettings.cpp b/Applications/DisplaySettings/DisplaySettings.cpp
index 303b20f8b3..96610be6f3 100644
--- a/Applications/DisplaySettings/DisplaySettings.cpp
+++ b/Applications/DisplaySettings/DisplaySettings.cpp
@@ -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);
diff --git a/Applications/HexEditor/HexEditorWidget.cpp b/Applications/HexEditor/HexEditorWidget.cpp
index 73bee7a7ef..f5c920af9e 100644
--- a/Applications/HexEditor/HexEditorWidget.cpp
+++ b/Applications/HexEditor/HexEditorWidget.cpp
@@ -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());
}
});
diff --git a/Applications/IRCClient/IRCClient.cpp b/Applications/IRCClient/IRCClient.cpp
index 947be679f3..6b078e281f 100644
--- a/Applications/IRCClient/IRCClient.cpp
+++ b/Applications/IRCClient/IRCClient.cpp
@@ -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);
}
diff --git a/DevTools/HackStudio/Debugger/VariablesModel.cpp b/DevTools/HackStudio/Debugger/VariablesModel.cpp
index b5351724f5..83aadc8c22 100644
--- a/DevTools/HackStudio/Debugger/VariablesModel.cpp
+++ b/DevTools/HackStudio/Debugger/VariablesModel.cpp
@@ -124,9 +124,10 @@ static Optional<u32> string_to_variable_value(const StringView& string_value, co
}
if (variable.type_name == "int") {
- bool success = false;
- auto value = string_value.to_int(success);
- return success ? value : Optional<u32>();
+ auto value = string_value.to_int();
+ if (value.has_value())
+ return value.value();
+ return {};
}
if (variable.type_name == "bool") {
diff --git a/DevTools/IPCCompiler/main.cpp b/DevTools/IPCCompiler/main.cpp
index 053e3a9611..1f2c353a13 100644
--- a/DevTools/IPCCompiler/main.cpp
+++ b/DevTools/IPCCompiler/main.cpp
@@ -227,9 +227,7 @@ int main(int argc, char** argv)
consume_specific('=');
consume_whitespace();
auto magic_string = extract_while([](char ch) { return !isspace(ch) && ch != '{'; });
- bool ok;
- endpoints.last().magic = magic_string.to_int(ok);
- ASSERT(ok);
+ endpoints.last().magic = magic_string.to_int().value();
consume_whitespace();
consume_specific('{');
parse_messages();
diff --git a/DevTools/Inspector/main.cpp b/DevTools/Inspector/main.cpp
index 38fe69f016..49cea918d9 100644
--- a/DevTools/Inspector/main.cpp
+++ b/DevTools/Inspector/main.cpp
@@ -65,11 +65,12 @@ int main(int argc, char** argv)
if (argc != 2)
print_usage_and_exit();
- bool ok;
- pid_t pid = String(argv[1]).to_int(ok);
- if (!ok)
+ auto pid_opt = String(argv[1]).to_int();
+ if (!pid_opt.has_value())
print_usage_and_exit();
+ pid_t pid = pid_opt.value();
+
GUI::Application app(argc, argv);
auto window = GUI::Window::construct();
diff --git a/Kernel/FileSystem/DevPtsFS.cpp b/Kernel/FileSystem/DevPtsFS.cpp
index cf0237584e..4b86dc6a1d 100644
--- a/Kernel/FileSystem/DevPtsFS.cpp
+++ b/Kernel/FileSystem/DevPtsFS.cpp
@@ -177,10 +177,9 @@ RefPtr<Inode> DevPtsFSInode::lookup(StringView name)
if (name == "." || name == "..")
return fs().get_inode(identifier());
- bool ok;
- unsigned pty_index = name.to_uint(ok);
- if (ok && ptys->contains(pty_index)) {
- return fs().get_inode({ fsid(), pty_index_to_inode_index(pty_index) });
+ auto pty_index = name.to_uint();
+ if (pty_index.has_value() && ptys->contains(pty_index.value())) {
+ return fs().get_inode({ fsid(), pty_index_to_inode_index(pty_index.value()) });
}
return {};
diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp
index 2f154871bd..8c5b3b9a01 100644
--- a/Kernel/FileSystem/ProcFS.cpp
+++ b/Kernel/FileSystem/ProcFS.cpp
@@ -1356,17 +1356,16 @@ RefPtr<Inode> ProcFSInode::lookup(StringView name)
}
}
}
- bool ok;
- unsigned name_as_number = name.to_uint(ok);
- if (ok) {
- bool process_exists = false;
- {
- InterruptDisabler disabler;
- process_exists = Process::from_pid(name_as_number);
- }
- if (process_exists)
- return fs().get_inode(to_identifier(fsid(), PDI_Root, name_as_number, FI_PID));
+ auto name_as_number = name.to_uint();
+ if (!name_as_number.has_value())
+ return {};
+ bool process_exists = false;
+ {
+ InterruptDisabler disabler;
+ process_exists = Process::from_pid(name_as_number.value());
}
+ if (process_exists)
+ return fs().get_inode(to_identifier(fsid(), PDI_Root, name_as_number.value(), FI_PID));
return {};
}
@@ -1413,18 +1412,17 @@ RefPtr<Inode> ProcFSInode::lookup(StringView name)
}
if (proc_file_type == FI_PID_fd) {
- bool ok;
- unsigned name_as_number = name.to_uint(ok);
- if (ok) {
- bool fd_exists = false;
- {
- InterruptDisabler disabler;
- if (auto* process = Process::from_pid(to_pid(identifier())))
- fd_exists = process->file_description(name_as_number);
- }
- if (fd_exists)
- return fs().get_inode(to_identifier_with_fd(fsid(), to_pid(identifier()), name_as_number));
+ auto name_as_number = name.to_uint();
+ if (!name_as_number.has_value())
+ return {};
+ bool fd_exists = false;
+ {
+ InterruptDisabler disabler;
+ if (auto* process = Process::from_pid(to_pid(identifier())))
+ fd_exists = process->file_description(name_as_number.value());
}
+ if (fd_exists)
+ return fs().get_inode(to_identifier_with_fd(fsid(), to_pid(identifier()), name_as_number.value()));
}
return {};
}
diff --git a/Kernel/init.cpp b/Kernel/init.cpp
index 0dacc83a6a..7e9006d8cd 100644
--- a/Kernel/init.cpp
+++ b/Kernel/init.cpp
@@ -251,10 +251,9 @@ void init_stage2()
root = root.substring(strlen("/dev/hda"), root.length() - strlen("/dev/hda"));
if (root.length()) {
- bool ok;
- unsigned partition_number = root.to_uint(ok);
+ auto partition_number = root.to_uint();
- if (!ok) {
+ if (!partition_number.has_value()) {
klog() << "init_stage2: couldn't parse partition number from root kernel parameter";
hang();
}
@@ -273,9 +272,9 @@ void init_stage2()
klog() << "init_stage2: couldn't read GPT from disk";
hang();
}
- auto partition = gpt.partition(partition_number);
+ auto partition = gpt.partition(partition_number.value());
if (!partition) {
- klog() << "init_stage2: couldn't get partition " << partition_number;
+ klog() << "init_stage2: couldn't get partition " << partition_number.value();
hang();
}
root_dev = *partition;
@@ -287,20 +286,20 @@ void init_stage2()
klog() << "init_stage2: couldn't read EBR from disk";
hang();
}
- auto partition = ebr.partition(partition_number);
+ auto partition = ebr.partition(partition_number.value());
if (!partition) {
- klog() << "init_stage2: couldn't get partition " << partition_number;
+ klog() << "init_stage2: couldn't get partition " << partition_number.value();
hang();
}
root_dev = *partition;
} else {
- if (partition_number < 1 || partition_number > 4) {
- klog() << "init_stage2: invalid partition number " << partition_number << "; expected 1 to 4";
+ if (partition_number.value() < 1 || partition_number.value() > 4) {
+ klog() << "init_stage2: invalid partition number " << partition_number.value() << "; expected 1 to 4";
hang();
}
- auto partition = mbr.partition(partition_number);
+ auto partition = mbr.partition(partition_number.value());
if (!partition) {
- klog() << "init_stage2: couldn't get partition " << partition_number;
+ klog() << "init_stage2: couldn't get partition " << partition_number.value();
hang();
}
root_dev = *partition;
diff --git a/Libraries/LibC/grp.cpp b/Libraries/LibC/grp.cpp
index f04e5d0545..8dc7e20c96 100644
--- a/Libraries/LibC/grp.cpp
+++ b/Libraries/LibC/grp.cpp
@@ -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) {
diff --git a/Libraries/LibC/netdb.cpp b/Libraries/LibC/netdb.cpp
index bc8aebc400..409e068da9 100644
--- a/Libraries/LibC/netdb.cpp
+++ b/Libraries/LibC/netdb.cpp
@@ -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();
diff --git a/Libraries/LibC/pwd.cpp b/Libraries/LibC/pwd.cpp
index 520a5fc8f5..1d32197b77 100644
--- a/Libraries/LibC/pwd.cpp
+++ b/Libraries/LibC/pwd.cpp
@@ -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;
diff --git a/Libraries/LibCore/ArgsParser.cpp b/Libraries/LibCore/ArgsParser.cpp
index 0a85ae9544..a256376b94 100644
--- a/Libraries/LibCore/ArgsParser.cpp
+++ b/Libraries/LibCore/ArgsParser.cpp
@@ -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));
diff --git a/Libraries/LibCore/ConfigFile.cpp b/Libraries/LibCore/ConfigFile.cpp
index 1f28ea53ae..25dfcf5b6a 100644
--- a/Libraries/LibCore/ConfigFile.cpp
+++ b/Libraries/LibCore/ConfigFile.cpp
@@ -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
diff --git a/Libraries/LibGUI/SpinBox.cpp b/Libraries/LibGUI/SpinBox.cpp
index 1537d1f646..86bd10cfbb 100644
--- a/Libraries/LibGUI/SpinBox.cpp
+++ b/Libraries/LibGUI/SpinBox.cpp
@@ -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));
};
diff --git a/Libraries/LibGUI/TextEditor.cpp b/Libraries/LibGUI/TextEditor.cpp
index 7370484c53..ef4f487714 100644
--- a/Libraries/LibGUI/TextEditor.cpp
+++ b/Libraries/LibGUI/TextEditor.cpp
@@ -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);
diff --git a/Libraries/LibGUI/Variant.h b/Libraries/LibGUI/Variant.h
index 7d0d5ea4fe..6aea78974c 100644
--- a/Libraries/LibGUI/Variant.h
+++ b/Libraries/LibGUI/Variant.h
@@ -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;
}
diff --git a/Libraries/LibGemini/Job.cpp b/Libraries/LibGemini/Job.cpp
index dc799f0c19..b4d0d4c10a 100644
--- a/Libraries/LibGemini/Job.cpp
+++ b/Libraries/LibGemini/Job.cpp
@@ -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) {
diff --git a/Libraries/LibGfx/Color.cpp b/Libraries/LibGfx/Color.cpp
index 3ababe371c..59f596ce21 100644
--- a/Libraries/LibGfx/Color.cpp
+++ b/Libraries/LibGfx/Color.cpp
@@ -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);
diff --git a/Libraries/LibHTTP/Job.cpp b/Libraries/LibHTTP/Job.cpp
index 64ad05f010..c1d5e8bae6 100644
--- a/Libraries/LibHTTP/Job.cpp
+++ b/Libraries/LibHTTP/Job.cpp
@@ -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); });
diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp
index ab79d1005c..92efb5ba4a 100644
--- a/Libraries/LibJS/Runtime/Object.cpp
+++ b/Libraries/LibJS/Runtime/Object.cpp
@@ -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();
diff --git a/Libraries/LibLine/Editor.cpp b/Libraries/LibLine/Editor.cpp
index f8600758db..f3728f67b8 100644
--- a/Libraries/LibLine/Editor.cpp
+++ b/Libraries/LibLine/Editor.cpp
@@ -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 };
diff --git a/Libraries/LibVT/Terminal.cpp b/Libraries/LibVT/Terminal.cpp
index 42c8dd7aec..0823730c81 100644
--- a/Libraries/LibVT/Terminal.cpp
+++ b/Libraries/LibVT/Terminal.cpp
@@ -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)
diff --git a/Libraries/LibWeb/DOM/HTMLCanvasElement.cpp b/Libraries/LibWeb/DOM/HTMLCanvasElement.cpp
index 46b518b214..4b7149082f 100644
--- a/Libraries/LibWeb/DOM/HTMLCanvasElement.cpp
+++ b/Libraries/LibWeb/DOM/HTMLCanvasElement.cpp
@@ -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
diff --git a/Libraries/LibWeb/DOM/HTMLImageElement.cpp b/Libraries/LibWeb/DOM/HTMLImageElement.cpp
index adeacc3cb0..414e5d4b76 100644
--- a/Libraries/LibWeb/DOM/HTMLImageElement.cpp
+++ b/Libraries/LibWeb/DOM/HTMLImageElement.cpp
@@ -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
diff --git a/Libraries/LibWeb/DOM/HTMLInputElement.cpp b/Libraries/LibWeb/DOM/HTMLInputElement.cpp
index 45b30f61c9..5523d9d287 100644
--- a/Libraries/LibWeb/DOM/HTMLInputElement.cpp
+++ b/Libraries/LibWeb/DOM/HTMLInputElement.cpp
@@ -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;
diff --git a/Libraries/LibWeb/Layout/LayoutFrame.cpp b/Libraries/LibWeb/Layout/LayoutFrame.cpp
index 7dbfb75730..94fe436125 100644
--- a/Libraries/LibWeb/Layout/LayoutFrame.cpp
+++ b/Libraries/LibWeb/Layout/LayoutFrame.cpp
@@ -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);
}
diff --git a/Libraries/LibWeb/Parser/HTMLParser.cpp b/Libraries/LibWeb/Parser/HTMLParser.cpp
index df87365c00..c22f5b301c 100644
--- a/Libraries/LibWeb/Parser/HTMLParser.cpp
+++ b/Libraries/LibWeb/Parser/HTMLParser.cpp
@@ -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));
diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp
index 876bf7ea64..ebd92596c7 100644
--- a/Shell/Shell.cpp
+++ b/Shell/Shell.cpp
@@ -463,10 +463,9 @@ int Shell::builtin_disown(int argc, const char** argv)
Vector<size_t> job_ids;
for (auto& job_id : str_job_ids) {
- bool ok;
- auto id = StringView { job_id }.to_uint(ok);
- if (ok)
- job_ids.append(id);
+ auto id = StringView(job_id).to_uint();
+ if (id.has_value())
+ job_ids.append(id.value());
else
printf("Invalid job id: %s\n", job_id);
}
diff --git a/Userland/allocate.cpp b/Userland/allocate.cpp
index dbda7e926c..12d81e44b0 100644
--- a/Userland/allocate.cpp
+++ b/Userland/allocate.cpp
@@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include <AK/Optional.h>
#include <AK/String.h>
#include <LibCore/ElapsedTimer.h>
#include <stdio.h>
@@ -36,7 +37,9 @@ void usage(void)
exit(1);
}
-enum Unit { Bytes, KiloBytes, MegaBytes };
+enum Unit { Bytes,
+ KiloBytes,
+ MegaBytes };
int main(int argc, char** argv)
{
@@ -44,11 +47,11 @@ int main(int argc, char** argv)
Unit unit = MegaBytes;
if (argc >= 2) {
- bool ok;
- count = String(argv[1]).to_uint(ok);
- if (!ok) {
+ auto number = String(argv[1]).to_uint();
+ if (!number.has_value()) {
usage();
}
+ count = number.value();
}
if (argc >= 3) {
diff --git a/Userland/chgrp.cpp b/Userland/chgrp.cpp
index e33ed746b9..d3e5538468 100644
--- a/Userland/chgrp.cpp
+++ b/Userland/chgrp.cpp
@@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include <AK/Optional.h>
#include <AK/String.h>
#include <grp.h>
#include <pwd.h>
@@ -52,10 +53,11 @@ int main(int argc, char** argv)
return 1;
}
- bool ok;
- new_gid = gid_arg.to_uint(ok);
+ auto number = gid_arg.to_uint();
- if (!ok) {
+ if (number.has_value()) {
+ new_gid = number.value();
+ } else {
auto* group = getgrnam(gid_arg.characters());
if (!group) {
fprintf(stderr, "Unknown group '%s'\n", gid_arg.characters());
diff --git a/Userland/chown.cpp b/Userland/chown.cpp
index a7ea33dbc5..54809aecd5 100644
--- a/Userland/chown.cpp
+++ b/Userland/chown.cpp
@@ -54,9 +54,10 @@ int main(int argc, char** argv)
return 1;
}
- bool ok;
- new_uid = parts[0].to_uint(ok);
- if (!ok) {
+ auto number = parts[0].to_uint();
+ if (number.has_value()) {
+ new_uid = number.value();
+ } else {
auto* passwd = getpwnam(parts[0].characters());
if (!passwd) {
fprintf(stderr, "Unknown user '%s'\n", parts[0].characters());
@@ -66,8 +67,10 @@ int main(int argc, char** argv)
}
if (parts.size() == 2) {
- new_gid = parts[1].to_uint(ok);
- if (!ok) {
+ auto number = parts[1].to_uint();
+ if (number.has_value()) {
+ new_gid = number.value();
+ } else {
auto* group = getgrnam(parts[1].characters());
if (!new_gid) {
fprintf(stderr, "Unknown group '%s'\n", parts[1].characters());
diff --git a/Userland/cut.cpp b/Userland/cut.cpp
index 3ce6a0a9cb..59010e87e6 100644
--- a/Userland/cut.cpp
+++ b/Userland/cut.cpp
@@ -89,74 +89,70 @@ static void expand_list(Vector<String>& tokens, Vector<Index>& indexes)
}
if (token[0] == '-') {
- bool ok = true;
- ssize_t index = token.substring(1, token.length() - 1).to_int(ok);
- if (!ok) {
+ auto index = token.substring(1, token.length() - 1).to_int();
+ if (!index.has_value()) {
fprintf(stderr, "cut: invalid byte/character position '%s'\n", token.characters());
print_usage_and_exit(1);
}
- if (index == 0) {
+ if (index.value() == 0) {
fprintf(stderr, "cut: byte/character positions are numbered from 1\n");
print_usage_and_exit(1);
}
- Index tmp = { 1, index, Index::Type::RangedIndex };
+ Index tmp = { 1, index.value(), Index::Type::RangedIndex };
add_if_not_exists(indexes, tmp);
} else if (token[token.length() - 1] == '-') {
- bool ok = true;
- ssize_t index = token.substring(0, token.length() - 1).to_int(ok);
- if (!ok) {
+ auto index = token.substring(0, token.length() - 1).to_int();
+ if (!index.has_value()) {
fprintf(stderr, "cut: invalid byte/character position '%s'\n", token.characters());
print_usage_and_exit(1);
}
- if (index == 0) {
+ if (index.value() == 0) {
fprintf(stderr, "cut: byte/character positions are numbered from 1\n");
print_usage_and_exit(1);
}
- Index tmp = { index, -1, Index::Type::SliceIndex };
+ Index tmp = { index.value(), -1, Index::Type::SliceIndex };
add_if_not_exists(indexes, tmp);
} else {
auto range = token.split('-');
if (range.size() == 2) {
- bool ok = true;
- ssize_t index1 = range[0].to_int(ok);
- if (!ok) {
+ auto index1 = range[0].to_int();
+ if (!index1.has_value()) {
fprintf(stderr, "cut: invalid byte/character position '%s'\n", range[0].characters());
print_usage_and_exit(1);
}
- ssize_t index2 = range[1].to_int(ok);
- if (!ok) {
+ auto index2 = range[1].to_int();
+ if (!index2.has_value()) {
fprintf(stderr, "cut: invalid byte/character position '%s'\n", range[1].characters());
print_usage_and_exit(1);
}
- if (index1 > index2) {
+ if (index1.value() > index2.value()) {
fprintf(stderr, "cut: invalid decreasing range\n");
print_usage_and_exit(1);
- } else if (index1 == 0 || index2 == 0) {
+ } else if (index1.value() == 0 || index2.value() == 0) {
fprintf(stderr, "cut: byte/character positions are numbered from 1\n");
print_usage_and_exit(1);
}
- Index tmp = { index1, index2, Index::Type::RangedIndex };
+ Index tmp = { index1.value(), index2.value(), Index::Type::RangedIndex };
add_if_not_exists(indexes, tmp);
} else if (range.size() == 1) {
- bool ok = true;
- ssize_t index = range[0].to_int(ok);
- if (!ok) {
+ auto index = range[0].to_int();
+ if (!index.has_value()) {
fprintf(stderr, "cut: invalid byte/character position '%s'\n", range[0].characters());
print_usage_and_exit(1);
}
- if (index == 0) {
+ if (index.value() == 0) {
fprintf(stderr, "cut: byte/character positions are numbered from 1\n");
print_usage_and_exit(1);
}
- Index tmp = { index, index, Index::Type::SingleIndex };
+ Index tmp = { index.value(), index.value(), Index::Type::SingleIndex };
add_if_not_exists(indexes, tmp);
} else {
fprintf(stderr, "cut: invalid byte or character range\n");
diff --git a/Userland/date.cpp b/Userland/date.cpp
index ac7efdccac..5ed9030cb5 100644
--- a/Userland/date.cpp
+++ b/Userland/date.cpp
@@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include <AK/Optional.h>
#include <AK/String.h>
#include <LibCore/DateTime.h>
#include <stdio.h>
@@ -44,12 +45,12 @@ int main(int argc, char** argv)
return 0;
}
if (argc == 3 && !strcmp(argv[1], "-s")) {
- bool ok;
- timespec ts = { String(argv[2]).to_uint(ok), 0 };
- if (!ok) {
+ auto number = StringView(argv[2]).to_uint();
+ if (!number.has_value()) {
fprintf(stderr, "date: Invalid timestamp value");
return 1;
}
+ timespec ts = { number.value(), 0 };
if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
perror("clock_settime");
return 1;
diff --git a/Userland/find.cpp b/Userland/find.cpp
index 43292ab6cc..0dce553689 100644
--- a/Userland/find.cpp
+++ b/Userland/find.cpp
@@ -120,10 +120,10 @@ class LinksCommand final : public StatCommand {
public:
LinksCommand(const char* arg)
{
- bool ok;
- m_links = StringView(arg).to_uint(ok);
- if (!ok)
+ auto number = StringView(arg).to_uint();
+ if (!number.has_value())
fatal_error("Invalid number: \033[1m%s", arg);
+ m_links = number.value();
}
private:
@@ -143,10 +143,10 @@ public:
m_uid = passwd->pw_uid;
} else {
// Attempt to parse it as decimal UID.
- bool ok;
- m_uid = StringView(arg).to_uint(ok);
- if (!ok)
+ auto number = StringView(arg).to_uint();
+ if (!number.has_value())
fatal_error("Invalid user: \033[1m%s", arg);
+ m_uid = number.value();
}
}
@@ -167,10 +167,10 @@ public:
m_gid = gr->gr_gid;
} else {
// Attempt to parse it as decimal GID.
- bool ok;
- m_gid = StringView(arg).to_int(ok);
- if (!ok)
+ auto number = StringView(arg).to_int();
+ if (!number.has_value())
fatal_error("Invalid group: \033[1m%s", arg);
+ m_gid = number.value();
}
}
@@ -192,10 +192,10 @@ public:
m_is_bytes = true;
view = view.substring_view(0, view.length() - 1);
}
- bool ok;
- m_size = view.to_uint(ok);
- if (!ok)
+ auto number = view.to_uint();
+ if (!number.has_value())
fatal_error("Invalid size: \033[1m%s", arg);
+ m_size = number.value();
}
private:
diff --git a/Userland/kill.cpp b/Userland/kill.cpp
index c868147355..2e9889929f 100644
--- a/Userland/kill.cpp
+++ b/Userland/kill.cpp
@@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include <AK/Optional.h>
#include <AK/String.h>
#include <signal.h>
#include <stdio.h>
@@ -45,24 +46,25 @@ int main(int argc, char** argv)
if (argc != 2 && argc != 3)
print_usage_and_exit();
- bool ok;
unsigned signum = SIGTERM;
int pid_argi = 1;
if (argc == 3) {
pid_argi = 2;
if (argv[1][0] != '-')
print_usage_and_exit();
- signum = String(&argv[1][1]).to_uint(ok);
- if (!ok) {
+ auto number = StringView(&argv[1][1]).to_uint();
+ if (!number.has_value()) {
printf("'%s' is not a valid signal number\n", &argv[1][1]);
return 2;
}
+ signum = number.value();
}
- pid_t pid = String(argv[pid_argi]).to_int(ok);
- if (!ok) {
+ auto pid_opt = String(argv[pid_argi]).to_int();
+ if (!pid_opt.has_value()) {
printf("'%s' is not a valid PID\n", argv[pid_argi]);
return 3;
}
+ pid_t pid = pid_opt.value();
int rc = kill(pid, signum);
if (rc < 0)
diff --git a/Userland/killall.cpp b/Userland/killall.cpp
index 444bef6543..1774b4e018 100644
--- a/Userland/killall.cpp
+++ b/Userland/killall.cpp
@@ -54,7 +54,6 @@ static int kill_all(const String& process_name, const unsigned signum)
int main(int argc, char** argv)
{
- bool ok;
unsigned signum = SIGTERM;
int name_argi = 1;
@@ -67,11 +66,12 @@ int main(int argc, char** argv)
if (argv[1][0] != '-')
print_usage_and_exit();
- signum = String(&argv[1][1]).to_uint(ok);
- if (!ok) {
+ auto number = String(&argv[1][1]).to_uint();
+ if (!number.has_value()) {
printf("'%s' is not a valid signal number\n", &argv[1][1]);
return 2;
}
+ signum = number.value();
}
return kill_all(argv[name_argi], signum);
diff --git a/Userland/pidof.cpp b/Userland/pidof.cpp
index 9066a5a1b6..fe3b052bb9 100644
--- a/Userland/pidof.cpp
+++ b/Userland/pidof.cpp
@@ -74,15 +74,16 @@ int main(int argc, char** argv)
pid_t pid_to_omit = 0;
if (omit_pid_value) {
- bool ok = true;
- if (!strcmp(omit_pid_value, "%PPID"))
+ if (!strcmp(omit_pid_value, "%PPID")) {
pid_to_omit = getppid();
- else
- pid_to_omit = StringView(omit_pid_value).to_uint(ok);
- if (!ok) {
- fprintf(stderr, "Invalid value for -o\n");
- args_parser.print_usage(stderr, argv[0]);
- return 1;
+ } else {
+ auto number = StringView(omit_pid_value).to_uint();
+ if (!number.has_value()) {
+ fprintf(stderr, "Invalid value for -o\n");
+ args_parser.print_usage(stderr, argv[0]);
+ return 1;
+ }
+ pid_to_omit = number.value();
}
}
return pid_of(process_name, single_shot, omit_pid_value != nullptr, pid_to_omit);
diff --git a/Userland/truncate.cpp b/Userland/truncate.cpp
index 2d25099e8b..ad0cbd531d 100644
--- a/Userland/truncate.cpp
+++ b/Userland/truncate.cpp
@@ -75,12 +75,12 @@ int main(int argc, char** argv)
break;
}
- bool ok;
- size = str.to_int(ok);
- if (!ok) {
+ auto size_opt = str.to_int();
+ if (!size_opt.has_value()) {
args_parser.print_usage(stderr, argv[0]);
return 1;
}
+ size = size_opt.value();
}
if (reference) {