diff --git a/libcxx/include/__format/formatter_bool.h b/libcxx/include/__format/formatter_bool.h --- a/libcxx/include/__format/formatter_bool.h +++ b/libcxx/include/__format/formatter_bool.h @@ -47,6 +47,7 @@ _LIBCPP_HIDE_FROM_ABI auto format(bool __value, auto& __ctx) const -> decltype(__ctx.out()) { switch (__parser_.__type_) { + case __format_spec::__type::__default: case __format_spec::__type::__string: return __formatter::__format_bool(__value, __ctx, __parser_.__get_parsed_std_specifications(__ctx)); diff --git a/libcxx/include/__format/formatter_char.h b/libcxx/include/__format/formatter_char.h --- a/libcxx/include/__format/formatter_char.h +++ b/libcxx/include/__format/formatter_char.h @@ -41,7 +41,7 @@ } _LIBCPP_HIDE_FROM_ABI auto format(_CharT __value, auto& __ctx) const -> decltype(__ctx.out()) { - if (__parser_.__type_ == __format_spec::__type::__char) + if (__parser_.__type_ == __format_spec::__type::__default || __parser_.__type_ == __format_spec::__type::__char) return __formatter::__format_char(__value, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); if constexpr (sizeof(_CharT) <= sizeof(int)) diff --git a/libcxx/include/__format/formatter_integral.h b/libcxx/include/__format/formatter_integral.h --- a/libcxx/include/__format/formatter_integral.h +++ b/libcxx/include/__format/formatter_integral.h @@ -207,10 +207,6 @@ char* __end, const char* __prefix, int __base) -> decltype(__ctx.out()) { - _LIBCPP_ASSERT( - __specs.__alignment_ != __format_spec::__alignment::__default, - "the caller should adjust the default to the value required by the type"); - char* __first = __formatter::__insert_sign(__begin, __negative, __specs.__std_.__sign_); if (__specs.__std_.__alternate_form_ && __prefix) while (*__prefix) @@ -280,6 +276,7 @@ return __formatter::__format_integer( __value, __ctx, __specs, __negative, __array.begin(), __array.end(), __value != 0 ? "0" : nullptr, 8); } + case __format_spec::__type::__default: case __format_spec::__type::__decimal: { array()> __array; return __formatter::__format_integer( diff --git a/libcxx/include/__format/formatter_output.h b/libcxx/include/__format/formatter_output.h --- a/libcxx/include/__format/formatter_output.h +++ b/libcxx/include/__format/formatter_output.h @@ -59,14 +59,11 @@ _LIBCPP_HIDE_FROM_ABI constexpr __padding_size_result __padding_size(size_t __size, size_t __width, __format_spec::__alignment __align) { _LIBCPP_ASSERT(__width > __size, "don't call this function when no padding is required"); - _LIBCPP_ASSERT(__align != __format_spec::__alignment::__default, - "the caller should adjust the default to the value required by the type"); _LIBCPP_ASSERT(__align != __format_spec::__alignment::__zero_padding, "the caller should have handled the zero-padding"); size_t __fill = __width - __size; switch (__align) { - case __format_spec::__alignment::__default: case __format_spec::__alignment::__zero_padding: __libcpp_unreachable(); @@ -81,6 +78,7 @@ size_t __after = __fill - __before; return {__before, __after}; } + case __format_spec::__alignment::__default: case __format_spec::__alignment::__right: return {__fill, 0}; } @@ -91,9 +89,6 @@ _LIBCPP_HIDE_FROM_ABI _OutIt __write_using_decimal_separators(_OutIt __out_it, const char* __begin, const char* __first, const char* __last, string&& __grouping, _CharT __sep, __format_spec::__parsed_specifications<_CharT> __specs) { - _LIBCPP_ASSERT(__specs.__alignment_ != __format_spec::__alignment::__default, - "the caller should adjust the default to the value required by the type"); - int __size = (__first - __begin) + // [sign][prefix] (__last - __first) + // data (__grouping.size() - 1); // number of separator characters diff --git a/libcxx/include/__format/parser_std_format_spec.h b/libcxx/include/__format/parser_std_format_spec.h --- a/libcxx/include/__format/parser_std_format_spec.h +++ b/libcxx/include/__format/parser_std_format_spec.h @@ -1040,18 +1040,10 @@ __format_spec::__process_display_type_bool_string(__parser); } -template -_LIBCPP_HIDE_FROM_ABI constexpr void __process_display_type_integer(__parser<_CharT>& __parser) { - if (__parser.__alignment_ == __alignment::__default) - __parser.__alignment_ = __alignment::__right; -} - template _LIBCPP_HIDE_FROM_ABI constexpr void __process_parsed_bool(__parser<_CharT>& __parser) { switch (__parser.__type_) { case __format_spec::__type::__default: - __parser.__type_ = __format_spec::__type::__string; - [[fallthrough]]; case __format_spec::__type::__string: __format_spec::__process_display_type_bool_string(__parser); break; @@ -1062,7 +1054,6 @@ case __format_spec::__type::__decimal: case __format_spec::__type::__hexadecimal_lower_case: case __format_spec::__type::__hexadecimal_upper_case: - __process_display_type_integer(__parser); break; default: @@ -1074,8 +1065,6 @@ _LIBCPP_HIDE_FROM_ABI constexpr void __process_parsed_char(__parser<_CharT>& __parser) { switch (__parser.__type_) { case __format_spec::__type::__default: - __parser.__type_ = __format_spec::__type::__char; - [[fallthrough]]; case __format_spec::__type::__char: __format_spec::__process_display_type_char(__parser); break; @@ -1086,7 +1075,6 @@ case __format_spec::__type::__decimal: case __format_spec::__type::__hexadecimal_lower_case: case __format_spec::__type::__hexadecimal_upper_case: - __format_spec::__process_display_type_integer(__parser); break; default: @@ -1098,15 +1086,12 @@ _LIBCPP_HIDE_FROM_ABI constexpr void __process_parsed_integer(__parser<_CharT>& __parser) { switch (__parser.__type_) { case __format_spec::__type::__default: - __parser.__type_ = __format_spec::__type::__decimal; - [[fallthrough]]; case __format_spec::__type::__binary_lower_case: case __format_spec::__type::__binary_upper_case: case __format_spec::__type::__octal: case __format_spec::__type::__decimal: case __format_spec::__type::__hexadecimal_lower_case: case __format_spec::__type::__hexadecimal_upper_case: - __format_spec::__process_display_type_integer(__parser); break; case __format_spec::__type::__char: @@ -1120,8 +1105,6 @@ template _LIBCPP_HIDE_FROM_ABI constexpr void __process_parsed_floating_point(__parser<_CharT>& __parser) { - __format_spec::__process_display_type_integer(__parser); - switch (__parser.__type_) { case __format_spec::__type::__default: // When no precision specified then it keeps default since that diff --git a/libcxx/include/format b/libcxx/include/format --- a/libcxx/include/format +++ b/libcxx/include/format @@ -376,6 +376,7 @@ __format::__parse_number_result __r = __format::__parse_arg_id(__begin, __end, __parse_ctx); + bool __parse = *__r.__ptr == _CharT(':'); switch (*__r.__ptr) { case _CharT(':'): // The arg-id has a format-specifier, advance the input to the format-spec. @@ -395,7 +396,7 @@ if (__type == __arg_t::__handle) __ctx.__handle(__r.__value).__parse(__parse_ctx); else - __format::__compile_time_visit_format_arg(__parse_ctx, __ctx, __type); + __format::__compile_time_visit_format_arg(__parse_ctx, __ctx, __type); } else _VSTD::visit_format_arg( [&](auto __arg) { @@ -405,7 +406,8 @@ __arg.format(__parse_ctx, __ctx); else { formatter __formatter; - __parse_ctx.advance_to(__formatter.parse(__parse_ctx)); + if (__parse) + __parse_ctx.advance_to(__formatter.parse(__parse_ctx)); __ctx.advance_to(__formatter.format(__arg, __ctx)); } },