5#if !defined(__XTD_CORE_INTERNAL__)
6#error "Do not include this file: Internal use only"
22template <
typename char_t,
typename floating_po
int_t>
23inline std::basic_string<char_t> __floating_point_to_binary(floating_point_t value,
int precision) {
29 return __binary_formatter<char_t>(data.output, precision);
33inline std::basic_string<char> __floating_point_to_binary<char, long double>(
long double value,
int precision) {
39 return __binary_formatter<char>(data.output, precision);
43inline std::basic_string<wchar_t> __floating_point_to_binary<wchar_t, long double>(
long double value,
int precision) {
49 return __binary_formatter<wchar_t>(data.output, precision);
53inline std::basic_string<char> __floating_point_to_binary<char, double>(
double value,
int precision) {
59 return __binary_formatter<char>(data.output, precision);
63inline std::basic_string<wchar_t> __floating_point_to_binary<wchar_t, double>(
double value,
int precision) {
69 return __binary_formatter<wchar_t>(data.output, precision);
73inline std::basic_string<char> __floating_point_to_binary<char, float>(
float value,
int precision) {
79 return __binary_formatter<char>(data.output, precision);
83inline std::basic_string<wchar_t> __floating_point_to_binary<wchar_t, float>(
float value,
int precision) {
89 return __binary_formatter<wchar_t>(data.output, precision);
92template <
typename char_t,
typename value_t>
93inline std::basic_string<char_t> __floating_point_formatter(
const std::basic_string<char_t>& format, value_t value,
const std::locale& loc) {
95 if (fmt.empty()) fmt = {
'G'};
97 std::vector<char_t> possible_formats {
'b',
'B',
'c',
'C',
'e',
'E',
'f',
'F',
'g',
'G',
'n',
'N',
'p',
'P',
'x',
'X'};
98 if (fmt.size() > 3 || std::find(possible_formats.begin(), possible_formats.end(), fmt[0]) == possible_formats.end() || (fmt.size() >= 2 && !std::isdigit(fmt[1])) || (fmt.size() == 3 && !std::isdigit(fmt[2])))
99 __format_exception(
"Custom format not yet implemented");
103 if (fmt.size() > 1) precision = std::stoi(fmt.substr(1));
105 __format_exception(
"Invalid format expression");
107 if ((fmt[0] ==
'f' || fmt[0] ==
'F' || fmt[0] ==
'n' || fmt[0] ==
'N' || fmt[0] ==
'p' || fmt[0] ==
'P' || fmt[0] ==
'r' || fmt[0] ==
'R') && fmt.size() == 1) precision = 2;
108 if ((fmt[0] ==
'e' || fmt[0] ==
'E') && fmt.size() == 1) precision = 6;
109 if ((fmt[0] ==
'g' || fmt[0] ==
'G') && fmt.size() == 1) precision =
sizeof(value) <= 4 ? 7 : 15;
111 std::basic_string<char_t> fmt_str({
'%',
'.',
'*',
'L'});
114 case 'B':
return __floating_point_to_binary<char_t>(value, precision);
116 case 'C':
return __currency_formatter<char_t>(
static_cast<long double>(value), loc);
122 case 'G':
return __sprintf((fmt_str + fmt[0]).c_str(), precision,
static_cast<long double>(value));
124 case 'N':
return __natural_formatter<char_t>(
static_cast<long double>(value), precision, loc);
125 case 'p':
return __sprintf((fmt_str + char_t(
'f')).c_str(), precision,
static_cast<long double>(value * 100)) + std::basic_string<char_t>({char_t(
' '), char_t(
'%')});
126 case 'P':
return __sprintf((fmt_str + char_t(
'F')).c_str(), precision,
static_cast<long double>(value * 100)) + std::basic_string<char_t>({char_t(
' '), char_t(
'%')});
128 case 'X':
return __hexfloat_formatter<char_t>(
static_cast<long double>(value), precision, loc);
129 default: __format_exception(
"Invalid format expression");
return {};
Contains __format method.