charconv.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (c) 2022 Dvir Yitzchaki.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  4. #ifndef BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP
  5. #define BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP
  6. #ifdef BOOST_NO_CXX17_HDR_CHARCONV
  7. #error "This header requires <charconv> which is unavailable"
  8. #endif // BOOST_NO_CXX17_HDR_CHARCONV
  9. #ifdef BOOST_NO_CXX17_STRUCTURED_BINDINGS
  10. #error "This header requires structured bindings which is unavailable"
  11. #endif // BOOST_NO_CXX17_STRUCTURED_BINDINGS
  12. #ifdef BOOST_NO_CXX17_IF_CONSTEXPR
  13. #error "This header requires constexpr if which is unavailable"
  14. #endif // BOOST_NO_CXX17_IF_CONSTEXPR
  15. #include <boost/convert/base.hpp>
  16. #include <charconv>
  17. #include <type_traits>
  18. namespace boost { namespace cnv { struct charconv; }}
  19. /// @brief std::to/from_chars-based extended converter
  20. /// @details The converter offers good overall performance and moderate formatting facilities.
  21. struct boost::cnv::charconv : boost::cnv::cnvbase<boost::cnv::charconv>
  22. {
  23. using this_type = boost::cnv::charconv;
  24. using base_type = boost::cnv::cnvbase<this_type>;
  25. private:
  26. friend struct boost::cnv::cnvbase<this_type>;
  27. template<typename in_type>
  28. cnv::range<char*>
  29. to_str(in_type value_in, char* buf) const
  30. {
  31. const auto [ptr, ec] = [&]{
  32. if constexpr (std::is_integral_v<in_type>) {
  33. return std::to_chars(buf, buf + bufsize_, value_in, static_cast<int>(base_));
  34. } else {
  35. return std::to_chars(buf, buf + bufsize_, value_in, chars_format(), precision_);
  36. }
  37. }();
  38. bool success = ec == std::errc{};
  39. return cnv::range<char*>(buf, success ? ptr : buf);
  40. }
  41. template<typename string_type, typename out_type>
  42. void
  43. str_to(cnv::range<string_type> range, optional<out_type>& result_out) const
  44. {
  45. out_type result = boost::make_default<out_type>();
  46. const auto [ptr, ec] = [&]{
  47. if constexpr (std::is_integral_v<out_type>) {
  48. return std::from_chars(&*range.begin(), &*range.end(), result, static_cast<int>(base_));
  49. } else {
  50. return std::from_chars(&*range.begin(), &*range.end(), result, chars_format());
  51. }
  52. }();
  53. if (ec == std::errc{})
  54. result_out = result;
  55. }
  56. std::chars_format chars_format() const
  57. {
  58. static constexpr std::chars_format format[] =
  59. {
  60. std::chars_format::fixed,
  61. std::chars_format::scientific,
  62. std::chars_format::hex
  63. };
  64. return format[int(notation_)];
  65. }
  66. std::chars_format fmt_ = std::chars_format::fixed;
  67. };
  68. #endif // BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP