From 5dae8f8d10d506abc3c75a1f66c1dfe620c84fc1 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 15 Feb 2022 20:27:51 +0100 Subject: refactor: improve project design --- src/conversion.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/conversion.cpp (limited to 'src/conversion.cpp') diff --git a/src/conversion.cpp b/src/conversion.cpp new file mode 100644 index 0000000..b8fb942 --- /dev/null +++ b/src/conversion.cpp @@ -0,0 +1,41 @@ +#include "conversion.hpp" + +#include +#include + +unsigned int str_to_uint(std::string str) +{ + if (str.at(0) == '-') + { + throw "Less than 0"; + } + + std::size_t waste_pos = 0; + + uint64_t num = 0; + + try + { + num = std::stoul(str, &waste_pos, NUMBER_BASE); + } + catch (const std::invalid_argument &exc) + { + throw "Not a number"; + } + catch (const std::out_of_range &exc) + { + throw "Out of range"; + } + + if (waste_pos != str.length()) + { + throw "Not a number"; + } + + if (num > UINT_MAX) + { + throw "Out of range"; + } + + return static_cast(num); +} -- cgit v1.2.3-18-g5258