aboutsummaryrefslogtreecommitdiff
path: root/src/conversion.cpp
blob: 4dbf65184c693a356e5e1b3352da0fe2b2a62ad8 (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<uint32_t> str_to_uint(const std::string_view &str) noexcept
{
	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<uint32_t>(num));
}