-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement the ?
format specifier for strings and characters
#3656
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
CaseyCarter
merged 21 commits into
microsoft:main
from
cpplearner:p2286r8-formatting-ranges
May 18, 2023
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f603144
Reduce code duplication in grapheme_break_property_data_gen.py
cpplearner bbd495f
Add Unicode properties needed for [format.string.escaped]
cpplearner b0ce1a7
Add _Fmt_codec::_Decode
cpplearner a6e47d4
Implement [format.string.escaped]
cpplearner d25643c
Add basic tests
cpplearner 21bf8f3
Add more tests
cpplearner cc9aabc
Update `yvals_core.h`
cpplearner 8809da5
Use the existing _Unicode_property_data structure
cpplearner f1a93bc
Add TRANSITION comment
cpplearner 6825e86
Remove helper lambdas
cpplearner 7dc7a77
List P2286R8 for C++23 only
cpplearner 7343a51
Drop unnecessary forward declaration
cpplearner 085efd1
Guard new stuff with _HAS_CXX23
cpplearner 34e322f
Put stuff into a new inline namespace
cpplearner d18d18b
Add comment for _Is_unicode_scalar_value
cpplearner 3fab086
Add newline between function definitions
cpplearner e27cc4c
Check '\0' only when needed
cpplearner d780c57
Tighten buffer size
cpplearner d85b6d6
Use raw string literals
cpplearner 62dd0d1
clang-format (no manual changes)
cpplearner 0243b80
Address review comments
cpplearner 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
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,125 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include <cassert> | ||
#include <format> | ||
#include <string> | ||
#include <string_view> | ||
#include <type_traits> | ||
#include <utility> | ||
|
||
using namespace std; | ||
|
||
template <typename CharT> | ||
[[nodiscard]] constexpr const CharT* choose_literal(const char* const str, const wchar_t* const wstr) noexcept { | ||
if constexpr (is_same_v<CharT, char>) { | ||
return str; | ||
} else { | ||
return wstr; | ||
} | ||
} | ||
|
||
#define STR(Literal) (choose_literal<CharT>(Literal, L##Literal)) | ||
|
||
template <class charT, class... Args> | ||
auto make_testing_format_args(Args&&... vals) { | ||
if constexpr (is_same_v<charT, wchar_t>) { | ||
return make_wformat_args(forward<Args>(vals)...); | ||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
return make_format_args(forward<Args>(vals)...); | ||
} | ||
} | ||
|
||
template <class CharT, class T> | ||
void test_escaped_character() { | ||
assert(format(STR("{:?}"), T('\t')) == STR(R"('\t')")); | ||
assert(format(STR("{:?}"), T('\n')) == STR(R"('\n')")); | ||
assert(format(STR("{:?}"), T('\r')) == STR(R"('\r')")); | ||
assert(format(STR("{:?}"), T('\"')) == STR(R"('"')")); | ||
assert(format(STR("{:?}"), T('\'')) == STR(R"('\'')")); | ||
assert(format(STR("{:?}"), T('\\')) == STR(R"('\\')")); | ||
|
||
assert(format(STR("{:?}"), T('\0')) == STR(R"('\u{0}')")); | ||
assert(format(STR("{:?}"), T('\v')) == STR(R"('\u{b}')")); | ||
assert(format(STR("{:?}"), T('\f')) == STR(R"('\u{c}')")); | ||
assert(format(STR("{:?}"), T('\x7F')) == STR(R"('\u{7f}')")); | ||
|
||
assert(format(STR("{:?}"), T(' ')) == STR("' '")); | ||
assert(format(STR("{:?}"), T('~')) == STR("'~'")); | ||
|
||
if constexpr (is_same_v<CharT, wchar_t> && is_same_v<T, wchar_t>) { | ||
assert(format(L"{:?}", L'\xA0') == LR"('\u{a0}')"); // U+00A0 NO-BREAK SPACE | ||
assert(format(L"{:?}", L'\x300') == LR"('\u{300}')"); // U+0300 COMBINING GRAVE ACCENT | ||
|
||
assert(format(L"{:?}", L'\xA1') == L"'\xA1'"); // U+00A1 INVERTED EXCLAMATION MARK | ||
|
||
assert(format(L"{:?}", L'\xD800') == LR"('\x{d800}')"); | ||
assert(format(L"{:?}", L'\xDFFF') == LR"('\x{dfff}')"); | ||
} | ||
} | ||
|
||
template <class CharT> | ||
void test_escaped_string() { | ||
assert(format(STR("{:?}"), STR("\t")) == STR(R"("\t")")); | ||
assert(format(STR("{:?}"), STR("\n")) == STR(R"("\n")")); | ||
assert(format(STR("{:?}"), STR("\r")) == STR(R"("\r")")); | ||
assert(format(STR("{:?}"), STR("\"")) == STR("\"\\\"\"")); | ||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert(format(STR("{:?}"), STR("\'")) == STR(R"("'")")); | ||
assert(format(STR("{:?}"), STR("\\")) == STR(R"("\\")")); | ||
|
||
assert(format(STR("{:?}"), STR("\v")) == STR(R"("\u{b}")")); | ||
assert(format(STR("{:?}"), STR("\f")) == STR(R"("\u{c}")")); | ||
assert(format(STR("{:?}"), STR("\x7F")) == STR(R"("\u{7f}")")); | ||
|
||
assert(format(STR("{:?}"), STR(" ")) == STR("\" \"")); | ||
assert(format(STR("{:?}"), STR("~")) == STR("\"~\"")); | ||
|
||
assert(format(STR("[{:?}]"), basic_string{STR("\0 \n \t \x02 \x1b"), 9}) == STR(R"(["\u{0} \n \t \u{2} \u{1b}"])")); | ||
assert(format(STR("[{:?}]"), basic_string_view{STR("\0 \n \t \x02 \x1b"), 9}) | ||
== STR(R"(["\u{0} \n \t \u{2} \u{1b}"])")); | ||
|
||
if constexpr (is_same_v<CharT, wchar_t>) { | ||
assert(format(L"{:?}", L"\xA0") == LR"("\u{a0}")"); // U+00A0 NO-BREAK SPACE | ||
assert(format(L"{:?}", L"\U0010FFFF") == LR"("\u{10ffff}")"); // noncharacter | ||
assert(format(L"{:?}", L"\x300") == LR"("\u{300}")"); // U+0300 COMBINING GRAVE ACCENT | ||
|
||
assert(format(L"{:?}", L"\xA1") == L"\"\xA1\""); // U+00A1 INVERTED EXCLAMATION MARK | ||
assert(format(L"{:?}", L"\U00010000") == L"\"\U00010000\""); // U+10000 LINEAR B SYLLABLE B008 A | ||
|
||
assert(format(L"{:?}", L"\xD800") == L"\"\\x{d800}\""); | ||
assert(format(L"{:?}", L"\xDFFF") == L"\"\\x{dfff}\""); | ||
assert(format(L"{:?}", L"\xDFFF\xD800") == L"\"\\x{dfff}\\x{d800}\""); | ||
|
||
assert(format(L"{:?}", L"\xA0\x300") == L"\"\\u{a0}\\u{300}\""); | ||
assert(format(L"{:?}", L" \x300") == L"\" \x300\""); | ||
assert(format(L"{:?}", L"~\x300") == L"\"~\x300\""); | ||
} | ||
} | ||
|
||
template <class CharT, class T> | ||
void test_format_specs() { | ||
assert(format(STR("{:5?}"), T('\n')) == STR(R"('\n' )")); | ||
try { | ||
(void) vformat(STR("{:.3?}"), make_testing_format_args<CharT>(T('\n'))); | ||
assert(false); | ||
} catch (const format_error&) { | ||
} | ||
|
||
assert(format(STR("{:5?}"), STR("\n")) == STR(R"("\n" )")); | ||
assert(format(STR("{:.3?}"), STR("\n")) == STR(R"("\n)")); | ||
assert(format(STR("{:5.3?}"), STR("\n")) == STR(R"("\n )")); | ||
assert(format(STR("{:>5.3?}"), STR("\n")) == STR(R"( "\n)")); | ||
} | ||
|
||
int main() { | ||
test_escaped_character<char, char>(); | ||
test_escaped_character<wchar_t, char>(); | ||
test_escaped_character<wchar_t, wchar_t>(); | ||
|
||
test_escaped_string<char>(); | ||
test_escaped_string<wchar_t>(); | ||
|
||
test_format_specs<char, char>(); | ||
test_format_specs<wchar_t, char>(); | ||
test_format_specs<wchar_t, wchar_t>(); | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/std/tests/P2286R8_formatting_ranges_legacy_text_encoding/env.lst
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,28 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
# This is `concepts_latest_matrix.lst` with `/execution-charset:.932` added. | ||
# clang is excluded since it doesn't support non-UTF-8 execution charsets. | ||
|
||
RUNALL_INCLUDE ..\prefix.lst | ||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RUNALL_CROSSLIST | ||
PM_CL="/w14640 /Zc:threadSafeInit- /EHsc /std:c++latest /execution-charset:.932" | ||
RUNALL_CROSSLIST | ||
PM_CL="/MD /D_ITERATOR_DEBUG_LEVEL=0 /permissive- /Zc:noexceptTypes-" | ||
PM_CL="/MD /D_ITERATOR_DEBUG_LEVEL=1 /permissive-" | ||
PM_CL="/MD /D_ITERATOR_DEBUG_LEVEL=0 /permissive- /Zc:char8_t- /Zc:preprocessor" | ||
PM_CL="/MDd /D_ITERATOR_DEBUG_LEVEL=0 /permissive- /Zc:wchar_t-" | ||
PM_CL="/MDd /D_ITERATOR_DEBUG_LEVEL=1 /permissive-" | ||
PM_CL="/MDd /D_ITERATOR_DEBUG_LEVEL=2 /permissive- /fp:except /Zc:preprocessor" | ||
PM_CL="/MT /D_ITERATOR_DEBUG_LEVEL=0 /permissive-" | ||
PM_CL="/MT /D_ITERATOR_DEBUG_LEVEL=0 /permissive- /analyze:only /analyze:autolog-" | ||
PM_CL="/MT /D_ITERATOR_DEBUG_LEVEL=1 /permissive-" | ||
PM_CL="/MTd /D_ITERATOR_DEBUG_LEVEL=0 /permissive- /fp:strict" | ||
PM_CL="/MTd /D_ITERATOR_DEBUG_LEVEL=1 /permissive-" | ||
PM_CL="/MTd /D_ITERATOR_DEBUG_LEVEL=2 /permissive" | ||
PM_CL="/MTd /D_ITERATOR_DEBUG_LEVEL=2 /permissive- /analyze:only /analyze:autolog-" | ||
# PM_CL="/permissive- /BE /c /MD" | ||
# PM_CL="/permissive- /BE /c /MTd" | ||
# PM_COMPILER="clang-cl" PM_CL="-fno-ms-compatibility -fno-delayed-template-parsing -Wno-unqualified-std-cast-call /permissive- /MD" | ||
# PM_COMPILER="clang-cl" PM_CL="-fno-ms-compatibility -fno-delayed-template-parsing -Wno-unqualified-std-cast-call /permissive- /MTd /fp:strict" | ||
# PM_COMPILER="clang-cl" PM_CL="-fno-ms-compatibility -fno-delayed-template-parsing -Wno-unqualified-std-cast-call /permissive- /MT /fp:strict -fsanitize=undefined -fno-sanitize-recover=undefined" |
22 changes: 22 additions & 0 deletions
22
tests/std/tests/P2286R8_formatting_ranges_legacy_text_encoding/test.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,22 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include <cassert> | ||
#include <format> | ||
|
||
using namespace std; | ||
|
||
void test_escaped_string() { | ||
assert(format("{:?}", "\x81\x40") == "\"\\u{3000}\""); // U+3000 IDEOGRAPHIC SPACE | ||
|
||
assert(format("{:?}", "\x81\x41") == "\"\x81\x41\""); | ||
|
||
assert(format("{:?}", "\x81") == "\"\\x{81}\""); | ||
assert(format("{:?}", "\xEB!") == "\"\\x{eb}!\""); | ||
|
||
assert(format("{:?}", "\x81\x40\x40\x81") == "\"\\u{3000}\x40\\x{81}\""); | ||
} | ||
|
||
int main() { | ||
test_escaped_string(); | ||
} |
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,6 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
RUNALL_INCLUDE ..\concepts_latest_matrix.lst | ||
RUNALL_CROSSLIST | ||
PM_CL="/utf-8" |
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,35 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include <cassert> | ||
#include <clocale> | ||
#include <format> | ||
|
||
using namespace std; | ||
|
||
void test_escaped_string() { | ||
assert(format("{:?}", "\u00A0") == "\"\\u{a0}\""); // U+00A0 NO-BREAK SPACE | ||
assert(format("{:?}", "\u0300") == "\"\\u{300}\""); // U+0300 COMBINING GRAVE ACCENT | ||
|
||
assert(format("{:?}", "\u00A1") == "\"\u00A1\""); // U+00A1 INVERTED EXCLAMATION MARK | ||
assert(format("{:?}", "\U00010000") == "\"\U00010000\""); // U+10000 LINEAR B SYLLABLE B008 A | ||
|
||
assert(format("{:?}", "\xC0\x80") == "\"\\x{c0}\\x{80}\""); // ill-formed code unit sequence | ||
|
||
assert(format("{:?}", "\u00A0\u0300") == "\"\\u{a0}\\u{300}\""); | ||
assert(format("{:?}", " \u0300") == "\" \u0300\""); | ||
assert(format("{:?}", "a\u0300") == "\"a\u0300\""); | ||
} | ||
|
||
int main() { | ||
test_escaped_string(); | ||
|
||
assert(setlocale(LC_ALL, ".1252") != nullptr); | ||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
test_escaped_string(); | ||
|
||
assert(setlocale(LC_ALL, ".932") != nullptr); | ||
test_escaped_string(); | ||
|
||
assert(setlocale(LC_ALL, ".UTF-8") != nullptr); | ||
test_escaped_string(); | ||
} |
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
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.