From 14ce33e2dca696145e987a6a7c0ce2f098fbb378 Mon Sep 17 00:00:00 2001 From: Alexey Ochapov Date: Fri, 25 Dec 2020 03:03:23 +0300 Subject: [PATCH] make `hex_digits` and shifts in `basic_data` constexpr --- include/fmt/format-inl.h | 13 ++++++------- include/fmt/format.h | 24 +++++++----------------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 1e38951e..4a527959 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -248,9 +248,6 @@ const typename basic_data::digit_pair basic_data::digits[] = { {'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'}, {'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'}}; -template -const char basic_data::hex_digits[] = "0123456789abcdef"; - #define FMT_POWERS_OF_10(factor) \ factor * 10, (factor)*100, (factor)*1000, (factor)*10000, (factor)*100000, \ (factor)*1000000, (factor)*10000000, (factor)*100000000, \ @@ -1071,10 +1068,12 @@ const char basic_data::background_color[] = "\x1b[48;2;"; template const char basic_data::reset_color[] = "\x1b[0m"; template const wchar_t basic_data::wreset_color[] = L"\x1b[0m"; template const char basic_data::signs[] = {0, '-', '+', ' '}; -template -const char basic_data::left_padding_shifts[] = {31, 31, 0, 1, 0}; -template -const char basic_data::right_padding_shifts[] = {0, 31, 0, 1, 0}; + +#if __cplusplus < 201703L +template const char basic_data::hex_digits[]; +template const char basic_data::left_padding_shifts[]; +template const char basic_data::right_padding_shifts[]; +#endif template struct bits { static FMT_CONSTEXPR_DECL const int value = diff --git a/include/fmt/format.h b/include/fmt/format.h index ec1a7ae1..f5c380ae 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -933,14 +933,14 @@ template struct FMT_EXTERN_TEMPLATE_API basic_data { // GCC generates slightly better code for pairs than chars. using digit_pair = char[2]; 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 background_color[]; static const char reset_color[5]; static const wchar_t wreset_color[5]; static const char signs[]; - static const char left_padding_shifts[5]; - static const char right_padding_shifts[5]; + static constexpr const char left_padding_shifts[] = {31, 31, 0, 1, 0}; + static constexpr const char right_padding_shifts[] = {0, 31, 0, 1, 0}; // DEPRECATED! These are for ABI compatibility. 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; Char* end = buffer; do { - const char* digits = upper ? "0123456789ABCDEF" - : (is_constant_evaluated() ? "0123456789abcdef" - : data::hex_digits); + const char* digits = upper ? "0123456789ABCDEF" : data::hex_digits; unsigned digit = (value & ((1 << BASE_BITS) - 1)); *--buffer = static_cast(BASE_BITS < 4 ? static_cast('0' + digit) : digits[digit]); @@ -1563,17 +1561,9 @@ FMT_CONSTEXPR20 OutputIt write_padded(OutputIt out, unsigned spec_width = to_unsigned(specs.width); size_t padding = spec_width > width ? spec_width - width : 0; size_t left_padding = 0; - if (is_constant_evaluated()) { - const char left_padding_shifts[] = {31, 31, 0, 1, 0}; - const char right_padding_shifts[] = {0, 31, 0, 1, 0}; - 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* 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()); it = fill(it, left_padding, specs.fill); it = f(it);