-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[libc++][format] Fixes formatting code units as integers. #73396
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
130 changes: 130 additions & 0 deletions
130
...lities/format/format.formatter/format.formatter.spec/formatter.char.fsigned-char.pass.cpp
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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
//===----------------------------------------------------------------------===// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17 | ||
// ADDITIONAL_COMPILE_FLAGS: -fsigned-char | ||
|
||
// <format> | ||
|
||
// C++23 the formatter is a debug-enabled specialization. | ||
// [format.formatter.spec]: | ||
// Each header that declares the template `formatter` provides the following | ||
// enabled specializations: | ||
// template<> struct formatter<char, char>; | ||
// template<> struct formatter<char, wchar_t>; | ||
// template<> struct formatter<wchar_t, wchar_t>; | ||
|
||
// P2909R4 "Fix formatting of code units as integers (Dude, where’s my char?)" | ||
// changed the behaviour of char (and wchar_t) when their underlying type is signed. | ||
|
||
#include <format> | ||
#include <cassert> | ||
#include <concepts> | ||
#include <iterator> | ||
#include <type_traits> | ||
|
||
#include "test_format_context.h" | ||
#include "test_macros.h" | ||
#include "make_string.h" | ||
#include "assert_macros.h" | ||
#include "concat_macros.h" | ||
|
||
#define STR(S) MAKE_STRING(CharT, S) | ||
#define SV(S) MAKE_STRING_VIEW(CharT, S) | ||
|
||
template <class StringT, class StringViewT, class ArgumentT> | ||
void test(StringT expected, StringViewT fmt, ArgumentT arg) { | ||
using CharT = typename StringT::value_type; | ||
auto parse_ctx = std::basic_format_parse_context<CharT>(fmt); | ||
std::formatter<ArgumentT, CharT> formatter; | ||
static_assert(std::semiregular<decltype(formatter)>); | ||
|
||
formatter.parse(parse_ctx); | ||
|
||
StringT result; | ||
auto out = std::back_inserter(result); | ||
using FormatCtxT = std::basic_format_context<decltype(out), CharT>; | ||
|
||
FormatCtxT format_ctx = test_format_context_create<decltype(out), CharT>(out, std::make_format_args<FormatCtxT>(arg)); | ||
formatter.format(arg, format_ctx); | ||
TEST_REQUIRE(result == expected, | ||
TEST_WRITE_CONCATENATED( | ||
"\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", result, '\n')); | ||
} | ||
|
||
template <class CharT> | ||
void test() { | ||
test(STR("\x00"), STR("}"), '\x00'); | ||
test(STR("a"), STR("}"), 'a'); | ||
test(STR("\x80"), STR("}"), '\x80'); | ||
test(STR("\xff"), STR("}"), '\xff'); | ||
|
||
test(STR("\x00"), STR("c}"), '\x00'); | ||
test(STR("a"), STR("c}"), 'a'); | ||
test(STR("\x80"), STR("c}"), '\x80'); | ||
test(STR("\xff"), STR("c}"), '\xff'); | ||
|
||
#if TEST_STD_VER > 20 | ||
test(STR(R"('\u{0}')"), STR("?}"), '\x00'); | ||
test(STR("'a'"), STR("?}"), 'a'); | ||
if constexpr (std::same_as<CharT, char>) { | ||
test(STR(R"('\x{80}')"), STR("?}"), '\x80'); | ||
test(STR(R"('\x{ff}')"), STR("?}"), '\xff'); | ||
} | ||
# ifndef TEST_HAS_NO_WIDE_CHARACTERS | ||
else { | ||
test(STR(R"('\u{80}')"), STR("?}"), '\x80'); | ||
test(STR("'\u00ff'"), STR("?}"), '\xff'); | ||
} | ||
# endif // TEST_HAS_NO_WIDE_CHARACTERS | ||
#endif // TEST_STD_VER > 20 | ||
|
||
test(STR("10000000"), STR("b}"), char(-128)); | ||
mordante marked this conversation as resolved.
Show resolved
Hide resolved
|
||
test(STR("11111111"), STR("b}"), char(-1)); | ||
test(STR("0"), STR("b}"), char(0)); | ||
test(STR("1"), STR("b}"), char(1)); | ||
test(STR("1111111"), STR("b}"), char(127)); | ||
|
||
test(STR("10000000"), STR("B}"), char(-128)); | ||
test(STR("11111111"), STR("B}"), char(-1)); | ||
test(STR("0"), STR("B}"), char(0)); | ||
test(STR("1"), STR("B}"), char(1)); | ||
test(STR("1111111"), STR("B}"), char(127)); | ||
|
||
test(STR("128"), STR("d}"), char(-128)); | ||
test(STR("255"), STR("d}"), char(-1)); | ||
test(STR("0"), STR("d}"), char(0)); | ||
test(STR("1"), STR("d}"), char(1)); | ||
test(STR("127"), STR("d}"), char(127)); | ||
|
||
test(STR("200"), STR("o}"), char(-128)); | ||
test(STR("377"), STR("o}"), char(-1)); | ||
test(STR("0"), STR("o}"), char(0)); | ||
test(STR("1"), STR("o}"), char(1)); | ||
test(STR("177"), STR("o}"), char(127)); | ||
|
||
test(STR("80"), STR("x}"), char(-128)); | ||
test(STR("ff"), STR("x}"), char(-1)); | ||
test(STR("0"), STR("x}"), char(0)); | ||
test(STR("1"), STR("x}"), char(1)); | ||
test(STR("7f"), STR("x}"), char(127)); | ||
|
||
test(STR("80"), STR("X}"), char(-128)); | ||
test(STR("FF"), STR("X}"), char(-1)); | ||
test(STR("0"), STR("X}"), char(0)); | ||
test(STR("1"), STR("X}"), char(1)); | ||
test(STR("7F"), STR("X}"), char(127)); | ||
} | ||
|
||
int main(int, char**) { | ||
test<char>(); | ||
#ifndef TEST_HAS_NO_WIDE_CHARACTERS | ||
test<wchar_t>(); | ||
#endif | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
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.