aboutsummaryrefslogtreecommitdiff
path: root/src/conversion.cpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-02-25 18:16:40 +0100
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:53 +0200
commite233dc28491c33e8a7dc0a11576d3b8ce91cce2c (patch)
tree40551d581baf43690eda5e0c3a7d083998136031 /src/conversion.cpp
parentd36ec4670770022c0ec8337f6df4de292ca941db (diff)
refactor: delete exception usages & game options
Diffstat (limited to 'src/conversion.cpp')
-rw-r--r--src/conversion.cpp32
1 files changed, 12 insertions, 20 deletions
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 <climits>
#include <stdexcept>
-unsigned int str_to_uint(const std::string_view &str)
+ConversionResult<unsigned int> str_to_uint(const std::string_view &str)
{
- if (str.at(0) == '-')
+ if (!ctre::match<IS_VALID_UINT>(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<IS_UINT_IN_RANGE>(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<unsigned int>(num);
+ return ConversionResult(true, static_cast<unsigned int>(num));
}