// 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 namespace boost::cnv { struct charconv; } /// @brief std::to/from_chars-based extended converter /// @details The converter offers good overall performance and moderate formatting facilities. struct boost::cnv::charconv : boost::cnv::cnvbase { using this_type = boost::cnv::charconv; using base_type = boost::cnv::cnvbase; private: friend struct boost::cnv::cnvbase; template cnv::range to_str(in_type value_in, char* buf) const { const auto [ptr, ec] = [&]{ if constexpr (std::is_integral_v) { return std::to_chars(buf, buf + bufsize_, value_in, static_cast(base_)); } else { return std::to_chars(buf, buf + bufsize_, value_in, chars_format(), precision_); } }(); bool success = ec == std::errc{}; return cnv::range(buf, success ? ptr : buf); } template void str_to(cnv::range range, optional& result_out) const { out_type result = boost::make_default(); const auto [ptr, ec] = [&]{ if constexpr (std::is_integral_v) { return std::from_chars(&*range.begin(), &*range.end(), result, static_cast(base_)); } else { return std::from_chars(&*range.begin(), &*range.end(), result, chars_format()); } }(); if (ec == 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_)]; } std::chars_format fmt_ = std::chars_format::fixed; }; #endif // BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP