aboutsummaryrefslogtreecommitdiff
path: root/src/conversion.hpp
blob: 9f6f933e1b3a1bb79b25ed3a3c9e3c0819ed2256 (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
#pragma once

#include <ctre.hpp>
#include <string_view>

constexpr uint32_t NUMBER_BASE = 10U;

template <typename ResultType>
class ConversionResult
{
public:
	explicit ConversionResult(const bool &success_, const ResultType &result_,
							  const std::string_view &fail_reason_ = "") noexcept
		: success(success_), result(result_), fail_reason(fail_reason_)
	{
	}

	const bool &success;
	const ResultType &result;
	const std::string_view &fail_reason;
};

static constexpr auto IS_VALID_UINT = ctll::fixed_string("^[0-9]+$");
static constexpr auto IS_UINT_IN_RANGE = ctll::fixed_string("^[0-9]{1,19}$");

/**
 * Converts a string to a unsigned integer.
 *
 * @param str A string that possibly is a unsigned integer
 * @returns A conversion result
 */
ConversionResult<uint32_t> str_to_uint(const std::string_view &str) noexcept;