Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement formattable, range_format, and format_kind #4116

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions stl/inc/chrono
Original file line number Diff line number Diff line change
Expand Up @@ -5799,67 +5799,69 @@ private:
_CHRONO _Chrono_formatter<_CharT> _Impl;
};

template <class _Rep, class _Period, class _CharT>
// Per LWG-3997, the _CharT template parameter is constrained to supported character types.
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved

template <class _Rep, class _Period, _Format_supported_charT _CharT>
struct formatter<_CHRONO duration<_Rep, _Period>, _CharT>
: _Fill_tm_formatter<_CHRONO duration<_Rep, _Period>, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO day, _CharT> : _Fill_tm_formatter<_CHRONO day, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO month, _CharT> : _Fill_tm_formatter<_CHRONO month, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO year, _CharT> : _Fill_tm_formatter<_CHRONO year, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO weekday, _CharT> : _Fill_tm_formatter<_CHRONO weekday, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO weekday_indexed, _CharT> : _Fill_tm_formatter<_CHRONO weekday_indexed, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO weekday_last, _CharT> : _Fill_tm_formatter<_CHRONO weekday_last, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO month_day, _CharT> : _Fill_tm_formatter<_CHRONO month_day, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO month_day_last, _CharT> : _Fill_tm_formatter<_CHRONO month_day_last, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO month_weekday, _CharT> : _Fill_tm_formatter<_CHRONO month_weekday, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO month_weekday_last, _CharT> : _Fill_tm_formatter<_CHRONO month_weekday_last, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO year_month, _CharT> : _Fill_tm_formatter<_CHRONO year_month, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO year_month_day, _CharT> : _Fill_tm_formatter<_CHRONO year_month_day, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO year_month_day_last, _CharT> : _Fill_tm_formatter<_CHRONO year_month_day_last, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO year_month_weekday, _CharT> : _Fill_tm_formatter<_CHRONO year_month_weekday, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO year_month_weekday_last, _CharT>
: _Fill_tm_formatter<_CHRONO year_month_weekday_last, _CharT> {};

template <class _Rep, class _Period, class _CharT>
template <class _Rep, class _Period, _Format_supported_charT _CharT>
struct formatter<_CHRONO hh_mm_ss<_CHRONO duration<_Rep, _Period>>, _CharT>
: _Fill_tm_formatter<_CHRONO hh_mm_ss<_CHRONO duration<_Rep, _Period>>, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO sys_info, _CharT> : _Fill_tm_formatter<_CHRONO sys_info, _CharT> {};

template <class _CharT>
template <_Format_supported_charT _CharT>
struct formatter<_CHRONO local_info, _CharT> : _Fill_tm_formatter<_CHRONO local_info, _CharT> {};

template <class _Duration, class _CharT>
template <class _Duration, _Format_supported_charT _CharT>
struct formatter<_CHRONO sys_time<_Duration>, _CharT> {
constexpr auto parse(basic_format_parse_context<_CharT>& _Parse_ctx) {
return _Impl.template _Parse<_CHRONO sys_time<_Duration>>(_Parse_ctx);
Expand All @@ -5874,7 +5876,7 @@ private:
_CHRONO _Chrono_formatter<_CharT> _Impl{_STATICALLY_WIDEN(_CharT, "UTC")};
};

template <class _Duration, class _CharT>
template <class _Duration, _Format_supported_charT _CharT>
struct formatter<_CHRONO utc_time<_Duration>, _CharT> {
constexpr auto parse(basic_format_parse_context<_CharT>& _Parse_ctx) {
return _Impl.template _Parse<_CHRONO utc_time<_Duration>>(_Parse_ctx);
Expand All @@ -5890,7 +5892,7 @@ private:
_CHRONO _Chrono_formatter<_CharT> _Impl{_STATICALLY_WIDEN(_CharT, "UTC")};
};

template <class _Duration, class _CharT>
template <class _Duration, _Format_supported_charT _CharT>
struct formatter<_CHRONO tai_time<_Duration>, _CharT> {
constexpr auto parse(basic_format_parse_context<_CharT>& _Parse_ctx) {
return _Impl.template _Parse<_CHRONO tai_time<_Duration>>(_Parse_ctx);
Expand All @@ -5909,7 +5911,7 @@ private:
_CHRONO _Chrono_formatter<_CharT> _Impl{_STATICALLY_WIDEN(_CharT, "TAI")};
};

template <class _Duration, class _CharT>
template <class _Duration, _Format_supported_charT _CharT>
struct formatter<_CHRONO gps_time<_Duration>, _CharT> {
constexpr auto parse(basic_format_parse_context<_CharT>& _Parse_ctx) {
return _Impl.template _Parse<_CHRONO gps_time<_Duration>>(_Parse_ctx);
Expand All @@ -5928,7 +5930,7 @@ private:
_CHRONO _Chrono_formatter<_CharT> _Impl{_STATICALLY_WIDEN(_CharT, "GPS")};
};

template <class _Duration, class _CharT>
template <class _Duration, _Format_supported_charT _CharT>
struct formatter<_CHRONO file_time<_Duration>, _CharT> {
constexpr auto parse(basic_format_parse_context<_CharT>& _Parse_ctx) {
return _Impl.template _Parse<_CHRONO file_time<_Duration>>(_Parse_ctx);
Expand All @@ -5945,14 +5947,14 @@ private:
_CHRONO _Chrono_formatter<_CharT> _Impl{_STATICALLY_WIDEN(_CharT, "UTC")};
};

template <class _Duration, class _CharT>
template <class _Duration, _Format_supported_charT _CharT>
struct formatter<_CHRONO local_time<_Duration>, _CharT> : _Fill_tm_formatter<_CHRONO local_time<_Duration>, _CharT> {};

template <class _Duration, class _CharT>
template <class _Duration, _Format_supported_charT _CharT>
struct formatter<_CHRONO _Local_time_format_t<_Duration>, _CharT>
: _Fill_tm_formatter<_CHRONO _Local_time_format_t<_Duration>, _CharT> {};

template <class _Duration, class _TimeZonePtr, class _CharT>
template <class _Duration, class _TimeZonePtr, _Format_supported_charT _CharT>
struct formatter<_CHRONO zoned_time<_Duration, _TimeZonePtr>, _CharT>
: formatter<_CHRONO _Local_time_format_t<_Duration>, _CharT> {

Expand Down
60 changes: 60 additions & 0 deletions stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -2229,6 +2229,27 @@ public:
}
};

#if _HAS_CXX23
template <class _CharT>
struct _Phony_fmt_iter_for {
using iterator_category = output_iterator_tag;
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
using value_type = _CharT;
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
using difference_type = ptrdiff_t;
using pointer = _CharT*;
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
using reference = _CharT&;

reference operator*() const;
pointer operator->() const;
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved

_Phony_fmt_iter_for& operator++();
_Phony_fmt_iter_for operator++(int);
};

_EXPORT_STD template <class _Ty, class _CharT>
concept formattable =
_Formattable_with<remove_reference_t<_Ty>, basic_format_context<_Phony_fmt_iter_for<_CharT>, _CharT>>;
#endif // _HAS_CXX23

template <class _Ty>
class _Fmt_buffer {
private:
Expand Down Expand Up @@ -2428,6 +2449,45 @@ using _Fmt_wit = back_insert_iterator<_Fmt_buffer<wchar_t>>;
_EXPORT_STD using format_context = basic_format_context<_Fmt_it, char>;
_EXPORT_STD using wformat_context = basic_format_context<_Fmt_wit, wchar_t>;

#if _HAS_CXX23
_EXPORT_STD enum class range_format { disabled, map, set, sequence, string, debug_string };

template <class _Ty>
struct _Invalid_format_kind {
static_assert(_Always_false<_Ty>, "A program that instantiates the primary template of format_kind is ill-formed. "
"(N4964 [format.range.fmtkind]/1)");
};

_EXPORT_STD template <class _Ty>
constexpr _Invalid_format_kind<_Ty> format_kind{};
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved

template <class _Ty>
inline constexpr bool _Is_two_tuple = false;

template <class _Ty, class _Uty>
inline constexpr bool _Is_two_tuple<pair<_Ty, _Uty>> = true;

template <class _Ty, class _Uty>
inline constexpr bool _Is_two_tuple<tuple<_Ty, _Uty>> = true;

template <_RANGES input_range _Rng>
requires same_as<_Rng, remove_cvref_t<_Rng>>
constexpr range_format format_kind<_Rng> = []() consteval {
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
using _Ref_value_t = remove_cvref_t<_RANGES range_reference_t<_Rng>>;
if constexpr (same_as<_Ref_value_t, _Rng>) {
return range_format::disabled;
} else if constexpr (requires { typename _Rng::key_type; }) {
if constexpr (requires { typename _Rng::mapped_type; } && _Is_two_tuple<_Ref_value_t>) {
return range_format::map;
} else {
return range_format::set;
}
} else {
return range_format::sequence;
}
}();
#endif // _HAS_CXX23

_FMT_P2286_BEGIN
template <class _CharT, class _OutputIt>
_NODISCARD _OutputIt _Fmt_write(_OutputIt _Out, monostate) {
Expand Down
3 changes: 2 additions & 1 deletion stl/inc/thread
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ basic_ostream<_Ch, _Tr>& operator<<(basic_ostream<_Ch, _Tr>& _Str, thread::id _I
}

#if _HAS_CXX23 && defined(__cpp_lib_concepts)
template <class _CharT>
// Per LWG-3997, the _CharT template parameter is constrained to supported character types.
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
template <_Format_supported_charT _CharT>
struct formatter<thread::id, _CharT> {
private:
using _Pc = basic_format_parse_context<_CharT>;
Expand Down
2 changes: 0 additions & 2 deletions tests/libcxx/expected_results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,6 @@ std/language.support/support.limits/support.limits.general/type_traits.version.c

# P2286R8 Formatting Ranges
std/utilities/format/format.formattable/concept.formattable.compile.pass.cpp FAIL
std/utilities/format/format.range/format.range.fmtkind/format_kind.compile.pass.cpp FAIL
std/utilities/format/format.range/format.range.fmtkind/range_format.compile.pass.cpp FAIL
std/utilities/format/format.tuple/format.functions.format.pass.cpp FAIL
std/utilities/format/format.tuple/format.functions.vformat.pass.cpp FAIL
std/utilities/format/format.tuple/format.pass.cpp FAIL
Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ tests\P2286R8_text_formatting_debug_enabled_specializations
tests\P2286R8_text_formatting_escaping
tests\P2286R8_text_formatting_escaping_legacy_text_encoding
tests\P2286R8_text_formatting_escaping_utf8
tests\P2286R8_text_formatting_formattable
tests\P2302R4_ranges_alg_contains
tests\P2302R4_ranges_alg_contains_subrange
tests\P2321R2_proxy_reference
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/P2286R8_text_formatting_formattable/env.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\concepts_latest_matrix.lst
Loading