From 9052a6febf4217d60bbcddda0277ba080b3ab8b2 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sun, 12 Mar 2023 15:02:03 -0400 Subject: LibGfx/PortableFormat: Simplify `read_number` signature The function signature goes from: `bool read_number(Streamer& streamer, TValue* value)` to `ErrorOr read_number(Streamer& streamer)` It allows us to, on one hand use `ErrorOr` for error propagation, removing an out parameter in the meantime, and on the other hand remove the useless template. --- .../Libraries/LibGfx/ImageFormats/PGMLoader.cpp | 6 ++-- .../Libraries/LibGfx/ImageFormats/PPMLoader.cpp | 15 +++++----- .../ImageFormats/PortableImageLoaderCommon.h | 34 ++++++++++------------ 3 files changed, 26 insertions(+), 29 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp index 0240548013..2ee82006b8 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp @@ -33,11 +33,11 @@ bool read_image_data(PGMLoadingContext& context, Streamer& streamer) Vector color_data; if (context.type == PGMLoadingContext::Type::ASCII) { - u16 value; - while (true) { - if (!read_number(streamer, &value)) + auto number_or_error = read_number(streamer); + if (number_or_error.is_error()) break; + auto value = number_or_error.value(); if (!read_whitespace(context, streamer)) break; diff --git a/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp index c662e76b87..c8859350af 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp @@ -22,30 +22,29 @@ bool read_image_data(PPMLoadingContext& context, Streamer& streamer) color_data.ensure_capacity(context.width * context.height); if (context.type == PPMLoadingContext::Type::ASCII) { - u16 red; - u16 green; - u16 blue; - while (true) { - if (!read_number(streamer, &red)) + auto const red_or_error = read_number(streamer); + if (red_or_error.is_error()) break; if (!read_whitespace(context, streamer)) break; - if (!read_number(streamer, &green)) + auto const green_or_error = read_number(streamer); + if (green_or_error.is_error()) break; if (!read_whitespace(context, streamer)) break; - if (!read_number(streamer, &blue)) + auto const blue_or_error = read_number(streamer); + if (blue_or_error.is_error()) break; if (!read_whitespace(context, streamer)) break; - Color color { (u8)red, (u8)green, (u8)blue }; + Color color { (u8)red_or_error.value(), (u8)green_or_error.value(), (u8)blue_or_error.value() }; if (context.format_details.max_val < 255) color = adjust_color(context.format_details.max_val, color); color_data.append(color); diff --git a/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h b/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h index a17b6c1921..8ab778c555 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h +++ b/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -30,8 +31,7 @@ static constexpr Color adjust_color(u16 max_val, Color color) return color; } -template -static bool read_number(Streamer& streamer, TValue* value) +static inline ErrorOr read_number(Streamer& streamer) { u8 byte {}; StringBuilder sb {}; @@ -45,14 +45,11 @@ static bool read_number(Streamer& streamer, TValue* value) sb.append(byte); } - auto const opt_value = sb.to_deprecated_string().to_uint(); - if (!opt_value.has_value()) { - *value = 0; - return false; - } + auto const maybe_value = TRY(sb.to_string()).to_number(); + if (!maybe_value.has_value()) + return Error::from_string_literal("Can't convert bytes to a number"); - *value = static_cast(opt_value.value()); - return true; + return *maybe_value; } template @@ -133,11 +130,11 @@ static bool read_whitespace(TContext& context, Streamer& streamer) template static bool read_width(TContext& context, Streamer& streamer) { - if (bool const result = read_number(streamer, &context.width); - !result || context.width == 0) { + auto number_or_error = read_number(streamer); + if (number_or_error.is_error()) return false; - } + context.width = number_or_error.value(); context.state = TContext::State::Width; return true; } @@ -145,11 +142,11 @@ static bool read_width(TContext& context, Streamer& streamer) template static bool read_height(TContext& context, Streamer& streamer) { - if (bool const result = read_number(streamer, &context.height); - !result || context.height == 0) { + auto number_or_error = read_number(streamer); + if (number_or_error.is_error()) return false; - } + context.height = number_or_error.value(); context.state = TContext::State::Height; return true; } @@ -157,10 +154,11 @@ static bool read_height(TContext& context, Streamer& streamer) template static bool read_max_val(TContext& context, Streamer& streamer) { - if (bool const result = read_number(streamer, &context.format_details.max_val); - !result || context.format_details.max_val == 0) { + auto number_or_error = read_number(streamer); + if (number_or_error.is_error()) return false; - } + + context.format_details.max_val = number_or_error.value(); if (context.format_details.max_val > 255) { dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't parse 2 byte color for {}", TContext::FormatDetails::image_type); -- cgit v1.2.3