-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<format>: Fix handling of replacement field when format-spec is absent
#4640
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
Merged
Stephan T. Lavavej (StephanTLavavej)
merged 5 commits into
microsoft:main
from
frederick-vs-ja:fix-replacement-field
May 20, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2e64d7f
Fix handling of replacement field when format-spec is absent
frederick-vs-ja a114513
Try to fix `_On_replacement_field` again
frederick-vs-ja ab24ba4
Fix a `formatter` specialization in the test
frederick-vs-ja 092693e
`&*_Parse_context.begin()` => `_Parse_context.begin()._Unwrapped()`
StephanTLavavej 8cad6d5
Minor nitpicks.
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
|
|
||
| #include <algorithm> | ||
| #include <cassert> | ||
| #include <cstddef> | ||
| #include <format> | ||
| #include <iterator> | ||
| #include <limits> | ||
|
|
@@ -58,10 +59,10 @@ struct not_const_formattable_type { | |
| template <> | ||
| struct std::formatter<basic_custom_formattable_type, char> { | ||
| constexpr basic_format_parse_context<char>::iterator parse(basic_format_parse_context<char>& parse_ctx) { | ||
| if (parse_ctx.begin() != parse_ctx.end()) { | ||
| if (parse_ctx.begin() != parse_ctx.end() && *parse_ctx.begin() != '}') { | ||
| throw format_error{"only empty specs please"}; | ||
| } | ||
| return parse_ctx.end(); | ||
| return parse_ctx.begin(); | ||
|
Comment on lines
64
to
+65
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be non-functional, correct? It's a matter of if we "match" the closing '}' or not. |
||
| } | ||
| format_context::iterator format(const basic_custom_formattable_type& val, format_context& ctx) const { | ||
| ctx.advance_to(copy(val.string_content.begin(), val.string_content.end(), ctx.out())); | ||
|
|
@@ -72,10 +73,10 @@ struct std::formatter<basic_custom_formattable_type, char> { | |
| template <> | ||
| struct std::formatter<not_const_formattable_type, char> { | ||
| constexpr basic_format_parse_context<char>::iterator parse(basic_format_parse_context<char>& parse_ctx) { | ||
| if (parse_ctx.begin() != parse_ctx.end()) { | ||
| if (parse_ctx.begin() != parse_ctx.end() && *parse_ctx.begin() != '}') { | ||
| throw format_error{"only empty specs please"}; | ||
| } | ||
| return parse_ctx.end(); | ||
| return parse_ctx.begin(); | ||
| } | ||
| format_context::iterator format(not_const_formattable_type& val, format_context& ctx) const { | ||
| ctx.advance_to(copy(val.string_content.begin(), val.string_content.end(), ctx.out())); | ||
|
|
@@ -276,6 +277,69 @@ void test_basic_format_context_construction() { | |
| static_assert(!is_constructible_with_trailing_empty_brace_impl<context, OutIt, const basic_format_args<context>&>); | ||
| } | ||
|
|
||
| // Test GH-4636 "<format>: Call to next_arg_id may result in unexpected error (regression)" | ||
|
|
||
| struct FormatNextArg {}; | ||
|
|
||
| template <class CharT> | ||
| struct std::formatter<FormatNextArg, CharT> { | ||
| public: | ||
| template <class ParseContext> | ||
| constexpr auto parse(ParseContext& ctx) { | ||
| auto it = ctx.begin(); | ||
| if (it != ctx.end() && *it != '}') { | ||
| throw format_error{"Expected empty spec"}; | ||
| } | ||
|
|
||
| arg_id = ctx.next_arg_id(); | ||
| return it; | ||
| } | ||
|
|
||
| template <class FormatContext> | ||
| auto format(FormatNextArg, FormatContext& ctx) const { | ||
| return format_to(ctx.out(), TYPED_LITERAL(CharT, "arg-id: {}"), arg_id); | ||
| } | ||
|
|
||
| private: | ||
| size_t arg_id; | ||
|
StephanTLavavej marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| template <class CharT> | ||
| void test_parsing_with_next_id() { | ||
| assert(format(TYPED_LITERAL(CharT, "{}, {}"), FormatNextArg{}, 0, FormatNextArg{}, TYPED_LITERAL(CharT, "1")) | ||
| == TYPED_LITERAL(CharT, "arg-id: 1, arg-id: 3")); | ||
| assert(format(TYPED_LITERAL(CharT, "{:}, {:}"), FormatNextArg{}, 2, FormatNextArg{}, TYPED_LITERAL(CharT, "3")) | ||
| == TYPED_LITERAL(CharT, "arg-id: 1, arg-id: 3")); | ||
| } | ||
|
|
||
| struct NeedMagicWord {}; | ||
|
|
||
| template <class CharT> | ||
| struct std::formatter<NeedMagicWord, CharT> { | ||
| constexpr auto parse(const basic_format_parse_context<CharT>& ctx) { | ||
| constexpr basic_string_view<CharT> magic_word{TYPED_LITERAL(CharT, "narf")}; | ||
| auto [i, j] = ranges::mismatch(ctx, magic_word); | ||
| if (j != magic_word.end()) { | ||
| throw format_error{"you didn't say the magic word!"}; | ||
| } | ||
|
|
||
| if (i != ctx.end() && *i != '}') { | ||
| throw format_error{"the whole spec must be the magic word!"}; | ||
| } | ||
| return i; | ||
| } | ||
|
|
||
| template <class OutIt> | ||
| auto format(NeedMagicWord, basic_format_context<OutIt, CharT>& ctx) const { | ||
| return ctx.out(); | ||
| } | ||
| }; | ||
|
|
||
| template <class CharT> | ||
| void test_parsing_needing_magic_word() { | ||
| assert(format(TYPED_LITERAL(CharT, "{:narf}"), NeedMagicWord{}).empty()); | ||
| } | ||
|
|
||
| int main() { | ||
| test_format_family_overloads<basic_custom_formattable_type>(); | ||
| test_format_family_overloads<not_const_formattable_type>(); | ||
|
|
@@ -297,5 +361,9 @@ int main() { | |
| test_basic_format_context_construction<wchar_t*, wchar_t>(); | ||
| test_basic_format_context_construction<wstring::iterator, wchar_t>(); | ||
| test_basic_format_context_construction<back_insert_iterator<wstring>, wchar_t>(); | ||
| return 0; | ||
|
|
||
| test_parsing_with_next_id<char>(); | ||
| test_parsing_with_next_id<wchar_t>(); | ||
| test_parsing_needing_magic_word<char>(); | ||
| test_parsing_needing_magic_word<wchar_t>(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.