tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
from_chars.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_CHARCONV_FROM_CHARS_HPP
5#define TETL_CHARCONV_FROM_CHARS_HPP
6
7#include <etl/_concepts/integral.hpp>
8#include <etl/_concepts/same_as.hpp>
9#include <etl/_cstddef/size_t.hpp>
10#include <etl/_strings/to_integer.hpp>
11#include <etl/_system_error/errc.hpp>
12
13namespace etl {
14
15/// \brief Primitive numerical input conversion
17 char const* ptr{nullptr};
19
20 [[nodiscard]] constexpr explicit operator bool() const noexcept
21 {
22 return ec == etl::errc{};
23 }
24
25 friend auto operator==(from_chars_result const&, from_chars_result const&) -> bool = default;
26};
27
28/// \brief Analyzes the character sequence [first,last) for a pattern described
29/// below. If no characters match the pattern or if the value obtained by
30/// parsing the matched characters is not representable in the type of value,
31/// value is unmodified, otherwise the characters matching the pattern are
32/// interpreted as a text representation of an arithmetic value, which is stored
33/// in value.
34template <integral Int>
35 requires(not same_as<Int, bool>)
36[[nodiscard]] constexpr auto from_chars(char const* first, char const* last, Int& value, int base = 10)
38{
39 constexpr auto options = strings::to_integer_options{.skip_whitespace = false, .check_overflow = true};
40 auto const [end, err, val] = strings::to_integer<Int, options>({first, last}, static_cast<Int>(base));
41
43 return from_chars_result{.ptr = end, .ec = errc::result_out_of_range};
44 }
46 return from_chars_result{.ptr = end, .ec = errc::invalid_argument};
47 }
48
49 value = val;
50 return from_chars_result{.ptr = end, .ec = {}};
51}
52
53} // namespace etl
54
55#endif // TETL_CHARCONV_FROM_CHARS_HPP
Definition find.hpp:9
to_integer_error
Definition to_integer.hpp:84
Definition adjacent_find.hpp:9
constexpr auto from_chars(char const *first, char const *last, Int &value, int base=10) -> from_chars_result
Analyzes the character sequence [first,last) for a pattern described below. If no characters match th...
Definition from_chars.hpp:36
errc
The scoped enumeration etl::errc defines the values of portable error conditions.
Definition errc.hpp:14
@ result_out_of_range
@ invalid_argument
Primitive numerical input conversion.
Definition from_chars.hpp:16
friend auto operator==(from_chars_result const &, from_chars_result const &) -> bool=default
etl::errc ec
Definition from_chars.hpp:18
char const * ptr
Definition from_chars.hpp:17
constexpr operator bool() const noexcept
Definition from_chars.hpp:20
Definition to_integer.hpp:79
bool check_overflow
Definition to_integer.hpp:81
bool skip_whitespace
Definition to_integer.hpp:80