blob: 71ba1b29115c76d90354adb80e58b7f4f48a1cd8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#include "conversion.hpp"
#include <climits>
#include <stdexcept>
unsigned int str_to_uint(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);
}
|