aboutsummaryrefslogtreecommitdiff
path: root/src/conversion.cpp
diff options
context:
space:
mode:
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));
}