blob: 5f9494ff07b3bb711fe793b0a5d4f52b386be5f7 (
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
|
#include "conversion.hpp"
#include <climits>
#include <stdexcept>
ConversionResult<unsigned int> str_to_uint(const std::string_view &str)
{
if (!ctre::match<IS_VALID_UINT>(str))
{
return ConversionResult(false, 0U, "Not a number");
}
if (!ctre::match<IS_UINT_IN_RANGE>(str))
{
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())
{
return ConversionResult(false, 0U, "Not a number");
}
if (num > UINT_MAX)
{
return ConversionResult(false, 0U, "Out of range");
}
return ConversionResult(true, static_cast<unsigned int>(num));
}
|