MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
StringUtils.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include <charconv>
7#include <concepts>
8#include <limits>
9#include <stdexcept>
10#include <string_view>
11#include <system_error>
12#include <fmt/format.h>
13
14namespace mxl::lib
15{
23 template<std::integral T>
24 T stringToInteger(std::string_view text, std::string_view name)
25 {
26 auto value = T{};
27 auto const* const first = text.data();
28 auto const* const last = text.data() + text.size();
29 auto const [end, error] = std::from_chars(first, last, value);
30 if (error == std::errc::result_out_of_range)
31 {
32 throw std::invalid_argument{
33 fmt::format("'{}' must be between {} and {}, got '{}'.", name, std::numeric_limits<T>::min(), std::numeric_limits<T>::max(), text)};
34 }
35 if ((error != std::errc{}) || (end != last))
36 {
37 throw std::invalid_argument{fmt::format("'{}' must be a decimal integer, got '{}'.", name, text)};
38 }
39 return value;
40 }
41}
T stringToInteger(std::string_view text, std::string_view name)