// Copyright (c) 2022 Dvir Yitzchaki.
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
#ifndef BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP
#define BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP
#ifdef BOOST_NO_CXX17_HDR_CHARCONV
#error "This header requires which is unavailable"
#endif // BOOST_NO_CXX17_HDR_CHARCONV
#ifdef BOOST_NO_CXX17_STRUCTURED_BINDINGS
#error "This header requires structured bindings which is unavailable"
#endif // BOOST_NO_CXX17_STRUCTURED_BINDINGS
#ifdef BOOST_NO_CXX17_IF_CONSTEXPR
#error "This header requires constexpr if which is unavailable"
#endif // BOOST_NO_CXX17_IF_CONSTEXPR
#include
#include
#include
#include
namespace boost::cnv { struct charconv; }
/// @brief std::to/from_chars-based extended converter
/// @details Good overall performance and moderate formatting facilities.
struct boost::cnv::charconv : public boost::cnv::cnvbase
{
using this_type = boost::cnv::charconv;
using base_type = boost::cnv::cnvbase;
template
cnv::range
to_str(in_type value_in, char* buf) const
{
auto [ptr, err] = [&]
{
if constexpr (std::is_integral_v)
return std::to_chars(buf, buf + bufsize_, value_in, int(base_));
else
return std::to_chars(buf, buf + bufsize_, value_in, chars_format(), precision_);
}();
return cnv::range(buf, err == std::errc{} ? ptr : buf);
}
template
void
str_to(cnv::range range, optional& result_out) const
{
out_type result = boost::make_default();
auto [ptr, err] = [&]
{
char_cptr beg = &*range.begin();
char_cptr end = beg + range.size();
if constexpr (std::is_integral_v)
return std::from_chars(beg, end, result, int(base_));
else
return std::from_chars(beg, end, result, chars_format());
}();
if (err == std::errc{})
result_out = result;
}
std::chars_format chars_format() const
{
static constexpr std::chars_format format[] =
{
std::chars_format::fixed,
std::chars_format::scientific,
std::chars_format::hex
};
return format[int(notation_)];
}
};
#endif // BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP