|
| 1 | +// Copyright 2023 - 2024 Matt Borland |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// https://www.boost.org/LICENSE_1_0.txt |
| 4 | + |
| 5 | +#ifndef BOOST_DECIMAL_FORMAT_HPP |
| 6 | +#define BOOST_DECIMAL_FORMAT_HPP |
| 7 | + |
| 8 | +// Many compilers seem to have <format> with completly broken support so narrow down our support range |
| 9 | +#if (__cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)) && !defined(BOOST_DECIMAL_DISABLE_CLIB) && \ |
| 10 | + ((defined(__GNUC__) && __GNUC__ >= 13) || (defined(__clang__) && __clang_major__ >= 18) || (defined(_MSC_VER) && _MSC_VER >= 1940)) |
| 11 | + |
| 12 | +#define BOOST_CRYPT_HAS_FORMAT_SUPPORT |
| 13 | + |
| 14 | +#include <boost/decimal/charconv.hpp> |
| 15 | +#include <algorithm> |
| 16 | +#include <format> |
| 17 | +#include <iostream> |
| 18 | +#include <iomanip> |
| 19 | +#include <string> |
| 20 | +#include <tuple> |
| 21 | +#include <cctype> |
| 22 | + |
| 23 | +// Default :g |
| 24 | +// Fixed :f |
| 25 | +// Scientific :e |
| 26 | +// Hex :a |
| 27 | +// |
| 28 | +// Capital letter for any of the above leads to all characters being uppercase |
| 29 | + |
| 30 | +namespace boost::decimal::detail { |
| 31 | + |
| 32 | +template <typename ParseContext> |
| 33 | +constexpr auto parse_impl(ParseContext &ctx) |
| 34 | +{ |
| 35 | + auto it {ctx.begin()}; |
| 36 | + int ctx_precision = 6; |
| 37 | + boost::decimal::chars_format fmt = boost::decimal::chars_format::general; |
| 38 | + bool is_upper = false; |
| 39 | + int padding_digits = 0; |
| 40 | + |
| 41 | + // Check for a padding character |
| 42 | + while (it != ctx.end() && *it >= '0' && *it <= '9') |
| 43 | + { |
| 44 | + padding_digits = padding_digits * 10 + (*it - '0'); |
| 45 | + ++it; |
| 46 | + } |
| 47 | + |
| 48 | + // If there is a . then we need to capture the precision argument |
| 49 | + if (*it == '.') |
| 50 | + { |
| 51 | + ++it; |
| 52 | + ctx_precision = 0; |
| 53 | + while (it != ctx.end() && *it >= '0' && *it <= '9') |
| 54 | + { |
| 55 | + ctx_precision = ctx_precision * 10 + (*it - '0'); |
| 56 | + ++it; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Lastly we capture the format to include if it's upper case |
| 61 | + if (it != ctx.end() && *it != '}') |
| 62 | + { |
| 63 | + switch (*it) |
| 64 | + { |
| 65 | + case 'G': |
| 66 | + is_upper = true; |
| 67 | + [[fallthrough]]; |
| 68 | + case 'g': |
| 69 | + fmt = chars_format::general; |
| 70 | + break; |
| 71 | + |
| 72 | + case 'F': |
| 73 | + [[fallthrough]]; |
| 74 | + case 'f': |
| 75 | + fmt = chars_format::fixed; |
| 76 | + break; |
| 77 | + |
| 78 | + case 'E': |
| 79 | + is_upper = true; |
| 80 | + [[fallthrough]]; |
| 81 | + case 'e': |
| 82 | + fmt = chars_format::scientific; |
| 83 | + break; |
| 84 | + |
| 85 | + case 'A': |
| 86 | + is_upper = true; |
| 87 | + [[fallthrough]]; |
| 88 | + case 'a': |
| 89 | + fmt = chars_format::hex; |
| 90 | + break; |
| 91 | + // LCOV_EXCL_START |
| 92 | + default: |
| 93 | + throw std::format_error("Invalid format specifier"); |
| 94 | + // LCOV_EXCL_STOP |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + ++it; |
| 99 | + |
| 100 | + return std::make_tuple(ctx_precision, fmt, is_upper, padding_digits, it); |
| 101 | +} |
| 102 | + |
| 103 | +} // Namespace boost::decimal::detail |
| 104 | + |
| 105 | +namespace std { |
| 106 | + |
| 107 | +template <boost::decimal::concepts::decimal_floating_point_type T> |
| 108 | +struct formatter<T> |
| 109 | +{ |
| 110 | + constexpr formatter() : ctx_precision(6), |
| 111 | + fmt(boost::decimal::chars_format::general), |
| 112 | + is_upper(false), |
| 113 | + padding_digits(0) |
| 114 | + {} |
| 115 | + |
| 116 | + int ctx_precision; |
| 117 | + boost::decimal::chars_format fmt; |
| 118 | + bool is_upper; |
| 119 | + int padding_digits; |
| 120 | + |
| 121 | + constexpr auto parse(format_parse_context &ctx) |
| 122 | + { |
| 123 | + const auto res {boost::decimal::detail::parse_impl(ctx)}; |
| 124 | + |
| 125 | + ctx_precision = std::get<0>(res); |
| 126 | + fmt = std::get<1>(res); |
| 127 | + is_upper = std::get<2>(res); |
| 128 | + padding_digits = std::get<3>(res); |
| 129 | + |
| 130 | + return std::get<4>(res); |
| 131 | + } |
| 132 | + |
| 133 | + template <typename FormatContext> |
| 134 | + auto format(const T &v, FormatContext &ctx) const |
| 135 | + { |
| 136 | + auto out = ctx.out(); |
| 137 | + std::array<char, 128> buffer {}; |
| 138 | + const auto r = boost::decimal::to_chars(buffer.data(), buffer.data() + buffer.size(), v, fmt, ctx_precision); |
| 139 | + |
| 140 | + std::string_view sv(buffer.data(), static_cast<std::size_t>(r.ptr - buffer.data())); |
| 141 | + std::string s(sv); |
| 142 | + |
| 143 | + if (is_upper) |
| 144 | + { |
| 145 | + std::transform(s.begin(), s.end(), s.begin(), |
| 146 | + [](unsigned char c) |
| 147 | + { return std::toupper(c); }); |
| 148 | + } |
| 149 | + |
| 150 | + if (s.size() < static_cast<std::size_t>(padding_digits)) |
| 151 | + { |
| 152 | + s.insert(s.begin(), static_cast<std::size_t>(padding_digits) - s.size(), ' '); |
| 153 | + } |
| 154 | + |
| 155 | + return std::copy(s.begin(), s.end(), out); |
| 156 | + } |
| 157 | +}; |
| 158 | + |
| 159 | +} // Namespace std |
| 160 | + |
| 161 | +#endif |
| 162 | + |
| 163 | +#endif //BOOST_DECIMAL_FORMAT_HPP |
0 commit comments