make hex_digits and shifts in basic_data constexpr

This commit is contained in:
Alexey Ochapov 2020-12-25 03:03:23 +03:00
parent 8131d4f094
commit 14ce33e2dc
No known key found for this signature in database
GPG Key ID: 9DC52E8F031B8DA8
2 changed files with 13 additions and 24 deletions

View File

@ -248,9 +248,6 @@ const typename basic_data<T>::digit_pair basic_data<T>::digits[] = {
{'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'}, {'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'},
{'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'}}; {'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'}};
template <typename T>
const char basic_data<T>::hex_digits[] = "0123456789abcdef";
#define FMT_POWERS_OF_10(factor) \ #define FMT_POWERS_OF_10(factor) \
factor * 10, (factor)*100, (factor)*1000, (factor)*10000, (factor)*100000, \ factor * 10, (factor)*100, (factor)*1000, (factor)*10000, (factor)*100000, \
(factor)*1000000, (factor)*10000000, (factor)*100000000, \ (factor)*1000000, (factor)*10000000, (factor)*100000000, \
@ -1071,10 +1068,12 @@ const char basic_data<T>::background_color[] = "\x1b[48;2;";
template <typename T> const char basic_data<T>::reset_color[] = "\x1b[0m"; template <typename T> const char basic_data<T>::reset_color[] = "\x1b[0m";
template <typename T> const wchar_t basic_data<T>::wreset_color[] = L"\x1b[0m"; template <typename T> const wchar_t basic_data<T>::wreset_color[] = L"\x1b[0m";
template <typename T> const char basic_data<T>::signs[] = {0, '-', '+', ' '}; template <typename T> const char basic_data<T>::signs[] = {0, '-', '+', ' '};
template <typename T>
const char basic_data<T>::left_padding_shifts[] = {31, 31, 0, 1, 0}; #if __cplusplus < 201703L
template <typename T> template <typename T> const char basic_data<T>::hex_digits[];
const char basic_data<T>::right_padding_shifts[] = {0, 31, 0, 1, 0}; template <typename T> const char basic_data<T>::left_padding_shifts[];
template <typename T> const char basic_data<T>::right_padding_shifts[];
#endif
template <typename T> struct bits { template <typename T> struct bits {
static FMT_CONSTEXPR_DECL const int value = static FMT_CONSTEXPR_DECL const int value =

View File

@ -933,14 +933,14 @@ template <typename T = void> struct FMT_EXTERN_TEMPLATE_API basic_data {
// GCC generates slightly better code for pairs than chars. // GCC generates slightly better code for pairs than chars.
using digit_pair = char[2]; using digit_pair = char[2];
static const digit_pair digits[]; static const digit_pair digits[];
static const char hex_digits[]; static constexpr const char hex_digits[] = "0123456789abcdef";
static const char foreground_color[]; static const char foreground_color[];
static const char background_color[]; static const char background_color[];
static const char reset_color[5]; static const char reset_color[5];
static const wchar_t wreset_color[5]; static const wchar_t wreset_color[5];
static const char signs[]; static const char signs[];
static const char left_padding_shifts[5]; static constexpr const char left_padding_shifts[] = {31, 31, 0, 1, 0};
static const char right_padding_shifts[5]; static constexpr const char right_padding_shifts[] = {0, 31, 0, 1, 0};
// DEPRECATED! These are for ABI compatibility. // DEPRECATED! These are for ABI compatibility.
static const uint32_t zero_or_powers_of_10_32[]; static const uint32_t zero_or_powers_of_10_32[];
@ -1144,9 +1144,7 @@ FMT_CONSTEXPR20 Char* format_uint(Char* buffer, UInt value, int num_digits,
buffer += num_digits; buffer += num_digits;
Char* end = buffer; Char* end = buffer;
do { do {
const char* digits = upper ? "0123456789ABCDEF" const char* digits = upper ? "0123456789ABCDEF" : data::hex_digits;
: (is_constant_evaluated() ? "0123456789abcdef"
: data::hex_digits);
unsigned digit = (value & ((1 << BASE_BITS) - 1)); unsigned digit = (value & ((1 << BASE_BITS) - 1));
*--buffer = static_cast<Char>(BASE_BITS < 4 ? static_cast<char>('0' + digit) *--buffer = static_cast<Char>(BASE_BITS < 4 ? static_cast<char>('0' + digit)
: digits[digit]); : digits[digit]);
@ -1563,17 +1561,9 @@ FMT_CONSTEXPR20 OutputIt write_padded(OutputIt out,
unsigned spec_width = to_unsigned(specs.width); unsigned spec_width = to_unsigned(specs.width);
size_t padding = spec_width > width ? spec_width - width : 0; size_t padding = spec_width > width ? spec_width - width : 0;
size_t left_padding = 0; size_t left_padding = 0;
if (is_constant_evaluated()) { auto* shifts = align == align::left ? data::left_padding_shifts
const char left_padding_shifts[] = {31, 31, 0, 1, 0}; : data::right_padding_shifts;
const char right_padding_shifts[] = {0, 31, 0, 1, 0}; left_padding = padding >> shifts[specs.align];
auto* shifts =
align == align::left ? left_padding_shifts : right_padding_shifts;
left_padding = padding >> shifts[specs.align];
} else {
auto* shifts = align == align::left ? data::left_padding_shifts
: data::right_padding_shifts;
left_padding = padding >> shifts[specs.align];
}
auto it = reserve(out, size + padding * specs.fill.size()); auto it = reserve(out, size + padding * specs.fill.size());
it = fill(it, left_padding, specs.fill); it = fill(it, left_padding, specs.fill);
it = f(it); it = f(it);