tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
to_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_TO_CHARS_HPP
5#define TETL_CHARCONV_TO_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/_iterator/distance.hpp>
11#include <etl/_strings/from_integer.hpp>
12#include <etl/_system_error/errc.hpp>
13
14namespace etl {
15
16/// \brief Primitive numerical output conversion.
18 char* ptr{nullptr};
20
21 [[nodiscard]] constexpr explicit operator bool() const noexcept
22 {
23 return ec == etl::errc{};
24 }
25
26 friend auto operator==(to_chars_result const&, to_chars_result const&) -> bool = default;
27};
28
29/// Converts value into a character string by successively filling the range
30/// [first, last), where [first, last) is required to be a valid range.
31///
32/// Integer formatters: value is converted to a string of digits in the given
33/// base (with no redundant leading zeroes). Digits in the range 10..35
34/// (inclusive) are represented as lowercase characters a..z. If value is less
35/// than zero, the representation starts with a minus sign. The library provides
36/// overloads for all signed and unsigned integer types and for the type char as
37/// the type of the parameter value.
38template <integral T>
39 requires(not same_as<T, bool>)
40[[nodiscard]] constexpr auto to_chars(char* first, char* last, T val, int base = 10) -> to_chars_result
41{
42 constexpr auto options = strings::from_integer_options{.terminate_with_null = false};
43
44 auto const len = static_cast<etl::size_t>(etl::distance(first, last));
45 auto const res = strings::from_integer<T, options>(val, first, len, base);
46 if (res.error == strings::from_integer_error::none) {
47 return to_chars_result{res.end, {}};
48 }
49 return to_chars_result{
50 .ptr = last,
52 };
53}
54
55[[nodiscard]] constexpr auto to_chars(char*, char*, bool, int = 10) -> to_chars_result = delete;
56
57} // namespace etl
58
59#endif // TETL_CHARCONV_TO_CHARS_HPP
constexpr auto distance(It first, It last) -> typename iterator_traits< It >::difference_type
Returns the number of hops from first to last.
Definition distance.hpp:17
Definition find.hpp:9
from_integer_error
Definition from_integer.hpp:20
Definition adjacent_find.hpp:9
constexpr auto to_chars(char *first, char *last, T val, int base=10) -> to_chars_result
Converts value into a character string by successively filling the range [first, last),...
Definition to_chars.hpp:40
errc
The scoped enumeration etl::errc defines the values of portable error conditions.
Definition errc.hpp:14
constexpr auto to_chars(char *, char *, bool, int=10) -> to_chars_result=delete
Definition from_integer.hpp:16
bool terminate_with_null
Definition from_integer.hpp:17
Primitive numerical output conversion.
Definition to_chars.hpp:17
etl::errc ec
Definition to_chars.hpp:19
constexpr operator bool() const noexcept
Definition to_chars.hpp:21
char * ptr
Definition to_chars.hpp:18
friend auto operator==(to_chars_result const &, to_chars_result const &) -> bool=default