mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 13:57: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
|
@ -177,7 +177,7 @@ static ErrorOr<String> resolve_slices(RefPtr<Shell> shell, String&& input_value,
|
|||
|
||||
size_t i = 0;
|
||||
for (auto& value : index_values) {
|
||||
auto maybe_index = value.bytes_as_string_view().to_int();
|
||||
auto maybe_index = value.bytes_as_string_view().to_number<int>();
|
||||
if (!maybe_index.has_value()) {
|
||||
shell->raise_error(Shell::ShellError::InvalidSliceContentsError, ByteString::formatted("Invalid value in slice index {}: {} (expected a number)", i, value), slice->position());
|
||||
return move(input_value);
|
||||
|
@ -227,7 +227,7 @@ static ErrorOr<Vector<String>> resolve_slices(RefPtr<Shell> shell, Vector<String
|
|||
|
||||
size_t i = 0;
|
||||
for (auto& value : index_values) {
|
||||
auto maybe_index = value.bytes_as_string_view().to_int();
|
||||
auto maybe_index = value.to_number<int>();
|
||||
if (!maybe_index.has_value()) {
|
||||
shell->raise_error(Shell::ShellError::InvalidSliceContentsError, ByteString::formatted("Invalid value in slice index {}: {} (expected a number)", i, value), slice->position());
|
||||
return move(values);
|
||||
|
@ -2697,8 +2697,8 @@ ErrorOr<RefPtr<Value>> Range::run(RefPtr<Shell> shell)
|
|||
values.append(make_ref_counted<StringValue>(TRY(builder.to_string())));
|
||||
} else {
|
||||
// Could be two numbers?
|
||||
auto start_int = start_str.bytes_as_string_view().to_int();
|
||||
auto end_int = end_str.bytes_as_string_view().to_int();
|
||||
auto start_int = start_str.to_number<int>();
|
||||
auto end_int = end_str.to_number<int>();
|
||||
if (start_int.has_value() && end_int.has_value()) {
|
||||
auto start = start_int.value();
|
||||
auto end = end_int.value();
|
||||
|
|
|
@ -298,7 +298,7 @@ ErrorOr<int> Shell::builtin_bg(Main::Arguments arguments)
|
|||
.max_values = 1,
|
||||
.accept_value = [&](StringView value) -> bool {
|
||||
// Check if it's a pid (i.e. literal integer)
|
||||
if (auto number = value.to_uint(); number.has_value()) {
|
||||
if (auto number = value.to_number<unsigned>(); number.has_value()) {
|
||||
job_id = number.value();
|
||||
is_pid = true;
|
||||
return true;
|
||||
|
@ -705,7 +705,7 @@ ErrorOr<int> Shell::builtin_fg(Main::Arguments arguments)
|
|||
.max_values = 1,
|
||||
.accept_value = [&](StringView value) -> bool {
|
||||
// Check if it's a pid (i.e. literal integer)
|
||||
if (auto number = value.to_uint(); number.has_value()) {
|
||||
if (auto number = value.to_number<unsigned>(); number.has_value()) {
|
||||
job_id = number.value();
|
||||
is_pid = true;
|
||||
return true;
|
||||
|
@ -776,7 +776,7 @@ ErrorOr<int> Shell::builtin_disown(Main::Arguments arguments)
|
|||
.max_values = INT_MAX,
|
||||
.accept_value = [&](StringView value) -> bool {
|
||||
// Check if it's a pid (i.e. literal integer)
|
||||
if (auto number = value.to_uint(); number.has_value()) {
|
||||
if (auto number = value.to_number<unsigned>(); number.has_value()) {
|
||||
job_ids.append(number.value());
|
||||
id_is_pid.append(true);
|
||||
return true;
|
||||
|
@ -1237,7 +1237,7 @@ ErrorOr<int> Shell::builtin_wait(Main::Arguments arguments)
|
|||
.max_values = INT_MAX,
|
||||
.accept_value = [&](StringView value) -> bool {
|
||||
// Check if it's a pid (i.e. literal integer)
|
||||
if (auto number = value.to_uint(); number.has_value()) {
|
||||
if (auto number = value.to_number<unsigned>(); number.has_value()) {
|
||||
job_ids.append(number.value());
|
||||
id_is_pid.append(true);
|
||||
return true;
|
||||
|
@ -1543,14 +1543,14 @@ ErrorOr<int> Shell::builtin_argsparser_parse(Main::Arguments arguments)
|
|||
case Type::String:
|
||||
return AST::make_ref_counted<AST::StringValue>(TRY(String::from_utf8(value)));
|
||||
case Type::I32:
|
||||
if (auto number = value.to_int(); number.has_value())
|
||||
if (auto number = value.to_number<int>(); number.has_value())
|
||||
return AST::make_ref_counted<AST::StringValue>(TRY(String::number(*number)));
|
||||
|
||||
warnln("Invalid value for type i32: {}", value);
|
||||
return OptionalNone {};
|
||||
case Type::U32:
|
||||
case Type::Size:
|
||||
if (auto number = value.to_uint(); number.has_value())
|
||||
if (auto number = value.to_number<unsigned>(); number.has_value())
|
||||
return AST::make_ref_counted<AST::StringValue>(TRY(String::number(*number)));
|
||||
|
||||
warnln("Invalid value for type u32|size: {}", value);
|
||||
|
@ -1860,7 +1860,7 @@ ErrorOr<int> Shell::builtin_argsparser_parse(Main::Arguments arguments)
|
|||
return false;
|
||||
}
|
||||
|
||||
auto number = value.to_uint();
|
||||
auto number = value.to_number<unsigned>();
|
||||
if (!number.has_value()) {
|
||||
warnln("Invalid value for --min: '{}', expected a non-negative number", value);
|
||||
return false;
|
||||
|
@ -1888,7 +1888,7 @@ ErrorOr<int> Shell::builtin_argsparser_parse(Main::Arguments arguments)
|
|||
return false;
|
||||
}
|
||||
|
||||
auto number = value.to_uint();
|
||||
auto number = value.to_number<unsigned>();
|
||||
if (!number.has_value()) {
|
||||
warnln("Invalid value for --max: '{}', expected a non-negative number", value);
|
||||
return false;
|
||||
|
|
|
@ -1052,7 +1052,7 @@ ErrorOr<RefPtr<AST::Node>> Shell::immediate_math(AST::ImmediateExpression& invok
|
|||
view.byte_offset_of(it) - *integer_or_word_start_offset);
|
||||
|
||||
if (all_of(integer_or_word, is_ascii_digit))
|
||||
tokens.append(*integer_or_word.as_string().to_int());
|
||||
tokens.append(*integer_or_word.as_string().to_number<int>());
|
||||
else
|
||||
tokens.append(TRY(expression.substring_from_byte_offset_with_shared_superstring(*integer_or_word_start_offset, integer_or_word.length())));
|
||||
|
||||
|
@ -1239,7 +1239,7 @@ ErrorOr<RefPtr<AST::Node>> Shell::immediate_math(AST::ImmediateExpression& invok
|
|||
auto integer_or_word = view.substring_view(*integer_or_word_start_offset);
|
||||
|
||||
if (all_of(integer_or_word, is_ascii_digit))
|
||||
tokens.append(*integer_or_word.as_string().to_int());
|
||||
tokens.append(*integer_or_word.as_string().to_number<int>());
|
||||
else
|
||||
tokens.append(TRY(expression.substring_from_byte_offset_with_shared_superstring(*integer_or_word_start_offset, integer_or_word.length())));
|
||||
|
||||
|
@ -1262,7 +1262,7 @@ ErrorOr<RefPtr<AST::Node>> Shell::immediate_math(AST::ImmediateExpression& invok
|
|||
builder.join(' ', TRY(const_cast<AST::Value&>(*value).resolve_as_list(const_cast<Shell&>(*this))));
|
||||
resolved_name = TRY(builder.to_string());
|
||||
|
||||
auto integer = resolved_name.bytes_as_string_view().to_int<i64>();
|
||||
auto integer = resolved_name.to_number<i64>();
|
||||
if (integer.has_value())
|
||||
return *integer;
|
||||
}
|
||||
|
|
|
@ -1165,7 +1165,7 @@ RefPtr<AST::Node> Parser::parse_redirection()
|
|||
if (number.is_empty()) {
|
||||
pipe_fd = -1;
|
||||
} else {
|
||||
auto fd = number.to_int();
|
||||
auto fd = number.to_number<int>();
|
||||
pipe_fd = fd.value_or(-1);
|
||||
}
|
||||
|
||||
|
@ -1200,7 +1200,7 @@ RefPtr<AST::Node> Parser::parse_redirection()
|
|||
if (number.is_empty()) {
|
||||
dest_pipe_fd = -1;
|
||||
} else {
|
||||
auto fd = number.to_int();
|
||||
auto fd = number.to_number<int>();
|
||||
dest_pipe_fd = fd.value_or(-1);
|
||||
}
|
||||
auto redir = create<AST::Fd2FdRedirection>(pipe_fd, dest_pipe_fd); // Redirection Fd2Fd
|
||||
|
@ -1791,7 +1791,7 @@ RefPtr<AST::Node> Parser::parse_history_designator()
|
|||
selector.event.kind = AST::HistorySelector::EventKind::IndexFromEnd;
|
||||
else
|
||||
selector.event.kind = AST::HistorySelector::EventKind::IndexFromStart;
|
||||
auto number = abs(selector.event.text.bytes_as_string_view().to_int().value_or(0));
|
||||
auto number = abs(selector.event.text.to_number<int>().value_or(0));
|
||||
if (number != 0)
|
||||
selector.event.index = number - 1;
|
||||
else
|
||||
|
@ -1819,7 +1819,7 @@ RefPtr<AST::Node> Parser::parse_history_designator()
|
|||
ssize_t offset = -1;
|
||||
if (isdigit(c)) {
|
||||
auto num = consume_while(is_digit);
|
||||
auto value = num.to_uint();
|
||||
auto value = num.to_number<unsigned>();
|
||||
if (!value.has_value())
|
||||
return {};
|
||||
word_selector_kind = AST::HistorySelector::WordSelectorKind::Index;
|
||||
|
|
|
@ -2084,7 +2084,7 @@ ErrorOr<RefPtr<AST::Node>> Parser::parse_io_redirect()
|
|||
Optional<int> io_number;
|
||||
|
||||
if (peek().type == Token::Type::IoNumber)
|
||||
io_number = consume().value.bytes_as_string_view().to_int();
|
||||
io_number = consume().value.to_number<int>();
|
||||
|
||||
if (auto io_file = TRY(parse_io_file(start_position, io_number)))
|
||||
return io_file;
|
||||
|
@ -2194,7 +2194,7 @@ ErrorOr<RefPtr<AST::Node>> Parser::parse_io_file(AST::Position start_position, O
|
|||
source_fd);
|
||||
}
|
||||
|
||||
auto maybe_target_fd = text.bytes_as_string_view().to_int();
|
||||
auto maybe_target_fd = text.to_number<int>();
|
||||
if (maybe_target_fd.has_value()) {
|
||||
auto target_fd = maybe_target_fd.release_value();
|
||||
if (is_less)
|
||||
|
|
|
@ -137,7 +137,7 @@ ByteString Shell::prompt() const
|
|||
if (next_char != 'w' && next_char != 'W')
|
||||
continue;
|
||||
|
||||
auto const max_component_count = number_string.to_uint().value();
|
||||
auto const max_component_count = number_string.to_number<unsigned>().value();
|
||||
|
||||
ByteString const home_path = getenv("HOME");
|
||||
|
||||
|
@ -412,7 +412,7 @@ ErrorOr<RefPtr<AST::Value const>> Shell::look_up_local_variable(StringView name)
|
|||
if (auto* frame = find_frame_containing_local_variable(name))
|
||||
return frame->local_variables.get(name).value();
|
||||
|
||||
if (auto index = name.to_uint(); index.has_value())
|
||||
if (auto index = name.to_number<unsigned>(); index.has_value())
|
||||
return get_argument(index.value());
|
||||
|
||||
return nullptr;
|
||||
|
@ -2620,7 +2620,7 @@ Optional<int> Shell::resolve_job_spec(StringView str)
|
|||
return {};
|
||||
|
||||
// %number -> job id <number>
|
||||
if (auto number = str.substring_view(1).to_uint(); number.has_value())
|
||||
if (auto number = str.substring_view(1).to_number<unsigned>(); number.has_value())
|
||||
return number.value();
|
||||
|
||||
// '%?str' -> iterate jobs and pick one with `str' in its command
|
||||
|
@ -2647,7 +2647,7 @@ void Shell::timer_event(Core::TimerEvent& event)
|
|||
auto const* autosave_env_ptr = getenv("HISTORY_AUTOSAVE_TIME_MS");
|
||||
auto option = autosave_env_ptr != NULL ? StringView { autosave_env_ptr, strlen(autosave_env_ptr) } : StringView {};
|
||||
|
||||
auto time = option.to_uint();
|
||||
auto time = option.to_number<unsigned>();
|
||||
if (!time.has_value() || time.value() == 0) {
|
||||
m_history_autosave_time.clear();
|
||||
stop_timer();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue