xtd 0.2.0
Loading...
Searching...
No Matches
__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
13template<typename char_t, typename value_t>
14inline std::basic_string<char_t> __binary_formatter(value_t value, int precision) {
15 std::basic_string<char_t> result = std::bitset<sizeof(value) * 8>(value).to_string(char_t('0'), char_t('1'));
16 while (result[0] != 0 && result[0] == char_t('0'))
17 result.erase(0, 1);
18 if (precision < 0 && result.size() > size_t(std::abs(precision)))
19 result.append(result.size() - std::abs(precision), char_t(' '));
20 else {
21 if (precision == 0) precision = 1;
22 if (size_t(precision) > result.size())
23 result.insert(0, size_t(precision) - result.size(), char_t('0'));
24 }
25 return result;
26}