xtd
1.0.0
Toggle main menu visibility
Loading...
Searching...
No Matches
xtd.core
include
xtd
internal
__binary_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 <bitset>
11
#include <cmath>
12
14
template
<
typename
char
_t,
typename
value_t>
15
[[nodiscard]]
inline
auto
__binary_formatter(value_t value,
int
precision) -> std::basic_string<char_t> {
16
std::basic_string<char_t> result = std::bitset<sizeof(value) * 8>(value).to_string(char_t(
'0'
), char_t(
'1'
));
17
while
(result[0] != 0 && result[0] == char_t(
'0'
))
18
result.erase(0, 1);
19
if
(precision < 0 && result.size() >
size_t
(std::abs(precision)))
20
result.append(result.size() - std::abs(precision), char_t(
' '
));
21
else
{
22
if
(precision == 0) precision = 1;
23
if
(
size_t
(precision) > result.size())
24
result.insert(0,
size_t
(precision) - result.size(), char_t(
'0'
));
25
}
26
return
result;
27
}
Generated on
for xtd by
Gammasoft
. All rights reserved.