1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 19:38:12 +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:
Shannon Booth 2023-12-23 15:59:14 +13:00 committed by Andreas Kling
parent a4ecc65398
commit e2e7c4d574
155 changed files with 397 additions and 412 deletions

View file

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