Skip to content

[libc++][format] Handle range-underlying-spec #81914

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
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
22 changes: 10 additions & 12 deletions libcxx/include/__format/parser_std_format_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,10 @@ class _LIBCPP_TEMPLATE_VIS __parser {
_LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator __parse(_ParseContext& __ctx, __fields __fields) {
auto __begin = __ctx.begin();
auto __end = __ctx.end();
if (__begin == __end || *__begin == _CharT('}'))
if (__begin == __end || *__begin == _CharT('}') || (__fields.__use_range_fill_ && *__begin == _CharT(':')))
return __begin;

if (__parse_fill_align(__begin, __end, __fields.__use_range_fill_) && __begin == __end)
if (__parse_fill_align(__begin, __end) && __begin == __end)
return __begin;

if (__fields.__sign_) {
Expand Down Expand Up @@ -574,12 +574,10 @@ class _LIBCPP_TEMPLATE_VIS __parser {
return false;
}

_LIBCPP_HIDE_FROM_ABI constexpr void __validate_fill_character(_CharT __fill, bool __use_range_fill) {
_LIBCPP_HIDE_FROM_ABI constexpr void __validate_fill_character(_CharT __fill) {
// The forbidden fill characters all code points formed from a single code unit, thus the
// check can be omitted when more code units are used.
if (__use_range_fill && (__fill == _CharT('{') || __fill == _CharT(':')))
std::__throw_format_error("The fill option contains an invalid value");
else if (__fill == _CharT('{'))
if (__fill == _CharT('{'))
std::__throw_format_error("The fill option contains an invalid value");
}

Expand All @@ -590,7 +588,7 @@ class _LIBCPP_TEMPLATE_VIS __parser {
# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
|| (same_as<_CharT, wchar_t> && sizeof(wchar_t) == 2)
# endif
_LIBCPP_HIDE_FROM_ABI constexpr bool __parse_fill_align(_Iterator& __begin, _Iterator __end, bool __use_range_fill) {
_LIBCPP_HIDE_FROM_ABI constexpr bool __parse_fill_align(_Iterator& __begin, _Iterator __end) {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
__begin != __end,
"when called with an empty input the function will cause "
Expand All @@ -606,7 +604,7 @@ class _LIBCPP_TEMPLATE_VIS __parser {
// The forbidden fill characters all are code points encoded
// in one code unit, thus the check can be omitted when more
// code units are used.
__validate_fill_character(*__begin, __use_range_fill);
__validate_fill_character(*__begin);

std::copy_n(__begin, __code_units, std::addressof(__fill_.__data[0]));
__begin += __code_units + 1;
Expand All @@ -623,7 +621,7 @@ class _LIBCPP_TEMPLATE_VIS __parser {
# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
template <contiguous_iterator _Iterator>
requires(same_as<_CharT, wchar_t> && sizeof(wchar_t) == 4)
_LIBCPP_HIDE_FROM_ABI constexpr bool __parse_fill_align(_Iterator& __begin, _Iterator __end, bool __use_range_fill) {
_LIBCPP_HIDE_FROM_ABI constexpr bool __parse_fill_align(_Iterator& __begin, _Iterator __end) {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
__begin != __end,
"when called with an empty input the function will cause "
Expand All @@ -632,7 +630,7 @@ class _LIBCPP_TEMPLATE_VIS __parser {
if (!__unicode::__is_scalar_value(*__begin))
std::__throw_format_error("The fill option contains an invalid value");

__validate_fill_character(*__begin, __use_range_fill);
__validate_fill_character(*__begin);

__fill_.__data[0] = *__begin;
__begin += 2;
Expand All @@ -651,14 +649,14 @@ class _LIBCPP_TEMPLATE_VIS __parser {
# else // _LIBCPP_HAS_NO_UNICODE
// range-fill and tuple-fill are identical
template <contiguous_iterator _Iterator>
_LIBCPP_HIDE_FROM_ABI constexpr bool __parse_fill_align(_Iterator& __begin, _Iterator __end, bool __use_range_fill) {
_LIBCPP_HIDE_FROM_ABI constexpr bool __parse_fill_align(_Iterator& __begin, _Iterator __end) {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
__begin != __end,
"when called with an empty input the function will cause "
"undefined behavior by evaluating data not in the input");
if (__begin + 1 != __end) {
if (__parse_alignment(*(__begin + 1))) {
__validate_fill_character(*__begin, __use_range_fill);
__validate_fill_character(*__begin);

__fill_.__data[0] = *__begin;
__begin += 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ void test_char_default(TestFunction check, ExceptionTest check_exception, auto&&

// when one is present there is no escaping,
check(SV("[H, e, l, l, o]"), SV("{::}"), input);
check(SV("[H, e, l, l, o]"), SV("{::<}"), input);
// unless forced by the type specifier.
check(SV("['H', 'e', 'l', 'l', 'o']"), SV("{::?}"), input);
check(SV("['H', 'e', 'l', 'l', 'o']"), SV("{::<?}"), input);

// ***** underlying has no format-spec

Expand All @@ -53,7 +55,6 @@ void test_char_default(TestFunction check, ExceptionTest check_exception, auto&&

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down Expand Up @@ -153,7 +154,6 @@ void test_char_string(TestFunction check, ExceptionTest check_exception, auto&&

check_exception("The format string contains an invalid escape sequence", SV("{:}<s}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<s}"), input);
check_exception("The fill option contains an invalid value", SV("{::<s}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-s}"), input);
Expand All @@ -177,6 +177,7 @@ void test_char_string(TestFunction check, ExceptionTest check_exception, auto&&

// *** type ***
check_exception("Type m requires a pair or a tuple with two elements", SV("{:m}"), input);
check_exception("The type option contains an invalid value for a character formatting argument", SV("{::<s}"), input);

// ***** Only underlying has a format-spec
check_exception("Type s and an underlying format specification can't be used together", SV("{:s:}"), input);
Expand Down Expand Up @@ -206,7 +207,7 @@ void test_char_escaped_string(TestFunction check, ExceptionTest check_exception,

check_exception("The format string contains an invalid escape sequence", SV("{:}<?s}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<?s}"), input);
check_exception("The fill option contains an invalid value", SV("{::<?s}"), input);
check_exception("The format specifier should consume the input or end with a '}'", SV("{::<?s}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-?s}"), input);
Expand Down Expand Up @@ -324,7 +325,6 @@ void test_bool(TestFunction check, ExceptionTest check_exception, auto&& input)

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down Expand Up @@ -440,7 +440,6 @@ void test_int(TestFunction check, ExceptionTest check_exception, auto&& input) {

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down Expand Up @@ -550,7 +549,6 @@ void test_floating_point(TestFunction check, ExceptionTest check_exception, auto

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down Expand Up @@ -688,7 +686,6 @@ void test_pointer(TestFunction check, ExceptionTest check_exception, auto&& inpu

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down Expand Up @@ -798,7 +795,6 @@ void test_string(TestFunction check, ExceptionTest check_exception, auto&& input

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down Expand Up @@ -910,7 +906,6 @@ void test_status(TestFunction check, ExceptionTest check_exception, auto&& input

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ void format_test_vector_bool(TestFunction check, ExceptionTest check_exception,

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ void test_char(TestFunction check, ExceptionTest check_exception) {

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down Expand Up @@ -87,7 +86,6 @@ void test_char(TestFunction check, ExceptionTest check_exception) {
check(SV("{__'a': 'A'___, __'b': 'B'___, __'c': 'C'___}"), SV("{::_^{}}"), input, 13);
check(SV("{#####'a': 'A', #####'b': 'B', #####'c': 'C'}"), SV("{::#>{}}"), input, 13);

check_exception("The fill option contains an invalid value", SV("{:::<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

Expand Down Expand Up @@ -157,7 +155,6 @@ void test_char_to_wchar(TestFunction check, ExceptionTest check_exception) {

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down Expand Up @@ -198,7 +195,6 @@ void test_char_to_wchar(TestFunction check, ExceptionTest check_exception) {
check(SV("{__'a': 'A'___, __'b': 'B'___, __'c': 'C'___}"), SV("{::_^{}}"), input, 13);
check(SV("{#####'a': 'A', #####'b': 'B', #####'c': 'C'}"), SV("{::#>{}}"), input, 13);

check_exception("The fill option contains an invalid value", SV("{:::<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

Expand Down Expand Up @@ -267,7 +263,6 @@ void test_bool(TestFunction check, ExceptionTest check_exception) {

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down Expand Up @@ -308,7 +303,6 @@ void test_bool(TestFunction check, ExceptionTest check_exception) {
check(SV("{_false: 0_, _true: 42_, _true: 1__}"), SV("{::_^{}}"), input, 10);
check(SV("{##false: 0, ##true: 42, ###true: 1}"), SV("{::#>{}}"), input, 10);

check_exception("The fill option contains an invalid value", SV("{:::<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

Expand Down Expand Up @@ -369,7 +363,6 @@ void test_int(TestFunction check, ExceptionTest check_exception, auto&& input) {

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down Expand Up @@ -410,7 +403,6 @@ void test_int(TestFunction check, ExceptionTest check_exception, auto&& input) {
check(SV("{_-42: 42__, __1: -1___, _42: -42__}"), SV("{::_^{}}"), input, 10);
check(SV("{###-42: 42, #####1: -1, ###42: -42}"), SV("{::#>{}}"), input, 10);

check_exception("The fill option contains an invalid value", SV("{:::<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

Expand Down Expand Up @@ -478,7 +470,6 @@ void test_floating_point(TestFunction check, ExceptionTest check_exception) {

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down Expand Up @@ -519,7 +510,6 @@ void test_floating_point(TestFunction check, ExceptionTest check_exception) {
check(SV("{_-42: 42__, __1: -1___}"), SV("{::_^{}}"), input, 10);
check(SV("{###-42: 42, #####1: -1}"), SV("{::#>{}}"), input, 10);

check_exception("The fill option contains an invalid value", SV("{:::<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

Expand Down Expand Up @@ -582,7 +572,6 @@ void test_pointer(TestFunction check, ExceptionTest check_exception) {

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:#}"), input);
Expand Down Expand Up @@ -621,7 +610,6 @@ void test_pointer(TestFunction check, ExceptionTest check_exception) {
check(SV("{__0x0: 0x0___}"), SV("{::_^{}}"), input, 13);
check(SV("{#####0x0: 0x0}"), SV("{::#>{}}"), input, 13);

check_exception("The fill option contains an invalid value", SV("{:::<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

Expand Down Expand Up @@ -685,7 +673,6 @@ void test_string(TestFunction check, ExceptionTest check_exception) {

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:#}"), input);
Expand Down Expand Up @@ -724,7 +711,6 @@ void test_string(TestFunction check, ExceptionTest check_exception) {
check(SV(R"({__"hello": "HELLO"___, __"world": "WORLD"___})"), SV("{::_^{}}"), input, 21);
check(SV(R"({#####"hello": "HELLO", #####"world": "WORLD"})"), SV("{::#>{}}"), input, 21);

check_exception("The fill option contains an invalid value", SV("{:::<}"), input);
check_exception("The format string contains an invalid escape sequence", SV("{::}<}"), input);
check_exception("The fill option contains an invalid value", SV("{::{<}"), input);

Expand Down Expand Up @@ -788,7 +774,6 @@ void test_status(TestFunction check, ExceptionTest check_exception) {

check_exception("The format string contains an invalid escape sequence", SV("{:}<}"), input);
check_exception("The fill option contains an invalid value", SV("{:{<}"), input);
check_exception("The fill option contains an invalid value", SV("{::<}"), input);

// *** sign ***
check_exception("The format specifier should consume the input or end with a '}'", SV("{:-}"), input);
Expand Down
Loading