Reduce template instantiations

This commit is contained in:
Victor Zverovich
2022-12-30 07:30:28 -08:00
parent 7e5a959564
commit 8fe4d97d5e
2 changed files with 18 additions and 20 deletions

View File

@ -2181,14 +2181,14 @@ template <typename Char> constexpr bool is_ascii_letter(Char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
} }
// Converts a character to ASCII. Returns a number > 127 on conversion failure. // Converts a character to ASCII. Returns '\0' on conversion failure.
template <typename Char, FMT_ENABLE_IF(std::is_integral<Char>::value)> template <typename Char, FMT_ENABLE_IF(std::is_integral<Char>::value)>
constexpr auto to_ascii(Char c) -> Char { constexpr auto to_ascii(Char c) -> char {
return c; return c <= 0xff ? static_cast<char>(c) : '\0';
} }
template <typename Char, FMT_ENABLE_IF(std::is_enum<Char>::value)> template <typename Char, FMT_ENABLE_IF(std::is_enum<Char>::value)>
constexpr auto to_ascii(Char c) -> underlying_t<Char> { constexpr auto to_ascii(Char c) -> char {
return c; return c <= 0xff ? static_cast<char>(c) : '\0';
} }
FMT_CONSTEXPR inline auto code_point_length_impl(char c) -> int { FMT_CONSTEXPR inline auto code_point_length_impl(char c) -> int {
@ -2295,8 +2295,8 @@ FMT_CONSTEXPR auto parse_align(const Char* begin, const Char* end)
return {begin, align}; return {begin, align};
} }
template <typename Char> FMT_CONSTEXPR bool is_name_start(Char c) { template <typename Char> constexpr auto is_name_start(Char c) -> bool {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || '_' == c; return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_';
} }
template <typename Char, typename Handler> template <typename Char, typename Handler>
@ -2394,9 +2394,9 @@ FMT_CONSTEXPR auto parse_precision(const Char* begin, const Char* end,
return parse_dynamic_spec(begin, end, value, ref, ctx); return parse_dynamic_spec(begin, end, value, ref, ctx);
} }
template <typename Char> FMT_CONSTEXPR inline auto parse_presentation_type(char type)
FMT_CONSTEXPR auto parse_presentation_type(Char type) -> presentation_type { -> presentation_type {
switch (to_ascii(type)) { switch (type) {
case 'd': case 'd':
return presentation_type::dec; return presentation_type::dec;
case 'o': case 'o':
@ -2434,6 +2434,7 @@ FMT_CONSTEXPR auto parse_presentation_type(Char type) -> presentation_type {
case '?': case '?':
return presentation_type::debug; return presentation_type::debug;
default: default:
throw_format_error("invalid type specifier");
return presentation_type::none; return presentation_type::none;
} }
} }
@ -2450,9 +2451,7 @@ FMT_CONSTEXPR FMT_INLINE auto parse_format_specs(
basic_format_parse_context<Char>& ctx, type arg_type) -> const Char* { basic_format_parse_context<Char>& ctx, type arg_type) -> const Char* {
if (1 < end - begin && begin[1] == '}' && is_ascii_letter(*begin) && if (1 < end - begin && begin[1] == '}' && is_ascii_letter(*begin) &&
*begin != 'L') { *begin != 'L') {
specs.type = parse_presentation_type(*begin++); specs.type = parse_presentation_type(to_ascii(*begin++));
if (specs.type == presentation_type::none)
throw_format_error("invalid type specifier");
return begin; return begin;
} }
if (begin == end) return begin; if (begin == end) return begin;
@ -2529,11 +2528,8 @@ FMT_CONSTEXPR FMT_INLINE auto parse_format_specs(
} }
// Parse type. // Parse type.
if (begin != end && *begin != '}') { if (begin != end && *begin != '}')
specs.type = parse_presentation_type(*begin++); specs.type = parse_presentation_type(to_ascii(*begin++));
if (specs.type == presentation_type::none)
throw_format_error("invalid type specifier");
}
return begin; return begin;
} }

View File

@ -120,10 +120,12 @@ struct custom_char {
template <typename T> template <typename T>
constexpr custom_char(T val) : value(static_cast<int>(val)) {} constexpr custom_char(T val) : value(static_cast<int>(val)) {}
operator int() const { return value; } operator char() const {
return value <= 0xff ? static_cast<char>(value) : '\0';
}
}; };
int to_ascii(custom_char c) { return c; } auto to_ascii(custom_char c) -> char { return c; }
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
template <> struct is_char<custom_char> : std::true_type {}; template <> struct is_char<custom_char> : std::true_type {};