aboutsummaryrefslogtreecommitdiff
path: root/src/conversion.cpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-02-19 17:30:16 +0100
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:53 +0200
commitcc3869ac0b24a97dbadc6b837b3c23af28baa627 (patch)
tree3fa7156e1734de03d6b8152bfae219774bb38196 /src/conversion.cpp
parent4210c8d2f8fc36e614b8bfb669511c00383002f0 (diff)
feat: add basic program stuff
Diffstat (limited to 'src/conversion.cpp')
-rw-r--r--src/conversion.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/conversion.cpp b/src/conversion.cpp
new file mode 100644
index 0000000..79b3587
--- /dev/null
+++ b/src/conversion.cpp
@@ -0,0 +1,41 @@
+#include "conversion.hpp"
+
+#include <climits>
+#include <stdexcept>
+
+unsigned int str_to_uint(const std::string_view &str)
+{
+ if (str.at(0) == '-')
+ {
+ throw "Less than 0";
+ }
+
+ 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)
+ {
+ 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<unsigned int>(num);
+}