xtd
1.0.0
Toggle main menu visibility
Loading...
Searching...
No Matches
xtd.core
include
xtd
internal
__numeric_formatter.hpp
Go to the documentation of this file.
1
3
#pragma once
5
#if !defined(__XTD_CORE_INTERNAL__)
6
#error "Do not include this file: Internal use only"
7
#endif
9
10
#include "
__binary_formatter.hpp
"
11
#include "
__floating_point_formatter.hpp
"
12
#include "
__sprintf.hpp
"
13
#include <algorithm>
14
16
template
<
typename
char
_t,
typename
value_t>
17
[[nodiscard]]
inline
auto
__numeric_formatter(
const
std::basic_string<char_t>& fmt, value_t value,
const
std::locale& loc) -> std::basic_string<char_t> {
18
auto
format
= fmt;
19
if
(
format
.empty())
format
= {
'G'
};
20
21
auto
possible_formats = {
'b'
,
'B'
,
'c'
,
'C'
,
'd'
,
'D'
,
'e'
,
'E'
,
'f'
,
'F'
,
'g'
,
'G'
,
'n'
,
'N'
,
'o'
,
'O'
,
'p'
,
'P'
,
'x'
,
'X'
};
22
if
(
format
.size() > 3 || std::find(possible_formats.begin(), possible_formats.end(), format[0]) == possible_formats.end() || (
format
.size() >= 2 && !std::isdigit(format[1])) || (
format
.size() == 3 && !std::isdigit(format[2])))
23
xtd::helpers::throw_helper::throws
(
xtd::helpers::exception_case::format
,
"Custom format not yet implemented"
);
24
25
int
precision = 0;
26
if
(format[0] ==
'b'
|| format[0] ==
'B'
|| format[0] ==
'd'
|| format[0] ==
'D'
|| format[0] ==
'o'
|| format[0] ==
'O'
|| format[0] ==
'x'
|| format[0] ==
'X'
) {
27
try
{
28
for
(
auto
c :
format
.substr(1))
29
if
(!std::isdigit(c) && c !=
' '
&& c !=
'+'
&& c !=
'-'
)
xtd::helpers::throw_helper::throws
(
xtd::helpers::exception_case::format
,
"Invalid format expression"
);
30
if
(
format
.size() > 1) precision = std::stoi(
format
.substr(1));
31
}
catch
(...) {
32
xtd::helpers::throw_helper::throws
(
xtd::helpers::exception_case::format
,
"Invalid format expression"
);
33
}
34
if
((format[0] ==
'd'
|| format[0] ==
'D'
) && precision > 0 && value < 0) precision += 1;
35
if
((format[0] ==
'd'
|| format[0] ==
'D'
) && precision < 0 && value < 0) precision -= 1;
36
}
37
38
std::basic_string<char_t> fmt_str({
'%'
,
'0'
,
'*'
,
'l'
,
'l'
});
39
switch
(format[0]) {
40
case
'b'
:
41
case
'B'
:
return
__binary_formatter<char_t>(value, precision);
42
case
'd'
:
43
case
'D'
:
44
case
'G'
:
return
__sprintf((fmt_str + char_t(std::is_unsigned<value_t>::value ?
'u'
:
'd'
)).c_str(), precision,
static_cast<
long
long
>
(value));
45
case
'o'
:
46
case
'O'
:
return
__sprintf((fmt_str + char_t(
'o'
)).c_str(), precision,
static_cast<
long
long
>
(value));
47
case
'x'
:
48
case
'X'
:
return
__sprintf((fmt_str + format[0]).c_str(), precision,
static_cast<
long
long
>
(value));
49
default
:
return
__floating_point_formatter(format,
static_cast<
long
double
>
(value), loc);
50
}
51
}
__binary_formatter.hpp
__floating_point_formatter.hpp
__sprintf.hpp
xtd::helpers::throw_helper::throws
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
xtd::format
auto format(const xtd::string &fmt, args_t &&... args) -> xtd::string
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition
format.hpp:21
xtd::helpers::exception_case::format
@ format
The format is not valid.
Definition
exception_case.hpp:51
Generated on
for xtd by
Gammasoft
. All rights reserved.