From e233dc28491c33e8a7dc0a11576d3b8ce91cce2c Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 25 Feb 2022 18:16:40 +0100 Subject: refactor: delete exception usages & game options --- src/conversion.cpp | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'src/conversion.cpp') diff --git a/src/conversion.cpp b/src/conversion.cpp index 79b3587..5f9494f 100644 --- a/src/conversion.cpp +++ b/src/conversion.cpp @@ -3,39 +3,31 @@ #include #include -unsigned int str_to_uint(const std::string_view &str) +ConversionResult str_to_uint(const std::string_view &str) { - if (str.at(0) == '-') + if (!ctre::match(str)) { - throw "Less than 0"; + return ConversionResult(false, 0U, "Not a number"); } - std::size_t waste_pos = 0; - - uint64_t num = 0; - - try - { - num = std::stoul(str.data(), &waste_pos, NUMBER_BASE); - } - catch (const std::invalid_argument &exc) + if (!ctre::match(str)) { - throw "Not a number"; - } - catch (const std::out_of_range &exc) - { - throw "Out of range"; + return ConversionResult(false, 0U, "Out of range"); } + std::size_t waste_pos = 0; + + auto num = std::stoul(str.data(), &waste_pos, NUMBER_BASE); + if (waste_pos != str.length()) { - throw "Not a number"; + return ConversionResult(false, 0U, "Not a number"); } if (num > UINT_MAX) { - throw "Out of range"; + return ConversionResult(false, 0U, "Out of range"); } - return static_cast(num); + return ConversionResult(true, static_cast(num)); } -- cgit v1.2.3-18-g5258