Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-02 08:08:12

0001 // Copyright (c) 2022 Dvir Yitzchaki.
0002 // Use, modification and distribution are subject to the Boost Software License,
0003 // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
0004 
0005 #ifndef BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP
0006 #define BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP
0007 
0008 #ifdef BOOST_NO_CXX17_HDR_CHARCONV
0009 #error "This header requires <charconv> which is unavailable"
0010 #endif // BOOST_NO_CXX17_HDR_CHARCONV
0011 
0012 #ifdef BOOST_NO_CXX17_STRUCTURED_BINDINGS
0013 #error "This header requires structured bindings which is unavailable"
0014 #endif // BOOST_NO_CXX17_STRUCTURED_BINDINGS
0015 
0016 #ifdef BOOST_NO_CXX17_IF_CONSTEXPR
0017 #error "This header requires constexpr if which is unavailable"
0018 #endif // BOOST_NO_CXX17_IF_CONSTEXPR
0019 
0020 #include <boost/convert/base.hpp>
0021 #include <boost/make_default.hpp>
0022 #include <charconv>
0023 #include <type_traits>
0024 
0025 namespace boost::cnv { struct charconv; }
0026 
0027 /// @brief   std::to/from_chars-based extended converter
0028 /// @details Good overall performance and moderate formatting facilities.
0029 struct boost::cnv::charconv : public boost::cnv::cnvbase<boost::cnv::charconv>
0030 {
0031     using this_type = boost::cnv::charconv;
0032     using base_type = boost::cnv::cnvbase<this_type>;
0033 
0034     template<typename in_type>
0035     cnv::range<char*>
0036     to_str(in_type value_in, char* buf) const
0037     {
0038         auto [ptr, err] = [&]
0039         {
0040             if constexpr (std::is_integral_v<in_type>)
0041                 return std::to_chars(buf, buf + bufsize_, value_in, int(base_));
0042             else
0043                 return std::to_chars(buf, buf + bufsize_, value_in, chars_format(), precision_);
0044         }();
0045         return cnv::range<char*>(buf, err == std::errc{} ? ptr : buf);
0046     }
0047     template<typename string_type, typename out_type>
0048     void
0049     str_to(cnv::range<string_type> range, optional<out_type>& result_out) const
0050     {
0051         out_type result = boost::make_default<out_type>();
0052         auto [ptr, err] = [&]
0053         {
0054             char_cptr beg = &*range.begin();
0055             char_cptr end = beg + range.size();
0056 
0057             if constexpr (std::is_integral_v<out_type>)
0058                 return std::from_chars(beg, end, result, int(base_));
0059             else
0060                 return std::from_chars(beg, end, result, chars_format());
0061         }();
0062         if (err == std::errc{})
0063             result_out = result;
0064     }
0065     std::chars_format chars_format() const
0066     {
0067         static constexpr std::chars_format format[] =
0068         {
0069             std::chars_format::fixed,
0070             std::chars_format::scientific,
0071             std::chars_format::hex
0072         };
0073         return format[int(notation_)];
0074     }
0075 };
0076 
0077 #endif // BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP