| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_FORMAT_SIMPLE_FLOATINGPOINT_HPP |
| 4 |
#define MPT_FORMAT_SIMPLE_FLOATINGPOINT_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
|
| 10 |
#if !defined(MPT_LIBCXX_QUIRK_NO_TO_CHARS_FLOAT) |
| 11 |
#define MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 1 |
| 12 |
#else |
| 13 |
#define MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 0 |
| 14 |
#endif |
| 15 |
|
| 16 |
#if MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 |
| 17 |
#include "mpt/base/algorithm.hpp" |
| 18 |
#endif |
| 19 |
#include "mpt/base/namespace.hpp" |
| 20 |
#include "mpt/format/default_floatingpoint.hpp" |
| 21 |
#include "mpt/format/helpers.hpp" |
| 22 |
#include "mpt/format/simple_spec.hpp" |
| 23 |
#include "mpt/string/utility.hpp" |
| 24 |
#include "mpt/string_transcode/transcode.hpp" |
| 25 |
|
| 26 |
#if MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 |
| 27 |
#include <charconv> |
| 28 |
#endif |
| 29 |
#if !MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 |
| 30 |
#include <iomanip> |
| 31 |
#include <ios> |
| 32 |
#endif |
| 33 |
#if MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 |
| 34 |
#include <iterator> |
| 35 |
#endif |
| 36 |
#if !MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 |
| 37 |
#include <limits> |
| 38 |
#include <locale> |
| 39 |
#include <sstream> |
| 40 |
#endif |
| 41 |
#include <string> |
| 42 |
#if MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 |
| 43 |
#include <system_error> |
| 44 |
#endif |
| 45 |
#include <type_traits> |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
namespace mpt { |
| 50 |
inline namespace MPT_INLINE_NS { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#if MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 |
| 55 |
|
| 56 |
|
| 57 |
template <typename Tstring, typename T> |
| 58 |
inline Tstring format_simple_floatingpoint_to_chars(const T & x, std::chars_format fmt) { |
| 59 |
std::string str(1, '\0'); |
| 60 |
bool done = false; |
| 61 |
while (!done) { |
| 62 |
std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), x, fmt); |
| 63 |
if (result.ec != std::errc{}) { |
| 64 |
str.resize(mpt::exponential_grow(str.size()), '\0'); |
| 65 |
} else { |
| 66 |
str.resize(result.ptr - str.data()); |
| 67 |
done = true; |
| 68 |
} |
| 69 |
} |
| 70 |
return mpt::convert_formatted_simple<Tstring>(str); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
template <typename Tstring, typename T> |
| 75 |
inline Tstring format_simple_floatingpoint_to_chars(const T & x, std::chars_format fmt, int precision) { |
| 76 |
std::string str(1, '\0'); |
| 77 |
bool done = false; |
| 78 |
while (!done) { |
| 79 |
std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), x, fmt, precision); |
| 80 |
if (result.ec != std::errc{}) { |
| 81 |
str.resize(mpt::exponential_grow(str.size()), '\0'); |
| 82 |
} else { |
| 83 |
str.resize(result.ptr - str.data()); |
| 84 |
done = true; |
| 85 |
} |
| 86 |
} |
| 87 |
return mpt::convert_formatted_simple<Tstring>(str); |
| 88 |
} |
| 89 |
|
| 90 |
template <typename Tstring> |
| 91 |
inline Tstring format_simple_floatingpoint_postprocess_width(Tstring str, const format_simple_spec<Tstring> & format) { |
| 92 |
format_simple_flags f = format.GetFlags(); |
| 93 |
std::size_t width = format.GetWidth(); |
| 94 |
if (f & format_simple_base::FillNul) { |
| 95 |
auto pos = str.begin(); |
| 96 |
if (str.length() > 0) { |
| 97 |
if (str[0] == mpt::char_constants<typename Tstring::value_type>::plus) { |
| 98 |
pos++; |
| 99 |
width++; |
| 100 |
} else if (str[0] == mpt::char_constants<typename Tstring::value_type>::minus) { |
| 101 |
pos++; |
| 102 |
width++; |
| 103 |
} |
| 104 |
} |
| 105 |
if (str.length() - std::distance(str.begin(), pos) < width) { |
| 106 |
str.insert(pos, width - str.length() - std::distance(str.begin(), pos), mpt::char_constants<typename Tstring::value_type>::number0); |
| 107 |
} |
| 108 |
} else { |
| 109 |
if (str.length() < width) { |
| 110 |
str.insert(0, width - str.length(), mpt::char_constants<typename Tstring::value_type>::space); |
| 111 |
} |
| 112 |
} |
| 113 |
return str; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
template <typename Tstring, typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true> |
| 118 |
inline Tstring format_simple(const T & x, const format_simple_spec<Tstring> & format) { |
| 119 |
using format_string_type = typename mpt::select_format_string_type<Tstring>::type; |
| 120 |
const format_simple_spec<format_string_type> f = mpt::transcode_format_simple_spec<format_string_type>(format); |
| 121 |
if (f.GetPrecision() != -1) { |
| 122 |
if (f.GetFlags() & format_simple_base::NotaSci) { |
| 123 |
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_postprocess_width(format_simple_floatingpoint_to_chars<format_string_type>(x, std::chars_format::scientific, f.GetPrecision()), f)); |
| 124 |
} else if (f.GetFlags() & format_simple_base::NotaFix) { |
| 125 |
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_postprocess_width(format_simple_floatingpoint_to_chars<format_string_type>(x, std::chars_format::fixed, f.GetPrecision()), f)); |
| 126 |
} else { |
| 127 |
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_postprocess_width(format_simple_floatingpoint_to_chars<format_string_type>(x, std::chars_format::general, f.GetPrecision()), f)); |
| 128 |
} |
| 129 |
} else { |
| 130 |
if (f.GetFlags() & format_simple_base::NotaSci) { |
| 131 |
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_postprocess_width(format_simple_floatingpoint_to_chars<format_string_type>(x, std::chars_format::scientific), f)); |
| 132 |
} else if (f.GetFlags() & format_simple_base::NotaFix) { |
| 133 |
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_postprocess_width(format_simple_floatingpoint_to_chars<format_string_type>(x, std::chars_format::fixed), f)); |
| 134 |
} else { |
| 135 |
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_postprocess_width(format_simple_floatingpoint_to_chars<format_string_type>(x, std::chars_format::general), f)); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
#else // !MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 |
| 142 |
|
| 143 |
|
| 144 |
template <typename Tchar> |
| 145 |
struct NumPunct : std::numpunct<Tchar> { |
| 146 |
private: |
| 147 |
unsigned int group; |
| 148 |
Tchar sep; |
| 149 |
|
| 150 |
public: |
| 151 |
NumPunct(unsigned int g, Tchar s) |
| 152 |
: group(g) |
| 153 |
, sep(s) { } |
| 154 |
std::string do_grouping() const override { |
| 155 |
return std::string(1, static_cast<char>(group)); |
| 156 |
} |
| 157 |
Tchar do_thousands_sep() const override { |
| 158 |
return sep; |
| 159 |
} |
| 160 |
}; |
| 161 |
|
| 162 |
template <typename Tostream, typename Tstring, typename T> |
| 163 |
inline void format_simple_floatingpoint_apply_stream_format(Tostream & o, const format_simple_spec<Tstring> & format, const T &) { |
| 164 |
if (format.GetGroup() > 0) { |
| 165 |
if (mpt::string_traits<Tstring>::length(format.GetGroupSep()) >= 1) { |
| 166 |
o.imbue(std::locale(o.getloc(), new NumPunct<typename Tostream::char_type>(format.GetGroup(), format.GetGroupSep()[0]))); |
| 167 |
} |
| 168 |
} |
| 169 |
format_simple_flags f = format.GetFlags(); |
| 170 |
std::size_t width = format.GetWidth(); |
| 171 |
int precision = format.GetPrecision(); |
| 172 |
if (precision != -1 && width != 0 && !(f & format_simple_base::NotaFix) && !(f & format_simple_base::NotaSci)) { |
| 173 |
// fixup: |
| 174 |
// precision behaves differently from .# |
| 175 |
// avoid default format when precision and width are set |
| 176 |
f &= ~format_simple_base::NotaNrm; |
| 177 |
f |= format_simple_base::NotaFix; |
| 178 |
} |
| 179 |
if (f & format_simple_base::BaseDec) { |
| 180 |
o << std::dec; |
| 181 |
} else if (f & format_simple_base::BaseHex) { |
| 182 |
o << std::hex; |
| 183 |
} |
| 184 |
if (f & format_simple_base::NotaNrm) { /*nothing*/ |
| 185 |
} else if (f & format_simple_base::NotaFix) { |
| 186 |
o << std::setiosflags(std::ios::fixed); |
| 187 |
} else if (f & format_simple_base::NotaSci) { |
| 188 |
o << std::setiosflags(std::ios::scientific); |
| 189 |
} |
| 190 |
if (f & format_simple_base::CaseLow) { |
| 191 |
o << std::nouppercase; |
| 192 |
} else if (f & format_simple_base::CaseUpp) { |
| 193 |
o << std::uppercase; |
| 194 |
} |
| 195 |
if (f & format_simple_base::FillOff) { /* nothing */ |
| 196 |
} else if (f & format_simple_base::FillNul) { |
| 197 |
o << std::setw(width) << std::setfill(mpt::char_constants<typename Tostream::char_type>::number0); |
| 198 |
} |
| 199 |
if (precision != -1) { |
| 200 |
o << std::setprecision(precision); |
| 201 |
} else { |
| 202 |
if constexpr (std::is_floating_point<T>::value) { |
| 203 |
if (f & format_simple_base::NotaNrm) { |
| 204 |
o << std::setprecision(std::numeric_limits<T>::max_digits10); |
| 205 |
} else if (f & format_simple_base::NotaFix) { |
| 206 |
o << std::setprecision(std::numeric_limits<T>::digits10); |
| 207 |
} else if (f & format_simple_base::NotaSci) { |
| 208 |
o << std::setprecision(std::numeric_limits<T>::max_digits10 - 1); |
| 209 |
} else { |
| 210 |
o << std::setprecision(std::numeric_limits<T>::max_digits10); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
template <typename Tstring, typename T> |
| 218 |
inline Tstring format_simple_floatingpoint_stream(const T & x, const format_simple_spec<Tstring> & f) { |
| 219 |
using stream_char_type = typename mpt::select_format_char_type<typename Tstring::value_type>::type; |
| 220 |
std::basic_ostringstream<stream_char_type> s; |
| 221 |
s.imbue(std::locale::classic()); |
| 222 |
mpt::format_simple_floatingpoint_apply_stream_format(s, f, x); |
| 223 |
s << x; |
| 224 |
return mpt::convert_formatted_simple<Tstring>(s.str()); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
template <typename Tstring, typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true> |
| 229 |
inline Tstring format_simple(const T & x, const format_simple_spec<Tstring> & format) { |
| 230 |
using format_string_type = typename mpt::select_format_string_type<Tstring>::type; |
| 231 |
const format_simple_spec<format_string_type> f = mpt::transcode_format_simple_spec<format_string_type>(format); |
| 232 |
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_stream<format_string_type>(x, f)); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
#endif // MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
} // namespace MPT_INLINE_NS |
| 242 |
} // namespace mpt |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
#endif // MPT_FORMAT_SIMPLE_FLOATINGPOINT_HPP |